Arcane  4.2.3.0
Developer documentation
Loading...
Searching...
No Matches
DistributedCoarsening.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/* DistributedCoarsening.h (C) 2000-2026 */
9/* */
10/* Distributed coarsening algorithms. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_ALINA_MPI_DISTRIBUTEDCOARSENING_H
13#define ARCCORE_ALINA_MPI_DISTRIBUTEDCOARSENING_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/BuiltinBackend.h"
27#include "arccore/alina/AlinaUtils.h"
28#include "arccore/alina/Coarsening.h"
29#include "arccore/alina/MessagePassingUtils.h"
30#include "arccore/alina/DistributedMatrix.h"
31
32#include <cstddef>
33#include <tuple>
34#include <memory>
35#include <numeric>
36#include <cassert>
37
38/*---------------------------------------------------------------------------*/
39/*---------------------------------------------------------------------------*/
40
41namespace Arcane::Alina
42{
43
44/*---------------------------------------------------------------------------*/
45/*---------------------------------------------------------------------------*/
49template <class Backend>
50struct DistributedPMISAggregation
51{
52 typedef typename Backend::value_type value_type;
53 typedef typename math::scalar_of<value_type>::type scalar_type;
54 typedef DistributedMatrix<Backend> matrix;
55 typedef CommunicationPattern<Backend> CommPattern;
56 using build_matrix = Backend::matrix;
57 using col_type = Backend::col_type;
58 using ptr_type = Backend::ptr_type;
60 using bool_matrix = bool_backend::matrix;
61
62 struct params
63 {
66
67 // Strong connectivity threshold
68 double eps_strong = 0.08;
69
70 // Block size for non-scalar problems.
71 Int32 block_size = 1;
72
73 params() = default;
74
75 params(const PropertyTree& p)
76 : ARCCORE_ALINA_PARAMS_IMPORT_CHILD(p, nullspace)
77 , ARCCORE_ALINA_PARAMS_IMPORT_VALUE(p, eps_strong)
78 , ARCCORE_ALINA_PARAMS_IMPORT_VALUE(p, block_size)
79 {
80 p.check_params({ "nullspace", "eps_strong", "block_size" });
81 }
82
83 void get(PropertyTree& p, const std::string& path) const
84 {
85 ARCCORE_ALINA_PARAMS_EXPORT_CHILD(p, path, nullspace);
86 ARCCORE_ALINA_PARAMS_EXPORT_VALUE(p, path, eps_strong);
87 ARCCORE_ALINA_PARAMS_EXPORT_VALUE(p, path, block_size);
88 }
89
90 } & prm;
91
92 std::shared_ptr<DistributedMatrix<bool_backend>> conn;
93 std::shared_ptr<matrix> p_tent;
94
95 DistributedPMISAggregation(const matrix& A, params& prm)
96 : prm(prm)
97 {
98 ptrdiff_t n = A.loc_rows();
100 UniqueArray<int> owner(n);
101
102 if (prm.block_size == 1) {
103 conn = conn_strength(A, prm.eps_strong);
104
105 ptrdiff_t naggr = aggregates(*conn, state, owner);
106 p_tent = tentative_prolongation(A.comm(), n, naggr, state, owner);
107 }
108 else {
109 typedef typename math::scalar_of<value_type>::type scalar;
110 using sbackend = BuiltinBackend<scalar, col_type, ptr_type>;
111
112 ptrdiff_t np = n / prm.block_size;
113
114 assert(np * prm.block_size == n && "Matrix size should be divisible by block_size");
115
116 DistributedMatrix<sbackend> A_pw(A.comm(),
117 pointwise_matrix(*A.local(), prm.block_size),
118 pointwise_matrix(*A.remote(), prm.block_size));
119
120 auto conn_pw = conn_strength(A_pw, prm.eps_strong);
121
122 UniqueArray<ptrdiff_t> state_pw(np);
123 UniqueArray<int> owner_pw(np);
124
125 ptrdiff_t naggr = aggregates(*conn_pw, state_pw, owner_pw);
126
127 conn = std::make_shared<DistributedMatrix<bool_backend>>(A.comm(),
128 expand_conn(*A.local(), *A_pw.local(), *conn_pw->local(), prm.block_size),
129 expand_conn(*A.remote(), *A_pw.remote(), *conn_pw->remote(), prm.block_size));
130
131 arccoreParallelFor(0, np, ForLoopRunInfo{}, [&](Int32 begin, Int32 size) {
132 for (ptrdiff_t ip = begin; ip < (begin + size); ++ip) {
133 ptrdiff_t i = ip * prm.block_size;
134 ptrdiff_t s = state_pw[ip];
135 int o = owner_pw[ip];
136
137 for (unsigned k = 0; k < prm.block_size; ++k) {
138 state[i + k] = (s < 0) ? s : (s * prm.block_size + k);
139 owner[i + k] = o;
140 }
141 }
142 });
143
144 p_tent = tentative_prolongation(A.comm(), n, naggr * prm.block_size, state, owner);
145 }
146 }
147
148 std::shared_ptr<DistributedMatrix<bool_backend>>
149 squared_interface(const DistributedMatrix<bool_backend>& A)
150 {
151 const CommunicationPattern<bool_backend>& C = A.cpat();
152
153 bool_matrix& A_loc = *A.local();
154 bool_matrix& A_rem = *A.remote();
155
156 ptrdiff_t A_rows = A.loc_rows();
157
158 ptrdiff_t A_beg = A.loc_col_shift();
159 ptrdiff_t A_end = A_beg + A_rows;
160
161 auto a_nbr = remote_rows(C, A, false);
162 bool_matrix& A_nbr = *a_nbr;
163
164 // Build mapping from global to local column numbers in the remote part of
165 // the square matrix.
166 UniqueArray<ptrdiff_t> rem_cols(A_rem.nbNonZero() + A_nbr.nbNonZero());
167
168 std::copy(A_nbr.col.data(), A_nbr.col.data() + A_nbr.nbNonZero(),
169 std::copy(A_rem.col.data(), A_rem.col.data() + A_rem.nbNonZero(), rem_cols.begin()));
170
171 std::sort(rem_cols.begin(), rem_cols.end());
172 rem_cols.erase(std::unique(rem_cols.begin(), rem_cols.end()), rem_cols.end());
173
174 ptrdiff_t n_rem_cols = 0;
175 std::unordered_map<ptrdiff_t, int> rem_idx(2 * rem_cols.size());
176 for (ptrdiff_t c : rem_cols) {
177 if (c >= A_beg && c < A_end)
178 continue;
179 rem_idx[c] = n_rem_cols++;
180 }
181
182 // Build the product.
183 auto s_loc = std::make_shared<bool_matrix>();
184 auto s_rem = std::make_shared<bool_matrix>();
185
186 bool_matrix& S_loc = *s_loc;
187 bool_matrix& S_rem = *s_rem;
188
189 S_loc.set_size(A_rows, A_rows, false);
190 S_rem.set_size(A_rows, 0, false);
191
192 S_loc.ptr[0] = 0;
193 S_rem.ptr[0] = 0;
194
195 ARCCORE_ALINA_TIC("analyze");
196 arccoreParallelFor(0, A_rows, ForLoopRunInfo{}, [&](Int32 begin, Int32 size) {
197 UniqueArray<ptrdiff_t> loc_marker(A_rows, -1);
198 UniqueArray<ptrdiff_t> rem_marker(n_rem_cols, -1);
199
200 for (ptrdiff_t ia = begin; ia < (begin + size); ++ia) {
201 ptrdiff_t loc_cols = 0;
202 ptrdiff_t rem_cols = 0;
203
204 for (ptrdiff_t ja = A_rem.ptr[ia], ea = A_rem.ptr[ia + 1]; ja < ea; ++ja) {
205 ptrdiff_t ca = C.local_index(A_rem.col[ja]);
206
207 for (ptrdiff_t jb = A_nbr.ptr[ca], eb = A_nbr.ptr[ca + 1]; jb < eb; ++jb) {
208 ptrdiff_t cb = A_nbr.col[jb];
209
210 if (cb >= A_beg && cb < A_end) {
211 cb -= A_beg;
212
213 if (loc_marker[cb] != ia) {
214 loc_marker[cb] = ia;
215 ++loc_cols;
216 }
217 }
218 else {
219 cb = rem_idx[cb];
220
221 if (rem_marker[cb] != ia) {
222 rem_marker[cb] = ia;
223 ++rem_cols;
224 }
225 }
226 }
227 }
228
229 for (ptrdiff_t ja = A_loc.ptr[ia], ea = A_loc.ptr[ia + 1]; ja < ea; ++ja) {
230 ptrdiff_t ca = A_loc.col[ja];
231
232 for (ptrdiff_t jb = A_rem.ptr[ca], eb = A_rem.ptr[ca + 1]; jb < eb; ++jb) {
233 ptrdiff_t cb = rem_idx[A_rem.col[jb]];
234
235 if (rem_marker[cb] != ia) {
236 rem_marker[cb] = ia;
237 ++rem_cols;
238 }
239 }
240 }
241
242 if (rem_cols) {
243 for (ptrdiff_t ja = A_loc.ptr[ia], ea = A_loc.ptr[ia + 1]; ja < ea; ++ja) {
244 ptrdiff_t ca = A_loc.col[ja];
245
246 for (ptrdiff_t jb = A_loc.ptr[ca], eb = A_loc.ptr[ca + 1]; jb < eb; ++jb) {
247 ptrdiff_t cb = A_loc.col[jb];
248
249 if (loc_marker[cb] != ia) {
250 loc_marker[cb] = ia;
251 ++loc_cols;
252 }
253 }
254 }
255 }
256
257 S_rem.ptr[ia + 1] = rem_cols;
258 S_loc.ptr[ia + 1] = rem_cols ? loc_cols : 0;
259 }
260 });
261 ARCCORE_ALINA_TOC("analyze");
262
263 S_loc.set_nonzeros(S_loc.scan_row_sizes(), false);
264 S_rem.set_nonzeros(S_rem.scan_row_sizes(), false);
265
266 ARCCORE_ALINA_TIC("compute");
267 arccoreParallelFor(0, A_rows, ForLoopRunInfo{}, [&](Int32 begin, Int32 size) {
268 UniqueArray<ptrdiff_t> loc_marker(A_rows, -1);
269 UniqueArray<ptrdiff_t> rem_marker(n_rem_cols, -1);
270
271 for (ptrdiff_t ia = begin; ia < (begin + size); ++ia) {
272 ptrdiff_t loc_beg = S_loc.ptr[ia];
273 ptrdiff_t rem_beg = S_rem.ptr[ia];
274 ptrdiff_t loc_end = loc_beg;
275 ptrdiff_t rem_end = rem_beg;
276
277 if (rem_beg == S_rem.ptr[ia + 1])
278 continue;
279
280 for (ptrdiff_t ja = A_loc.ptr[ia], ea = A_loc.ptr[ia + 1]; ja < ea; ++ja) {
281 ptrdiff_t ca = A_loc.col[ja];
282
283 for (ptrdiff_t jb = A_loc.ptr[ca], eb = A_loc.ptr[ca + 1]; jb < eb; ++jb) {
284 ptrdiff_t cb = A_loc.col[jb];
285
286 if (loc_marker[cb] < loc_beg) {
287 loc_marker[cb] = loc_end;
288 S_loc.col[loc_end] = cb;
289 ++loc_end;
290 }
291 }
292
293 for (ptrdiff_t jb = A_rem.ptr[ca], eb = A_rem.ptr[ca + 1]; jb < eb; ++jb) {
294 ptrdiff_t gb = A_rem.col[jb];
295 ptrdiff_t cb = rem_idx[gb];
296
297 if (rem_marker[cb] < rem_beg) {
298 rem_marker[cb] = rem_end;
299 S_rem.col[rem_end] = gb;
300 ++rem_end;
301 }
302 }
303 }
304
305 for (ptrdiff_t ja = A_rem.ptr[ia], ea = A_rem.ptr[ia + 1]; ja < ea; ++ja) {
306 ptrdiff_t ca = C.local_index(A_rem.col[ja]);
307
308 for (ptrdiff_t jb = A_nbr.ptr[ca], eb = A_nbr.ptr[ca + 1]; jb < eb; ++jb) {
309 ptrdiff_t gb = A_nbr.col[jb];
310
311 if (gb >= A_beg && gb < A_end) {
312 ptrdiff_t cb = gb - A_beg;
313
314 if (loc_marker[cb] < loc_beg) {
315 loc_marker[cb] = loc_end;
316 S_loc.col[loc_end] = cb;
317 ++loc_end;
318 }
319 }
320 else {
321 ptrdiff_t cb = rem_idx[gb];
322
323 if (rem_marker[cb] < rem_beg) {
324 rem_marker[cb] = rem_end;
325 S_rem.col[rem_end] = gb;
326 ++rem_end;
327 }
328 }
329 }
330 }
331 }
332 });
333 ARCCORE_ALINA_TOC("compute");
334
335 return std::make_shared<DistributedMatrix<bool_backend>>(A.comm(), s_loc, s_rem);
336 }
337
338 template <class B>
339 std::shared_ptr<DistributedMatrix<bool_backend>>
340 conn_strength(const DistributedMatrix<B>& A, scalar_type eps_strong)
341 {
342 typedef typename B::value_type val_type;
343 typedef CSRMatrix<val_type> B_matrix;
344
345 ARCCORE_ALINA_TIC("conn_strength");
346 ptrdiff_t n = A.loc_rows();
347
348 const B_matrix& A_loc = *A.local();
349 const B_matrix& A_rem = *A.remote();
350 const CommunicationPattern<B>& C = A.cpat();
351
352 scalar_type eps_squared = eps_strong * eps_strong;
353
354 auto d = diagonal(A_loc);
355 numa_vector<val_type>& D = *d;
356
357 UniqueArray<val_type> D_loc(C.send.count());
358 UniqueArray<val_type> D_rem(C.recv.count());
359
360 for (size_t i = 0, nv = C.send.count(); i < nv; ++i)
361 D_loc[i] = D[C.send.col[i]];
362
363 if (D_loc.size() != 0)
364 C.exchange(&D_loc[0], &D_rem[0]);
365
366 auto s_loc = std::make_shared<bool_matrix>();
367 auto s_rem = std::make_shared<bool_matrix>();
368
369 bool_matrix& S_loc = *s_loc;
370 bool_matrix& S_rem = *s_rem;
371
372 S_loc.set_size(n, n, true);
373 S_rem.set_size(n, 0, true);
374
375 S_loc.val.resize(A_loc.nbNonZero());
376 S_rem.val.resize(A_rem.nbNonZero());
377
378 arccoreParallelFor(0, n, ForLoopRunInfo{}, [&](Int32 begin, Int32 size) {
379 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
380 val_type eps_dia_i = eps_squared * D[i];
381
382 for (ptrdiff_t j = A_loc.ptr[i], e = A_loc.ptr[i + 1]; j < e; ++j) {
383 ptrdiff_t c = A_loc.col[j];
384 val_type v = A_loc.val[j];
385
386 if ((S_loc.val[j] = (c == i || (eps_dia_i * D[c] < v * v))))
387 ++S_loc.ptr[i + 1];
388 }
389
390 for (ptrdiff_t j = A_rem.ptr[i], e = A_rem.ptr[i + 1]; j < e; ++j) {
391 ptrdiff_t c = C.local_index(A_rem.col[j]);
392 val_type v = A_rem.val[j];
393
394 if ((S_rem.val[j] = (eps_dia_i * D_rem[c] < v * v)))
395 ++S_rem.ptr[i + 1];
396 }
397 }
398 });
399
400 S_loc.setNbNonZero(S_loc.scan_row_sizes());
401 S_rem.setNbNonZero(S_rem.scan_row_sizes());
402
403 S_loc.col.resize(S_loc.nbNonZero());
404 S_rem.col.resize(S_rem.nbNonZero());
405
406 arccoreParallelFor(0, n, ForLoopRunInfo{}, [&](Int32 begin, Int32 size) {
407 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
408 ptrdiff_t loc_head = S_loc.ptr[i];
409 ptrdiff_t rem_head = S_rem.ptr[i];
410
411 for (ptrdiff_t j = A_loc.ptr[i], e = A_loc.ptr[i + 1]; j < e; ++j)
412 if (S_loc.val[j])
413 S_loc.col[loc_head++] = A_loc.col[j];
414
415 for (ptrdiff_t j = A_rem.ptr[i], e = A_rem.ptr[i + 1]; j < e; ++j)
416 if (S_rem.val[j])
417 S_rem.col[rem_head++] = A_rem.col[j];
418 }
419 });
420 ARCCORE_ALINA_TOC("conn_strength");
421
422 return std::make_shared<DistributedMatrix<bool_backend>>(A.comm(), s_loc, s_rem);
423 }
424
425 ptrdiff_t aggregates(const DistributedMatrix<bool_backend>& A,
426 UniqueArray<ptrdiff_t>& loc_state,
427 UniqueArray<int>& loc_owner)
428 {
429 ARCCORE_ALINA_TIC("PMIS");
430 static const int tag_exc_cnt = 4001;
431 static const int tag_exc_pts = 4002;
432
433 const bool_matrix& A_loc = *A.local();
434 const bool_matrix& A_rem = *A.remote();
435
436 ptrdiff_t n = A_loc.nbRow();
437
438 AlinaCommunicator comm = A.comm();
439
440 // 1. Get symbolic square of the connectivity matrix.
441 ARCCORE_ALINA_TIC("symbolic square");
442 auto S = squared_interface(A);
443 const bool_matrix& S_loc = *S->local();
444 const bool_matrix& S_rem = *S->remote();
445 const CommunicationPattern<bool_backend>& Sp = S->cpat();
446 ARCCORE_ALINA_TOC("symbolic square");
447
448 // 2. Apply PMIS algorithm to the symbolic square.
449 ptrdiff_t n_undone = 0;
450 UniqueArray<ptrdiff_t> rem_state(Sp.recv.count(), DistributedPMISAggregation::undone);
451 UniqueArray<int> rem_owner(Sp.recv.count(), -1);
452 UniqueArray<ptrdiff_t> send_state(Sp.send.count());
453 UniqueArray<int> send_owner(Sp.send.count());
454
455 // Remove lonely nodes.
456 std::atomic<ptrdiff_t> atomic_n_undone = 0;
457 arccoreParallelFor(0, n, ForLoopRunInfo{}, [&](Int32 begin, Int32 size) {
458 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
459 ptrdiff_t wl = A_loc.ptr[i + 1] - A_loc.ptr[i];
460 ptrdiff_t wr = S_rem.ptr[i + 1] - S_rem.ptr[i];
461
462 if (wl + wr == 1) {
463 loc_state[i] = DistributedPMISAggregation::deleted;
464 ++atomic_n_undone;
465 }
466 else {
467 loc_state[i] = DistributedPMISAggregation::undone;
468 }
469
470 loc_owner[i] = -1;
471 }
472 });
473
474 n_undone = n - atomic_n_undone;
475
476 // Exchange state
477 for (ptrdiff_t i = 0, m = Sp.send.count(); i < m; ++i)
478 send_state[i] = loc_state[Sp.send.col[i]];
479 if (send_state.size() != 0)
480 Sp.exchange(&send_state[0], &rem_state[0]);
481
482 UniqueArray<UniqueArray<ptrdiff_t>> send_pts(Sp.recv.nbr.size());
483 UniqueArray<ptrdiff_t> recv_pts;
484
485 UniqueArray<MessagePassing::Request> send_cnt_req(Sp.recv.nbr.size());
486 UniqueArray<MessagePassing::Request> send_pts_req(Sp.recv.nbr.size());
487
488 ptrdiff_t naggr = 0;
489
490 UniqueArray<ptrdiff_t> nbr;
491
492 while (true) {
493 for (size_t i = 0; i < Sp.recv.nbr.size(); ++i)
494 send_pts[i].clear();
495
496 if (n_undone) {
497 for (ptrdiff_t i = 0; i < n; ++i) {
498 if (loc_state[i] != DistributedPMISAggregation::undone)
499 continue;
500
501 if (S_rem.ptr[i + 1] > S_rem.ptr[i]) {
502 // Boundary points
503 bool selectable = true;
504 for (ptrdiff_t j = S_rem.ptr[i], e = S_rem.ptr[i + 1]; j < e; ++j) {
505 int d, c;
506 std::tie(d, c) = Sp.remote_info(S_rem.col[j]);
507
508 if (rem_state[c] == DistributedPMISAggregation::undone && Sp.recv.nbr[d] > comm.rank) {
509 selectable = false;
510 break;
511 }
512 }
513
514 if (!selectable)
515 continue;
516
517 ptrdiff_t id = naggr++;
518 loc_owner[i] = comm.rank;
519 loc_state[i] = id;
520 --n_undone;
521
522 // A gives immediate neighbors
523 for (ptrdiff_t j = A_loc.ptr[i], e = A_loc.ptr[i + 1]; j < e; ++j) {
524 ptrdiff_t c = A_loc.col[j];
525 if (c != i) {
526 if (loc_state[c] == DistributedPMISAggregation::undone)
527 --n_undone;
528 loc_owner[c] = comm.rank;
529 loc_state[c] = id;
530 }
531 }
532
533 for (ptrdiff_t j = A_rem.ptr[i], e = A_rem.ptr[i + 1]; j < e; ++j) {
534 ptrdiff_t c = A_rem.col[j];
535 int d, k;
536 std::tie(d, k) = Sp.remote_info(c);
537
538 rem_state[k] = id;
539
540 send_pts[d].push_back(c);
541 send_pts[d].push_back(id);
542 }
543
544 // S gives removed neighbors
545 for (ptrdiff_t j = S_loc.ptr[i], e = S_loc.ptr[i + 1]; j < e; ++j) {
546 ptrdiff_t c = S_loc.col[j];
547 if (c != i && loc_state[c] == DistributedPMISAggregation::undone) {
548 loc_owner[c] = comm.rank;
549 loc_state[c] = id;
550 --n_undone;
551 }
552 }
553
554 for (ptrdiff_t j = S_rem.ptr[i], e = S_rem.ptr[i + 1]; j < e; ++j) {
555 ptrdiff_t c = S_rem.col[j];
556 int d, k;
557 std::tie(d, k) = Sp.remote_info(c);
558
559 if (rem_state[k] == DistributedPMISAggregation::undone) {
560 rem_state[k] = id;
561 send_pts[d].push_back(c);
562 send_pts[d].push_back(id);
563 }
564 }
565 }
566 else {
567 // Inner points
568 ptrdiff_t id = naggr++;
569 loc_owner[i] = comm.rank;
570 loc_state[i] = id;
571 --n_undone;
572
573 nbr.clear();
574
575 for (ptrdiff_t j = A_loc.ptr[i], e = A_loc.ptr[i + 1]; j < e; ++j) {
576 ptrdiff_t c = A_loc.col[j];
577
578 if (c != i && loc_state[c] != DistributedPMISAggregation::deleted) {
579 if (loc_state[c] == DistributedPMISAggregation::undone)
580 --n_undone;
581 loc_owner[c] = comm.rank;
582 loc_state[c] = id;
583 nbr.push_back(c);
584 }
585 }
586
587 for (ptrdiff_t k : nbr) {
588 for (ptrdiff_t j = A_loc.ptr[k], e = A_loc.ptr[k + 1]; j < e; ++j) {
589 ptrdiff_t c = A_loc.col[j];
590 if (c != k && loc_state[c] == DistributedPMISAggregation::undone) {
591 loc_owner[c] = comm.rank;
592 loc_state[c] = id;
593 --n_undone;
594 }
595 }
596 }
597 }
598 }
599 }
600
601 for (size_t i = 0; i < Sp.recv.nbr.size(); ++i) {
602 int npts = send_pts[i].size();
603 send_cnt_req[i] = comm.doISend(&npts, 1, Sp.recv.nbr[i], tag_exc_cnt);
604
605 if (!npts)
606 continue;
607 send_pts_req[i] = comm.doISend(&send_pts[i][0], npts, Sp.recv.nbr[i], tag_exc_pts);
608 }
609
610 for (size_t i = 0; i < Sp.send.nbr.size(); ++i) {
611 int npts;
612 comm.doReceive(&npts, 1, Sp.send.nbr[i], tag_exc_cnt);
613
614 if (!npts)
615 continue;
616 recv_pts.resize(npts);
617 comm.doReceive(&recv_pts[0], npts, Sp.send.nbr[i], tag_exc_pts);
618
619 for (int k = 0; k < npts; k += 2) {
620 ptrdiff_t c = recv_pts[k] - Sp.loc_col_shift();
621 ptrdiff_t id = recv_pts[k + 1];
622
623 if (loc_state[c] == DistributedPMISAggregation::undone)
624 --n_undone;
625
626 loc_owner[c] = Sp.send.nbr[i];
627 loc_state[c] = id;
628 }
629 }
630
631 for (size_t i = 0; i < Sp.recv.nbr.size(); ++i) {
632 int npts = send_pts[i].size();
633 comm.wait(send_cnt_req[i]);
634 if (npts == 0)
635 continue;
636 comm.wait(send_pts_req[i]);
637 }
638
639 for (ptrdiff_t i = 0, m = Sp.send.count(); i < m; ++i)
640 send_state[i] = loc_state[Sp.send.col[i]];
641 if (send_state.size() != 0)
642 Sp.exchange(&send_state[0], &rem_state[0]);
643
644 if (0 == comm.reduceSum(n_undone))
645 break;
646 }
647
648 // Some of the aggregates could potentially vanish during expansion
649 // step (*) above. We need to exclude those and renumber the rest.
650 ARCCORE_ALINA_TIC("drop empty aggregates");
651 for (ptrdiff_t i = 0, m = Sp.send.count(); i < m; ++i)
652 send_owner[i] = loc_owner[Sp.send.col[i]];
653 if (send_owner.size() != 0)
654 Sp.exchange(&send_owner[0], &rem_owner[0]);
655
656 UniqueArray<ptrdiff_t> new_id(naggr + 1, 0);
657 for (ptrdiff_t i = 0; i < n; ++i) {
658 if (loc_owner[i] == comm.rank && loc_state[i] >= 0)
659 new_id[loc_state[i] + 1] = 1;
660 }
661
662 for (size_t i = 0; i < Sp.recv.count(); ++i) {
663 if (rem_owner[i] == comm.rank && rem_state[i] >= 0)
664 new_id[rem_state[i] + 1] = 1;
665 }
666
667 std::partial_sum(new_id.begin(), new_id.end(), new_id.begin());
668
669 if (comm.reduceSum(naggr - new_id.back()) > 0) {
670 naggr = new_id.back();
671
672 for (ptrdiff_t i = 0; i < n; ++i) {
673 if (loc_owner[i] == comm.rank && loc_state[i] >= 0) {
674 loc_state[i] = new_id[loc_state[i]];
675 }
676 }
677
678 for (size_t i = 0; i < Sp.recv.nbr.size(); ++i) {
679 send_pts[i].clear();
680 }
681
682 for (auto p = Sp.remote_begin(); p != Sp.remote_end(); ++p) {
683 ptrdiff_t c = p->first;
684
685 int d, k;
686 std::tie(d, k) = p->second;
687
688 if (rem_owner[k] == comm.rank && rem_state[k] >= 0) {
689 send_pts[d].push_back(c);
690 send_pts[d].push_back(new_id[rem_state[k]]);
691 }
692 }
693
694 for (size_t i = 0; i < Sp.recv.nbr.size(); ++i) {
695 int npts = send_pts[i].size();
696 send_cnt_req[i] = comm.doISend(&npts, 1, Sp.recv.nbr[i], tag_exc_cnt);
697
698 if (!npts)
699 continue;
700 send_pts_req[i] = comm.doISend(&send_pts[i][0], npts, Sp.recv.nbr[i], tag_exc_pts);
701 }
702
703 for (size_t i = 0; i < Sp.send.nbr.size(); ++i) {
704 int npts;
705 comm.doReceive(&npts, 1, Sp.send.nbr[i], tag_exc_cnt);
706
707 if (!npts)
708 continue;
709 recv_pts.resize(npts);
710 comm.doReceive(&recv_pts[0], npts, Sp.send.nbr[i], tag_exc_pts);
711
712 for (int k = 0; k < npts; k += 2) {
713 ptrdiff_t c = recv_pts[k] - Sp.loc_col_shift();
714 ptrdiff_t id = recv_pts[k + 1];
715
716 loc_state[c] = id;
717 }
718 }
719
720 for (size_t i = 0; i < Sp.recv.nbr.size(); ++i) {
721 int npts = send_pts[i].size();
722 comm.wait(send_cnt_req[i]);
723 if (!npts)
724 continue;
725 comm.wait(send_pts_req[i]);
726 }
727 }
728
729 ARCCORE_ALINA_TOC("drop empty aggregates");
730 ARCCORE_ALINA_TOC("PMIS");
731
732 return naggr;
733 }
734
735 std::shared_ptr<matrix>
736 tentative_prolongation(AlinaCommunicator comm, ptrdiff_t n, ptrdiff_t naggr,
737 UniqueArray<ptrdiff_t>& state, UniqueArray<int>& owner)
738 {
739 auto p_loc = std::make_shared<build_matrix>();
740 auto p_rem = std::make_shared<build_matrix>();
741 build_matrix& P_loc = *p_loc;
742 build_matrix& P_rem = *p_rem;
743
744 ARCCORE_ALINA_TIC("tentative prolongation");
745
746 if (int null_cols = prm.nullspace.cols) {
747 ptrdiff_t nba = naggr / prm.block_size;
748
749 UniqueArray<ptrdiff_t> fdom = comm.exclusive_sum(n);
750 UniqueArray<ptrdiff_t> cdom = comm.exclusive_sum(naggr);
751
752 UniqueArray<int> scounts(comm.size, 0);
753 UniqueArray<int> rcounts(comm.size);
754
755 // Precompute the shape of the prolongation operator.
756 // Each row contains exactly nullspace.cols non-zero entries.
757 // Rows that do not belong to any aggregate are empty.
758 P_loc.set_size(n, null_cols * nba, true);
759 P_rem.set_size(n, 0, true);
760
761 // Also count the number of local DOFs in local aggregates
762 ptrdiff_t loc_dofs = 0;
763
764 for (ptrdiff_t i = 0; i < n; ++i) {
765 if (state[i] == DistributedPMISAggregation::deleted)
766 continue;
767
768 if (owner[i] == comm.rank) {
769 P_loc.ptr[i + 1] = null_cols;
770 ++loc_dofs;
771 }
772 else {
773 P_rem.ptr[i + 1] = null_cols;
774 ++scounts[owner[i]];
775 }
776 }
777
778 // Setup the exchange
779 SmallSpan<const int> send_counts_view(scounts);
780 SmallSpan<int> receive_counts_view(rcounts);
781 MessagePassing::mpAllToAll(comm.messagePassingMng(), send_counts_view, receive_counts_view, 1);
782
783 P_loc.set_nonzeros(P_loc.scan_row_sizes());
784 P_rem.set_nonzeros(P_rem.scan_row_sizes());
785
786 int snbr = 0;
787 int rnbr = 0;
788 for (int i = 0; i < comm.size; ++i) {
789 if (scounts[i])
790 ++snbr;
791 if (rcounts[i])
792 ++rnbr;
793 }
794
795 UniqueArray<int> send_nbr;
796 send_nbr.reserve(snbr);
797 UniqueArray<int> recv_nbr;
798 recv_nbr.reserve(rnbr);
799 UniqueArray<int> send_ptr;
800 send_ptr.reserve(snbr + 1);
801 send_ptr.push_back(0);
802 UniqueArray<int> recv_ptr;
803 recv_ptr.reserve(rnbr + 1);
804 recv_ptr.push_back(0);
805
806 for (int i = 0; i < comm.size; ++i) {
807 if (scounts[i]) {
808 send_nbr.push_back(i);
809 send_ptr.push_back(send_ptr.back() + scounts[i]);
810 }
811 if (rcounts[i]) {
812 recv_nbr.push_back(i);
813 recv_ptr.push_back(recv_ptr.back() + rcounts[i]);
814 }
815 }
816
817 int send_dofs = send_ptr.back();
818 int recv_dofs = recv_ptr.back();
819
820 UniqueArray<ptrdiff_t> send_agg(send_dofs); // IDs of the aggregates we are sending
821 UniqueArray<ptrdiff_t> send_dof(send_dofs); // DOFs included in the aggregates
822 UniqueArray<double> send_row(send_dofs * null_cols); // Rows of the nullspace matrix corresponding to the DOFs
823
824 UniqueArray<ptrdiff_t> recv_agg(recv_dofs); // IDs of the aggregates we are receiving
825 UniqueArray<ptrdiff_t> recv_dof(recv_dofs); // DOFs included in the aggregates
826 UniqueArray<double> recv_row(recv_dofs * null_cols); // Rows of the nullspace matrix corresponding to the DOFs
827
828 // Prepare the data to send
829 UniqueArray<ptrdiff_t> send_rank_ptr(comm.size + 1);
830 send_rank_ptr[0] = 0;
831 std::partial_sum(scounts.begin(), scounts.end(), send_rank_ptr.begin() + 1);
832 for (ptrdiff_t i = 0; i < n; ++i) {
833 auto s = state[i];
834 auto o = owner[i];
835
836 if (s == DistributedPMISAggregation::deleted)
837 continue;
838 if (o == comm.rank)
839 continue;
840
841 auto head = send_rank_ptr[o]++;
842
843 send_agg[head] = s;
844 send_dof[head] = i + fdom[comm.rank];
845 std::copy_n(&prm.nullspace.B[i * null_cols], null_cols, &send_row[head * null_cols]);
846 }
847
848 // Exchange the data
849 UniqueArray<MessagePassing::Request> send_req(3 * snbr);
850 UniqueArray<MessagePassing::Request> recv_req(3 * rnbr);
851
852 for (int i = 0; i < rnbr; ++i) {
853 int n = recv_nbr[i];
854 int p = recv_ptr[i];
855 int w = recv_ptr[i + 1] - p;
856
857 MessagePassing::Request* req = &recv_req[3 * i];
858
859 req[0] = comm.doIReceive(&recv_agg[p], w, n, tag_exc_agg);
860 req[1] = comm.doIReceive(&recv_dof[p], w, n, tag_exc_dof);
861 req[2] = comm.doIReceive(&recv_row[null_cols * p], null_cols * w, n, tag_exc_row);
862 }
863
864 for (int i = 0; i < snbr; ++i) {
865 int n = send_nbr[i];
866 int p = send_ptr[i];
867 int w = send_ptr[i + 1] - p;
868
869 MessagePassing::Request* req = &send_req[3 * i];
870
871 req[0] = comm.doISend(&send_agg[p], w, n, tag_exc_agg);
872 req[1] = comm.doISend(&send_dof[p], w, n, tag_exc_dof);
873 req[2] = comm.doISend(&send_row[null_cols * p], null_cols * w, n, tag_exc_row);
874 }
875
876 ARCCORE_ALINA_TIC("MPI Wait");
877 comm.waitAll(recv_req);
878 comm.waitAll(send_req);
879 ARCCORE_ALINA_TOC("MPI Wait");
880
881 // Sort the fine-level points by the aggregate number.
882 // The order vector contains tuples of (aggr, dof, src, dst),
883 // where src points to a row in B, and dst points to a row in P
884 UniqueArray<std::tuple<ptrdiff_t, ptrdiff_t, double*, value_type*>> order;
885 order.reserve(loc_dofs + recv_dofs);
886 for (ptrdiff_t i = 0; i < n; ++i) {
887 auto s = state[i];
888 auto o = owner[i];
889
890 if (s == DistributedPMISAggregation::deleted)
891 continue;
892 if (o != comm.rank)
893 continue;
894
895 order.emplace_back(s / prm.block_size, i + fdom[comm.rank],
896 &prm.nullspace.B[i * null_cols], &P_loc.val[P_loc.ptr[i]]);
897 }
898 for (ptrdiff_t i = 0; i < recv_dofs; ++i) {
899 order.emplace_back(recv_agg[i] / prm.block_size, recv_dof[i],
900 &recv_row[i * null_cols], nullptr);
901 }
902 std::sort(order.begin(), order.end());
903
904 UniqueArray<ptrdiff_t> aggr_ptr(nba + 1, 0);
905 for (size_t i = 0; i < order.size(); ++i)
906 ++aggr_ptr[std::get<0>(order[i]) + 1];
907 std::partial_sum(aggr_ptr.begin(), aggr_ptr.end(), aggr_ptr.begin());
908
909 // Compute the tentative prolongation operator and null-space vectors
910 // for the coarser level.
911 UniqueArray<double> Bnew;
912 Bnew.resize(nba * null_cols * null_cols);
913
914 arccoreParallelFor(0, nba, ForLoopRunInfo{}, [&](Int32 begin, Int32 size) {
915 Alina::detail::QRFactorization<double> qr;
916 UniqueArray<double> Bpart;
917
918 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
919 auto aggr_beg = aggr_ptr[i];
920 auto aggr_end = aggr_ptr[i + 1];
921 auto d = aggr_end - aggr_beg;
922
923 Bpart.resize(d * null_cols);
924
925 for (ptrdiff_t j = aggr_beg, r = 0; j < aggr_end; ++j, ++r) {
926 auto src = std::get<2>(order[j]);
927 for (int c = 0; c < null_cols; ++c)
928 Bpart[r + d * c] = src[c];
929 }
930
931 qr.factorize(d, null_cols, &Bpart[0], Alina::detail::col_major);
932
933 for (ptrdiff_t r = 0, k = i * null_cols * null_cols; r < null_cols; ++r)
934 for (int c = 0; c < null_cols; ++c, ++k)
935 Bnew[k] = qr.R(r, c);
936
937 for (ptrdiff_t j = aggr_beg, r = 0; j < aggr_end; ++j, ++r) {
938 auto src = std::get<2>(order[j]);
939 auto dst = std::get<3>(order[j]);
940
941 if (dst) {
942 // TODO: this is just a workaround to make non-scalar value
943 // types compile. Most probably this won't actually work.
944 for (int c = 0; c < null_cols; ++c)
945 dst[c] = qr.Q(r, c) * math::identity<value_type>();
946 }
947 else {
948 for (int c = 0; c < null_cols; ++c)
949 src[c] = qr.Q(r, c);
950 }
951 }
952 }
953 });
954
955 // Exchange the computed rows of the prolongation operator with the
956 // owners.
957 for (int i = 0; i < snbr; ++i) {
958 int n = send_nbr[i];
959 int p = send_ptr[i];
960 int w = send_ptr[i + 1] - p;
961 send_req[i] = comm.doIReceive(&send_row[null_cols * p], null_cols * w, n, tag_exc_row);
962 }
963
964 for (int i = 0; i < rnbr; ++i) {
965 int n = recv_nbr[i];
966 int p = recv_ptr[i];
967 int w = recv_ptr[i + 1] - p;
968 recv_req[i] = comm.doISend(&recv_row[null_cols * p], null_cols * w, n, tag_exc_row);
969 }
970
971 // Fill column numbers
972 arccoreParallelFor(0, n, ForLoopRunInfo{}, [&](Int32 begin, Int32 size) {
973 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
974 ptrdiff_t s = state[i];
975 if (s == DistributedPMISAggregation::deleted)
976 continue;
977
978 int d = owner[i];
979 if (d == comm.rank) {
980 auto col = &P_loc.col[P_loc.ptr[i]];
981 for (int j = 0; j < null_cols; ++j) {
982 col[j] = null_cols * s / prm.block_size + j;
983 }
984 }
985 else {
986 auto col = &P_rem.col[P_rem.ptr[i]];
987 for (int j = 0; j < null_cols; ++j) {
988 col[j] = null_cols * (s + cdom[d]) / prm.block_size + j;
989 }
990 }
991 }
992 });
993
994 ARCCORE_ALINA_TIC("MPI Wait");
995 comm.waitAll(send_req);
996 comm.waitAll(recv_req);
997 ARCCORE_ALINA_TOC("MPI Wait");
998
999 // Use the P rows computed by the neighbors
1000 for (ptrdiff_t k = 0; k < send_dofs; ++k) {
1001 auto i = send_dof[k] - fdom[comm.rank];
1002 auto src = &send_row[k * null_cols];
1003 auto dst = &P_rem.val[P_rem.ptr[i]];
1004
1005 for (ptrdiff_t j = 0; j < null_cols; ++j) {
1006 dst[j] = src[j] * math::identity<value_type>();
1007 }
1008 }
1009
1010 std::swap(prm.nullspace.B, Bnew);
1011 }
1012 else {
1013 UniqueArray<ptrdiff_t> dom = comm.exclusive_sum(naggr);
1014
1015 P_loc.set_size(n, naggr, true);
1016 P_rem.set_size(n, 0, true);
1017
1018 arccoreParallelFor(0, n, ForLoopRunInfo{}, [&](Int32 begin, Int32 size) {
1019 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
1020 if (state[i] == DistributedPMISAggregation::deleted)
1021 continue;
1022
1023 if (owner[i] == comm.rank) {
1024 ++P_loc.ptr[i + 1];
1025 }
1026 else {
1027 ++P_rem.ptr[i + 1];
1028 }
1029 }
1030 });
1031
1032 P_loc.set_nonzeros(P_loc.scan_row_sizes());
1033 P_rem.set_nonzeros(P_rem.scan_row_sizes());
1034
1035 arccoreParallelFor(0, n, ForLoopRunInfo{}, [&](Int32 begin, Int32 size) {
1036 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
1037 ptrdiff_t s = state[i];
1038 if (s == DistributedPMISAggregation::deleted)
1039 continue;
1040
1041 int d = owner[i];
1042 if (d == comm.rank) {
1043 P_loc.col[P_loc.ptr[i]] = s;
1044 P_loc.val[P_loc.ptr[i]] = math::identity<value_type>();
1045 }
1046 else {
1047 P_rem.col[P_rem.ptr[i]] = s + dom[d];
1048 P_rem.val[P_rem.ptr[i]] = math::identity<value_type>();
1049 }
1050 }
1051 });
1052 }
1053 ARCCORE_ALINA_TOC("tentative prolongation");
1054
1055 return std::make_shared<matrix>(comm, p_loc, p_rem);
1056 }
1057
1058 template <class pw_matrix>
1059 std::shared_ptr<bool_matrix>
1060 expand_conn(const build_matrix& A, const pw_matrix& Ap, const bool_matrix& Cp,
1061 unsigned block_size) const
1062 {
1063 ptrdiff_t np = Cp.nbRow();
1064 ptrdiff_t n = np * block_size;
1065
1066 auto c = std::make_shared<bool_matrix>();
1067 bool_matrix& C = *c;
1068
1069 C.set_size(n, n, true);
1070 C.val.resize(A.nbNonZero());
1071
1072 arccoreParallelFor(0, np, ForLoopRunInfo{}, [&](Int32 begin, Int32 size) {
1073 UniqueArray<ptrdiff_t> j(block_size);
1074 UniqueArray<ptrdiff_t> e(block_size);
1075
1076 for (ptrdiff_t ip = begin; ip < (begin + size); ++ip) {
1077 ptrdiff_t ia = ip * block_size;
1078
1079 for (unsigned k = 0; k < block_size; ++k) {
1080 j[k] = A.ptr[ia + k];
1081 e[k] = A.ptr[ia + k + 1];
1082 }
1083
1084 for (ptrdiff_t jp = Ap.ptr[ip], ep = Ap.ptr[ip + 1]; jp < ep; ++jp) {
1085 ptrdiff_t cp = Ap.col[jp];
1086 bool sp = Cp.val[jp];
1087
1088 ptrdiff_t col_end = (cp + 1) * block_size;
1089
1090 for (unsigned k = 0; k < block_size; ++k) {
1091 ptrdiff_t beg = j[k];
1092 ptrdiff_t end = e[k];
1093
1094 while (beg < end && A.col[beg] < col_end) {
1095 C.val[beg++] = sp;
1096
1097 if (sp)
1098 ++C.ptr[ia + k + 1];
1099 }
1100
1101 j[k] = beg;
1102 }
1103 }
1104 }
1105 });
1106
1107 C.setNbNonZero(C.scan_row_sizes());
1108 C.col.resize(C.nbNonZero());
1109
1110 arccoreParallelFor(0, np, ForLoopRunInfo{}, [&](Int32 begin, Int32 size) {
1111 UniqueArray<ptrdiff_t> j(block_size);
1112 UniqueArray<ptrdiff_t> e(block_size);
1113 UniqueArray<ptrdiff_t> h(block_size);
1114
1115 for (ptrdiff_t ip = begin; ip < (begin + size); ++ip) {
1116 ptrdiff_t ia = ip * block_size;
1117
1118 for (unsigned k = 0; k < block_size; ++k) {
1119 j[k] = A.ptr[ia + k];
1120 e[k] = A.ptr[ia + k + 1];
1121 h[k] = C.ptr[ia + k];
1122 }
1123
1124 for (ptrdiff_t jp = Ap.ptr[ip], ep = Ap.ptr[ip + 1]; jp < ep; ++jp) {
1125 ptrdiff_t cp = Ap.col[jp];
1126 bool sp = Cp.val[jp];
1127
1128 ptrdiff_t col_end = (cp + 1) * block_size;
1129
1130 for (unsigned k = 0; k < block_size; ++k) {
1131 ptrdiff_t beg = j[k];
1132 ptrdiff_t end = e[k];
1133 ptrdiff_t hed = h[k];
1134
1135 while (beg < end && A.col[beg] < col_end) {
1136 if (sp)
1137 C.col[hed++] = A.col[beg];
1138 ++beg;
1139 }
1140
1141 j[k] = beg;
1142 h[k] = hed;
1143 }
1144 }
1145 }
1146 });
1147
1148 return c;
1149 }
1150
1151 private:
1152
1153 static const int undone = -2;
1154 static const int deleted = -1;
1155
1156 static const int tag_exc_agg = 4011;
1157 static const int tag_exc_dof = 4012;
1158 static const int tag_exc_row = 4013;
1159};
1160
1161/*---------------------------------------------------------------------------*/
1162/*---------------------------------------------------------------------------*/
1166template <class Backend>
1167struct DistributedAggregationCoarsening
1168{
1169 typedef typename Backend::value_type value_type;
1170 typedef typename math::scalar_of<value_type>::type scalar_type;
1171 using build_matrix = Backend::matrix;
1172
1173 struct params
1174 {
1175 // aggregation params
1176 typedef typename DistributedPMISAggregation<Backend>::params aggr_params;
1177 aggr_params aggr;
1178
1193 float over_interp = 1.5f;
1194
1195 params() = default;
1196
1197 params(const PropertyTree& p)
1198 : ARCCORE_ALINA_PARAMS_IMPORT_CHILD(p, aggr)
1199 , ARCCORE_ALINA_PARAMS_IMPORT_VALUE(p, over_interp)
1200 {
1201 p.check_params({ "aggr", "over_interp" });
1202 }
1203
1204 void get(Alina::PropertyTree& p, const std::string& path) const
1205 {
1206 ARCCORE_ALINA_PARAMS_EXPORT_CHILD(p, path, aggr);
1207 ARCCORE_ALINA_PARAMS_EXPORT_VALUE(p, path, over_interp);
1208 }
1209 } prm;
1210
1211 DistributedAggregationCoarsening(const params& prm = params())
1212 : prm(prm)
1213 {}
1214
1215 std::tuple<std::shared_ptr<DistributedMatrix<Backend>>,
1216 std::shared_ptr<DistributedMatrix<Backend>>>
1217 transfer_operators(const DistributedMatrix<Backend>& A)
1218 {
1219 DistributedPMISAggregation<Backend> aggr(A, prm.aggr);
1220 return std::make_tuple(aggr.p_tent, transpose(*aggr.p_tent));
1221 }
1222
1223 std::shared_ptr<DistributedMatrix<Backend>>
1224 coarse_operator(const DistributedMatrix<Backend>& A,
1225 const DistributedMatrix<Backend>& P,
1226 const DistributedMatrix<Backend>& R) const
1227 {
1228 return detail::scaled_galerkin(A, P, R, 1 / prm.over_interp);
1229 }
1230};
1231
1232/*---------------------------------------------------------------------------*/
1233/*---------------------------------------------------------------------------*/
1234
1235template <class Backend>
1236unsigned block_size(const DistributedAggregationCoarsening<Backend>& c)
1237{
1238 return c.prm.aggr.block_size;
1239}
1240
1241/*---------------------------------------------------------------------------*/
1242/*---------------------------------------------------------------------------*/
1246template <class Backend>
1247struct DistributedSmoothedAggregationCoarsening
1248{
1249 typedef typename Backend::value_type value_type;
1250 typedef typename math::scalar_of<value_type>::type scalar_type;
1251 using build_matrix = Backend::matrix;
1252 using col_type = Backend::col_type;
1253 using ptr_type = Backend::ptr_type;
1254 using bool_backend = BuiltinBackend<char, col_type, ptr_type>;
1255 using bool_matrix = bool_backend::matrix;
1256
1257 struct params
1258 {
1259 // aggregation params
1260 typedef typename DistributedPMISAggregation<Backend>::params aggr_params;
1261 aggr_params aggr;
1262
1264 scalar_type relax = 1.0;
1265
1266 // Estimate the matrix spectral radius.
1267 // This usually improves convergence rate and results in faster solves,
1268 // but costs some time during setup.
1269 bool estimate_spectral_radius = false;
1270
1271 // Number of power iterations to apply for the spectral radius
1272 // estimation. Use Gershgorin disk theorem when power_iters = 0.
1273 int power_iters = 0;
1274
1275 params() = default;
1276
1277 params(const PropertyTree& p)
1278 : ARCCORE_ALINA_PARAMS_IMPORT_CHILD(p, aggr)
1279 , ARCCORE_ALINA_PARAMS_IMPORT_VALUE(p, relax)
1280 , ARCCORE_ALINA_PARAMS_IMPORT_VALUE(p, estimate_spectral_radius)
1281 , ARCCORE_ALINA_PARAMS_IMPORT_VALUE(p, power_iters)
1282 {
1283 p.check_params({ "aggr", "relax", "estimate_spectral_radius", "power_iters" });
1284 }
1285
1286 void get(PropertyTree& p, const std::string& path) const
1287 {
1288 ARCCORE_ALINA_PARAMS_EXPORT_CHILD(p, path, aggr);
1289 ARCCORE_ALINA_PARAMS_EXPORT_VALUE(p, path, relax);
1290 ARCCORE_ALINA_PARAMS_EXPORT_VALUE(p, path, estimate_spectral_radius);
1291 ARCCORE_ALINA_PARAMS_EXPORT_VALUE(p, path, power_iters);
1292 }
1293 } prm;
1294
1295 DistributedSmoothedAggregationCoarsening(const params& prm = params())
1296 : prm(prm)
1297 {}
1298
1299 std::tuple<std::shared_ptr<DistributedMatrix<Backend>>,
1300 std::shared_ptr<DistributedMatrix<Backend>>>
1301 transfer_operators(const DistributedMatrix<Backend>& A)
1302 {
1303 typedef DistributedMatrix<Backend> DM;
1304 using build_matrix = Backend::matrix;
1305
1306 DistributedPMISAggregation<Backend> aggr(A, prm.aggr);
1307 prm.aggr.eps_strong *= 0.5;
1308
1309 AlinaCommunicator comm = A.comm();
1310 const build_matrix& A_loc = *A.local();
1311 const build_matrix& A_rem = *A.remote();
1312
1313 bool_matrix& S_loc = *aggr.conn->local();
1314 bool_matrix& S_rem = *aggr.conn->remote();
1315
1316 ARCCORE_ALINA_TIC("filtered matrix");
1317 ptrdiff_t n = A.loc_rows();
1318
1319 scalar_type omega = prm.relax;
1320 if (prm.estimate_spectral_radius) {
1321 omega *= static_cast<scalar_type>(4.0 / 3) / spectral_radius<true>(A, prm.power_iters);
1322 }
1323 else {
1324 omega *= static_cast<scalar_type>(2.0 / 3);
1325 }
1326
1327 auto af_loc = std::make_shared<build_matrix>();
1328 auto af_rem = std::make_shared<build_matrix>();
1329
1330 build_matrix& Af_loc = *af_loc;
1331 build_matrix& Af_rem = *af_rem;
1332
1333 numa_vector<value_type> Af_loc_val(S_loc.nbNonZero(), false);
1334 numa_vector<value_type> Af_rem_val(S_rem.nbNonZero(), false);
1335
1336 Af_loc.own_data = false;
1337 Af_loc.setNbRow(S_loc.nbRow());
1338 Af_loc.ncols = S_loc.ncols;
1339 Af_loc.setNbNonZero(S_loc.nbNonZero());
1340 Af_loc.ptr.setPointerZeroCopy(S_loc.ptr.data());
1341 Af_loc.col.setPointerZeroCopy(S_loc.col.data());
1342 Af_loc.val.setPointerZeroCopy(Af_loc_val.data());
1343
1344 Af_rem.own_data = false;
1345 Af_rem.setNbRow(S_rem.nbRow());
1346 Af_rem.ncols = S_rem.ncols;
1347 Af_rem.setNbNonZero(S_rem.nbNonZero());
1348 Af_rem.ptr.setPointerZeroCopy(S_rem.ptr.data());
1349 Af_rem.col.setPointerZeroCopy(S_rem.col.data());
1350 Af_rem.val.setPointerZeroCopy(Af_rem_val.data());
1351
1352 numa_vector<value_type> Df(n, false);
1353
1354 arccoreParallelFor(0, n, ForLoopRunInfo{}, [&](Int32 begin, Int32 size) {
1355 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
1356
1357 ptrdiff_t loc_head = Af_loc.ptr[i];
1358 ptrdiff_t rem_head = Af_rem.ptr[i];
1359
1360 value_type dia_f = math::zero<value_type>();
1361
1362 for (ptrdiff_t j = A_loc.ptr[i], e = A_loc.ptr[i + 1]; j < e; ++j)
1363 if (A_loc.col[j] == i || !S_loc.val[j])
1364 dia_f += A_loc.val[j];
1365
1366 for (ptrdiff_t j = A_rem.ptr[i], e = A_rem.ptr[i + 1]; j < e; ++j)
1367 if (!S_rem.val[j])
1368 dia_f += A_rem.val[j];
1369
1370 dia_f = -omega * math::inverse(dia_f);
1371
1372 for (ptrdiff_t j = A_loc.ptr[i], e = A_loc.ptr[i + 1]; j < e; ++j) {
1373 if (A_loc.col[j] == i) {
1374 Af_loc.val[loc_head++] = (1 - omega) * math::identity<value_type>();
1375 }
1376 else if (S_loc.val[j]) {
1377 Af_loc.val[loc_head++] = dia_f * A_loc.val[j];
1378 }
1379 }
1380
1381 for (ptrdiff_t j = A_rem.ptr[i], e = A_rem.ptr[i + 1]; j < e; ++j) {
1382 if (S_rem.val[j]) {
1383 Af_rem.val[rem_head++] = dia_f * A_rem.val[j];
1384 }
1385 }
1386 }
1387 });
1388
1389 auto Af = std::make_shared<DM>(comm, af_loc, af_rem);
1390 ARCCORE_ALINA_TOC("filtered matrix");
1391
1392 // 5. Smooth tentative prolongation with the filtered matrix.
1393 ARCCORE_ALINA_TIC("smoothing");
1394 auto P = product(*Af, *aggr.p_tent);
1395 ARCCORE_ALINA_TOC("smoothing");
1396
1397 return std::make_tuple(P, transpose(*P));
1398 }
1399
1400 std::shared_ptr<DistributedMatrix<Backend>>
1401 coarse_operator(const DistributedMatrix<Backend>& A,
1402 const DistributedMatrix<Backend>& P,
1403 const DistributedMatrix<Backend>& R) const
1404 {
1405 return detail::galerkin(A, P, R);
1406 }
1407};
1408
1409/*---------------------------------------------------------------------------*/
1410/*---------------------------------------------------------------------------*/
1411
1412template <class Backend>
1413unsigned block_size(const DistributedSmoothedAggregationCoarsening<Backend>& c)
1414{
1415 return c.prm.aggr.block_size;
1416}
1417
1418/*---------------------------------------------------------------------------*/
1419/*---------------------------------------------------------------------------*/
1420
1421} // namespace Arcane::Alina
1422
1423/*---------------------------------------------------------------------------*/
1424/*---------------------------------------------------------------------------*/
1425
1426#endif
Call to handle communication pattern.
Distributed Matrix using message passing.
Class to store parameters as a hierarchical key/value tree.
Definition AlinaUtils.h:112
1D data vector with value semantics (STL style).
C void mpAllToAll(IMessagePassingMng *pm, Span< const char > send_buf, Span< char > recv_buf, Int32 count)
__host__ __device__ Real3x3 transpose(const Real3x3 &t)
Transpose the matrix.
Definition MathUtils.h:265
void arccoreParallelFor(const ComplexForLoopRanges< RankValue, IndexType_ > &loop_ranges, const ForLoopRunInfo &run_info, const LambdaType &lambda_function, const ReducerArgs &... reducer_args)
Applies the lambda function lambda_function concurrently over the iteration interval given by loop_ra...
Definition ParallelFor.h:86
std::int32_t Int32
Signed integer type of 32 bits.
Distributed non-smoothed aggregation coarsening scheme.
nullspace_params nullspace
Near nullspace parameters.
Distributed smoothed aggregation coarsening scheme.