Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
IDRSTABL.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2020 Chris Schoutrop <c.e.m.schoutrop@tue.nl>
5// Copyright (C) 2020 Mischa Senders <m.j.senders@student.tue.nl>
6// Copyright (C) 2020 Lex Kuijpers <l.kuijpers@student.tue.nl>
7// Copyright (C) 2020 Jens Wehner <j.wehner@esciencecenter.nl>
8// Copyright (C) 2020 Jan van Dijk <j.v.dijk@tue.nl>
9// Copyright (C) 2020 Adithya Vijaykumar
10//
11// This Source Code Form is subject to the terms of the Mozilla
12// Public License v. 2.0. If a copy of the MPL was not distributed
13// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
14// SPDX-License-Identifier: MPL-2.0
15/*
16
17The IDR(S)Stab(L) method is a combination of IDR(S) and BiCGStab(L)
18
19This implementation of IDRSTABL is based on
201. Aihara, K., Abe, K., & Ishiwata, E. (2014). A variant of IDRstab with
21reliable update strategies for solving sparse linear systems. Journal of
22Computational and Applied Mathematics, 259, 244-258.
23 doi:10.1016/j.cam.2013.08.028
24 2. Aihara, K., Abe, K., & Ishiwata, E. (2015). Preconditioned
25IDRSTABL Algorithms for Solving Nonsymmetric Linear Systems. International
26Journal of Applied Mathematics, 45(3).
27 3. Saad, Y. (2003). Iterative Methods for Sparse Linear Systems:
28Second Edition. Philadelphia, PA: SIAM.
29 4. Sonneveld, P., & Van Gijzen, M. B. (2009). IDR(s): A Family
30of Simple and Fast Algorithms for Solving Large Nonsymmetric Systems of Linear
31Equations. SIAM Journal on Scientific Computing, 31(2), 1035-1062.
32 doi:10.1137/070685804
33 5. Sonneveld, P. (2012). On the convergence behavior of IDR (s)
34and related methods. SIAM Journal on Scientific Computing, 34(5), A2576-A2598.
35
36 Right-preconditioning based on Ref. 3 is implemented here.
37*/
38
39#ifndef EIGEN_IDRSTABL_H
40#define EIGEN_IDRSTABL_H
41
42// IWYU pragma: private
43#include "./InternalHeaderCheck.h"
44
45namespace Eigen {
46
47namespace internal {
48
49template <typename MatrixType, typename Rhs, typename Dest, typename Preconditioner>
50bool idrstabl(const MatrixType &mat, const Rhs &rhs, Dest &x, const Preconditioner &precond, Index &iters,
51 typename Dest::RealScalar &tol_error, Index L, Index S) {
52 /*
53 Setup and type definitions.
54 */
55 using Scalar = typename Dest::Scalar;
56 using RealScalar = typename Dest::RealScalar;
57 using VectorType = Matrix<Scalar, Dynamic, 1>;
58 using DenseMatrixType = Matrix<Scalar, Dynamic, Dynamic, ColMajor>;
59
60 const Index N = x.rows();
61
62 Index k = 0; // Iteration counter
63 const Index maxIters = iters;
64
65 const RealScalar rhs_norm = rhs.stableNorm();
66 const RealScalar tol = internal::iterative_solver_threshold(tol_error, rhs_norm,
67 internal::iterative_solver_residual_floor<RealScalar>());
68
69 if (rhs_norm == 0) {
70 /*
71 If b==0, then the exact solution is x=0.
72 rhs_norm is needed for other calculations anyways, this exit is a freebie.
73 */
74 x.setZero();
75 iters = 0;
76 tol_error = 0.0;
77 return true;
78 }
79 // Construct decomposition objects beforehand.
80 FullPivLU<DenseMatrixType> lu_solver;
81
82 if (S >= N || L >= N) {
83 /*
84 The matrix is very small, or the choice of L and S is very poor
85 in that case solving directly will be best.
86 */
87 lu_solver.compute(DenseMatrixType(mat));
88 x = lu_solver.solve(rhs);
89 iters = 0;
90 tol_error = (rhs - mat * x).stableNorm() / rhs_norm;
91 return true;
92 }
93
94 // Define maximum sizes to prevent any reallocation later on.
95 DenseMatrixType u(N, L + 1);
96 DenseMatrixType r(N, L + 1);
97
98 DenseMatrixType V(N * (L + 1), S);
99
100 VectorType alpha(S);
101 VectorType gamma(L);
102 VectorType update(N);
103
104 /*
105 Main IDRSTABL algorithm
106 */
107 // Set up the initial residual
108 VectorType x0 = x;
109 r.col(0).noalias() = rhs - mat * x;
110 x.setZero(); // The final solution will be x0+x
111
112 tol_error = r.col(0).stableNorm();
113
114 // FOM = Full orthogonalisation method
115 DenseMatrixType h_FOM = DenseMatrixType::Zero(S, S - 1);
116
117 // Construct an initial U matrix of size N x S
118 DenseMatrixType U(N * (L + 1), S);
119 for (Index col_index = 0; col_index < S; ++col_index) {
120 // Arnoldi-like process to generate a set of orthogonal vectors spanning
121 // {u,A*u,A*A*u,...,A^(S-1)*u}. This construction can be combined with the
122 // Full Orthogonalization Method (FOM) from Ref.3 to provide a possible
123 // early exit with no additional MV.
124 if (col_index != 0) {
125 /*
126 Modified Gram-Schmidt strategy:
127 */
128 VectorType w = mat * precond.solve(u.col(0));
129 for (Index i = 0; i < col_index; ++i) {
130 auto v = U.col(i).head(N);
131 h_FOM(i, col_index - 1) = v.dot(w);
132 w -= h_FOM(i, col_index - 1) * v;
133 }
134 u.col(0) = w;
135 h_FOM(col_index, col_index - 1) = u.col(0).stableNorm();
136
137 if (numext::abs(h_FOM(col_index, col_index - 1)) != RealScalar(0)) {
138 /*
139 This only happens if u is NOT exactly zero. In case it is exactly zero
140 it would imply that that this u has no component in the direction of the
141 current residual.
142
143 By then setting u to zero it will not contribute any further (as it
144 should). Whereas attempting to normalize results in division by zero.
145
146 Such cases occur if:
147 1. The basis of dimension <S is sufficient to exactly solve the linear
148 system. I.e. the current residual is in span{r,Ar,...A^{m-1}r}, where
149 (m-1)<=S.
150 2. Two vectors generated from r, Ar,... are (numerically)
151 parallel.
152
153 In case 1, the exact solution to the system can be obtained from the
154 "Full Orthogonalization Method" (Algorithm 6.4 in the book of Saad),
155 without any additional MV.
156
157 Contrary to what one would suspect, the comparison with ==0.0 for
158 floating-point types is intended here. Any arbitrary non-zero u is fine
159 to continue, however if u contains either NaN or Inf the algorithm will
160 break down.
161 */
162 u.col(0) /= h_FOM(col_index, col_index - 1);
163 }
164 } else {
165 u.col(0) = r.col(0);
166 u.col(0).normalize();
167 }
168
169 U.col(col_index).head(N) = u.col(0);
170 }
171
172 if (S > 1) {
173 // Check for early FOM exit.
174 Scalar beta = r.col(0).stableNorm();
175 VectorType e1 = VectorType::Zero(S - 1);
176 e1(0) = beta;
177 lu_solver.compute(h_FOM.topLeftCorner(S - 1, S - 1));
178 VectorType y = lu_solver.solve(e1);
179 VectorType x2 = x + U.topLeftCorner(N, S - 1) * y;
180
181 // Using proposition 6.7 in Saad, one MV can be saved to calculate the
182 // residual
183 RealScalar FOM_residual = (h_FOM(S - 1, S - 2) * y(S - 2) * U.col(S - 1).head(N)).stableNorm();
184
185 if (FOM_residual < tol) {
186 // Exit, the FOM algorithm was already accurate enough
187 iters = k;
188 // Convert back to the unpreconditioned solution
189 x = precond.solve(x2);
190 // x contains the updates to x0, add those back to obtain the solution
191 x += x0;
192 tol_error = FOM_residual / rhs_norm;
193 return true;
194 }
195 }
196
197 /*
198 Select an initial (N x S) matrix R0.
199 1. Generate random R0, orthonormalize the result.
200 2. This results in R0, however to save memory and compute we only need the
201 adjoint of R0. This is given by the matrix R_T. Additionally, the matrix
202 (mat.adjoint()*R_tilde).adjoint()=R_tilde.adjoint()*mat by the
203 anti-distributivity property of the adjoint. This results in AR_T, which is
204 constant if R_T does not have to be regenerated and can be precomputed.
205 Based on reference 4, this has zero probability in exact arithmetic.
206 */
207
208 // Original IDRSTABL and Kensuke choose S random vectors:
209 DenseMatrixType R_T = internal::random_orthonormal_basis<DenseMatrixType>(N, S).adjoint();
210 DenseMatrixType AR_T = DenseMatrixType(R_T * mat);
211
212 // Pre-allocate sigma.
213 DenseMatrixType sigma(S, S);
214
215 bool reset_while = false; // Should the while loop be reset?
216
217 while (k < maxIters) {
218 for (Index j = 1; j <= L; ++j) {
219 /*
220 The IDR Step
221 */
222 // Construction of the sigma-matrix, and the decomposition of sigma.
223 for (Index i = 0; i < S; ++i) {
224 sigma.col(i).noalias() = AR_T * precond.solve(U.block(N * (j - 1), i, N, 1));
225 }
226
227 lu_solver.compute(sigma);
228 // Obtain the update coefficients alpha
229 if (j == 1) {
230 // alpha=inverse(sigma)*(R_T*r_0);
231 alpha = lu_solver.solve(R_T * r.col(0));
232 } else {
233 // alpha=inverse(sigma)*(AR_T*r_{j-2})
234 alpha = lu_solver.solve(AR_T * precond.solve(r.col(j - 2)));
235 }
236
237 // Obtain new solution and residual from this update
238 update.noalias() = U.topRows(N) * alpha;
239 r.col(0).noalias() -= mat * precond.solve(update);
240 x += update;
241
242 for (Index i = 1; i <= j - 2; ++i) {
243 // This only affects the case L>2
244 r.col(i).noalias() -= U.block(N * (i + 1), 0, N, S) * alpha;
245 }
246 if (j > 1) {
247 // r=[r;A*r_{j-2}]
248 r.col(j - 1).noalias() = mat * precond.solve(r.col(j - 2));
249 }
250 tol_error = r.col(0).stableNorm();
251
252 if (tol_error < tol) {
253 // If at this point the algorithm has converged, exit.
254 reset_while = true;
255 break;
256 }
257
258 bool break_normalization = false;
259 for (Index q = 1; q <= S; ++q) {
260 if (q == 1) {
261 // u = [r_0, ..., r_{j-1}]; u_j is computed below.
262 u.leftCols(j) = r.leftCols(j);
263 } else {
264 // u=[u_1;u_2;...;u_j]
265 u.leftCols(j) = u.middleCols(1, j);
266 }
267
268 // Obtain the update coefficients beta implicitly
269 // beta=lu_sigma.solve(AR_T * u.block(N * (j - 1), 0, N, 1)
270 u.leftCols(j).reshaped() -= U.topRows(N * j) * lu_solver.solve(AR_T * precond.solve(u.col(j - 1)));
271
272 // u=[u;Au_{j-1}]
273 u.col(j).noalias() = mat * precond.solve(u.col(j - 1));
274
275 // Orthonormalize u_j to the columns of V_j(:,1:q-1)
276 if (q > 1) {
277 /*
278 Modified Gram-Schmidt-like procedure to make u orthogonal to the
279 columns of V from Ref. 1.
280
281 The vector mu from Ref. 1 is obtained implicitly:
282 mu=V.block(N * j, 0, N, q - 1).adjoint() * u.block(N * j, 0, N, 1).
283 */
284 for (Index i = 0; i <= q - 2; ++i) {
285 auto v = V.col(i).segment(N * j, N);
286 Scalar h = v.squaredNorm();
287 h = v.dot(u.col(j)) / h;
288 u.leftCols(j + 1).reshaped() -= h * V.col(i).head(N * (j + 1));
289 }
290 }
291 // Normalize u and assign to a column of V
292 Scalar normalization_constant = u.col(j).stableNorm();
293 // If u is exactly zero, this will lead to a NaN. Small, non-zero u is
294 // fine.
295 if (normalization_constant == RealScalar(0.0)) {
296 break_normalization = true;
297 break;
298 } else {
299 u.leftCols(j + 1) /= normalization_constant;
300 }
301
302 V.col(q - 1).head(N * (j + 1)) = u.leftCols(j + 1).reshaped();
303 }
304
305 if (!break_normalization) {
306 U.topRows(N * (j + 1)) = V.topRows(N * (j + 1));
307 } else {
308 // Zero block row j rather than leaving it uninitialized (k == 0) or keeping a stale
309 // block from the previous cycle (k >= 1), so the next step's sigma and alpha are zero.
310 U.middleRows(N * j, N).setZero();
311 }
312 }
313 if (reset_while) {
314 break;
315 }
316
317 // r=[r;mat*r_{L-1}]
318 r.col(L).noalias() = mat * precond.solve(r.col(L - 1));
319
320 /*
321 The polynomial step
322 */
323 ColPivHouseholderQR<DenseMatrixType> qr_solver(r.rightCols(L));
324 gamma = qr_solver.solve(r.col(0));
325
326 // Update solution and residual using the "minimized residual coefficients"
327 update.noalias() = r.leftCols(L) * gamma;
328 x += update;
329 r.col(0).noalias() -= mat * precond.solve(update);
330
331 // Update iteration info
332 ++k;
333 tol_error = r.col(0).stableNorm();
334
335 if (tol_error < tol) {
336 // Slightly early exit by moving the criterion before the update of U,
337 // after the main while loop the result of that calculation would not be
338 // needed.
339 break;
340 }
341
342 /*
343 U=U0-sum(gamma_j*U_j)
344 Consider the first iteration. Then U only contains U0, so at the start of
345 the while-loop U should be U0. Therefore only the first N rows of U have to
346 be updated.
347 */
348 for (Index i = 1; i <= L; ++i) {
349 U.topRows(N) -= U.block(N * i, 0, N, S) * gamma(i - 1);
350 }
351 }
352
353 /*
354 Exit after the while loop terminated.
355 */
356 iters = k;
357 // Convert back to the unpreconditioned solution
358 x = precond.solve(x);
359 // x contains the updates to x0, add those back to obtain the solution
360 x += x0;
361 tol_error = tol_error / rhs_norm;
362 return true;
363}
364
365} // namespace internal
366
367template <typename MatrixType_, typename Preconditioner_ = DiagonalPreconditioner<typename MatrixType_::Scalar>>
368class IDRSTABL;
369
370namespace internal {
371
372template <typename MatrixType_, typename Preconditioner_>
373struct traits<IDRSTABL<MatrixType_, Preconditioner_>> {
374 using MatrixType = MatrixType_;
375 using Preconditioner = Preconditioner_;
376};
377
378} // namespace internal
379
420
421template <typename MatrixType_, typename Preconditioner_>
422class IDRSTABL : public IterativeSolverBase<IDRSTABL<MatrixType_, Preconditioner_>> {
423 protected:
425 using Base::m_error;
426 using Base::m_info;
427 using Base::m_isInitialized;
428 using Base::m_iterations;
429 using Base::matrix;
430 Index m_L = 2;
431 Index m_S = 4;
432
433 public:
434 using MatrixType = MatrixType_;
435 using Scalar = typename MatrixType::Scalar;
436 using RealScalar = typename MatrixType::RealScalar;
437 using Preconditioner = Preconditioner_;
438
439 public:
441 IDRSTABL() = default;
442
453 template <typename MatrixDerived>
454 explicit IDRSTABL(const EigenBase<MatrixDerived> &A) : Base(A.derived()) {}
455
462 template <typename Rhs, typename Dest>
463 void _solve_vector_with_guess_impl(const Rhs &b, Dest &x) const {
464 m_iterations = Base::maxIterations();
465 m_error = Base::m_tolerance;
466 bool ret = internal::idrstabl(matrix(), b, x, Base::m_preconditioner, m_iterations, m_error, m_L, m_S);
467
468 m_info = (!ret) ? NumericalIssue : m_error <= 10 * Base::m_tolerance ? Success : NoConvergence;
469 }
470
473 void setL(Index L) {
474 eigen_assert(L >= 1 && "L needs to be positive");
475 m_L = L;
476 }
477
479 void setS(Index S) {
480 eigen_assert(S >= 1 && "S needs to be positive");
481 m_S = S;
482 }
483};
484
485} // namespace Eigen
486
487#endif /* EIGEN_IDRSTABL_H */
The IDR(s)STAB(l) is a combination of IDR(s) and BiCGSTAB(l). It is a short-recurrences Krylov method...
Definition IDRSTABL.h:422
void setL(Index L)
Definition IDRSTABL.h:473
void setS(Index S)
Definition IDRSTABL.h:479
IDRSTABL(const EigenBase< MatrixDerived > &A)
Definition IDRSTABL.h:454
void _solve_vector_with_guess_impl(const Rhs &b, Dest &x) const
Definition IDRSTABL.h:463
IDRSTABL()=default
Index maxIterations() const
Definition IterativeSolverBase.h:307
@ NumericalIssue
Definition Constants.h:459
@ Success
Definition Constants.h:457
@ NoConvergence
Definition Constants.h:461
Definition EigenBase.h:34