Arcane  v4.1.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
DistributedPreconditionedSolver.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/* DistributedPreconditionedSolver.h (C) 2000-2026 */
9/* */
10/* Iterative solver wrapper for distributed linear systems. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_ALINA_DISTRIBUTEDPRECONDITIONEDSOLVER_H
13#define ARCCORE_ALINA_DISTRIBUTEDPRECONDITIONEDSOLVER_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 <iostream>
27
28#include "arccore/alina/AlinaUtils.h"
29#include "arccore/alina/DistributedInnerProduct.h"
30#include "arccore/alina/DistributedMatrix.h"
31
32/*---------------------------------------------------------------------------*/
33/*---------------------------------------------------------------------------*/
34
35namespace Arcane::Alina
36{
37
38/*---------------------------------------------------------------------------*/
39/*---------------------------------------------------------------------------*/
43template <class Precond, class IterativeSolver>
44class DistributedPreconditionedSolver
46{
47 static_assert(backend::backends_compatible<
48 typename IterativeSolver::BackendType,
49 typename Precond::BackendType>::value,
50 "Backends for preconditioner and iterative solver should be compatible");
51
52 public:
53
54 typedef typename IterativeSolver::BackendType backend_type;
55 using BackendType = backend_type;
57 typedef typename backend_type::value_type value_type;
58 typedef typename backend_type::params backend_params;
59 typedef typename BuiltinBackend<value_type>::matrix build_matrix;
60 typedef typename math::scalar_of<value_type>::type scalar_type;
61
62 struct params
63 {
64 typename Precond::params precond;
65 typename IterativeSolver::params solver;
66
67 params() {}
68
69 params(const PropertyTree& p)
70 : ARCCORE_ALINA_PARAMS_IMPORT_CHILD(p, precond)
71 , ARCCORE_ALINA_PARAMS_IMPORT_CHILD(p, solver)
72 {
73 p.check_params({ "precond", "solver" });
74 }
75
76 void get(PropertyTree& p, const std::string& path = "") const
77 {
78 ARCCORE_ALINA_PARAMS_EXPORT_CHILD(p, path, precond);
79 ARCCORE_ALINA_PARAMS_EXPORT_CHILD(p, path, solver);
80 }
81 } prm;
82
83 template <class Matrix>
84 DistributedPreconditionedSolver(mpi_communicator comm, const Matrix& A,
85 const params& prm = params(),
86 const backend_params& bprm = backend_params())
87 : prm(prm)
88 , n(backend::nbRow(A))
89 , P(comm, A, prm.precond, bprm)
90 , S(backend::nbRow(A), prm.solver, bprm, DistributedInnerProduct(comm))
91 {}
92
93 DistributedPreconditionedSolver(mpi_communicator comm,
94 std::shared_ptr<matrix> A,
95 const params& prm = params(),
96 const backend_params& bprm = backend_params())
97 : prm(prm)
98 , n(A->loc_rows())
99 , P(comm, A, prm.precond, bprm)
100 , S(n, prm.solver, bprm, DistributedInnerProduct(comm))
101 {
102 }
103
104 template <class Backend>
105 DistributedPreconditionedSolver(mpi_communicator comm,
106 std::shared_ptr<DistributedMatrix<Backend>> A,
107 const params& prm = params(),
108 const backend_params& bprm = backend_params())
109 : prm(prm)
110 , n(A->loc_rows())
111 , P(comm, std::make_shared<matrix>(*A), prm.precond, bprm)
112 , S(n, prm.solver, bprm, DistributedInnerProduct(comm))
113 {
114 A->move_to_backend(bprm);
115 }
116
117 DistributedPreconditionedSolver(mpi_communicator comm, std::shared_ptr<build_matrix> A,
118 const params& prm = params(),
119 const backend_params& bprm = backend_params())
120 : prm(prm)
121 , n(backend::nbRow(*A))
122 , P(comm, A, prm.precond, bprm)
123 , S(backend::nbRow(*A), prm.solver, bprm, DistributedInnerProduct(comm))
124 {}
125
126 template <class Matrix, class Vec1, class Vec2>
127 SolverResult operator()(const Matrix& A, const Vec1& rhs, Vec2&& x) const
128 {
129 return S(A, P, rhs, x);
130 }
131
132 template <class Vec1, class Vec2>
133 SolverResult operator()(const Vec1& rhs, Vec2&& x) const
134 {
135 return S(P, rhs, x);
136 }
137
138 template <class Vec1, class Vec2>
139 void apply(const Vec1& rhs, Vec2&& x) const
140 {
141 backend::clear(x);
142 (*this)(rhs, x);
143 }
144
145 const Precond& precond() const
146 {
147 return P;
148 }
149
150 Precond& precond()
151 {
152 return P;
153 }
154
155 const IterativeSolver& solver() const
156 {
157 return S;
158 }
159
160 std::shared_ptr<matrix> system_matrix_ptr() const
161 {
162 return P.system_matrix_ptr();
163 }
164
165 const matrix& system_matrix() const
166 {
167 return P.system_matrix();
168 }
169
170 void get_params(Alina::PropertyTree& p) const
171 {
172 prm.get(p);
173 }
174
175 size_t size() const
176 {
177 return n;
178 }
179
180 friend std::ostream& operator<<(std::ostream& os, const DistributedPreconditionedSolver& M)
181 {
182 return os << M.S << std::endl
183 << M.P;
184 }
185
186 private:
187
188 size_t n;
189
190 Precond P;
191 IterativeSolver S;
192};
193
194/*---------------------------------------------------------------------------*/
195/*---------------------------------------------------------------------------*/
196
197} // namespace Arcane::Alina
198
199/*---------------------------------------------------------------------------*/
200/*---------------------------------------------------------------------------*/
201
202#endif
Distributed Matrix using message passing.
IterativeSolver::params solver
Iterative solver parameters.
Metafunction that checks if two backends are compatible.