Arcane  v4.1.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
DistributedRelaxation.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/* DistributedRelaxation.h (C) 2000-2026 */
9/* */
10/* Relaxation with distribution support. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_ALINA_DISTRIBUTEDRELAXATION_H
13#define ARCCORE_ALINA_DISTRIBUTEDRELAXATION_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/BackendInterface.h"
27#include "arccore/alina/BuiltinBackend.h"
28#include "arccore/alina/Relaxation.h"
29#include "arccore/alina/DistributedMatrix.h"
30
31/*---------------------------------------------------------------------------*/
32/*---------------------------------------------------------------------------*/
33
34namespace Arcane::Alina
35{
36
37/*---------------------------------------------------------------------------*/
38/*---------------------------------------------------------------------------*/
39
40template <class Backend>
41struct DistributedChebyshevRelaxation
42: public ChebyshevRelaxation<Backend>
43{
44 typedef Backend backend_type;
46 typedef typename Backend::params backend_params;
47 typedef typename Base::params params;
48
49 DistributedChebyshevRelaxation(const DistributedMatrix<Backend>& A,
50 const params& prm = params(),
51 const backend_params& bprm = backend_params())
52 : Base(A, prm, bprm)
53 {}
54};
55
56/*---------------------------------------------------------------------------*/
57/*---------------------------------------------------------------------------*/
58
59template <class Backend>
60struct DistributedDampedJacobiRelaxation
61: public DampedJacobiRelaxation<Backend>
62{
63 typedef Backend backend_type;
65 typedef typename Backend::params backend_params;
66 typedef typename Base::params params;
67
68 DistributedDampedJacobiRelaxation(const DistributedMatrix<Backend>& A,
69 const params& prm = params(),
70 const backend_params& bprm = backend_params())
71 : Base(*A.local(), prm, bprm)
72 {}
73};
74
75/*---------------------------------------------------------------------------*/
76/*---------------------------------------------------------------------------*/
77
78template <class Backend>
79struct DistributedGaussSeidelRelaxation
80: public GaussSeidelRelaxation<Backend>
81{
82 typedef Backend backend_type;
84 typedef typename Backend::params backend_params;
85 typedef typename Base::params params;
86
87 DistributedGaussSeidelRelaxation(const DistributedMatrix<Backend>& A,
88 const params& prm = params(),
89 const backend_params& bprm = backend_params())
90 : Base(*A.local(), prm, bprm)
91 {}
92
93 template <class Matrix, class VectorRHS, class VectorX, class VectorTMP>
94 void apply_pre(const Matrix& A, const VectorRHS& rhs, VectorX& x, VectorTMP& t) const
95 {
96 Base::apply_pre(*A.local_backend(), rhs, x, t);
97 }
98
99 template <class Matrix, class VectorRHS, class VectorX, class VectorTMP>
100 void apply_post(const Matrix& A, const VectorRHS& rhs, VectorX& x, VectorTMP& t) const
101 {
102 Base::apply_post(*A.local_backend(), rhs, x, t);
103 }
104
105 template <class Matrix, class VectorRHS, class VectorX>
106 void apply(const Matrix& A, const VectorRHS& rhs, VectorX& x) const
107 {
108 Base::apply(*A.local_backend(), rhs, x);
109 }
110};
111
112/*---------------------------------------------------------------------------*/
113/*---------------------------------------------------------------------------*/
114
115template <class Backend>
116struct DistributedILU0Relaxation
117: public ILU0Relaxation<Backend>
118{
119 typedef Backend backend_type;
120 typedef ILU0Relaxation<Backend> Base;
121 typedef typename Backend::params backend_params;
122 typedef typename Base::params params;
123
124 DistributedILU0Relaxation(const DistributedMatrix<Backend>& A,
125 const params& prm = params(),
126 const backend_params& bprm = backend_params())
127 : Base(*A.local(), prm, bprm)
128 {}
129};
130
131/*---------------------------------------------------------------------------*/
132/*---------------------------------------------------------------------------*/
133
134template <class Backend>
135struct DistributedILUKRelaxation
136: public ILUKRelaxation<Backend>
137{
138 typedef Backend backend_type;
139 typedef ILUKRelaxation<Backend> Base;
140 typedef typename Backend::params backend_params;
141 typedef typename Base::params params;
142
143 DistributedILUKRelaxation(const DistributedMatrix<Backend>& A,
144 const params& prm = params(),
145 const backend_params& bprm = backend_params())
146 : Base(*A.local(), prm, bprm)
147 {}
148};
149
150/*---------------------------------------------------------------------------*/
151/*---------------------------------------------------------------------------*/
152
153template <class Backend>
154struct DistributedILUPRelaxation
155: public ILUPRelaxation<Backend>
156{
157 typedef Backend backend_type;
158 typedef ILUPRelaxation<Backend> Base;
159 typedef typename Backend::params backend_params;
160 typedef typename Base::params params;
161
162 DistributedILUPRelaxation(const DistributedMatrix<Backend>& A,
163 const params& prm = params(),
164 const backend_params& bprm = backend_params())
165 : Base(*A.local(), prm, bprm)
166 {}
167};
168
169/*---------------------------------------------------------------------------*/
170/*---------------------------------------------------------------------------*/
171
172template <class Backend>
173struct DistributedILUTRelaxation
174: public ILUTRelaxation<Backend>
175{
176 typedef Backend backend_type;
177 typedef ILUTRelaxation<Backend> Base;
178 typedef typename Backend::params backend_params;
179 typedef typename Base::params params;
180
181 DistributedILUTRelaxation(const DistributedMatrix<Backend>& A,
182 const params& prm = params(),
183 const backend_params& bprm = backend_params())
184 : Base(*A.local(), prm, bprm)
185 {}
186};
187
188/*---------------------------------------------------------------------------*/
189/*---------------------------------------------------------------------------*/
190
191template <class Backend>
192struct DistributedSPAI0Relaxation
193{
194 typedef Backend backend_type;
195 typedef typename Backend::value_type value_type;
196 typedef typename Backend::matrix_diagonal matrix_diagonal;
197 typedef typename math::scalar_of<value_type>::type scalar_type;
198 typedef Alina::detail::empty_params params;
199 typedef typename Backend::params backend_params;
200
201 DistributedSPAI0Relaxation(const DistributedMatrix<Backend>& A,
202 const params&, const backend_params& bprm = backend_params())
203 {
204 typedef CSRMatrix<value_type> build_matrix;
205
206 const ptrdiff_t n = A.loc_rows();
207 const build_matrix& A_loc = *A.local();
208 const build_matrix& A_rem = *A.remote();
209
210 auto m = std::make_shared<numa_vector<value_type>>(n, false);
211 typedef CSRMatrix<value_type> build_matrix;
212
213 arccoreParallelFor(0, n, ForLoopRunInfo{}, [&](Int32 begin, Int32 size) {
214 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
215 value_type num = math::zero<value_type>();
216 scalar_type den = math::zero<scalar_type>();
217
218 for (ptrdiff_t j = A_loc.ptr[i], e = A_loc.ptr[i + 1]; j < e; ++j) {
219 value_type v = A_loc.val[j];
220 scalar_type norm_v = math::norm(v);
221 den += norm_v * norm_v;
222 if (A_loc.col[j] == i)
223 num += v;
224 }
225
226 for (ptrdiff_t j = A_rem.ptr[i], e = A_rem.ptr[i + 1]; j < e; ++j) {
227 value_type v = A_rem.val[j];
228 scalar_type norm_v = math::norm(v);
229 den += norm_v * norm_v;
230 }
231
232 (*m)[i] = math::inverse(den) * num;
233 }
234 });
235
236 M = Backend::copy_vector(m, bprm);
237 }
238
239 template <class Matrix, class VectorRHS, class VectorX, class VectorTMP>
240 void apply_pre(const Matrix& A, const VectorRHS& rhs, VectorX& x, VectorTMP& tmp) const
241 {
242 static const scalar_type one = math::identity<scalar_type>();
243 backend::residual(rhs, A, x, tmp);
244 backend::vmul(one, *M, tmp, one, x);
245 }
246
247 template <class Matrix, class VectorRHS, class VectorX, class VectorTMP>
248 void apply_post(const Matrix& A, const VectorRHS& rhs, VectorX& x, VectorTMP& tmp) const
249 {
250 static const scalar_type one = math::identity<scalar_type>();
251 backend::residual(rhs, A, x, tmp);
252 backend::vmul(one, *M, tmp, one, x);
253 }
254
255 template <class Matrix, class VectorRHS, class VectorX>
256 void apply(const Matrix&, const VectorRHS& rhs, VectorX& x) const
257 {
258 backend::vmul(math::identity<scalar_type>(), *M, rhs, math::zero<scalar_type>(), x);
259 }
260
261 private:
262
263 std::shared_ptr<matrix_diagonal> M;
264};
265
266/*---------------------------------------------------------------------------*/
267/*---------------------------------------------------------------------------*/
268
269template <class Backend>
270struct DistributedSPAI1Relaxation
271: public SPAI1Relaxation<Backend>
272{
273 typedef Backend backend_type;
274 typedef SPAI1Relaxation<Backend> Base;
275 typedef typename Backend::params backend_params;
276 typedef typename Base::params params;
277
278 DistributedSPAI1Relaxation(const DistributedMatrix<Backend>& A,
279 const params& prm = params(),
280 const backend_params& bprm = backend_params())
281 : Base(*A.local(), prm, bprm)
282 {}
283};
284
285/*---------------------------------------------------------------------------*/
286/*---------------------------------------------------------------------------*/
290template <class Relaxation>
291struct AsDistributedPreconditioner
292{
293 typedef typename Relaxation::params params;
294 typedef typename Relaxation::BackendType backend_type;
295 using BackendType = backend_type;
296 typedef typename backend_type::params backend_params;
297 typedef typename backend_type::value_type value_type;
298 typedef typename math::scalar_of<value_type>::type scalar_type;
299 typedef DistributedMatrix<backend_type> matrix;
300 typedef typename backend_type::vector vector;
301
302 template <class Matrix>
303 AsDistributedPreconditioner(mpi_communicator comm,
304 const Matrix& A,
305 const params& prm = params(),
306 const backend_params& bprm = backend_params())
307 : A(std::make_shared<matrix>(comm, A, backend::nbRow(A)))
308 , S(A, prm, bprm)
309 {
310 this->A->move_to_backend(bprm);
311 }
312
313 AsDistributedPreconditioner(mpi_communicator,
314 std::shared_ptr<matrix> A,
315 const params& prm = params(),
316 const backend_params& bprm = backend_params())
317 : A(A)
318 , S(*A, prm, bprm)
319 {
320 this->A->move_to_backend(bprm);
321 }
322
323 template <class Vec1, class Vec2>
324 void apply(const Vec1& rhs, Vec2&& x) const
325 {
326 S.apply(*A, rhs, x);
327 }
328
329 std::shared_ptr<matrix> system_matrix_ptr() const
330 {
331 return A;
332 }
333
334 const matrix& system_matrix() const
335 {
336 return *system_matrix_ptr();
337 }
338
339 private:
340
341 std::shared_ptr<matrix> A;
342 Relaxation S;
343
344 friend std::ostream& operator<<(std::ostream& os, const AsDistributedPreconditioner& p)
345 {
346 os << "Relaxation as preconditioner" << std::endl;
347 os << " unknowns: " << p.system_matrix().glob_rows() << std::endl;
348 os << " nonzeros: " << p.system_matrix().glob_nonzeros() << std::endl;
349
350 return os;
351 }
352};
353
354/*---------------------------------------------------------------------------*/
355/*---------------------------------------------------------------------------*/
356
357} // namespace Arcane::Alina
358
359/*---------------------------------------------------------------------------*/
360/*---------------------------------------------------------------------------*/
361
362#endif
363
364/*---------------------------------------------------------------------------*/
365/*---------------------------------------------------------------------------*/
ChebyshevRelaxation(const Matrix &A, const params &prm, const typename Backend::params &backend_prm)
Constructs smoother for the system matrix.
Definition Relaxation.h:303
Distributed Matrix using message passing.
Class to handle empty parameters list.
Definition AlinaUtils.h:90
Informations d'exécution d'une boucle.
Matrix class, to be used by user.
void arccoreParallelFor(const ComplexForLoopRanges< RankValue > &loop_ranges, const ForLoopRunInfo &run_info, const LambdaType &lambda_function, const ReducerArgs &... reducer_args)
Applique en concurrence la fonction lambda lambda_function sur l'intervalle d'itération donné par loo...
Definition ParallelFor.h:85
std::int32_t Int32
Type entier signé sur 32 bits.
Sparse matrix stored in CSR (Compressed Sparse Row) format.
Definition CSRMatrix.h:98
DampedJacobiRelaxation(const Matrix &A, const params &prm, const typename Backend::params &backend_prm)
Constructs smoother for the system matrix.
Definition Relaxation.h:446
void apply_pre(const Matrix &A, const VectorRHS &rhs, VectorX &x, VectorTMP &) const
Apply pre-relaxation.
Definition Relaxation.h:548
GaussSeidelRelaxation(const Matrix &A, const params &prm, const typename Backend::params &)
Constructs smoother for the system matrix.
Definition Relaxation.h:537
void apply_post(const Matrix &A, const VectorRHS &rhs, VectorX &x, VectorTMP &) const
Apply post-relaxation.
Definition Relaxation.h:558
ILU0Relaxation(const Matrix &A, const params &prm, const typename Backend::params &bprm)
Constructs smoother for the system matrix.
Definition Relaxation.h:874
ILUKRelaxation(const Matrix &A, const params &prm, const typename Backend::params &bprm)
Constructs smoother for the system matrix.
ILUPRelaxation(const Matrix &A, const params &prm, const typename Backend::params &bprm)
Constructs smoother for the system matrix.
ILUTRelaxation(const Matrix &A, const params &prm, const typename Backend::params &bprm)
Constructs smoother for the system matrix.
Alina::detail::empty_params params
Relaxation parameters.
SPAI1Relaxation(const Matrix &A, const params &, const typename Backend::params &backend_prm)
Constructs smoother for the system matrix.
Convenience wrapper around MPI_Comm.