Arcane  v4.1.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
GMRESSolver.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* solver_gmres.h (C) 2000-2026 */
9/* */
10/* GMRES solver method. . */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_ALINA_GMRESSOLVER_H
13#define ARCCORE_ALINA_GMRESSOLVER_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16/*
17 * This file is based on the work on AMGCL library (version march 2026)
18 * which can be found at https://github.com/ddemidov/amgcl.
19 *
20 * Copyright (c) 2012-2022 Denis Demidov <dennis.demidov@gmail.com>
21 * SPDX-License-Identifier: MIT
22 */
23/*---------------------------------------------------------------------------*/
24/*---------------------------------------------------------------------------*/
25
26#include "arccore/alina/SolverUtils.h"
27#include "arccore/alina/SolverBase.h"
28
29/*---------------------------------------------------------------------------*/
30/*---------------------------------------------------------------------------*/
31
32namespace Arcane::Alina
33{
34
35/*---------------------------------------------------------------------------*/
36/*---------------------------------------------------------------------------*/
40struct GMRESSolverParams
41{
42 using params = GMRESSolverParams;
43
45 Int32 M = 30;
46
48 ePreconditionerSideType pside = ePreconditionerSideType::right;
49
52
54 double tol = 1.0e-8;
55
57 double abstol = std::numeric_limits<double>::min();
58
60 //** Useful for searching for the null-space vectors of the system */
61 bool ns_search = false;
62
64 bool verbose = false;
65
66 GMRESSolverParams() = default;
67
68 GMRESSolverParams(const PropertyTree& p)
69 : ARCCORE_ALINA_PARAMS_IMPORT_VALUE(p, M)
70 , ARCCORE_ALINA_PARAMS_IMPORT_VALUE(p, pside)
71 , ARCCORE_ALINA_PARAMS_IMPORT_VALUE(p, maxiter)
72 , ARCCORE_ALINA_PARAMS_IMPORT_VALUE(p, tol)
73 , ARCCORE_ALINA_PARAMS_IMPORT_VALUE(p, abstol)
74 , ARCCORE_ALINA_PARAMS_IMPORT_VALUE(p, ns_search)
75 , ARCCORE_ALINA_PARAMS_IMPORT_VALUE(p, verbose)
76 {
77 p.check_params({ "M", "pside", "maxiter", "tol", "abstol", "ns_search", "verbose" });
78 }
79
80 void get(PropertyTree& p, const std::string& path) const
81 {
82 ARCCORE_ALINA_PARAMS_EXPORT_VALUE(p, path, M);
83 ARCCORE_ALINA_PARAMS_EXPORT_VALUE(p, path, pside);
84 ARCCORE_ALINA_PARAMS_EXPORT_VALUE(p, path, maxiter);
85 ARCCORE_ALINA_PARAMS_EXPORT_VALUE(p, path, tol);
86 ARCCORE_ALINA_PARAMS_EXPORT_VALUE(p, path, abstol);
87 ARCCORE_ALINA_PARAMS_EXPORT_VALUE(p, path, ns_search);
88 ARCCORE_ALINA_PARAMS_EXPORT_VALUE(p, path, verbose);
89 }
90};
91
92/*---------------------------------------------------------------------------*/
93/*---------------------------------------------------------------------------*/
102template <class Backend, class InnerProduct = detail::default_inner_product>
104: public SolverBase
105{
106 public:
107
108 using backend_type = Backend;
109 using BackendType = Backend;
110
111 typedef typename Backend::vector vector;
112 typedef typename Backend::value_type value_type;
113 typedef typename Backend::params backend_params;
114
115 typedef typename math::scalar_of<value_type>::type scalar_type;
116 typedef typename math::rhs_of<value_type>::type rhs_type;
117 typedef typename math::inner_product_impl<rhs_type>::return_type coef_type;
118
119 using params = GMRESSolverParams;
120
122 GMRESSolver(size_t n,
123 const params& prm = params(),
124 const backend_params& backend_prm = backend_params(),
125 const InnerProduct& inner_product = InnerProduct())
126 : prm(prm)
127 , n(n)
128 , H(prm.M + 1, prm.M)
129 , s(prm.M + 1)
130 , cs(prm.M + 1)
131 , sn(prm.M + 1)
132 , r(Backend::create_vector(n, backend_prm))
133 , inner_product(inner_product)
134 {
135 v.reserve(prm.M + 1);
136 for (unsigned i = 0; i <= prm.M; ++i)
137 v.push_back(Backend::create_vector(n, backend_prm));
138 }
139
155 template <class Matrix, class Precond, class Vec1, class Vec2>
156 SolverResult operator()(Matrix const& A, Precond const& P, Vec1 const& rhs, Vec2& x) const
157 {
158 static const scalar_type zero = math::zero<scalar_type>();
159 static const scalar_type one = math::identity<scalar_type>();
160
161 ScopedStreamModifier ss(std::cout);
162
163 scalar_type norm_rhs = norm(rhs);
164 if (norm_rhs < Alina::detail::eps<scalar_type>(1)) {
165 if (prm.ns_search) {
166 norm_rhs = math::identity<scalar_type>();
167 }
168 else {
169 backend::clear(x);
170 return SolverResult(0, norm_rhs);
171 }
172 }
173
174 scalar_type eps = std::max(prm.tol * norm_rhs, prm.abstol);
175 scalar_type norm_r = zero;
176
177 size_t iter = 0;
178 while (true) {
179 if (prm.pside == ePreconditionerSideType::left) {
180 backend::residual(rhs, A, x, *v[0]);
181 P.apply(*v[0], *r);
182 }
183 else {
184 backend::residual(rhs, A, x, *r);
185 }
186
187 // -- Check stopping condition
188 norm_r = norm(*r);
189 if (norm_r < eps || iter >= prm.maxiter)
190 break;
191
192 // -- Inner GMRES iteration
193 backend::axpby(math::inverse(norm_r), *r, zero, *v[0]);
194
195 std::fill(s.begin(), s.end(), 0);
196 s[0] = norm_r;
197
198 unsigned j = 0;
199 while (true) {
200 // -- Arnoldi process
201 //
202 // Build an orthonormal basis V and matrix H such that
203 // A V_{i-1} = V_{i} H
204 vector& v_new = *v[j + 1];
205
206 preconditioner_spmv(prm.pside, P, A, *v[j], v_new, *r);
207
208 for (unsigned k = 0; k <= j; ++k) {
209 H(k, j) = inner_product(v_new, *v[k]);
210 backend::axpby(-H(k, j), *v[k], one, v_new);
211 }
212 H(j + 1, j) = norm(v_new);
213
214 backend::axpby(math::inverse(H(j + 1, j)), v_new, zero, v_new);
215
216 for (unsigned k = 0; k < j; ++k)
217 detail::apply_plane_rotation(H(k, j), H(k + 1, j), cs[k], sn[k]);
218
219 detail::generate_plane_rotation(H(j, j), H(j + 1, j), cs[j], sn[j]);
220 detail::apply_plane_rotation(H(j, j), H(j + 1, j), cs[j], sn[j]);
221 detail::apply_plane_rotation(s[j], s[j + 1], cs[j], sn[j]);
222
223 scalar_type inner_res = std::abs(s[j + 1]);
224
225 if (prm.verbose && iter % 5 == 0)
226 std::cout << iter << "\t" << std::scientific << inner_res / norm_rhs << std::endl;
227
228 // Check for termination
229 ++j, ++iter;
230 if (iter >= prm.maxiter || j >= prm.M || inner_res <= eps)
231 break;
232 }
233
234 // -- GMRES terminated: eval solution
235 for (unsigned i = j; i-- > 0;) {
236 s[i] /= H(i, i);
237 for (unsigned k = 0; k < i; ++k)
238 s[k] -= H(k, i) * s[i];
239 }
240
241 // -- Apply step
242 vector& dx = *r;
243 backend::lin_comb(j, s, v, zero, dx);
244
245 if (prm.pside == ePreconditionerSideType::left) {
246 backend::axpby(one, dx, one, x);
247 }
248 else {
249 vector& tmp = *v[0];
250 P.apply(dx, tmp);
251 backend::axpby(one, tmp, one, x);
252 }
253 }
254
255 return std::make_tuple(iter, norm_r / norm_rhs);
256 }
257
268 template <class Precond, class Vec1, class Vec2>
269 std::tuple<size_t, scalar_type>
270 operator()(Precond const& P, Vec1 const& rhs, Vec2& x) const
271 {
272 return (*this)(P.system_matrix(), P, rhs, x);
273 }
274
275 friend std::ostream& operator<<(std::ostream& os, const GMRESSolver& s)
276 {
277 return os << "Type: GMRES(" << s.prm.M << ")"
278 << "\nUnknowns: " << s.n
279 << "\nMemory footprint: " << human_readable_memory(s.bytes())
280 << std::endl;
281 }
282
283 public:
284
285 params prm;
286
287 size_t bytes() const
288 {
289 size_t b = 0;
290
291 b += H.size() * sizeof(coef_type);
292 b += backend::bytes(s);
293 b += backend::bytes(cs);
294 b += backend::bytes(sn);
295 b += backend::bytes(*r);
296
297 for (const auto& x : v)
298 b += backend::bytes(*x);
299
300 return b;
301 }
302
303 private:
304
305 size_t n;
306
308 mutable std::vector<coef_type> s, cs, sn;
309 std::shared_ptr<vector> r;
310 std::vector<std::shared_ptr<vector>> v;
311
312 InnerProduct inner_product;
313
314 template <class Vec>
315 scalar_type norm(const Vec& x) const
316 {
317 return std::abs(sqrt(inner_product(x, x)));
318 }
319};
320
321/*---------------------------------------------------------------------------*/
322/*---------------------------------------------------------------------------*/
323
324} // namespace Arcane::Alina
325
326/*---------------------------------------------------------------------------*/
327/*---------------------------------------------------------------------------*/
328
329#endif
Generalized Minimal Residual (GMRES) method.
std::tuple< size_t, scalar_type > operator()(Precond const &P, Vec1 const &rhs, Vec2 &x) const
Computes the solution for the given right-hand side.
SolverResult operator()(Matrix const &A, Precond const &P, Vec1 const &rhs, Vec2 &x) const
GMRESSolver(size_t n, const params &prm=params(), const backend_params &backend_prm=backend_params(), const InnerProduct &inner_product=InnerProduct())
Preallocates necessary data structures for the system of size n.
size_t bytes() const
Memory used in bytes.
Save ostream flags in constructor, restore in destructor.
Base class for solvers.
Definition SolverBase.h:31
Result of a solving.
Definition AlinaUtils.h:52
Matrix class, to be used by user.
std::int32_t Int32
Type entier signé sur 32 bits.
Parameters for GMRES solver.
Definition GMRESSolver.h:41
ePreconditionerSideType pside
Preconditioning kind (left/right).
Definition GMRESSolver.h:48
bool verbose
Verbose output (show iterations and error)
Definition GMRESSolver.h:64
Int32 maxiter
Maximum number of iterations.
Definition GMRESSolver.h:51
Int32 M
Number of iterations before restart.
Definition GMRESSolver.h:45
double tol
Target relative residual error.
Definition GMRESSolver.h:54
bool ns_search
Ignore the trivial solution x=0 when rhs is zero.
Definition GMRESSolver.h:61
double abstol
Target absolute residual error.
Definition GMRESSolver.h:57