Arcane  4.2.3.0
Developer documentation
Loading...
Searching...
No Matches
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(const AlinaContext& context,
85 AlinaCommunicator comm, const Matrix& A,
86 const params& prm = params(),
87 const backend_params& bprm = backend_params())
88 : prm(prm)
89 , n(backend::nbRow(A))
90 , P(context, comm, A, prm.precond, bprm)
91 , S(context, backend::nbRow(A), prm.solver, bprm, DistributedInnerProduct(comm))
92 {}
93
94 DistributedPreconditionedSolver(const AlinaContext& context,
95 AlinaCommunicator comm,
96 std::shared_ptr<matrix> A,
97 const params& prm = params(),
98 const backend_params& bprm = backend_params())
99 : prm(prm)
100 , n(A->loc_rows())
101 , P(context, comm, A, prm.precond, bprm)
102 , S(context, n, prm.solver, bprm, DistributedInnerProduct(comm))
103 {
104 }
105
106 template <class Backend>
107 DistributedPreconditionedSolver(const AlinaContext& context,
108 AlinaCommunicator comm,
109 std::shared_ptr<DistributedMatrix<Backend>> A,
110 const params& prm = params(),
111 const backend_params& bprm = backend_params())
112 : prm(prm)
113 , n(A->loc_rows())
114 , P(context, comm, std::make_shared<matrix>(*A), prm.precond, bprm)
115 , S(context, n, prm.solver, bprm, DistributedInnerProduct(comm))
116 {
117 A->move_to_backend(bprm);
118 }
119
120 DistributedPreconditionedSolver(const AlinaContext& context,
121 AlinaCommunicator comm,
122 std::shared_ptr<build_matrix> A,
123 const params& prm = params(),
124 const backend_params& bprm = backend_params())
125 : prm(prm)
126 , n(backend::nbRow(*A))
127 , P(comm, A, prm.precond, bprm)
128 , S(context, backend::nbRow(*A), prm.solver, bprm, DistributedInnerProduct(comm))
129 {}
130
131 template <class Matrix, class Vec1, class Vec2>
132 SolverResult operator()(const Matrix& A, const Vec1& rhs, Vec2&& x) const
133 {
134 return S(A, P, rhs, x);
135 }
136
137 template <class Vec1, class Vec2>
138 SolverResult operator()(const Vec1& rhs, Vec2&& x) const
139 {
140 return S(P, rhs, x);
141 }
142
143 template <class Vec1, class Vec2>
144 void apply(const Vec1& rhs, Vec2&& x) const
145 {
146 backend::clear(x);
147 (*this)(rhs, x);
148 }
149
150 const Precond& precond() const
151 {
152 return P;
153 }
154
155 Precond& precond()
156 {
157 return P;
158 }
159
160 const IterativeSolver& solver() const
161 {
162 return S;
163 }
164
165 std::shared_ptr<matrix> system_matrix_ptr() const
166 {
167 return P.system_matrix_ptr();
168 }
169
170 const matrix& system_matrix() const
171 {
172 return P.system_matrix();
173 }
174
175 void get_params(Alina::PropertyTree& p) const
176 {
177 prm.get(p);
178 }
179
180 size_t size() const
181 {
182 return n;
183 }
184
185 friend std::ostream& operator<<(std::ostream& os, const DistributedPreconditionedSolver& M)
186 {
187 return os << M.S << std::endl
188 << M.P;
189 }
190
191 private:
192
193 size_t n;
194
195 Precond P;
196 IterativeSolver S;
197};
198
199/*---------------------------------------------------------------------------*/
200/*---------------------------------------------------------------------------*/
201
202} // namespace Arcane::Alina
203
204/*---------------------------------------------------------------------------*/
205/*---------------------------------------------------------------------------*/
206
207#endif
Distributed Matrix using message passing.
Class to store parameters as a hierarchical key/value tree.
Definition AlinaUtils.h:113
IterativeSolver::params solver
Iterative solver parameters.
Metafunction that checks if two backends are compatible.