From 2b43308208a9cd474da5b0fb7e232a444460be08 Mon Sep 17 00:00:00 2001 From: Peter Boyle Date: Fri, 25 Aug 2023 17:38:07 -0400 Subject: [PATCH] First cut non-local coarsening --- tests/debug/Test_general_coarse.cc | 329 ++++++++++++++++++++++ tests/debug/Test_general_coarse_pvdagm.cc | 226 +++++++++++++++ 2 files changed, 555 insertions(+) create mode 100644 tests/debug/Test_general_coarse.cc create mode 100644 tests/debug/Test_general_coarse_pvdagm.cc diff --git a/tests/debug/Test_general_coarse.cc b/tests/debug/Test_general_coarse.cc new file mode 100644 index 00000000..169f9378 --- /dev/null +++ b/tests/debug/Test_general_coarse.cc @@ -0,0 +1,329 @@ + /************************************************************************************* + + Grid physics library, www.github.com/paboyle/Grid + + Source file: ./tests/Test_padded_cell.cc + + Copyright (C) 2023 + +Author: Peter Boyle + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + See the full license in the file "LICENSE" in the top level distribution directory + *************************************************************************************/ + /* END LEGAL */ +#include +#include +#include +#include + +#include +#include +#include + +using namespace std; +using namespace Grid; + +template +class HermOpAdaptor : public LinearOperatorBase +{ + LinearOperatorBase & wrapped; +public: + HermOpAdaptor(LinearOperatorBase &wrapme) : wrapped(wrapme) {}; + void OpDiag (const Field &in, Field &out) { assert(0); } + void OpDir (const Field &in, Field &out,int dir,int disp) { assert(0); } + void OpDirAll (const Field &in, std::vector &out){ assert(0); }; + void Op (const Field &in, Field &out){ + wrapped.HermOp(in,out); + } + void AdjOp (const Field &in, Field &out){ + wrapped.HermOp(in,out); + } + void HermOpAndNorm(const Field &in, Field &out,RealD &n1,RealD &n2){ assert(0); } + void HermOp(const Field &in, Field &out){ + wrapped.HermOp(in,out); + } + +}; + +template +class PVdagMLinearOperator : public LinearOperatorBase { + Matrix &_Mat; + Matrix &_PV; +public: + PVdagMLinearOperator(Matrix &Mat,Matrix &PV): _Mat(Mat),_PV(PV){}; + + void OpDiag (const Field &in, Field &out) { assert(0); } + void OpDir (const Field &in, Field &out,int dir,int disp) { assert(0); } + void OpDirAll (const Field &in, std::vector &out){ assert(0); }; + void Op (const Field &in, Field &out){ + Field tmp(in.Grid()); + _Mat.M(in,tmp); + _PV.Mdag(tmp,out); + } + void AdjOp (const Field &in, Field &out){ + Field tmp(in.Grid()); + _PV.M(tmp,out); + _Mat.Mdag(in,tmp); + } + void HermOpAndNorm(const Field &in, Field &out,RealD &n1,RealD &n2){ assert(0); } + void HermOp(const Field &in, Field &out){ + std::cout << "HermOp"< class DumbOperator : public LinearOperatorBase { +public: + LatticeComplex scale; + DumbOperator(GridBase *grid) : scale(grid) + { + scale = 0.0; + LatticeComplex scalesft(grid); + LatticeComplex scaletmp(grid); + for(int d=0;d<4;d++){ + Lattice > x(grid); LatticeCoordinate(x,d+1); + LatticeCoordinate(scaletmp,d+1); + scalesft = Cshift(scaletmp,d+1,1); + scale = 100.0*scale + where( mod(x ,2)==(Integer)0, scalesft,scaletmp); + } + std::cout << " scale\n" << scale << std::endl; + } + // Support for coarsening to a multigrid + void OpDiag (const Field &in, Field &out) {}; + void OpDir (const Field &in, Field &out,int dir,int disp){}; + void OpDirAll (const Field &in, std::vector &out) {}; + + void Op (const Field &in, Field &out){ + out = scale * in; + } + void AdjOp (const Field &in, Field &out){ + out = scale * in; + } + void HermOp(const Field &in, Field &out){ + double n1, n2; + HermOpAndNorm(in,out,n1,n2); + } + void HermOpAndNorm(const Field &in, Field &out,double &n1,double &n2){ + ComplexD dot; + + out = scale * in; + + dot= innerProduct(in,out); + n1=real(dot); + + dot = innerProduct(out,out); + n2=real(dot); + } +}; + + +int main (int argc, char ** argv) +{ + Grid_init(&argc,&argv); + + const int Ls=4; + + GridCartesian * UGrid = SpaceTimeGrid::makeFourDimGrid(GridDefaultLatt(), GridDefaultSimd(Nd,vComplex::Nsimd()),GridDefaultMpi()); + GridRedBlackCartesian * UrbGrid = SpaceTimeGrid::makeFourDimRedBlackGrid(UGrid); + + GridCartesian * FGrid = SpaceTimeGrid::makeFiveDimGrid(Ls,UGrid); + GridRedBlackCartesian * FrbGrid = SpaceTimeGrid::makeFiveDimRedBlackGrid(Ls,UGrid); + + // Construct a coarsened grid + Coordinate clatt = GridDefaultLatt(); + for(int d=0;d seeds4({1,2,3,4}); + std::vector seeds5({5,6,7,8}); + std::vector cseeds({5,6,7,8}); + GridParallelRNG RNG5(FGrid); RNG5.SeedFixedIntegers(seeds5); + GridParallelRNG RNG4(UGrid); RNG4.SeedFixedIntegers(seeds4); + GridParallelRNG CRNG(Coarse5d);CRNG.SeedFixedIntegers(cseeds); + + LatticeFermion src(FGrid); random(RNG5,src); + LatticeFermion result(FGrid); result=Zero(); + LatticeFermion ref(FGrid); ref=Zero(); + LatticeFermion tmp(FGrid); + LatticeFermion err(FGrid); + LatticeGaugeField Umu(UGrid); + //SU::HotConfiguration(RNG4,Umu); + SU::ColdConfiguration(Umu); + // auto U = peekLorentz(Umu,0); + // Umu=Zero(); // Make operator local for now + // pokeLorentz(Umu,U,0); + + RealD mass=0.5; + RealD M5=1.8; + + DomainWallFermionD Ddwf(Umu,*FGrid,*FrbGrid,*UGrid,*UrbGrid,mass,M5); + DomainWallFermionD Dpv(Umu,*FGrid,*FrbGrid,*UGrid,*UrbGrid,1.0,M5); + + const int nbasis = 20; + const int cb = 0 ; + LatticeFermion prom(FGrid); + + std::vector subspace(nbasis,FGrid); + + std::cout< HermDefOp(Ddwf); + DumbOperator Diagonal(FGrid); + typedef Aggregation Subspace; + + Subspace Aggregates(Coarse5d,FGrid,cb); + Aggregates.CreateSubspaceRandom(RNG5); + + std::cout< LittleDiracOperator; + typedef LittleDiracOperator::CoarseVector CoarseVector; + + NextToNearestStencilGeometry5D geom; + LittleDiracOperator LittleDiracOp(geom,FGrid,Coarse5d); + LittleDiracOp.CoarsenOperator(HermDefOp,Aggregates); + // LittleDiracOp.CoarsenOperator(Diagonal,Aggregates); + + std::cout< PVdagM(Ddwf,Dpv); + HermOpAdaptor HOA(PVdagM); + + // Run power method on HOA?? + PowerMethod PM; PM(HOA,src); + + // Warning: This routine calls PVdagM.Op, not PVdagM.HermOp + Subspace AggregatesPD(Coarse5d,FGrid,cb); + AggregatesPD.CreateSubspaceChebyshev(RNG5, + HOA, + nbasis, + 5000.0, + 0.02, + 100, + 50, + 50, + 0.0); + + LittleDiracOperator LittleDiracOpPV(geom,FGrid,Coarse5d); + LittleDiracOpPV.CoarsenOperator(PVdagM,AggregatesPD); + + std::cout< + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + See the full license in the file "LICENSE" in the top level distribution directory + *************************************************************************************/ + /* END LEGAL */ +#include +#include +#include +#include + +#include +#include +#include + +using namespace std; +using namespace Grid; + +template +class HermOpAdaptor : public LinearOperatorBase +{ + LinearOperatorBase & wrapped; +public: + HermOpAdaptor(LinearOperatorBase &wrapme) : wrapped(wrapme) {}; + void OpDiag (const Field &in, Field &out) { assert(0); } + void OpDir (const Field &in, Field &out,int dir,int disp) { assert(0); } + void OpDirAll (const Field &in, std::vector &out){ assert(0); }; + void Op (const Field &in, Field &out){ + wrapped.HermOp(in,out); + } + void AdjOp (const Field &in, Field &out){ + wrapped.HermOp(in,out); + } + void HermOpAndNorm(const Field &in, Field &out,RealD &n1,RealD &n2){ assert(0); } + void HermOp(const Field &in, Field &out){ + wrapped.HermOp(in,out); + } + +}; + +template +class PVdagMLinearOperator : public LinearOperatorBase { + Matrix &_Mat; + Matrix &_PV; +public: + PVdagMLinearOperator(Matrix &Mat,Matrix &PV): _Mat(Mat),_PV(PV){}; + + void OpDiag (const Field &in, Field &out) { assert(0); } + void OpDir (const Field &in, Field &out,int dir,int disp) { assert(0); } + void OpDirAll (const Field &in, std::vector &out){ assert(0); }; + void Op (const Field &in, Field &out){ + Field tmp(in.Grid()); + _Mat.M(in,tmp); + _PV.Mdag(tmp,out); + } + void AdjOp (const Field &in, Field &out){ + Field tmp(in.Grid()); + _PV.M(tmp,out); + _Mat.Mdag(in,tmp); + } + void HermOpAndNorm(const Field &in, Field &out,RealD &n1,RealD &n2){ assert(0); } + void HermOp(const Field &in, Field &out){ + std::cout << "HermOp"< class DumbOperator : public LinearOperatorBase { +public: + LatticeComplex scale; + DumbOperator(GridBase *grid) : scale(grid) + { + scale = 0.0; + LatticeComplex scalesft(grid); + LatticeComplex scaletmp(grid); + for(int d=0;d<4;d++){ + Lattice > x(grid); LatticeCoordinate(x,d+1); + LatticeCoordinate(scaletmp,d+1); + scalesft = Cshift(scaletmp,d+1,1); + scale = 100.0*scale + where( mod(x ,2)==(Integer)0, scalesft,scaletmp); + } + std::cout << " scale\n" << scale << std::endl; + } + // Support for coarsening to a multigrid + void OpDiag (const Field &in, Field &out) {}; + void OpDir (const Field &in, Field &out,int dir,int disp){}; + void OpDirAll (const Field &in, std::vector &out) {}; + + void Op (const Field &in, Field &out){ + out = scale * in; + } + void AdjOp (const Field &in, Field &out){ + out = scale * in; + } + void HermOp(const Field &in, Field &out){ + double n1, n2; + HermOpAndNorm(in,out,n1,n2); + } + void HermOpAndNorm(const Field &in, Field &out,double &n1,double &n2){ + ComplexD dot; + + out = scale * in; + + dot= innerProduct(in,out); + n1=real(dot); + + dot = innerProduct(out,out); + n2=real(dot); + } +}; + + +int main (int argc, char ** argv) +{ + Grid_init(&argc,&argv); + + const int Ls=16; + + GridCartesian * UGrid = SpaceTimeGrid::makeFourDimGrid(GridDefaultLatt(), GridDefaultSimd(Nd,vComplex::Nsimd()),GridDefaultMpi()); + GridRedBlackCartesian * UrbGrid = SpaceTimeGrid::makeFourDimRedBlackGrid(UGrid); + + GridCartesian * FGrid = SpaceTimeGrid::makeFiveDimGrid(Ls,UGrid); + GridRedBlackCartesian * FrbGrid = SpaceTimeGrid::makeFiveDimRedBlackGrid(Ls,UGrid); + + // Construct a coarsened grid + Coordinate clatt = GridDefaultLatt(); + for(int d=0;d seeds4({1,2,3,4}); + std::vector seeds5({5,6,7,8}); + std::vector cseeds({5,6,7,8}); + GridParallelRNG RNG5(FGrid); RNG5.SeedFixedIntegers(seeds5); + GridParallelRNG RNG4(UGrid); RNG4.SeedFixedIntegers(seeds4); + GridParallelRNG CRNG(Coarse5d);CRNG.SeedFixedIntegers(cseeds); + + LatticeFermion src(FGrid); random(RNG5,src); + LatticeFermion result(FGrid); result=Zero(); + LatticeFermion ref(FGrid); ref=Zero(); + LatticeFermion tmp(FGrid); + LatticeFermion err(FGrid); + LatticeGaugeField Umu(UGrid); + + FieldMetaData header; + std::string file("ckpoint_lat.4000"); + NerscIO::readConfiguration(Umu,header,file); + + RealD mass=0.5; + RealD M5=1.8; + + DomainWallFermionD Ddwf(Umu,*FGrid,*FrbGrid,*UGrid,*UrbGrid,mass,M5); + DomainWallFermionD Dpv(Umu,*FGrid,*FrbGrid,*UGrid,*UrbGrid,1.0,M5); + + const int nbasis = 20; + const int cb = 0 ; + LatticeFermion prom(FGrid); + + typedef GeneralCoarsenedMatrix LittleDiracOperator; + typedef LittleDiracOperator::CoarseVector CoarseVector; + + NextToNearestStencilGeometry5D geom; + + std::cout< PVdagM(Ddwf,Dpv); + HermOpAdaptor HOA(PVdagM); + + // Run power method on HOA?? + PowerMethod PM; PM(HOA,src); + + // Warning: This routine calls PVdagM.Op, not PVdagM.HermOp + typedef Aggregation Subspace; + Subspace AggregatesPD(Coarse5d,FGrid,cb); + AggregatesPD.CreateSubspaceChebyshev(RNG5, + HOA, + nbasis, + 5000.0, + 0.02, + 100, + 50, + 50, + 0.0); + + LittleDiracOperator LittleDiracOpPV(geom,FGrid,Coarse5d); + LittleDiracOpPV.CoarsenOperator(PVdagM,AggregatesPD); + + std::cout<