CHROMA
syssolver_linop_cg.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /*! \file
3  * \brief Solve a M*psi=chi linear system by CG2
4  */
5 
6 #ifndef __syssolver_linop_cg_h__
7 #define __syssolver_linop_cg_h__
8 #include "chroma_config.h"
9 #include "handle.h"
10 #include "syssolver.h"
11 #include "linearop.h"
15 
16 
17 namespace Chroma
18 {
19 
20  //! CG system solver namespace
21  namespace LinOpSysSolverCGEnv
22  {
23  //! Register the syssolver
24  bool registerAll();
25  }
26 
27 
28  //! Solve a M*psi=chi linear system by CG2
29  /*! \ingroup invert
30  */
31  template<typename T>
33  {
34  public:
35  //! Constructor
36  /*!
37  * \param M_ Linear operator ( Read )
38  * \param invParam inverter parameters ( Read )
39  */
41  const SysSolverCGParams& invParam_) :
42  A(A_), invParam(invParam_)
43  {}
44 
45  //! Destructor is automatic
47 
48  //! Return the subset on which the operator acts
49  const Subset& subset() const {return A->subset();}
50 
51  //! Solver the linear system
52  /*!
53  * \param psi solution ( Modify )
54  * \param chi source ( Read )
55  * \return syssolver results
56  */
58  {
59  START_CODE();
60  SystemSolverResults_t res; // initialized by a constructor
61  StopWatch swatch;
62  swatch.reset();
63  swatch.start();
64 
65  T chi_tmp;
66  (*A)(chi_tmp, chi, MINUS);
67  res = InvCG2(*A, chi_tmp, psi, invParam.RsdCG, invParam.MaxCG);
68 
69 #ifdef CHROMA_DO_ONE_CG_RESTART
70  // Save existing n_count
71  int n_count = res.n_count;
72 
73  // One automatic restart (if enabled)
75  res.n_count += n_count;
76 #endif
77  swatch.stop();
78  double time = swatch.getTimeInSeconds();
79 
80  {
81  T r;
82  r[A->subset()]=chi;
83  T tmp;
84  (*A)(tmp, psi, PLUS);
85  r[A->subset()] -= tmp;
86  res.resid = sqrt(norm2(r, A->subset()));
87  }
88  QDPIO::cout << "CG_SOLVER: " << res.n_count << " iterations. Rsd = " << res.resid << " Relative Rsd = " << res.resid/sqrt(norm2(chi,A->subset())) << std::endl;
89  QDPIO::cout << "CG_SOLVER_TIME: "<<time<< " sec" << std::endl;
90 
91 
92 
93  END_CODE();
94 
95  return res;
96  }
97 
98 
99  private:
100  // Hide default constructor
102 
105  };
106 
107 } // End namespace
108 
109 #endif
110 
Class for counted reference semantics.
Definition: handle.h:33
Solve a M*psi=chi linear system by CG2.
const Subset & subset() const
Return the subset on which the operator acts.
~LinOpSysSolverCG()
Destructor is automatic.
LinOpSysSolverCG(Handle< LinearOperator< T > > A_, const SysSolverCGParams &invParam_)
Constructor.
SystemSolverResults_t operator()(T &psi, const T &chi) const
Solver the linear system.
Handle< LinearOperator< T > > A
SystemSolver disambiguator.
Linear Operator.
Definition: linearop.h:27
SystemSolverResults_t InvCG2(const LinearOperator< LatticeFermionF > &M, const LatticeFermionF &chi, LatticeFermionF &psi, const Real &RsdCG, int MaxCG)
Conjugate-Gradient (CGNE) algorithm for a generic Linear Operator.
Definition: invcg2.cc:240
Class for counted reference semantics.
Conjugate-Gradient algorithm for a generic Linear Operator.
Linear Operators.
bool registerAll()
Register all the factories.
Asqtad Staggered-Dirac operator.
Definition: klein_gord.cc:10
int n_count
Definition: invbicg.cc:78
LatticeFermion tmp
Definition: mespbg5p_w.cc:36
LinOpSysSolverMGProtoClover::T T
@ MINUS
Definition: chromabase.h:45
@ PLUS
Definition: chromabase.h:45
multi1d< LatticeFermion > chi(Ncb)
LatticeFermion psi
Definition: mespbg5p_w.cc:35
START_CODE()
Params for CG inverter.
Holds return info from SystemSolver call.
Definition: syssolver.h:17
Linear system solvers.
Solve a CG1 system.
Disambiguator for LinOp system solvers.