From 624c0ac3ef894108609ec207102c6fb3ebdbf7c9 Mon Sep 17 00:00:00 2001 From: Peter Boyle Date: Mon, 25 May 2015 13:43:58 +0100 Subject: [PATCH] Updates now schur red black solver working --- lib/algorithms/SparseMatrix.h | 18 ++--- lib/algorithms/iterative/ConjugateGradient.h | 49 ++++++++----- lib/algorithms/iterative/SchurRedBlack.h | 73 +++++++++++--------- 3 files changed, 82 insertions(+), 58 deletions(-) diff --git a/lib/algorithms/SparseMatrix.h b/lib/algorithms/SparseMatrix.h index 6c54fc92..7bfe959b 100644 --- a/lib/algorithms/SparseMatrix.h +++ b/lib/algorithms/SparseMatrix.h @@ -10,6 +10,7 @@ namespace Grid { ///////////////////////////////////////////////////////////////////////////////////////////// template class SparseMatrixBase { public: + GridBase *_grid; // Full checkerboar operations virtual RealD M (const Field &in, Field &out)=0; virtual RealD Mdag (const Field &in, Field &out)=0; @@ -18,6 +19,7 @@ namespace Grid { ni=M(in,tmp); no=Mdag(tmp,out); } + SparseMatrixBase(GridBase *grid) : _grid(grid) {}; }; ///////////////////////////////////////////////////////////////////////////////////////////// @@ -25,7 +27,7 @@ namespace Grid { ///////////////////////////////////////////////////////////////////////////////////////////// template class CheckerBoardedSparseMatrixBase : public SparseMatrixBase { public: - + GridBase *_cbgrid; // half checkerboard operaions virtual void Meooe (const Field &in, Field &out)=0; virtual void Mooee (const Field &in, Field &out)=0; @@ -44,9 +46,7 @@ namespace Grid { Meooe(out,tmp); Mooee(in,out); - out=out-tmp; // axpy_norm - RealD n=norm2(out); - return n; + return axpy_norm(out,-1.0,tmp,out); } virtual RealD MpcDag (const Field &in, Field &out){ Field tmp(in._grid); @@ -56,15 +56,15 @@ namespace Grid { MeooeDag(out,tmp); MooeeDag(in,out); - out=out-tmp; // axpy_norm - RealD n=norm2(out); - return n; + return axpy_norm(out,-1.0,tmp,out); } - virtual void MpcDagMpc(const Field &in, Field &out,RealD ni,RealD no) { + virtual void MpcDagMpc(const Field &in, Field &out,RealD &ni,RealD &no) { Field tmp(in._grid); ni=Mpc(in,tmp); - no=Mpc(tmp,out); + no=MpcDag(tmp,out); + // std::cout<<"MpcDagMpc "<(grid), _cbgrid(cbgrid) {}; }; } diff --git a/lib/algorithms/iterative/ConjugateGradient.h b/lib/algorithms/iterative/ConjugateGradient.h index 85b196e9..d4f89662 100644 --- a/lib/algorithms/iterative/ConjugateGradient.h +++ b/lib/algorithms/iterative/ConjugateGradient.h @@ -9,17 +9,21 @@ namespace Grid { ///////////////////////////////////////////////////////////// template - class ConjugateGradient : public OperatorFunction { + class ConjugateGradient : public HermitianOperatorFunction { public: RealD Tolerance; Integer MaxIterations; - + int verbose; ConjugateGradient(RealD tol,Integer maxit) : Tolerance(tol), MaxIterations(maxit) { + verbose=0; }; - void operator() (LinearOperatorBase &Linop,const Field &src, Field &psi) {assert(0);}; + void operator() (HermitianOperatorBase &Linop,const Field &src, Field &psi){ + psi.checkerboard = src.checkerboard; + conformable(psi,src); + RealD cp,c,a,d,b,ssq,qq,b_pred; Field p(src); @@ -37,14 +41,16 @@ public: a =norm2(p); cp =a; ssq=norm2(src); - - std::cout < sol_e = M_ee^-1 * ( src_e - Meo sol_o )... * */ - namespace Grid { /////////////////////////////////////////////////////////////////////////////////////////////////////// // Take a matrix and form a Red Black solver calling a Herm solver // Use of RB info prevents making SchurRedBlackSolve conform to standard interface /////////////////////////////////////////////////////////////////////////////////////////////////////// - template class SchurRedBlackSolve : public OperatorFunction{ + template class SchurRedBlackSolve { private: - SparseMatrixBase & _Matrix; - OperatorFunction & _HermitianRBSolver; + HermitianOperatorFunction & _HermitianRBSolver; int CBfactorise; public: ///////////////////////////////////////////////////// // Wrap the usual normal equations Schur trick ///////////////////////////////////////////////////// - SchurRedBlackSolve(SparseMatrixBase &Matrix, OperatorFunction &HermitianRBSolver) - : _Matrix(Matrix), _HermitianRBSolver(HermitianRBSolver) { + SchurRedBlackSolve(HermitianOperatorFunction &HermitianRBSolver) : + _HermitianRBSolver(HermitianRBSolver) + { CBfactorise=0; }; - void operator() (const Field &in, Field &out){ + template + void operator() (Matrix & _Matrix,const Field &in, Field &out){ // FIXME CGdiagonalMee not implemented virtual function - // FIXME need to make eo grid from full grid. // FIXME use CBfactorise to control schur decomp - const int Even=0; - const int Odd =1; - - // Make a cartesianRedBlack from full Grid - GridRedBlackCartesian grid(in._grid); + GridBase *grid = _Matrix._cbgrid; + GridBase *fgrid= _Matrix._grid; - Field src_e(&grid); - Field src_o(&grid); - Field sol_e(&grid); - Field sol_o(&grid); - Field tmp(&grid); - Field Mtmp(&grid); - + Field src_e(grid); + Field src_o(grid); + Field sol_e(grid); + Field sol_o(grid); + Field tmp(grid); + Field Mtmp(grid); + Field resid(fgrid); + pickCheckerboard(Even,src_e,in); pickCheckerboard(Odd ,src_o,in); ///////////////////////////////////////////////////// // src_o = Mdag * (source_o - Moe MeeInv source_e) ///////////////////////////////////////////////////// - _Matrix.MooeeInv(src_e,tmp); // MooeeInv(source[Even],tmp,DaggerNo,Even); - _Matrix.Meooe (tmp,Mtmp); // Meo (tmp,src,Odd,DaggerNo); - tmp=src_o-Mtmp; // axpy (tmp,src,source[Odd],-1.0); - _Matrix.MpcDag(tmp,src_o); // Mprec(tmp,src,Mtmp,DaggerYes); + _Matrix.MooeeInv(src_e,tmp); assert( tmp.checkerboard ==Even); + _Matrix.Meooe (tmp,Mtmp); assert( Mtmp.checkerboard ==Odd); + tmp=src_o-Mtmp; assert( tmp.checkerboard ==Odd); + _Matrix.MpcDag(tmp,src_o); assert(src_o.checkerboard ==Odd); ////////////////////////////////////////////////////////////// // Call the red-black solver ////////////////////////////////////////////////////////////// - _HermitianRBSolver(src_o,sol_o); // CGNE_prec_MdagM(solution[Odd],src); + HermitianCheckerBoardedOperator _HermOpEO(_Matrix); + std::cout << "SchurRedBlack solver calling the MpcDagMp solver" <