Arcane  v4.1.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
DistributedDirectSolverBase.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/* DistributedDirectSolverBase.h (C) 2000-2026 */
9/* */
10/* Base class for distributed direct solver. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_ALINA_MPI_DISTRIBUTEDDIRECTSOLVERBASE_H
13#define ARCCORE_ALINA_MPI_DISTRIBUTEDDIRECTSOLVERBASE_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/MessagePassingUtils.h"
27#include "arccore/alina/DistributedMatrix.h"
28
29/*---------------------------------------------------------------------------*/
30/*---------------------------------------------------------------------------*/
31
32namespace Arcane::Alina
33{
34
35/*---------------------------------------------------------------------------*/
36/*---------------------------------------------------------------------------*/
40template <class value_type, class Solver>
41class DistributedDirectSolverBase
42{
43 public:
44
45 typedef typename math::scalar_of<value_type>::type scalar_type;
46 typedef typename math::rhs_of<value_type>::type rhs_type;
47 typedef CSRMatrix<value_type> build_matrix;
48
49 DistributedDirectSolverBase() {}
50
51 void init(mpi_communicator comm, const build_matrix& Astrip)
52 {
53 this->comm = comm;
54 n = Astrip.nbRow();
55
56 std::vector<int> domain = comm.exclusive_sum(n);
57 std::vector<int> active;
58 active.reserve(comm.size);
59
60 // Find out how many ranks are active (own non-zero matrix rows):
61 int active_rank = 0;
62 for (int i = 0; i < comm.size; ++i) {
63 if (domain[i + 1] - domain[i] > 0) {
64 if (comm.rank == i)
65 active_rank = active.size();
66 active.push_back(i);
67 }
68 }
69
70 // Consolidate the matrix on a fewer processes.
71 int nmasters = std::min<int>(active.size(), solver().comm_size(domain.back()));
72 int slaves_per_master = (active.size() + nmasters - 1) / nmasters;
73 int group_beg = (active_rank / slaves_per_master) * slaves_per_master;
74
75 group_master = active[group_beg];
76
77 // Communicator for masters (used to solve the coarse problem):
78 MPI_Comm_split(comm,
79 comm.rank == group_master ? 0 : MPI_UNDEFINED,
80 comm.rank, &masters_comm);
81
82 if (!n)
83 return; // I am not active
84
85 // Shift from row pointers to row widths:
86 std::vector<ptrdiff_t> widths(n);
87 for (ptrdiff_t i = 0; i < n; ++i)
88 widths[i] = Astrip.ptr[i + 1] - Astrip.ptr[i];
89
90 if (comm.rank == group_master) {
91 int group_end = std::min<int>(group_beg + slaves_per_master, active.size());
92 group_beg += 1;
93 int group_size = group_end - group_beg;
94
95 UniqueArray<MessagePassing::Request> cnt_req(group_size);
96 UniqueArray<MessagePassing::Request> col_req(group_size);
97 UniqueArray<MessagePassing::Request> val_req(group_size);
98
99 solve_req.resize(group_size);
100 slaves.reserve(group_size);
101 counts.reserve(group_size);
102
103 // Count rows in local chunk of the consolidated matrix,
104 // see who is reporting to us.
105 int nloc = n;
106 for (int j = group_beg; j < group_end; ++j) {
107 int i = active[j];
108
109 int m = domain[i + 1] - domain[i];
110 nloc += m;
111 counts.push_back(m);
112 slaves.push_back(i);
113 }
114
115 // Get matrix chunks from my slaves.
116 build_matrix A;
117 A.set_size(nloc, domain.back(), false);
118 A.ptr[0] = 0;
119
120 cons_f.resize(A.nbRow());
121 cons_x.resize(A.nbRow());
122
123 int shift = n + 1;
124 std::copy(widths.begin(), widths.end(), &A.ptr[1]);
125
126 for (int j = 0; j < group_size; ++j) {
127 int i = slaves[j];
128
129 cnt_req[j] = comm.doIReceive(&A.ptr[shift], counts[j], i, cnt_tag);
130
131 shift += counts[j];
132 }
133
134 comm.waitAll(cnt_req);
135
136 A.set_nonzeros(A.scan_row_sizes());
137
138 std::copy(Astrip.col.data(), Astrip.col.data() + Astrip.nbNonZero(), A.col.data());
139 std::copy(Astrip.val.data(), Astrip.val.data() + Astrip.nbNonZero(), A.val.data());
140
141 shift = Astrip.nbNonZero();
142 for (int j = 0, d0 = domain[comm.rank]; j < group_size; ++j) {
143 int i = slaves[j];
144
145 int nnz = A.ptr[domain[i + 1] - d0] - A.ptr[domain[i] - d0];
146
147 col_req[j] = comm.doIReceive(A.col + shift, nnz, i, col_tag);
148 val_req[j] = comm.doIReceive(A.val + shift, nnz, i, val_tag);
149
150 shift += nnz;
151 }
152
153 comm.waitAll(col_req);
154 comm.waitAll(val_req);
155
156 solver().init(mpi_communicator(masters_comm), A);
157 }
158 else {
159 comm.doSend(widths.data(), n, group_master, cnt_tag);
160 comm.doSend(Astrip.col.data(), Astrip.nbNonZero(), group_master, col_tag);
161 comm.doSend(Astrip.val.data(), Astrip.nbNonZero(), group_master, val_tag);
162 }
163
164 host_v.resize(n);
165 }
166
167 template <class B>
168 void init(mpi_communicator comm, const DistributedMatrix<B>& A)
169 {
170 const build_matrix& A_loc = *A.local();
171 const build_matrix& A_rem = *A.remote();
172
173 build_matrix a;
174
175 a.set_size(A.loc_rows(), A.glob_cols(), false);
176 a.set_nonzeros(A_loc.nbNonZero() + A_rem.nbNonZero());
177 a.ptr[0] = 0;
178
179 for (size_t i = 0, head = 0; i < A_loc.nbRow(); ++i) {
180 ptrdiff_t shift = A.loc_col_shift();
181
182 for (ptrdiff_t j = A_loc.ptr[i], e = A_loc.ptr[i + 1]; j < e; ++j) {
183 a.col[head] = A_loc.col[j] + shift;
184 a.val[head] = A_loc.val[j];
185 ++head;
186 }
187
188 for (ptrdiff_t j = A_rem.ptr[i], e = A_rem.ptr[i + 1]; j < e; ++j) {
189 a.col[head] = A_rem.col[j];
190 a.val[head] = A_rem.val[j];
191 ++head;
192 }
193
194 a.ptr[i + 1] = head;
195 }
196
197 init(comm, a);
198 }
199
200 virtual ~DistributedDirectSolverBase()
201 {
202 if (masters_comm != MPI_COMM_NULL)
203 MPI_Comm_free(&masters_comm);
204 }
205
206 Solver& solver()
207 {
208 return *static_cast<Solver*>(this);
209 }
210
211 const Solver& solver() const
212 {
213 return *static_cast<const Solver*>(this);
214 }
215
216 template <class VecF, class VecX>
217 void operator()(const VecF& f, VecX& x) const
218 {
219 if (!n)
220 return;
221
222 backend::copy(f, host_v);
223
224 if (comm.rank == group_master) {
225 std::copy(host_v.begin(), host_v.end(), cons_f.begin());
226
227 int shift = n, j = 0;
228 for (int i : slaves) {
229 solve_req[j] = comm.doIReceive(&cons_f[shift], counts[j], i, rhs_tag);
230 shift += counts[j++];
231 }
232
233 comm.waitAll(solve_req);
234
235 solver().solve(cons_f, cons_x);
236
237 std::copy(cons_x.begin(), cons_x.begin() + n, host_v.begin());
238 shift = n;
239 j = 0;
240
241 for (int i : slaves) {
242 solve_req[j] = comm.doISend(&cons_x[shift], counts[j], i, sol_tag);
243 shift += counts[j++];
244 }
245
246 comm.waitAll(solve_req);
247 }
248 else {
249 comm.doSend(host_v.data(), n, group_master, rhs_tag);
250 comm.doReceive(host_v.data(), n, group_master, sol_tag);
251 }
252
253 backend::copy(host_v, x);
254 }
255
256 private:
257
258 static const int cnt_tag = 5001;
259 static const int col_tag = 5002;
260 static const int val_tag = 5003;
261 static const int rhs_tag = 5004;
262 static const int sol_tag = 5005;
263
264 mpi_communicator comm;
265 int n;
266 int group_master;
267 MPI_Comm masters_comm;
268 std::vector<int> slaves;
269 std::vector<int> counts;
270 mutable std::vector<rhs_type> cons_f, cons_x, host_v;
271 mutable UniqueArray<MessagePassing::Request> solve_req;
272};
273
274/*---------------------------------------------------------------------------*/
275/*---------------------------------------------------------------------------*/
276
277} // namespace Arcane::Alina
278
279/*---------------------------------------------------------------------------*/
280/*---------------------------------------------------------------------------*/
281
282#endif
void resize(size_t new_size)
Set the new size. WARNING: this method do not handle the delete of the current value.
Definition CSRMatrix.h:72
Distributed Matrix using message passing.
Vecteur 1D de données avec sémantique par valeur (style STL).
Sparse matrix stored in CSR (Compressed Sparse Row) format.
Definition CSRMatrix.h:98
Convenience wrapper around MPI_Comm.