Loading...
Searching...
No Matches
IterativeSolver.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
29
30#ifndef GPR_LINALG_ITERATIVE_SOLVER_H_
31#define GPR_LINALG_ITERATIVE_SOLVER_H_
32
33#include <Eigen/Core>
34#include <cassert>
35#include <cmath>
36#include <functional>
37#include <limits>
38
40
41#ifdef GPR_LINALG_SCALAPACK
42#include "ScalapackBackend.h"
43#endif
44
45namespace gpr {
46namespace linalg {
47
48struct PCGResult {
49 Eigen::VectorXd x;
52 bool converged;
53};
54
56 double rtol = 1e-12;
58 bool log_progress = false;
59};
60
71[[nodiscard]] inline double pcg_dot(const Eigen::VectorXd& a,
72 const Eigen::VectorXd& b)
73{
74 assert(a.size() == b.size());
75 const Eigen::Index n = a.size();
76 long double accum = 0.0L;
77 for (Eigen::Index i = 0; i < n; ++i) {
78 accum +=
79 static_cast<long double>(a(i)) * static_cast<long double>(b(i));
80 }
81 return static_cast<double>(accum);
82}
83
102 const std::function<Eigen::VectorXd(const Eigen::VectorXd&)>& apply_A,
103 const std::function<Eigen::VectorXd(const Eigen::VectorXd&)>& apply_M_inv,
104 const Eigen::VectorXd& b, const PCGSettings& settings = {})
105{
106 const Eigen::Index n = b.size();
107 const int max_iter = settings.max_iterations > 0 ? settings.max_iterations
108 : static_cast<int>(2 * n);
109
110 Eigen::VectorXd x = Eigen::VectorXd::Zero(n);
111 Eigen::VectorXd r = b; // r_0 = b - A x_0 = b
112 Eigen::VectorXd z = apply_M_inv(r);
113 Eigen::VectorXd p = z;
114
115 const double b_norm = b.stableNorm();
116 const double tolerance = settings.rtol * b_norm;
117
118 double rz_old = pcg_dot(r, z);
119
120 PCGResult result;
121 result.iterations = 0;
122 result.converged = false;
123 result.final_residual = b_norm;
124
125 if (b_norm == 0.0) {
126 result.x = x;
127 result.converged = true;
128 result.final_residual = 0.0;
129 return result;
130 }
131
132 const auto finish = [&](int iterations) {
133 result.x = x;
134 result.iterations = iterations;
135 result.final_residual = (b - apply_A(x)).stableNorm();
136 result.converged = std::isfinite(tolerance) && tolerance >= 0.0 &&
137 std::isfinite(result.final_residual) &&
138 result.final_residual <= tolerance;
139 return result;
140 };
141 if (!std::isfinite(b_norm) || !std::isfinite(tolerance) ||
142 tolerance < 0.0 || !(rz_old > 0.0) || !std::isfinite(rz_old))
143 return finish(0);
144
145 for (int k = 0; k < max_iter; ++k) {
146 Eigen::VectorXd Ap = apply_A(p);
147 const double pAp = pcg_dot(p, Ap);
148 if (!(pAp > 0.0) || !std::isfinite(pAp)) {
149 // Loss of positive-definiteness. The matvec or the iteration
150 // hit a non-PD direction; bail out with the best-so-far x.
151 return finish(k);
152 }
153 const double alpha = rz_old / pAp;
154
155 x.noalias() += alpha * p;
156 r.noalias() -= alpha * Ap;
157
158 const double r_norm = r.stableNorm();
159 if (settings.log_progress) {
160 std::fprintf(stderr, " PCG iter %d: ||r||=%.6e\n", k + 1, r_norm);
161 }
162 if (r_norm <= tolerance) {
163 // Recurrence residuals accumulate rounding error independently
164 // of b - A x. Only an explicit product certifies convergence.
165 r = b - apply_A(x);
166 const double explicit_norm = r.stableNorm();
167 if (std::isfinite(explicit_norm) && explicit_norm <= tolerance) {
168 result.x = x;
169 result.iterations = k + 1;
170 result.final_residual = explicit_norm;
171 result.converged = true;
172 return result;
173 }
174 // Restart conjugacy from the corrected residual: directions
175 // generated by the drifted recurrence cannot be reused.
176 z = apply_M_inv(r);
177 rz_old = pcg_dot(r, z);
178 if (!(rz_old > 0.0) || !std::isfinite(rz_old)) return finish(k + 1);
179 p = z;
180 continue;
181 }
182
183 z = apply_M_inv(r);
184 const double rz_new = pcg_dot(r, z);
185 if (!(rz_new > 0.0) || !std::isfinite(rz_new)) return finish(k + 1);
186 const double beta = rz_new / rz_old;
187 p = z + beta * p;
188 rz_old = rz_new;
189 }
190
191 return finish(max_iter);
192}
193
194} // namespace linalg
195} // namespace gpr
196
197#endif // GPR_LINALG_ITERATIVE_SOLVER_H_
@ z
Definition Enums.h:28
@ x
Definition Enums.h:28
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
PCGResult preconditioned_cg(const std::function< Eigen::VectorXd(const Eigen::VectorXd &)> &apply_A, const std::function< Eigen::VectorXd(const Eigen::VectorXd &)> &apply_M_inv, const Eigen::VectorXd &b, const PCGSettings &settings={})
double pcg_dot(const Eigen::VectorXd &a, const Eigen::VectorXd &b)
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
Panel finish(Real left, Real right, const Value &k, const Value &g, const Value &absolute)
double final_residual
Explicitly evaluated ||b - A x|| at termination.
Eigen::VectorXd x
Solution vector.
int iterations
Number of CG iterations actually performed.
bool converged
true if the explicit residual meets the tolerance
double rtol
Relative residual tolerance.
int max_iterations
0 = use 2 * n as the iteration cap
bool log_progress
Emit per-iteration residual to stderr.