CHROMA
inv_rel_cg1.cc
Go to the documentation of this file.
1 /*! \file
2  * \brief Conjugate-Gradient algorithm for a generic Linear Operator
3  */
4 
5 #include "chromabase.h"
7 
8 namespace Chroma {
9 
10 //! Conjugate-Gradient (CGNE) algorithm for a generic Linear Operator
11 /*! \ingroup invert
12  * This subroutine uses the Conjugate Gradient (CG) algorithm to find
13  * the solution of the set of linear equations
14  *
15  * Chi = A . Psi
16  */
17 #undef CHROMA_INV_REL_CG1_RSD_CHK
18 
19 template<typename T>
21  const T& chi,
22  T& psi,
23  const Real& RsdCG,
24  int MaxCG,
25  int& n_count)
26 {
27  START_CODE();
28 
29  const Subset& s = A.subset();
30 
31  Real chi_sq = Real(norm2(chi,s));
32  Real rsd_sq = (RsdCG * RsdCG)*chi_sq;
33 
34  //
35  // r[0] := Chi - A . Psi[0]
36 
37  //
38  // r := [ Chi - A . psi ]
39 
40 
41  psi[s] = zero;
42 
43  T tmp1, tmp2;
44  T r;
45 
46  r[s] = chi;
47  Real inner_tol ;
48 
49  // p[1] := r[0]
50  T p;
51  p[s] = r;
52 
53 
54  // Cp = |r[0]|^2
55  Double c = norm2(r, s); /* 2 Nc Ns flops */
56  Double cp = c;
57  Double zeta = Double(1)/c;
58 
59  QDPIO::cout << "InvRelCG1: k = 0 cp = " << cp << " rsd_sq = " << rsd_sq
60 << std::endl;
61 
62  // IF |r[0]| <= RsdCG |Chi| THEN RETURN;
63  if ( toBool(cp <= rsd_sq) )
64  {
65  n_count = 0;
66  END_CODE();
67  return;
68  }
69 
70  //
71  // FOR k FROM 1 TO MaxCG DO
72  //
73  LatticeFermion q;
74  q=zero;
75 
76  for(int k = 1; k <= MaxCG; ++k) {
77 
78  // The sqrt(norm2(p)) part is taken care of by using relative residua
79  // in the inner solve
80  inner_tol = sqrt(rsd_sq)*sqrt(zeta);
81 
82  A(q,p,PLUS,inner_tol);
83 
84 #if 0
85  // Funkyness work out || Ap - q || / || p ||
86  LatticeFermion tmp;
87  tmp = zero;
88  A(tmp,p,PLUS);
89  tmp[s] -= q;
90  Double tmpnorm=sqrt(norm2(tmp,s));
91  QDPIO::cout << "Inner tol = " << inner_tol << " || Ap - q || = " << tmpnorm << std::endl;
92 #endif
93 
94  // a[k] := | r[k-1] |**2 / < p[k], Ap[k] > ;
95  // +
96  // First compute d = < q,p >
97  Double d = innerProductReal(q, p, s);
98  // a = Real(c);
99  Real a = Real(c)/Real(d);
100 
101  // Psi[k] += a[k] p[k]
102  psi[s] += a * p; /* 2 Nc Ns flops */
103 
104  // r[k] -= a[k] A . p[k] ;
105  //
106  // r = r - a A p
107  r[s] -= a * q;
108 
109  c = norm2(r,s);
110 
111 #ifdef CHROMA_INV_REL_CG1_RSD_CHK
112  {
113  LatticeFermion rcheck;
114  A(rcheck, psi, PLUS);
115  rcheck[s] -= chi;
116  QDPIO::cout << "InvCG1: inter " << k<< " || b - Ax ||^2 = " << norm2(rcheck,s) << " || r ||^2 = " << c << std::endl;
117  }
118 #endif
119 
120  zeta += Double(1)/c;
121 
122  // b[k+1] := |r[k]|**2 / |r[k-1]|**2
123  Real b = Real(c) / Real(cp);
124  // p[k+1] := r[k] + b[k+1] p[k]
125  p[s] = r + b*p; /* Nc Ns flops */
126 
127  cp = c; /* 2 Nc Ns flops */
128 
129  if ( toBool(cp <= rsd_sq) )
130  {
131  n_count = k;
132  END_CODE();
133  return;
134  }
135  }
136  n_count = MaxCG;
137  QDPIO::cerr << "Nonconvergence Warning: n_count =" << n_count << std::endl;
138  END_CODE();
139 }
140 
141 
142 // Fix here for now
143 template<>
145  const LatticeFermion& chi,
146  LatticeFermion& psi,
147  const Real& RsdCG,
148  int MaxCG,
149  int& n_count)
150 {
152 }
153 
154 } // end namespace Chroma
Primary include file for CHROMA library code.
Linear Operator.
Definition: linearop.h:27
void InvRelCG1_a(const LinearOperator< T > &A, const T &chi, T &psi, const Real &RsdCG, int MaxCG, int &n_count)
Conjugate-Gradient (CGNE) algorithm for a generic Linear Operator.
Definition: inv_rel_cg1.cc:20
void InvRelCG1(const LinearOperator< LatticeFermion > &A, const LatticeFermion &chi, LatticeFermion &psi, const Real &RsdCG, int MaxCG, int &n_count)
Conjugate-Gradient (CGNE) algorithm for a generic Linear Operator.
Definition: inv_rel_cg1.cc:144
Conjugate-Gradient algorithm for a generic Linear Operator.
Double q
Definition: mesq.cc:17
Double tmp2
Definition: mesq.cc:30
Asqtad Staggered-Dirac operator.
Definition: klein_gord.cc:10
const WilsonTypeFermAct< multi1d< LatticeFermion > > Handle< const ConnectState > const multi1d< Real > enum InvType invType const multi1d< Real > & RsdCG
Definition: pbg5p_w.cc:30
int n_count
Definition: invbicg.cc:78
LatticeFermion tmp
Definition: mespbg5p_w.cc:36
Double c
Definition: invbicg.cc:108
LinOpSysSolverMGProtoClover::T T
const WilsonTypeFermAct< multi1d< LatticeFermion > > Handle< const ConnectState > const multi1d< Real > enum InvType invType const multi1d< Real > int MaxCG
Definition: pbg5p_w.cc:32
Real rsd_sq
Definition: invbicg.cc:121
@ PLUS
Definition: chromabase.h:45
Double cp
Definition: invbicg.cc:107
multi1d< LatticeFermion > chi(Ncb)
Complex a
Definition: invbicg.cc:95
LatticeFermion psi
Definition: mespbg5p_w.cc:35
DComplex d
Definition: invbicg.cc:99
START_CODE()
A(A, psi, r, Ncb, PLUS)
Complex b
Definition: invbicg.cc:96
Double zero
Definition: invbicg.cc:106
int k
Definition: invbicg.cc:119
multi1d< LatticeFermion > s(Ncb)
FloatingPoint< double > Double
Definition: gtest.h:7351