00001
00002
00003
00004
00005
00006 #include "chromabase.h"
00007 #include "actions/ferm/invert/invcg1.h"
00008
00009 using namespace QDP::Hints;
00010
00011 namespace Chroma {
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 template<typename T>
00069 SystemSolverResults_t
00070 InvCG1_a(const LinearOperator<T>& A,
00071 const T& chi,
00072 T& psi,
00073 const Real& RsdCG,
00074 int MaxCG, int MinCG=0)
00075 {
00076 START_CODE();
00077
00078 const Subset& s = A.subset();
00079
00080 SystemSolverResults_t res;
00081
00082 T p; moveToFastMemoryHint(p);
00083 T ap; moveToFastMemoryHint(ap);
00084
00085
00086 moveToFastMemoryHint(psi,true);
00087
00088 T r; moveToFastMemoryHint(r);
00089 T chi_internal; moveToFastMemoryHint(chi_internal);
00090
00091
00092 chi_internal[s]=chi;
00093
00094 Real rsd_sq = (RsdCG * RsdCG) * Real(norm2(chi_internal,s));
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 A(ap, psi, PLUS);
00105
00106
00107 r[s] = chi_internal - ap;
00108
00109
00110 p[s] = r;
00111
00112
00113 Double cp = norm2(r, s);
00114
00115 #if 0
00116 QDPIO::cout << "InvCG1: k = 0 cp = " << cp << " rsd_sq = " << rsd_sq
00117 << endl;
00118 #endif
00119
00120
00121 if ( toBool(cp <= rsd_sq) )
00122 {
00123 res.n_count = 0;
00124 res.resid = sqrt(cp);
00125 revertFromFastMemoryHint(psi, true);
00126 END_CODE();
00127 return res;
00128 }
00129
00130
00131
00132
00133 Real b;
00134 Double c;
00135 Complex a;
00136 Real d;
00137
00138 for(int k = 1; k <= MaxCG; ++k)
00139 {
00140
00141 c = cp;
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 A(ap, p, PLUS);
00152
00153
00154 d = innerProductReal(p, ap, s);
00155
00156
00157 a = Real(c)/d;
00158
00159
00160
00161 psi[s] += a * p;
00162
00163
00164
00165
00166
00167 r[s] -= a * ap;
00168
00169
00170
00171
00172
00173
00174 cp = norm2(r, s);
00175
00176 #if 0
00177 QDPIO::cout << "InvCG1: k = " << k << " cp = " << cp << endl;
00178 #endif
00179
00180 if ( toBool(cp <= rsd_sq) && (toBool(MinCG <= 0 ) || toBool( k >= MinCG )))
00181 {
00182 res.n_count = k;
00183 res.resid = sqrt(cp);
00184
00185
00186 {
00187 A(ap, psi, PLUS);
00188 Double actual_res = norm2(chi - ap,s);
00189 res.resid = sqrt(actual_res);
00190 #if 0
00191 QDPIO::cout << "InvCG1: true residual: " << res.resid << endl;
00192 #endif
00193 }
00194 revertFromFastMemoryHint(psi,true);
00195 END_CODE();
00196 return res;
00197 }
00198
00199
00200 b = Real(cp) / Real(c);
00201
00202
00203 p[s] = r + b*p;
00204 }
00205 res.n_count = MaxCG;
00206 res.resid = sqrt(cp);
00207 QDP_error_exit("too many CG iterations: count = %d", res.n_count);
00208 revertFromFastMemoryHint(psi,true);
00209 END_CODE();
00210 return res;
00211 }
00212
00213
00214 template<>
00215 SystemSolverResults_t
00216 InvCG1(const LinearOperator<LatticeFermion>& A,
00217 const LatticeFermion& chi,
00218 LatticeFermion& psi,
00219 const Real& RsdCG,
00220 int MaxCG,int MinCG)
00221 {
00222 return InvCG1_a(A, chi, psi, RsdCG, MaxCG,MinCG);
00223 }
00224
00225 template<>
00226 SystemSolverResults_t
00227 InvCG1(const LinearOperator<LatticeStaggeredFermion>& A,
00228 const LatticeStaggeredFermion& chi,
00229 LatticeStaggeredFermion& psi,
00230 const Real& RsdCG,
00231 int MaxCG,int MinCG)
00232 {
00233 return InvCG1_a(A, chi, psi, RsdCG, MaxCG,MinCG);
00234 }
00235
00236 }