/************************************************************************************* Grid physics library, www.github.com/paboyle/Grid Source file: ./examples/Example_pvdagm_3level.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 using namespace std; using namespace Grid; template void readFile(T& out, std::string const fname){ #ifdef HAVE_LIME std::cout << Grid::GridLogMessage << "Reading: " << fname << std::endl; Grid::emptyUserRecord record; Grid::ScidacReader SR; SR.open(fname); SR.readScidacFieldRecord(out, record); SR.close(); #endif } template void writeFile(T& in, std::string const fname){ #ifdef HAVE_LIME std::cout << Grid::GridLogMessage << "Writing: " << fname << std::endl; Grid::emptyUserRecord record; Grid::ScidacWriter SW(in.Grid()->IsBoss()); SW.open(fname); SW.writeScidacFieldRecord(in, record); SW.close(); #endif } template void saveSubspace(std::vector &subspace, std::string const fname){ #ifdef HAVE_LIME std::cout << Grid::GridLogMessage << "Saving subspace (" << subspace.size() << " vectors) to: " << fname << std::endl; Grid::emptyUserRecord record; Grid::ScidacWriter SW(subspace[0].Grid()->IsBoss()); SW.open(fname); for (int k = 0; k < (int)subspace.size(); k++) SW.writeScidacFieldRecord(subspace[k], record); SW.close(); #endif } template void loadSubspace(std::vector &subspace, std::string const fname){ #ifdef HAVE_LIME std::cout << Grid::GridLogMessage << "Loading subspace (" << subspace.size() << " vectors) from: " << fname << std::endl; Grid::emptyUserRecord record; Grid::ScidacReader SR; SR.open(fname); for (int k = 0; k < (int)subspace.size(); k++) SR.readScidacFieldRecord(subspace[k], record); SR.close(); #endif } template class PVdagMLinearOperator : public LinearOperatorBase { Matrix &_Mat; Matrix &_PV; int nApp; int nAppDag; public: PVdagMLinearOperator(Matrix &Mat,Matrix &PV): _Mat(Mat),_PV(PV), nApp(0), nAppDag(0) {}; 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); nApp++; } void AdjOp (const Field &in, Field &out){ Field tmp(in.Grid()); _PV.M(in,tmp); _Mat.Mdag(tmp,out); nAppDag++; } void clear() { nApp = 0; nAppDag = 0; } void getApplications() { std::cout << GridLogMessage << "# applications of PVdagM: " << nApp << std::endl; std::cout << GridLogMessage << "# applications of PVdagM^dag: " << nAppDag << std::endl; std::cout << GridLogMessage << "# applications total: " << nApp + nAppDag << std::endl; } void HermOpAndNorm(const Field &in, Field &out,RealD &n1,RealD &n2){ HermOp(in,out); ComplexD dot = innerProduct(in,out); n1=real(dot); n2=norm2(out); } void HermOp(const Field &in, Field &out){ Field tmp(in.Grid()); Op(in,tmp); AdjOp(tmp,out); } }; template class MdagPVLinearOperator : public LinearOperatorBase { Matrix &_Mat; Matrix &_PV; public: MdagPVLinearOperator(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()); _PV.M(in,tmp); _Mat.Mdag(tmp,out); } void AdjOp (const Field &in, Field &out){ Field tmp(in.Grid()); _Mat.M(in,tmp); _PV.Mdag(tmp,out); } void HermOpAndNorm(const Field &in, Field &out,RealD &n1,RealD &n2){ ComplexD dot = innerProduct(in,out); n1=real(dot); n2=norm2(out); } void HermOp(const Field &in, Field &out){ Field tmp(in.Grid()); Op(in,tmp); AdjOp(tmp,out); } }; template class ShiftedPVdagMLinearOperator : public LinearOperatorBase { Matrix &_Mat; Matrix &_PV; RealD shift; public: ShiftedPVdagMLinearOperator(RealD _shift,Matrix &Mat,Matrix &PV): shift(_shift),_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); out = out + shift * in; } void AdjOp (const Field &in, Field &out){ Field tmp(in.Grid()); _PV.M(tmp,out); _Mat.Mdag(in,tmp); out = out + shift * in; } void HermOpAndNorm(const Field &in, Field &out,RealD &n1,RealD &n2){ assert(0); } void HermOp(const Field &in, Field &out){ Field tmp(in.Grid()); Op(in,tmp); AdjOp(tmp,out); } }; // Lüscher deflated guesser (arXiv:0706.2298 Sec A.3) for a non-Hermitian solve. // C_{st} = ; guess = sum_s c_s psi[s] where c = C^{-1} psi† src. template class LuscherGuesser : public LinearFunction { const std::vector ψ Eigen::MatrixXcd C_inv; public: using LinearFunction::operator(); LuscherGuesser(const std::vector &psi_, const Eigen::MatrixXcd &Cinv_) : psi(psi_), C_inv(Cinv_) {} virtual void operator()(const Field &src, Field &guess) { int N = psi.size(); Eigen::VectorXcd b(N); for (int t = 0; t < N; t++) b(t) = TensorRemove(innerProduct(psi[t], src)); Eigen::VectorXcd c = C_inv * b; guess = Zero(); for (int s = 0; s < N; s++) guess += ComplexD(c(s)) * psi[s]; } }; template class MGPreconditioner : public LinearFunction< Lattice > { public: using LinearFunction >::operator(); typedef Aggregation Aggregates; typedef typename Aggregation::FineField FineField; typedef typename Aggregation::CoarseVector CoarseVector; typedef typename Aggregation::CoarseMatrix CoarseMatrix; typedef LinearOperatorBase FineOperator; typedef LinearFunction FineSmoother; typedef LinearOperatorBase CoarseOperator; typedef LinearFunction CoarseSolver; Aggregates & _Aggregates; FineOperator & _FineOperator; FineSmoother & _PreSmoother; FineSmoother & _PostSmoother; CoarseOperator & _CoarseOperator; CoarseSolver & _CoarseSolve; CoarseSolver & _CoarseGuesser; int level; void Level(int lv) {level = lv; }; MGPreconditioner(Aggregates &Agg, FineOperator &Fine, FineSmoother &PreSmoother, FineSmoother &PostSmoother, CoarseOperator &CoarseOperator_, CoarseSolver &CoarseSolve_, CoarseSolver &CoarseGuesser_) : _Aggregates(Agg), _FineOperator(Fine), _PreSmoother(PreSmoother), _PostSmoother(PostSmoother), _CoarseOperator(CoarseOperator_), _CoarseSolve(CoarseSolve_), _CoarseGuesser(CoarseGuesser_), level(1) { } virtual void operator()(const FineField &in, FineField & out) { GridBase *CoarseGrid = _Aggregates.CoarseGrid; CoarseVector Csrc(CoarseGrid); CoarseVector Csol(CoarseGrid); FineField vec1(in.Grid()); FineField vec2(in.Grid()); double t; out = Zero(); t=-usecond(); _PreSmoother(in,out); t+=usecond(); std::cout< class ShiftedLinearOperator : public LinearOperatorBase { LinearOperatorBase &_Op; RealD shift; public: ShiftedLinearOperator(RealD _shift, LinearOperatorBase &Op) : shift(_shift), _Op(Op) {} 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) { _Op.Op(in, out); out = out + shift * in; } void AdjOp (const Field &in, Field &out) { _Op.AdjOp(in, out); out = out + shift * in; } void HermOpAndNorm(const Field &in, Field &out, RealD &n1, RealD &n2) { assert(0); } void HermOp (const Field &in, Field &out) { Field tmp(in.Grid()); Op(in,tmp); AdjOp(tmp,out); } }; template void runMG( GridCartesian *FGrid, GridCartesian *Coarse5d, GridCartesian *CoarseCoarse5d, NextToNearestStencilGeometry5D geom, PVdagM_t &PVdagM, ShiftedPVdagM_t &ShiftedPVdagM, Subspace &AggregatesPD ) { std::vector subspace = AggregatesPD.subspace; assert((int)subspace.size() == NB); const int nbasis = NB; const int cb = 0; CoarseVector c_src(Coarse5d); CoarseVector c_res(Coarse5d); Complex one(1.0); LatticeFermionD f_src(FGrid); LatticeFermionD f_res(FGrid); TrivialPrecon simpleC; TrivialPrecon simple_fine; ////////////////////////////////////////////////////////////////////// // Level 0→1: coarsen PVdagM, build LinOpCoarse ////////////////////////////////////////////////////////////////////// LittleDiracOperator LittleDiracOpPV(geom, FGrid, Coarse5d); LittleDiracOpPV.CoarsenOperator(PVdagM, AggregatesPD); NonHermitianLinearOperator LinOpCoarse(LittleDiracOpPV); ////////////////////////////////////////////////////////////////////// // Baseline: plain PGCR on LinOpCoarse (reference for comparison) ////////////////////////////////////////////////////////////////////// std::cout< L2PGCR_baseline(3.0e-2,1100,LinOpCoarse,simpleC,10,10); L2PGCR_baseline.Level(2); L2PGCR_baseline.Name("Cbaseline"); c_src = one; c_res = Zero(); L2PGCR_baseline(c_src,c_res); ////////////////////////////////////////////////////////////////////// // psi_coarse: coarse projections of pre-GS fine null vectors. // These are the Level 1 near-null vectors, promoted from Level 0. // Used as the aggregation basis for Level 1→2 coarsening. ////////////////////////////////////////////////////////////////////// std::vector psi_coarse(nbasis, Coarse5d); for (int k = 0; k < nbasis; k++) AggregatesPD.ProjectToSubspace(psi_coarse[k], subspace[k]); ////////////////////////////////////////////////////////////////////// // Diagnostics: W (fine projected matrix) and C (Galerkin check) ////////////////////////////////////////////////////////////////////// { Eigen::MatrixXcd W = Eigen::MatrixXcd::Zero(nbasis, nbasis); LatticeFermion ftmp(FGrid); for (int j = 0; j < nbasis; j++) { PVdagM.Op(subspace[j], ftmp); for (int i = 0; i < nbasis; i++) W(i,j) = TensorRemove(innerProduct(subspace[i], ftmp)); } RealD normW = W.norm(); std::cout << GridLogMessage << "Fine projected matrix ||W|| = " << normW << std::endl; Eigen::MatrixXcd C = Eigen::MatrixXcd::Zero(nbasis, nbasis); CoarseVector Ac(Coarse5d); for (int l = 0; l < nbasis; l++) { LinOpCoarse.Op(psi_coarse[l], Ac); for (int k = 0; k < nbasis; k++) C(k,l) = TensorRemove(innerProduct(psi_coarse[k], Ac)); } RealD normC = C.norm(); RealD normCmCdag = (C - C.adjoint()).norm(); std::cout << GridLogMessage << "Coarse null matrix ||C|| = " << normC << std::endl; std::cout << GridLogMessage << "Coarse null matrix ||C - C†||/||C|| = " << normCmCdag/normC << std::endl; std::cout << GridLogMessage << "Galerkin check ||C||/||W|| = " << normC/normW << std::endl; } ////////////////////////////////////////////////////////////////////// // Level 1→2: set up aggregation using psi_coarse as subspace. // Block factor 2,2,3,2 (removes odd local sublattice in z given MPI // geometry 3×6×4×4 where z-local at Level 1 is 6). // psi_coarse are assigned directly; CoarsenOperator performs // block-GS orthogonalisation before building LinOpCoarseCoarse. ////////////////////////////////////////////////////////////////////// // innerProduct(CoarseSiteObj, CoarseSiteObj) returns iScalar, so CComplex // for the L1→L2 level must be iScalar, not vTComplex. typedef typename CoarseVector::vector_object CoarseSiteObj; typedef iScalar vTTComplex; typedef GeneralCoarsenedMatrix LittleDiracOperatorL2; typedef typename LittleDiracOperatorL2::CoarseVector CoarseCoarseVector; typedef Aggregation SubspaceL2; typedef MGPreconditioner L1to2MG; SubspaceL2 AggregatesL2(CoarseCoarse5d, Coarse5d, cb); for (int k = 0; k < nbasis; k++) AggregatesL2.subspace[k] = psi_coarse[k]; NextToNearestStencilGeometry5D geom2(CoarseCoarse5d); LittleDiracOperatorL2 LittleDiracOpL2(geom2, Coarse5d, CoarseCoarse5d); LittleDiracOpL2.CoarsenOperator(LinOpCoarse, AggregatesL2); NonHermitianLinearOperator LinOpCC(LittleDiracOpL2); TrivialPrecon simpleCC; ////////////////////////////////////////////////////////////////////// // Lüscher deflation guesser for L3PGCR. // Step 1: project psi_coarse[k] (promoted fine null vectors) to // CoarseCoarseVector space — these cover the zero-momentum // component of the near-null space of LinOpCC. // Step 2: breed Nextra additional null vectors directly on LinOpCC // using GCR with random sources — these pick up near-null // modes at all spatial frequencies not spanned by step 1. // Step 3: build C_{st} = over the // full augmented basis and invert directly via Eigen LU. ////////////////////////////////////////////////////////////////////// std::vector psi_cc(nbasis, CoarseCoarse5d); for (int k = 0; k < nbasis; k++) AggregatesL2.ProjectToSubspace(psi_cc[k], psi_coarse[k]); { int Nextra = nbasis; // breed as many extra as we have promoted ones if ( getenv("CC_NEXTRA") ) Nextra = atoi(getenv("CC_NEXTRA")); GridParallelRNG RNG_CC(CoarseCoarse5d); RNG_CC.SeedFixedIntegers({11,13,17,19}); PrecGeneralisedConjugateResidualNonHermitian nullGCR(1e-2, 200, LinOpCC, simpleCC, 32, 32); CoarseCoarseVector tmp(CoarseCoarse5d); for (int k = 0; k < Nextra; k++) { CoarseCoarseVector src(CoarseCoarse5d); gaussian(RNG_CC, src); tmp = Zero(); nullGCR(src, tmp); psi_cc.push_back(tmp); } std::cout << GridLogMessage << "LinOpCC deflation basis: " << nbasis << " promoted + " << Nextra << " bred = " << psi_cc.size() << " total" << std::endl; } const int Naug = psi_cc.size(); Eigen::MatrixXcd Ccc = Eigen::MatrixXcd::Zero(Naug, Naug); { CoarseCoarseVector Acc(CoarseCoarse5d); for (int l = 0; l < Naug; l++) { LinOpCC.Op(psi_cc[l], Acc); for (int k = 0; k < Naug; k++) Ccc(k,l) = TensorRemove(innerProduct(psi_cc[k], Acc)); } } { RealD normCcc = Ccc.norm(); RealD normCccmCdag = (Ccc - Ccc.adjoint()).norm(); std::cout << GridLogMessage << "Coarse-coarse deflation matrix ||Ccc|| = " << normCcc << std::endl; std::cout << GridLogMessage << "Coarse-coarse deflation matrix ||Ccc-Ccc†||/||Ccc|| = " << normCccmCdag/normCcc << std::endl; } Eigen::MatrixXcd Ccc_inv = Ccc.inverse(); LuscherGuesser CCDeflGuesser(psi_cc, Ccc_inv); ////////////////////////////////////////////////////////////////////// // Level 2 solver: plain GCR, no further coarsening ////////////////////////////////////////////////////////////////////// PrecGeneralisedConjugateResidualNonHermitian L3PGCR(1.0e-1,200,LinOpCC,simpleCC,16,16); L3PGCR.Level(3); L3PGCR.Name("CCouter"); ////////////////////////////////////////////////////////////////////// // Coarse-level GCR smoother for Level 1→2 V-cycle. // Mirrors fine-grid SmootherGCR: shifted operator + fixed step count. // coarse_smoother_shift and coarse_smoother_nstep are the tuning knobs. ////////////////////////////////////////////////////////////////////// RealD coarse_smoother_shift = 0.01; int coarse_smoother_nstep = 12; if(getenv("coarse_smoother_shift")) coarse_smoother_shift = atof(getenv("coarse_smoother_shift")); if(getenv("coarse_smoother_nstep")) coarse_smoother_nstep = atoi(getenv("coarse_smoother_nstep")); ShiftedLinearOperator ShiftedLinOpCoarse(coarse_smoother_shift, LinOpCoarse); PrecGeneralisedConjugateResidualNonHermitian CoarseSmootherGCR(0.01,1,ShiftedLinOpCoarse,simpleC,coarse_smoother_nstep,coarse_smoother_nstep); CoarseSmootherGCR.SetZeroGuess(1); // smoother slot: caller zeroes guess CoarseSmootherGCR.Level(2); CoarseSmootherGCR.Name("Csmoother"); ////////////////////////////////////////////////////////////////////// // Level 1→2 V-cycle preconditioner. ////////////////////////////////////////////////////////////////////// L1to2MG L1to2Precon(AggregatesL2, LinOpCoarse, simpleC, // no pre-smoother (matches fine-grid setup) CoarseSmootherGCR, // post-smoother: 12 GCR steps LinOpCC, L3PGCR, CCDeflGuesser); // Lüscher guesser: psi_cc C^{-1} psi_cc† ////////////////////////////////////////////////////////////////////// // Standalone Level 1 two-level solve test. // Compare against plain PGCR baseline above. ////////////////////////////////////////////////////////////////////// std::cout< L2MGsolver(3.0e-2,200,LinOpCoarse,L1to2Precon,16,16); L2MGsolver.Level(2); L2MGsolver.Name("Couter"); c_res = Zero(); L2MGsolver(c_src,c_res); std::cout << GridLogMessage << "Level 1 two-level test: PVdagM operator uses:" << std::endl; PVdagM.getApplications(); PVdagM.clear(); ////////////////////////////////////////////////////////////////////// // Full three-level outer solve ////////////////////////////////////////////////////////////////////// std::cout< SmootherGCR(0.01,1,ShiftedPVdagM,simple_fine,16,16); SmootherGCR.SetZeroGuess(1); // pre/post smoother slots zero their guess SmootherGCR.Level(1); SmootherGCR.Name("Fsmoother"); f_src = one; // Pre-smoother: none (TrivialPrecon); post-smoother: shifted PGCR. // Coarse solver: L2MGsolver (PGCR preconditioned by Level 1→2 V-cycle). TwoLevelMG ThreeLevelPrecon(AggregatesPD, PVdagM, simple_fine, SmootherGCR, LinOpCoarse, L2MGsolver, simpleC); PrecGeneralisedConjugateResidualNonHermitian L1PGCR(1.0e-8,1000,PVdagM,ThreeLevelPrecon,16,16); L1PGCR.Level(1); L1PGCR.Name("Fouter"); f_res = Zero(); L1PGCR(f_src,f_res); std::cout << GridLogMessage << "Three-level outer solve: PVdagM operator uses:" << std::endl; PVdagM.getApplications(); PVdagM.clear(); } int main (int argc, char ** argv) { Grid_init(&argc,&argv); const int Ls = 24; RealD M5 = 1.8; RealD b = 1.5; RealD c = 0.5; RealD mass = 0.00078; if ( getenv("MASS") ) mass = atof(getenv("MASS")); const int nbasis = 60; std::cout << GridLogMessage << "Mass: " << mass << ", Ls: " << Ls << ", b=" << b << ", c=" << c << std::endl; std::cout << GridLogMessage << "nbasis: " << nbasis << std::endl; std::vector lat_size {48, 48, 48, 96}; GridCartesian * UGrid = SpaceTimeGrid::makeFourDimGrid(lat_size, GridDefaultSimd(Nd,vComplex::Nsimd()),GridDefaultMpi()); GridRedBlackCartesian * UrbGrid = SpaceTimeGrid::makeFourDimRedBlackGrid(UGrid); GridCartesian * FGrid = SpaceTimeGrid::makeFiveDimGrid(Ls,UGrid); GridRedBlackCartesian * FrbGrid = SpaceTimeGrid::makeFiveDimRedBlackGrid(Ls,UGrid); // Level 1 coarse grid: block 2^4 from fine (48×48×48×96 → 24×24×24×48, Ls=1) Coordinate clatt = lat_size; for (int d = 0; d < 4; d++) clatt[d] /= 2; std::cout << GridLogMessage << "Level 1 coarse lattice: " << clatt << std::endl; GridCartesian *Coarse4d = SpaceTimeGrid::makeFourDimGrid(clatt, GridDefaultSimd(Nd,vComplex::Nsimd()),GridDefaultMpi()); GridCartesian *Coarse5d = SpaceTimeGrid::makeFiveDimGrid(1,Coarse4d); // Level 2 coarse-coarse grid: block 2,2,3,3 from Level 1 (24×24×24×48 → 12×12×8×16, Ls=1). // MPI geometry 3.6.4.4 (288 ranks): fine local {16,8,12,24}. // Level 1 local {8,4,6,12}; Level 2 local {4,2,2,4}. // z blocked by 3: z-Level1-local=6; 6/3=2 (even), 6/2=3 (odd) → must use 3. // t blocked by 3: t-Level1-local=12; 12/3=4 divisible by Nsimd=4 (gen-simd-width=64). // t-block=2 gives t2-local=6, 6 mod 4 ≠ 0, fails Grid SIMD assertion. ✓ // With {4,2,2,4}: Nsimd=4 goes into x or t (both =4). ✓ Coordinate clatt2 = clatt; clatt2[0] /= 2; clatt2[1] /= 2; clatt2[2] /= 3; clatt2[3] /= 3; std::cout << GridLogMessage << "Level 2 coarse-coarse lattice: " << clatt2 << std::endl; GridCartesian *CoarseCoarse4d = SpaceTimeGrid::makeFourDimGrid(clatt2, GridDefaultSimd(Nd,vComplex::Nsimd()),GridDefaultMpi()); GridCartesian *CoarseCoarse5d = SpaceTimeGrid::makeFiveDimGrid(1,CoarseCoarse4d); std::vector seeds4({1,2,3,4}); std::vector seeds5({5,6,7,8}); GridParallelRNG RNG5(FGrid); RNG5.SeedFixedIntegers(seeds5); GridParallelRNG RNG4(UGrid); RNG4.SeedFixedIntegers(seeds4); LatticeGaugeField Umu(UGrid); std::cout << GridLogMessage << "Reading gauge field" << std::endl; FieldMetaData header; std::string file("/ccs/home/poare/ckpoint_lat.1000"); NerscIO::readConfiguration(Umu,header,file); RealD b_ = 1.5; RealD c_ = 0.5; MobiusFermionD Ddwf(Umu,*FGrid,*FrbGrid,*UGrid,*UrbGrid,mass,M5,b_,c_); MobiusFermionD Dpv (Umu,*FGrid,*FrbGrid,*UGrid,*UrbGrid,1.0, M5,b_,c_); typedef PVdagMLinearOperator PVdagM_t; typedef ShiftedPVdagMLinearOperator ShiftedPVdagM_t; typedef GeneralCoarsenedMatrix LittleDiracOperator; typedef LittleDiracOperator::CoarseVector CoarseVector; typedef Aggregation Subspace; typedef MGPreconditioner TwoLevelMG; PVdagM_t PVdagM(Ddwf,Dpv); ShiftedPVdagM_t ShiftedPVdagM(0.01,Ddwf,Dpv); NextToNearestStencilGeometry5D geom(Coarse5d); // Subspace cache: save after generation, reload on subsequent runs to skip expensive setup. // Set SUBSPACE_FILE to override the default path. std::string subspace_file = "/lustre/orion/phy157/proj-shared/phy157_dwf/paboyle/subspace_nb" + std::to_string(nbasis) + ".scidac"; if ( getenv("SUBSPACE_FILE") ) subspace_file = std::string(getenv("SUBSPACE_FILE")); // Check if subspace file exists (boss rank checks, result broadcast via GlobalSum). uint64_t file_exists = 0; if ( UGrid->IsBoss() ) { std::ifstream f(subspace_file); file_exists = f.good() ? 1 : 0; } UGrid->GlobalSum(file_exists); const int cb = 0; Subspace AggregatesGCR(Coarse5d,FGrid,cb); if ( file_exists ) { std::cout << GridLogMessage << "*** Loading subspace from disk ***" << std::endl; loadSubspace(AggregatesGCR.subspace, subspace_file); // Insurance: GLOBAL (whole-lattice) orthonormalise, matching CreateSubspaceGCR // (Aggregates.h:196), in case the cached file predates it. Span-preserving // and globally orthonormal -- NOT the block Orthogonalise() below, which would // defeat the raw-null discipline (runMG promotes the RAW subspace to build L2; // block-GS here -> psi_coarse = e_k). The raw copy in runMG happens AFTER this. AggregatesGCR.GlobalOrthonormalise(); // AggregatesGCR.Orthogonalise(); std::cout << GridLogMessage << "Subspace loaded, globally orthonormalised (raw block basis preserved)." << std::endl; } else { std::cout << GridLogMessage << "*** GCR subspace generation ***" << std::endl; AggregatesGCR.CreateSubspaceGCR(RNG5,PVdagM,nbasis); std::cout << GridLogMessage << "Subspace generation: PVdagM operator uses:" << std::endl; PVdagM.getApplications(); PVdagM.clear(); saveSubspace(AggregatesGCR.subspace, subspace_file); std::cout << GridLogMessage << "Subspace saved to: " << subspace_file << std::endl; } runMG( FGrid, Coarse5d, CoarseCoarse5d, geom, PVdagM, ShiftedPVdagM, AggregatesGCR ); std::cout << GridLogMessage << "Done" << std::endl; Grid_finalize(); return 0; }