2015-05-17 00:19:03 +01:00
|
|
|
#ifndef GRID_CONJUGATE_GRADIENT_H
|
|
|
|
#define GRID_CONJUGATE_GRADIENT_H
|
|
|
|
|
|
|
|
namespace Grid {
|
|
|
|
|
2015-05-18 07:47:05 +01:00
|
|
|
/////////////////////////////////////////////////////////////
|
|
|
|
// Base classes for iterative processes based on operators
|
|
|
|
// single input vec, single output vec.
|
|
|
|
/////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
template<class Field>
|
2015-06-05 10:17:10 +01:00
|
|
|
class ConjugateGradient : public OperatorFunction<Field> {
|
2015-05-18 07:47:05 +01:00
|
|
|
public:
|
|
|
|
RealD Tolerance;
|
|
|
|
Integer MaxIterations;
|
|
|
|
ConjugateGradient(RealD tol,Integer maxit) : Tolerance(tol), MaxIterations(maxit) {
|
|
|
|
};
|
2015-05-17 00:19:03 +01:00
|
|
|
|
2015-05-25 13:43:58 +01:00
|
|
|
|
2015-06-05 10:17:10 +01:00
|
|
|
void operator() (LinearOperatorBase<Field> &Linop,const Field &src, Field &psi){
|
2015-05-17 00:19:03 +01:00
|
|
|
|
2015-05-25 13:43:58 +01:00
|
|
|
psi.checkerboard = src.checkerboard;
|
|
|
|
conformable(psi,src);
|
|
|
|
|
2015-05-17 00:19:03 +01:00
|
|
|
RealD cp,c,a,d,b,ssq,qq,b_pred;
|
|
|
|
|
|
|
|
Field p(src);
|
|
|
|
Field mmp(src);
|
|
|
|
Field r(src);
|
|
|
|
|
|
|
|
//Initial residual computation & set up
|
|
|
|
RealD guess = norm2(psi);
|
2015-12-22 08:54:40 +00:00
|
|
|
assert(std::isnan(guess)==0);
|
2015-12-21 15:29:46 +00:00
|
|
|
|
2015-06-05 10:17:10 +01:00
|
|
|
Linop.HermOpAndNorm(psi,mmp,d,b);
|
2015-05-17 00:19:03 +01:00
|
|
|
|
|
|
|
r= src-mmp;
|
|
|
|
p= r;
|
|
|
|
|
|
|
|
a =norm2(p);
|
|
|
|
cp =a;
|
|
|
|
ssq=norm2(src);
|
2015-05-25 13:43:58 +01:00
|
|
|
|
2015-07-29 09:53:39 +01:00
|
|
|
std::cout<<GridLogIterative <<std::setprecision(4)<< "ConjugateGradient: guess "<<guess<<std::endl;
|
|
|
|
std::cout<<GridLogIterative <<std::setprecision(4)<< "ConjugateGradient: src "<<ssq <<std::endl;
|
|
|
|
std::cout<<GridLogIterative <<std::setprecision(4)<< "ConjugateGradient: mp "<<d <<std::endl;
|
|
|
|
std::cout<<GridLogIterative <<std::setprecision(4)<< "ConjugateGradient: mmp "<<b <<std::endl;
|
|
|
|
std::cout<<GridLogIterative <<std::setprecision(4)<< "ConjugateGradient: cp,r "<<cp <<std::endl;
|
|
|
|
std::cout<<GridLogIterative <<std::setprecision(4)<< "ConjugateGradient: p "<<a <<std::endl;
|
2015-05-25 13:43:58 +01:00
|
|
|
|
2015-05-18 07:47:05 +01:00
|
|
|
RealD rsq = Tolerance* Tolerance*ssq;
|
2015-05-17 00:19:03 +01:00
|
|
|
|
|
|
|
//Check if guess is really REALLY good :)
|
|
|
|
if ( cp <= rsq ) {
|
2015-05-18 07:47:05 +01:00
|
|
|
return;
|
2015-05-17 00:19:03 +01:00
|
|
|
}
|
|
|
|
|
2015-07-29 09:53:39 +01:00
|
|
|
std::cout<<GridLogIterative << std::setprecision(4)<< "ConjugateGradient: k=0 residual "<<cp<<" rsq"<<rsq<<std::endl;
|
2015-12-29 23:49:41 +00:00
|
|
|
|
|
|
|
GridStopWatch LinalgTimer;
|
|
|
|
GridStopWatch MatrixTimer;
|
|
|
|
GridStopWatch SolverTimer;
|
|
|
|
|
|
|
|
SolverTimer.Start();
|
2015-05-17 00:19:03 +01:00
|
|
|
int k;
|
2015-05-18 07:47:05 +01:00
|
|
|
for (k=1;k<=MaxIterations;k++){
|
2015-05-17 00:19:03 +01:00
|
|
|
|
|
|
|
c=cp;
|
2015-12-29 23:49:41 +00:00
|
|
|
|
|
|
|
MatrixTimer.Start();
|
2015-06-05 10:17:10 +01:00
|
|
|
Linop.HermOpAndNorm(p,mmp,d,qq);
|
2015-12-29 23:49:41 +00:00
|
|
|
MatrixTimer.Stop();
|
2015-05-25 13:43:58 +01:00
|
|
|
|
2015-12-29 23:49:41 +00:00
|
|
|
LinalgTimer.Start();
|
2015-05-25 13:43:58 +01:00
|
|
|
RealD qqck = norm2(mmp);
|
|
|
|
ComplexD dck = innerProduct(p,mmp);
|
2015-05-19 13:57:35 +01:00
|
|
|
|
2015-05-17 00:19:03 +01:00
|
|
|
a = c/d;
|
|
|
|
b_pred = a*(a*qq-d)/c;
|
2015-05-19 13:57:35 +01:00
|
|
|
|
|
|
|
cp = axpy_norm(r,-a,mmp,r);
|
2015-05-17 00:19:03 +01:00
|
|
|
b = cp/c;
|
|
|
|
|
|
|
|
// Fuse these loops ; should be really easy
|
|
|
|
psi= a*p+psi;
|
|
|
|
p = p*b+r;
|
|
|
|
|
2015-12-29 23:49:41 +00:00
|
|
|
LinalgTimer.Stop();
|
2015-07-29 09:53:39 +01:00
|
|
|
std::cout<<GridLogIterative<<"ConjugateGradient: Iteration " <<k<<" residual "<<cp<< " target"<< rsq<<std::endl;
|
2015-06-20 22:22:56 +01:00
|
|
|
|
2015-05-17 00:19:03 +01:00
|
|
|
// Stopping condition
|
|
|
|
if ( cp <= rsq ) {
|
|
|
|
|
2015-12-29 23:49:41 +00:00
|
|
|
SolverTimer.Stop();
|
2015-06-05 10:17:10 +01:00
|
|
|
Linop.HermOpAndNorm(psi,mmp,d,qq);
|
2015-05-17 00:19:03 +01:00
|
|
|
p=mmp-src;
|
|
|
|
|
|
|
|
RealD mmpnorm = sqrt(norm2(mmp));
|
|
|
|
RealD psinorm = sqrt(norm2(psi));
|
|
|
|
RealD srcnorm = sqrt(norm2(src));
|
|
|
|
RealD resnorm = sqrt(norm2(p));
|
|
|
|
RealD true_residual = resnorm/srcnorm;
|
2015-05-19 13:57:35 +01:00
|
|
|
|
2015-07-23 17:31:13 +01:00
|
|
|
std::cout<<GridLogMessage<<"ConjugateGradient: Converged on iteration " <<k
|
2015-06-20 22:22:56 +01:00
|
|
|
<<" computed residual "<<sqrt(cp/ssq)
|
|
|
|
<<" true residual "<<true_residual
|
2015-12-29 23:49:41 +00:00
|
|
|
<<" target "<<Tolerance;
|
|
|
|
std::cout<<" Time elapsed: Total "<< SolverTimer.Elapsed() << " Matrix "<<MatrixTimer.Elapsed() << " Linalg "<<LinalgTimer.Elapsed();
|
|
|
|
std::cout<<std::endl;
|
2015-12-21 15:29:46 +00:00
|
|
|
|
|
|
|
assert(true_residual/Tolerance < 1000.0);
|
|
|
|
|
2015-05-19 13:57:35 +01:00
|
|
|
return;
|
2015-05-17 00:19:03 +01:00
|
|
|
}
|
|
|
|
}
|
2015-07-23 17:31:13 +01:00
|
|
|
std::cout<<GridLogMessage<<"ConjugateGradient did NOT converge"<<std::endl;
|
2015-05-18 07:47:05 +01:00
|
|
|
assert(0);
|
2015-05-17 00:19:03 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
#endif
|