Grid 0.7.0
ConjugateGradientReliableUpdate.h
Go to the documentation of this file.
1/*************************************************************************************
2
3 Grid physics library, www.github.com/paboyle/Grid
4
5 Source file: ./lib/algorithms/iterative/ConjugateGradientReliableUpdate.h
6
7 Copyright (C) 2015
8
9Author: Christopher Kelly <ckelly@phys.columbia.edu>
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License along
22 with this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24
25 See the full license in the file "LICENSE" in the top level distribution directory
26*************************************************************************************/
27/* END LEGAL */
28#ifndef GRID_CONJUGATE_GRADIENT_RELIABLE_UPDATE_H
29#define GRID_CONJUGATE_GRADIENT_RELIABLE_UPDATE_H
30
32
33template<class FieldD,class FieldF,
34 typename std::enable_if< getPrecision<FieldD>::value == 2, int>::type = 0,
35 typename std::enable_if< getPrecision<FieldF>::value == 1, int>::type = 0>
37public:
38 bool ErrorOnNoConverge; // throw an assert when the CG fails to converge.
39 // Defaults true.
42 Integer IterationsToComplete; //Number of iterations the CG took to finish. Filled in upon completion
44
45 bool DoFinalCleanup; //Final DP cleanup, defaults to true
46 Integer IterationsToCleanup; //Final DP cleanup step iterations
47
51 RealD Delta; //reliable update parameter. A reliable update is performed when the residual drops by a factor of Delta relative to its value at the last update
52
53 //Optional ability to switch to a different linear operator once the tolerance reaches a certain point. Useful for single/half -> single/single
56
57
58 ConjugateGradientReliableUpdate(RealD tol, Integer maxit, RealD _delta, GridBase* _sp_grid, LinearOperatorBase<FieldF> &_Linop_f, LinearOperatorBase<FieldD> &_Linop_d, bool err_on_no_conv = true)
59 : Tolerance(tol),
60 MaxIterations(maxit),
61 Delta(_delta),
62 Linop_f(_Linop_f),
63 Linop_d(_Linop_d),
64 SinglePrecGrid(_sp_grid),
65 ErrorOnNoConverge(err_on_no_conv),
66 DoFinalCleanup(true),
67 Linop_fallback(NULL)
68 {
69 assert(Delta > 0. && Delta < 1. && "Expect 0 < Delta < 1");
70 };
71
72 void setFallbackLinop(LinearOperatorBase<FieldF> &_Linop_fallback, const RealD _fallback_transition_tol){
73 Linop_fallback = &_Linop_fallback;
74 fallback_transition_tol = _fallback_transition_tol;
75 }
76
77 void operator()(const FieldD &src, FieldD &psi) {
78 GRID_TRACE("ConjugateGradientReliableUpdate");
79 LinearOperatorBase<FieldF> *Linop_f_use = &Linop_f;
80 bool using_fallback = false;
81
82 psi.Checkerboard() = src.Checkerboard();
83 conformable(psi, src);
84
85 RealD cp, c, a, d, b, ssq, qq, b_pred;
86
87 FieldD p(src);
88 FieldD mmp(src);
89 FieldD r(src);
90
91 // Initial residual computation & set up
92 RealD guess = norm2(psi);
93 assert(std::isnan(guess) == 0);
94
95 Linop_d.HermOpAndNorm(psi, mmp, d, b);
96
97 r = src - mmp;
98 p = r;
99
100 a = norm2(p);
101 cp = a;
102 ssq = norm2(src);
103
104 std::cout << GridLogIterative << std::setprecision(4) << "ConjugateGradientReliableUpdate: guess " << guess << std::endl;
105 std::cout << GridLogIterative << std::setprecision(4) << "ConjugateGradientReliableUpdate: src " << ssq << std::endl;
106 std::cout << GridLogIterative << std::setprecision(4) << "ConjugateGradientReliableUpdate: mp " << d << std::endl;
107 std::cout << GridLogIterative << std::setprecision(4) << "ConjugateGradientReliableUpdate: mmp " << b << std::endl;
108 std::cout << GridLogIterative << std::setprecision(4) << "ConjugateGradientReliableUpdate: cp,r " << cp << std::endl;
109 std::cout << GridLogIterative << std::setprecision(4) << "ConjugateGradientReliableUpdate: p " << a << std::endl;
110
111 RealD rsq = Tolerance * Tolerance * ssq;
112
113 // Check if guess is really REALLY good :)
114 if (cp <= rsq) {
115 std::cout << GridLogMessage << "ConjugateGradientReliableUpdate guess was REALLY good\n";
116 std::cout << GridLogMessage << "\tComputed residual " << std::sqrt(cp / ssq)<<std::endl;
117 return;
118 }
119
120 //Single prec initialization
121 precisionChangeWorkspace pc_wk_sp_to_dp(src.Grid(), SinglePrecGrid);
122 precisionChangeWorkspace pc_wk_dp_to_sp(SinglePrecGrid, src.Grid());
123
124 FieldF r_f(SinglePrecGrid);
125 r_f.Checkerboard() = r.Checkerboard();
126 precisionChange(r_f, r, pc_wk_dp_to_sp);
127
128 FieldF psi_f(r_f);
129 psi_f = Zero();
130
131 FieldF p_f(r_f);
132 FieldF mmp_f(r_f);
133
134 RealD MaxResidSinceLastRelUp = cp; //initial residual
135
136 std::cout << GridLogIterative << std::setprecision(4)
137 << "ConjugateGradient: k=0 residual " << cp << " target " << rsq << std::endl;
138
139 GridStopWatch LinalgTimer;
140 GridStopWatch MatrixTimer;
141 GridStopWatch SolverTimer;
142 GridStopWatch PrecChangeTimer;
143
144 SolverTimer.Start();
145 int k = 0;
146 int l = 0;
147
148 for (k = 1; k <= MaxIterations; k++) {
149 c = cp;
150
151 MatrixTimer.Start();
152 Linop_f_use->HermOpAndNorm(p_f, mmp_f, d, qq);
153 MatrixTimer.Stop();
154
155 LinalgTimer.Start();
156
157 a = c / d;
158 b_pred = a * (a * qq - d) / c;
159
160 cp = axpy_norm(r_f, -a, mmp_f, r_f);
161 b = cp / c;
162
163 // Fuse these loops ; should be really easy
164 psi_f = a * p_f + psi_f;
165 //p_f = p_f * b + r_f;
166
167 LinalgTimer.Stop();
168
169 std::cout << GridLogIterative << "ConjugateGradientReliableUpdate: Iteration " << k
170 << " residual " << cp << " target " << rsq << std::endl;
171 std::cout << GridLogDebug << "a = "<< a << " b_pred = "<< b_pred << " b = "<< b << std::endl;
172 std::cout << GridLogDebug << "qq = "<< qq << " d = "<< d << " c = "<< c << std::endl;
173
174 if(cp > MaxResidSinceLastRelUp){
175 std::cout << GridLogIterative << "ConjugateGradientReliableUpdate: updating MaxResidSinceLastRelUp : " << MaxResidSinceLastRelUp << " -> " << cp << std::endl;
176 MaxResidSinceLastRelUp = cp;
177 }
178
179 // Stopping condition
180 if (cp <= rsq) {
181 //Although not written in the paper, I assume that I have to add on the final solution
182 PrecChangeTimer.Start();
183 precisionChange(mmp, psi_f, pc_wk_sp_to_dp);
184 PrecChangeTimer.Stop();
185 psi = psi + mmp;
186
187
188 SolverTimer.Stop();
189 Linop_d.HermOpAndNorm(psi, mmp, d, qq);
190 p = mmp - src;
191
192 RealD srcnorm = std::sqrt(norm2(src));
193 RealD resnorm = std::sqrt(norm2(p));
194 RealD true_residual = resnorm / srcnorm;
195
196 std::cout << GridLogMessage << "ConjugateGradientReliableUpdate Converged on iteration " << k << " after " << l << " reliable updates" << std::endl;
197 std::cout << GridLogMessage << "\tComputed residual " << std::sqrt(cp / ssq)<<std::endl;
198 std::cout << GridLogMessage << "\tTrue residual " << true_residual<<std::endl;
199 std::cout << GridLogMessage << "\tTarget " << Tolerance << std::endl;
200
201 std::cout << GridLogMessage << "Time breakdown "<<std::endl;
202 std::cout << GridLogMessage << "\tElapsed " << SolverTimer.Elapsed() <<std::endl;
203 std::cout << GridLogMessage << "\tMatrix " << MatrixTimer.Elapsed() <<std::endl;
204 std::cout << GridLogMessage << "\tLinalg " << LinalgTimer.Elapsed() <<std::endl;
205 std::cout << GridLogMessage << "\tPrecChange " << PrecChangeTimer.Elapsed() <<std::endl;
206 std::cout << GridLogMessage << "\tPrecChange avg time " << PrecChangeTimer.Elapsed()/(2*l+1) <<std::endl;
207
208
211
212 if(DoFinalCleanup){
213 //Do a final CG to cleanup
214 std::cout << GridLogMessage << "ConjugateGradientReliableUpdate performing final cleanup.\n";
217 CG(Linop_d,src,psi);
219 }
220 else if (ErrorOnNoConverge) assert(true_residual / Tolerance < 10000.0);
221
222 std::cout << GridLogMessage << "ConjugateGradientReliableUpdate complete.\n";
223 return;
224 }
225 else if(cp < Delta * MaxResidSinceLastRelUp) { //reliable update
226 std::cout << GridLogMessage << "ConjugateGradientReliableUpdate "
227 << cp << "(residual) < " << Delta << "(Delta) * " << MaxResidSinceLastRelUp << "(MaxResidSinceLastRelUp) on iteration " << k << " : performing reliable update\n";
228 PrecChangeTimer.Start();
229 precisionChange(mmp, psi_f, pc_wk_sp_to_dp);
230 PrecChangeTimer.Stop();
231 psi = psi + mmp;
232
233 MatrixTimer.Start();
234 Linop_d.HermOpAndNorm(psi, mmp, d, qq);
235 MatrixTimer.Stop();
236
237 r = src - mmp;
238
239 psi_f = Zero();
240 PrecChangeTimer.Start();
241 precisionChange(r_f, r, pc_wk_dp_to_sp);
242 PrecChangeTimer.Stop();
243 cp = norm2(r);
244 MaxResidSinceLastRelUp = cp;
245
246 b = cp/c;
247
248 std::cout << GridLogMessage << "ConjugateGradientReliableUpdate new residual " << cp << std::endl;
249
250 l = l+1;
251 }
252
253 p_f = p_f * b + r_f; //update search vector after reliable update appears to help convergence
254
255 if(!using_fallback && Linop_fallback != NULL && cp < fallback_transition_tol){
256 std::cout << GridLogMessage << "ConjugateGradientReliableUpdate switching to fallback linear operator on iteration " << k << " at residual " << cp << std::endl;
257 Linop_f_use = Linop_fallback;
258 using_fallback = true;
259 }
260
261
262 }
263 std::cout << GridLogMessage << "ConjugateGradientReliableUpdate did NOT converge"
264 << std::endl;
265
266 if (ErrorOnNoConverge) assert(0);
269 }
270};
271
272
274
275
276
277#endif
RealD axpy_norm(Lattice< vobj > &ret, sobj a, const Lattice< vobj > &x, const Lattice< vobj > &y)
void conformable(const Lattice< obj1 > &lhs, const Lattice< obj2 > &rhs)
RealD norm2(const Lattice< vobj > &arg)
void precisionChange(Lattice< VobjOut > &out, const Lattice< VobjIn > &in, const precisionChangeWorkspace &workspace)
GridLogger GridLogIterative(1, "Iterative", GridLogColours, "BLUE")
GridLogger GridLogDebug(1, "Debug", GridLogColours, "PURPLE")
GridLogger GridLogMessage(1, "Message", GridLogColours, "NORMAL")
#define NAMESPACE_BEGIN(A)
Definition Namespace.h:35
#define NAMESPACE_END(A)
Definition Namespace.h:36
uint32_t Integer
Definition Simd.h:58
double RealD
Definition Simd.h:61
#define GRID_TRACE(name)
Definition Tracing.h:68
void operator()(const FieldD &src, FieldD &psi)
ConjugateGradientReliableUpdate(RealD tol, Integer maxit, RealD _delta, GridBase *_sp_grid, LinearOperatorBase< FieldF > &_Linop_f, LinearOperatorBase< FieldD > &_Linop_d, bool err_on_no_conv=true)
LinearOperatorBase< FieldF > * Linop_fallback
void setFallbackLinop(LinearOperatorBase< FieldF > &_Linop_fallback, const RealD _fallback_transition_tol)
void Start(void)
Definition Timer.h:92
GridTime Elapsed(void) const
Definition Timer.h:113
void Stop(void)
Definition Timer.h:99
virtual void HermOpAndNorm(const Field &in, Field &out, RealD &n1, RealD &n2)=0
Definition Simd.h:194