15constexpr int CG_DEFAULT_MAXITERS = 10000;
16constexpr double CG_DEFAULT_ACCURACY = 1e-12;
19template <
typename Op>
class CG {
24 double accuracy = CG_DEFAULT_ACCURACY;
26 double maxiters = CG_DEFAULT_MAXITERS;
35 CG(Op &op,
double _accuracy) : M(op) { accuracy = _accuracy; };
37 CG(Op &op,
double _accuracy,
int _maxiters) : M(op) {
48 struct timeval start, end;
51 p.copy_boundary_condition(in);
52 Dp.copy_boundary_condition(in);
53 DDp.copy_boundary_condition(in);
54 out.copy_boundary_condition(in);
55 double pDp = 0, rr = 0, rrnew = 0, rr_start = 0;
57 double target_rr, source_norm = 0;
59 gettimeofday(&start, NULL);
61 onsites(M.par) { source_norm +=
squarenorm(in[X]); }
63 target_rr = accuracy * accuracy * source_norm;
68 r[X] = in[X] - DDp[X];
75 for (i = 0; i < maxiters; i++) {
84 out[X] = out[X] + alpha * p[X];
85 r[X] = r[X] - alpha * DDp[X];
89 hila::out0 <<
"CG step " i <<
", residue " <<
sqrt(rrnew / target_rr) <<
"\n";
91 if (rrnew < target_rr)
94 p[M.par] = beta * p[X] + r[X];
98 gettimeofday(&end, NULL);
100 1e-3 * (end.tv_usec - start.tv_usec) + 1e3 * (end.tv_sec - start.tv_sec);
102 hila::out0 <<
"Conjugate Gradient: " << i <<
" steps in " << timing <<
"ms, ";
103 hila::out0 <<
"relative residue:" << rrnew / source_norm <<
"\n";
hila::arithmetic_type< T > squarenorm(const Array< n, m, T > &rhs)
Return square norm of Array.
Array< n, m, T > sqrt(Array< n, m, T > a)
Square root.
The conjugate gradient operator. Applies the inverse square of an operator on a vector.
CG(Op &op, double _accuracy)
Constructor: operator and accuracy.
CG(Op &op, double _accuracy, int _maxiters)
Constructor: operator, accuracy and maximum number of iterations.
void apply(Field< vector_type > &in, Field< vector_type > &out)
CG(Op &op)
Constructor: initialize the operator.
typename Op::vector_type vector_type
Get the type the operator applies to.
The field class implements the standard methods for accessing Fields. Hilapp replaces the parity acce...
void copy_boundary_condition(const Field< A > &rhs)
Copy the boundary condition from another field.
std::ostream out0
This writes output only from main process (node 0)