Arcane  4.2.3.0
Developer documentation
Loading...
Searching...
No Matches
DistributedMatrix.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/* DistributedMatrix.h (C) 2000-2026 */
9/* */
10/* Distributed Matrix using message passing. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_ALINA_MPI_DISTRIBUTED_MATRIX_H
13#define ARCCORE_ALINA_MPI_DISTRIBUTED_MATRIX_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/AlinaUtils.h"
27#include "arccore/alina/MessagePassingUtils.h"
28
29#include "arccore/accelerator/Atomic.h"
30
31#include <vector>
32#include <algorithm>
33
34#include <memory>
35#include <unordered_map>
36#include <random>
37
38#include <mpi.h>
39
40/*---------------------------------------------------------------------------*/
41/*---------------------------------------------------------------------------*/
42
43namespace Arcane::Alina
44{
45
46/*---------------------------------------------------------------------------*/
47/*---------------------------------------------------------------------------*/
51template <class Backend>
52class CommunicationPattern
53{
54 public:
55
56 typedef typename Backend::value_type value_type;
57 typedef typename math::rhs_of<value_type>::type rhs_type;
58 typedef typename math::scalar_of<value_type>::type scalar_type;
59 typedef typename Backend::matrix matrix;
60 typedef typename Backend::vector vector;
61 typedef typename Backend::params backend_params;
62 typedef typename Backend::col_type col_type;
63 typedef typename Backend::ptr_type ptr_type;
64
65 struct
66 {
67 std::vector<ptrdiff_t> nbr;
68 std::vector<ptr_type> ptr;
69 std::vector<col_type> col;
70
71 size_t count() const
72 {
73 return col.size();
74 }
75
76 mutable std::vector<rhs_type> val;
78 } send;
79
80 struct
81 {
82 std::vector<ptrdiff_t> nbr;
83 std::vector<ptr_type> ptr;
84
85 size_t count() const
86 {
87 return val.size();
88 }
89
90 mutable std::vector<rhs_type> val;
92 } recv;
93
94 std::shared_ptr<vector> x_rem;
95
96 CommunicationPattern(AlinaCommunicator comm,
97 ptrdiff_t n_loc_cols,
98 size_t n_rem_cols, const col_type* p_rem_cols)
99 : comm(comm)
100 , loc_cols(n_loc_cols)
101 {
102 ARCCORE_ALINA_TIC("communication pattern");
103 // Get domain boundaries
104 UniqueArray<ptrdiff_t> domain = comm.exclusive_sum(n_loc_cols);
105 loc_beg = domain[comm.rank];
106
107 // Renumber remote columns,
108 // find out how many remote values we need from each process.
109 std::vector<col_type> rem_cols(p_rem_cols, p_rem_cols + n_rem_cols);
110
111 std::sort(rem_cols.begin(), rem_cols.end());
112 rem_cols.erase(std::unique(rem_cols.begin(), rem_cols.end()), rem_cols.end());
113
114 ptrdiff_t ncols = rem_cols.size();
115 ptrdiff_t rnbr = 0, snbr = 0, send_size = 0;
116
117 {
118 UniqueArray<int> rcounts(comm.size, 0);
119 UniqueArray<int> scounts(comm.size);
120
121 // Build index for column renumbering;
122 // count how many domains send us data and how much.
123 idx.reserve(2 * ncols);
124 for (int i = 0, d = 0, last = -1; i < ncols; ++i) {
125 while (rem_cols[i] >= domain[d + 1])
126 ++d;
127
128 ++rcounts[d];
129
130 if (last < d) {
131 last = d;
132 ++rnbr;
133 }
134
135 idx.insert(idx.end(), std::make_pair(rem_cols[i], std::make_tuple(rnbr - 1, i)));
136 }
137
138 recv.val.resize(ncols);
139 recv.req.resize(rnbr);
140
141 recv.nbr.reserve(rnbr);
142 recv.ptr.reserve(rnbr + 1);
143 recv.ptr.push_back(0);
144
145 for (int d = 0; d < comm.size; ++d) {
146 if (rcounts[d]) {
147 recv.nbr.push_back(d);
148 recv.ptr.push_back(recv.ptr.back() + rcounts[d]);
149 }
150 }
151 MessagePassing::mpAllToAll(comm.messagePassingMng(), rcounts, scounts, 1);
152
153 for (ptrdiff_t d = 0; d < comm.size; ++d) {
154 if (scounts[d]) {
155 ++snbr;
156 send_size += scounts[d];
157 }
158 }
159
160 send.col.resize(send_size);
161 send.val.resize(send_size);
162 send.req.resize(snbr);
163
164 send.nbr.reserve(snbr);
165 send.ptr.reserve(snbr + 1);
166 send.ptr.push_back(0);
167
168 for (ptrdiff_t d = 0; d < comm.size; ++d) {
169 if (scounts[d]) {
170 send.nbr.push_back(d);
171 send.ptr.push_back(send.ptr.back() + scounts[d]);
172 }
173 }
174 }
175
176 // What columns do you need from me?
177 for (size_t i = 0; i < send.nbr.size(); ++i)
178 send.req[i] = comm.doIReceive(&send.col[send.ptr[i]], send.ptr[i + 1] - send.ptr[i],
179 send.nbr[i], tag_exc_cols);
180
181 // Here is what I need from you:
182 for (size_t i = 0; i < recv.nbr.size(); ++i)
183 recv.req[i] = comm.doISend(&rem_cols[recv.ptr[i]], recv.ptr[i + 1] - recv.ptr[i],
184 recv.nbr[i], tag_exc_cols);
185
186 ARCCORE_ALINA_TIC("MPI Wait");
187 comm.waitAll(recv.req);
188 comm.waitAll(send.req);
189 ARCCORE_ALINA_TOC("MPI Wait");
190
191 // Shift columns to send to local numbering:
192 for (col_type& c : send.col)
193 c -= loc_beg;
194
195 ARCCORE_ALINA_TOC("communication pattern");
196 }
197
198 template <class OtherBackend>
199 CommunicationPattern(const CommunicationPattern<OtherBackend>& C)
200 : comm(C.comm)
201 , idx(C.idx)
202 , loc_beg(C.loc_beg)
203 , loc_cols(C.loc_cols)
204 {
205 send.nbr = C.send.nbr;
206 send.ptr = C.send.ptr;
207 send.col = C.send.col;
208 send.val.resize(C.send.val.size());
209 send.req.resize(C.send.req.size());
210
211 recv.nbr = C.recv.nbr;
212 recv.ptr = C.recv.ptr;
213 recv.val.resize(C.recv.val.size());
214 recv.req.resize(C.recv.req.size());
215 }
216
217 void move_to_backend(const backend_params& bprm = backend_params())
218 {
219 if (!x_rem) {
220 x_rem = Backend::create_vector(recv.count(), bprm);
221 }
222
223 if (!gather) {
224 gather = std::make_shared<Gather>(loc_cols, send.col, bprm);
225 }
226 }
227
228 int domain(ptrdiff_t col) const
229 {
230 return std::get<0>(idx.at(col));
231 }
232
233 int local_index(ptrdiff_t col) const
234 {
235 return std::get<1>(idx.at(col));
236 }
237
238 std::tuple<int, int> remote_info(ptrdiff_t col) const
239 {
240 return idx.at(col);
241 }
242
243 std::unordered_map<ptrdiff_t, std::tuple<int, int>>::const_iterator
244 remote_begin() const
245 {
246 return idx.cbegin();
247 }
248
249 std::unordered_map<ptrdiff_t, std::tuple<int, int>>::const_iterator
250 remote_end() const
251 {
252 return idx.cend();
253 }
254
255 size_t renumber(size_t n, col_type* col) const
256 {
257 for (size_t i = 0; i < n; ++i)
258 col[i] = std::get<1>(idx.at(col[i]));
259 return recv.count();
260 }
261
262 bool needs_remote() const
263 {
264 return !recv.val.empty();
265 }
266
267 template <class Vector>
268 void start_exchange(const Vector& x) const
269 {
270 // Start receiving ghost values from our neighbours.
271 for (size_t i = 0; i < recv.nbr.size(); ++i)
272 recv.req[i] = comm.doIReceive(&recv.val[recv.ptr[i]], recv.ptr[i + 1] - recv.ptr[i],
273 recv.nbr[i], tag_exc_vals);
274
275 // Start sending our data to neighbours.
276 if (!send.val.empty()) {
277 (*gather)(x, send.val);
278
279 for (size_t i = 0; i < send.nbr.size(); ++i)
280 send.req[i] = comm.doISend(&send.val[send.ptr[i]], send.ptr[i + 1] - send.ptr[i],
281 send.nbr[i], tag_exc_vals);
282 }
283 }
284
285 void finish_exchange() const
286 {
287 ARCCORE_ALINA_TIC("MPI Wait");
288 comm.waitAll(recv.req);
289 comm.waitAll(send.req);
290 ARCCORE_ALINA_TOC("MPI Wait");
291
292 if (!recv.val.empty())
293 backend::copy(recv.val, *x_rem);
294 }
295
296 template <typename T>
297 void exchange(const T* send_val, T* recv_val) const
298 {
299 for (size_t i = 0; i < recv.nbr.size(); ++i)
300 recv.req[i] = comm.doIReceive(&recv_val[recv.ptr[i]], recv.ptr[i + 1] - recv.ptr[i],
301 recv.nbr[i], tag_exc_vals);
302
303 for (size_t i = 0; i < send.nbr.size(); ++i)
304 send.req[i] = comm.doISend(const_cast<T*>(&send_val[send.ptr[i]]), send.ptr[i + 1] - send.ptr[i],
305 send.nbr[i], tag_exc_vals);
306
307 ARCCORE_ALINA_TIC("MPI Wait");
308 comm.waitAll(recv.req);
309 comm.waitAll(send.req);
310 ARCCORE_ALINA_TOC("MPI Wait");
311 }
312
313 AlinaCommunicator mpi_comm() const
314 {
315 return comm;
316 }
317
318 ptrdiff_t loc_col_shift() const
319 {
320 return loc_beg;
321 }
322
323 private:
324
325 using Gather = Backend::gather;
326
327 static const int tag_set_comm = 1001;
328 static const int tag_exc_cols = 1002;
329 static const int tag_exc_vals = 1003;
330
332
333 std::unordered_map<ptrdiff_t, std::tuple<int, int>> idx;
334 std::shared_ptr<Gather> gather;
335 ptrdiff_t loc_beg;
336 ptrdiff_t loc_cols;
337
338 template <class B>
339 friend class CommunicationPattern;
340};
341
342/*---------------------------------------------------------------------------*/
343/*---------------------------------------------------------------------------*/
347template <class Backend>
348class DistributedMatrix
349{
350 public:
351
352 typedef typename Backend::value_type value_type;
353 typedef typename math::rhs_of<value_type>::type rhs_type;
354 typedef typename math::scalar_of<value_type>::type scalar_type;
355 typedef typename Backend::params backend_params;
356 typedef typename Backend::matrix matrix;
357 typedef CommunicationPattern<Backend> CommPattern;
358 typedef typename Backend::matrix build_matrix;
359
360 DistributedMatrix(AlinaCommunicator comm,
361 std::shared_ptr<build_matrix> a_loc,
362 std::shared_ptr<build_matrix> a_rem,
363 std::shared_ptr<CommPattern> c = std::shared_ptr<CommPattern>())
364 : a_loc(a_loc)
365 , a_rem(a_rem)
366 {
367 if (c) {
368 C = c;
369 }
370 else {
371 C = std::make_shared<CommPattern>(comm, a_loc->ncols, a_rem->nbNonZero(), a_rem->col);
372 }
373
374 a_rem->ncols = C->recv.count();
375
376 n_loc_rows = a_loc->nbRow();
377 n_loc_cols = a_loc->ncols;
378 n_loc_nonzeros = a_loc->nbNonZero() + a_rem->nbNonZero();
379
380 n_glob_rows = comm.reduceSum(n_loc_rows);
381 n_glob_cols = comm.reduceSum(n_loc_cols);
382 n_glob_nonzeros = comm.reduceSum(n_loc_nonzeros);
383 }
384
385 // Copy the distributed_matrix from another backend
386 template <class OtherBackend>
387 DistributedMatrix(const DistributedMatrix<OtherBackend>& A)
388 : a_loc(std::make_shared<build_matrix>(*A.local()))
389 , a_rem(std::make_shared<build_matrix>(*A.remote()))
390 {
391 C = std::make_shared<CommPattern>(A.cpat());
392
393 this->a_rem->ncols = C->recv.count();
394
395 n_loc_rows = A.loc_rows();
396 n_loc_cols = A.loc_cols();
397 n_loc_nonzeros = A.loc_nonzeros();
398 n_glob_rows = A.glob_rows();
399 n_glob_cols = A.glob_cols();
400 n_glob_nonzeros = A.glob_nonzeros();
401 }
402
403 template <class Matrix>
404 DistributedMatrix(AlinaCommunicator comm,
405 const Matrix& A,
406 ptrdiff_t _n_loc_cols = -1)
407 : n_loc_rows(backend::nbRow(A))
408 , n_loc_cols(_n_loc_cols < 0 ? n_loc_rows : _n_loc_cols)
409 , n_loc_nonzeros(backend::nonzeros(A))
410 {
411 // Get sizes of each domain in comm.
412 UniqueArray<ptrdiff_t> domain = comm.exclusive_sum(n_loc_cols);
413 ptrdiff_t loc_beg = domain[comm.rank];
414 ptrdiff_t loc_end = domain[comm.rank + 1];
415
416 n_glob_cols = domain.back();
417 n_glob_rows = comm.reduceSum(n_loc_rows);
418 n_glob_nonzeros = comm.reduceSum(n_loc_nonzeros);
419
420 // Split the matrix into local and remote parts.
421 a_loc = std::make_shared<build_matrix>();
422 a_rem = std::make_shared<build_matrix>();
423
424 build_matrix& A_loc = *a_loc;
425 build_matrix& A_rem = *a_rem;
426
427 A_loc.set_size(n_loc_rows, n_loc_cols, true);
428 A_rem.set_size(n_loc_rows, 0, true);
429
430 arccoreParallelFor(0, n_loc_rows, ForLoopRunInfo{}, [&](Int32 begin, Int32 size) {
431 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
432 for (auto a = backend::row_begin(A, i); a; ++a) {
433 ptrdiff_t c = a.col();
434
435 if (loc_beg <= c && c < loc_end)
436 ++A_loc.ptr[i + 1];
437 else
438 ++A_rem.ptr[i + 1];
439 }
440 }
441 });
442
443 A_loc.set_nonzeros(A_loc.scan_row_sizes());
444 A_rem.set_nonzeros(A_rem.scan_row_sizes());
445
446 arccoreParallelFor(0, n_loc_rows, ForLoopRunInfo{}, [&](Int32 begin, Int32 size) {
447 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
448 ptrdiff_t loc_head = A_loc.ptr[i];
449 ptrdiff_t rem_head = A_rem.ptr[i];
450
451 for (auto a = backend::row_begin(A, i); a; ++a) {
452 ptrdiff_t c = a.col();
453 value_type v = a.value();
454
455 if (loc_beg <= c && c < loc_end) {
456 A_loc.col[loc_head] = c - loc_beg;
457 A_loc.val[loc_head] = v;
458 ++loc_head;
459 }
460 else {
461 A_rem.col[rem_head] = c;
462 A_rem.val[rem_head] = v;
463 ++rem_head;
464 }
465 }
466 }
467 });
468
469 C = std::make_shared<CommPattern>(comm, n_loc_cols, a_rem->nbNonZero(), a_rem->col);
470 a_rem->ncols = C->recv.count();
471 }
472
473 AlinaCommunicator comm() const
474 {
475 return C->mpi_comm();
476 }
477
478 std::shared_ptr<build_matrix> local() const
479 {
480 return a_loc;
481 }
482
483 std::shared_ptr<build_matrix> remote() const
484 {
485 return a_rem;
486 }
487
488 std::shared_ptr<matrix> local_backend() const
489 {
490 return A_loc;
491 }
492
493 std::shared_ptr<matrix> remote_backend() const
494 {
495 return A_rem;
496 }
497
498 ptrdiff_t loc_rows() const
499 {
500 return n_loc_rows;
501 }
502
503 ptrdiff_t loc_cols() const
504 {
505 return n_loc_cols;
506 }
507
508 ptrdiff_t loc_col_shift() const
509 {
510 return C->loc_col_shift();
511 }
512
513 ptrdiff_t loc_nonzeros() const
514 {
515 return n_loc_nonzeros;
516 }
517
518 ptrdiff_t glob_rows() const
519 {
520 return n_glob_rows;
521 }
522
523 ptrdiff_t glob_cols() const
524 {
525 return n_glob_cols;
526 }
527
528 ptrdiff_t glob_nonzeros() const
529 {
530 return n_glob_nonzeros;
531 }
532
533 const CommunicationPattern<Backend>& cpat() const
534 {
535 return *C;
536 }
537
538 void set_local(std::shared_ptr<matrix> a)
539 {
540 A_loc = a;
541 }
542
543 void move_to_backend(const backend_params& bprm = backend_params(), bool keep_src = false)
544 {
545 ARCCORE_ALINA_TIC("move to backend");
546 if (!A_loc) {
547 A_loc = Backend::copy_matrix(a_loc, bprm);
548 }
549
550 if (!A_rem && a_rem && a_rem->nbNonZero() > 0) {
551 if (keep_src) {
552 auto rem_copy = std::make_shared<build_matrix>(*a_rem);
553 C->renumber(rem_copy->nbNonZero(), rem_copy->col);
554 A_rem = Backend::copy_matrix(rem_copy, bprm);
555 }
556 else {
557 C->renumber(a_rem->nbNonZero(), a_rem->col);
558 A_rem = Backend::copy_matrix(a_rem, bprm);
559 }
560 }
561
562 C->move_to_backend(bprm);
563
564 if (!keep_src) {
565 a_loc.reset();
566 a_rem.reset();
567 }
568 ARCCORE_ALINA_TOC("move to backend");
569 }
570
571 template <class A, class VecX, class B, class VecY>
572 void mul(A alpha, const VecX& x, B beta, VecY& y) const
573 {
574 const auto one = math::identity<scalar_type>();
575
576 C->start_exchange(x);
577
578 // Compute local part of the product.
579 backend::spmv(alpha, *A_loc, x, beta, y);
580
581 // Compute remote part of the product.
582 C->finish_exchange();
583
584 if (C->needs_remote())
585 backend::spmv(alpha, *A_rem, *C->x_rem, one, y);
586 }
587
588 template <class Vec1, class Vec2, class Vec3>
589 void residual(const Vec1& f, const Vec2& x, Vec3& r) const
590 {
591 const auto one = math::identity<scalar_type>();
592
593 C->start_exchange(x);
594 backend::residual(f, *A_loc, x, r);
595
596 C->finish_exchange();
597
598 if (C->needs_remote())
599 backend::spmv(-one, *A_rem, *C->x_rem, one, r);
600 }
601
602 private:
603
604 std::shared_ptr<CommPattern> C;
605 std::shared_ptr<matrix> A_loc, A_rem;
606 std::shared_ptr<build_matrix> a_loc, a_rem;
607
608 ptrdiff_t n_loc_rows, n_glob_rows;
609 ptrdiff_t n_loc_cols, n_glob_cols;
610 ptrdiff_t n_loc_nonzeros, n_glob_nonzeros;
611};
612
613/*---------------------------------------------------------------------------*/
614/*---------------------------------------------------------------------------*/
615
616template <class Backend>
617std::shared_ptr<DistributedMatrix<Backend>>
618transpose(const DistributedMatrix<Backend>& A)
619{
620 ARCCORE_ALINA_TIC("MPI Transpose");
621 typedef typename Backend::value_type value_type;
622 typedef CommunicationPattern<Backend> CommPattern;
623 typedef typename Backend::matrix build_matrix;
624 typedef typename Backend::col_type col_type;
625
626 static const int tag_cnt = 2001;
627 static const int tag_col = 2002;
628 static const int tag_val = 2003;
629
630 AlinaCommunicator comm = A.comm();
631 const CommPattern& C = A.cpat();
632
633 build_matrix& A_loc = *A.local();
634 build_matrix& A_rem = *A.remote();
635
636 ptrdiff_t nrows = A_loc.ncols;
637 ptrdiff_t ncols = A_loc.nbRow();
638
639 UniqueArray<MessagePassing::Request> recv_cnt_req(C.send.req.size());
640 UniqueArray<MessagePassing::Request> recv_col_req(C.send.req.size());
641 UniqueArray<MessagePassing::Request> recv_val_req(C.send.req.size());
642
643 UniqueArray<MessagePassing::Request> send_cnt_req(C.recv.req.size());
644 UniqueArray<MessagePassing::Request> send_col_req(C.recv.req.size());
645 UniqueArray<MessagePassing::Request> send_val_req(C.recv.req.size());
646
647 // Our transposed remote part becomes remote part of someone else,
648 // and the other way around.
649 std::shared_ptr<build_matrix> t_ptr;
650 {
651 std::vector<col_type> tmp_col(A_rem.col.data(), A_rem.col.data() + A_rem.nbNonZero());
652 C.renumber(tmp_col.size(), tmp_col.data());
653
654 col_type* a_rem_col = tmp_col.data();
655 col_type* a_rem_col_backup = A_rem.col.data();
656 A_rem.col.setPointerZeroCopy(a_rem_col);
657
658 //std::swap(a_rem_col, A_rem.col);
659
660 t_ptr = transpose(A_rem);
661
662 A_rem.col.setPointerZeroCopy(a_rem_col_backup);
663 //std::swap(a_rem_col, A_rem.col);
664 }
665 build_matrix& t_rem = *t_ptr;
666
667 // Shift to global numbering:
668 UniqueArray<ptrdiff_t> domain = comm.exclusive_sum(ncols);
669 ptrdiff_t loc_beg = domain[comm.rank];
670 for (size_t i = 0; i < t_rem.nbNonZero(); ++i)
671 t_rem.col[i] += loc_beg;
672
673 // Shift from row pointers to row sizes:
674 std::vector<ptrdiff_t> row_size(t_rem.nbRow());
675 for (size_t i = 0; i < t_rem.nbRow(); ++i)
676 row_size[i] = t_rem.ptr[i + 1] - t_rem.ptr[i];
677
678 // Sizes of transposed remote blocks:
679 // 1. Exchange rem_ptr
680 std::vector<ptrdiff_t> rem_ptr(C.send.count() + 1);
681 rem_ptr[0] = 0;
682
683 for (size_t i = 0; i < C.send.nbr.size(); ++i) {
684 ptrdiff_t beg = C.send.ptr[i];
685 ptrdiff_t end = C.send.ptr[i + 1];
686
687 recv_cnt_req[i] = comm.doIReceive(&rem_ptr[beg + 1], end - beg, C.send.nbr[i], tag_cnt);
688 }
689
690 for (size_t i = 0; i < C.recv.nbr.size(); ++i) {
691 ptrdiff_t beg = C.recv.ptr[i];
692 ptrdiff_t end = C.recv.ptr[i + 1];
693
694 send_cnt_req[i] = comm.doISend(&row_size[beg], end - beg, C.recv.nbr[i], tag_cnt);
695 }
696
697 ARCCORE_ALINA_TIC("MPI Wait");
698 comm.waitAll(recv_cnt_req);
699 ARCCORE_ALINA_TOC("MPI Wait");
700 std::partial_sum(rem_ptr.begin(), rem_ptr.end(), rem_ptr.begin());
701
702 // 2. Start exchange of rem_col, rem_val
703 std::vector<col_type> rem_col(rem_ptr.back());
704 std::vector<value_type> rem_val(rem_ptr.back());
705
706 for (size_t i = 0; i < C.send.nbr.size(); ++i) {
707 ptrdiff_t rbeg = C.send.ptr[i];
708 ptrdiff_t rend = C.send.ptr[i + 1];
709
710 ptrdiff_t cbeg = rem_ptr[rbeg];
711 ptrdiff_t cend = rem_ptr[rend];
712
713 recv_col_req[i] = comm.doIReceive(&rem_col[cbeg], cend - cbeg, C.send.nbr[i], tag_col);
714 recv_val_req[i] = comm.doIReceive(&rem_val[cbeg], cend - cbeg, C.send.nbr[i], tag_val);
715 }
716
717 for (size_t i = 0; i < C.recv.nbr.size(); ++i) {
718 ptrdiff_t rbeg = C.recv.ptr[i];
719 ptrdiff_t rend = C.recv.ptr[i + 1];
720
721 ptrdiff_t cbeg = t_rem.ptr[rbeg];
722 ptrdiff_t cend = t_rem.ptr[rend];
723
724 send_col_req[i] = comm.doISend(&t_rem.col[cbeg], cend - cbeg, C.recv.nbr[i], tag_col);
725 send_val_req[i] = comm.doISend(&t_rem.val[cbeg], cend - cbeg, C.recv.nbr[i], tag_val);
726 }
727
728 // 3. While rem_col and rem_val are in flight,
729 // start constructing our remote part:
730 auto T_ptr = std::make_shared<build_matrix>();
731 build_matrix& T_rem = *T_ptr;
732 T_rem.set_size(nrows, 0, true);
733
734 for (size_t i = 0; i < C.send.count(); ++i)
735 T_rem.ptr[1 + C.send.col[i]] += rem_ptr[i + 1] - rem_ptr[i];
736
737 T_rem.scan_row_sizes();
738 T_rem.set_nonzeros();
739
740 // 4. Finish rem_col and rem_val exchange, and
741 // finish contruction of our remote part.
742 ARCCORE_ALINA_TIC("MPI Wait");
743 comm.waitAll(recv_col_req);
744 comm.waitAll(recv_val_req);
745 ARCCORE_ALINA_TOC("MPI Wait");
746
747 for (size_t i = 0; i < C.send.count(); ++i) {
748 ptrdiff_t row = C.send.col[i];
749 ptrdiff_t head = T_rem.ptr[row];
750
751 for (ptrdiff_t j = rem_ptr[i]; j < rem_ptr[i + 1]; ++j, ++head) {
752 T_rem.col[head] = rem_col[j];
753 T_rem.val[head] = rem_val[j];
754 }
755
756 T_rem.ptr[row] = head;
757 }
758
759 std::rotate(T_rem.ptr.data(), T_rem.ptr.data() + nrows, T_rem.ptr.data() + nrows + 1);
760 T_rem.ptr[0] = 0;
761
762 ARCCORE_ALINA_TIC("MPI Wait");
763 comm.waitAll(send_cnt_req);
764 comm.waitAll(send_col_req);
765 comm.waitAll(send_val_req);
766 ARCCORE_ALINA_TOC("MPI Wait");
767
768 ARCCORE_ALINA_TOC("MPI Transpose");
769
770 return std::make_shared<DistributedMatrix<Backend>>(comm, transpose(A_loc), T_ptr);
771}
772
773/*---------------------------------------------------------------------------*/
774/*---------------------------------------------------------------------------*/
775
776template <class Backend>
777std::shared_ptr<typename Backend::matrix>
778remote_rows(const CommunicationPattern<Backend>& C,
780 bool need_values = true)
781{
782 typedef typename Backend::matrix build_matrix;
783
784 static const int tag_ptr = 3001;
785 static const int tag_col = 3002;
786 static const int tag_val = 3003;
787
788 ARCCORE_ALINA_TIC("remote_rows");
789 AlinaCommunicator comm = C.mpi_comm();
790
791 build_matrix& B_loc = *B.local();
792 build_matrix& B_rem = *B.remote();
793 ptrdiff_t B_beg = B.loc_col_shift();
794
795 size_t nrecv = C.recv.nbr.size();
796 size_t nsend = C.send.nbr.size();
797
798 // Create blocked matrix to send to each domain
799 // that needs data from us:
800 UniqueArray<MessagePassing::Request> send_ptr_req(nsend);
801 UniqueArray<MessagePassing::Request> send_col_req(nsend);
802 UniqueArray<MessagePassing::Request> send_val_req(nsend);
803
804 std::vector<build_matrix> send_rows(nsend);
805
806 for (size_t k = 0; k < nsend; ++k) {
807 ptrdiff_t beg = C.send.ptr[k];
808 ptrdiff_t end = C.send.ptr[k + 1];
809
810 build_matrix& m = send_rows[k];
811 m.set_size(end - beg, 0, false);
812
813 size_t nnz = 0;
814 for (ptrdiff_t i = 0, ii = beg; ii < end; ++i, ++ii) {
815 ptrdiff_t r = C.send.col[ii];
816
817 ptrdiff_t w = (B_loc.ptr[r + 1] - B_loc.ptr[r]) + (B_rem.ptr[r + 1] - B_rem.ptr[r]);
818
819 m.ptr[i] = w;
820 nnz += w;
821 }
822 m.setNbNonZero(nnz);
823
824 send_ptr_req[k] = comm.doISend(m.ptr.data(), m.nbRow(), C.send.nbr[k], tag_ptr);
825
826 m.set_nonzeros(nnz, need_values);
827
828 for (ptrdiff_t i = 0, ii = beg, head = 0; ii < end; ++i, ++ii) {
829 ptrdiff_t r = C.send.col[ii];
830
831 // Contribution of the local part:
832 for (ptrdiff_t j = B_loc.ptr[r]; j < B_loc.ptr[r + 1]; ++j) {
833 m.col[head] = B_loc.col[j] + B_beg;
834
835 if (need_values)
836 m.val[head] = B_loc.val[j];
837
838 ++head;
839 }
840
841 // Contribution of the remote part:
842 for (ptrdiff_t j = B_rem.ptr[r]; j < B_rem.ptr[r + 1]; ++j) {
843 m.col[head] = B_rem.col[j];
844
845 if (need_values)
846 m.val[head] = B_rem.val[j];
847
848 ++head;
849 }
850 }
851
852 send_col_req[k] = comm.doISend(m.col.data(), m.nbNonZero(), C.send.nbr[k], tag_col);
853 if (need_values)
854 send_val_req[k] = comm.doISend(m.val.data(), m.nbNonZero(), C.send.nbr[k], tag_val);
855 }
856
857 // Receive rows of B in block format from our neighbors:
858 UniqueArray<MessagePassing::Request> recv_ptr_req(nrecv);
859 UniqueArray<MessagePassing::Request> recv_col_req(nrecv);
860 UniqueArray<MessagePassing::Request> recv_val_req(nrecv);
861
862 auto B_nbr = std::make_shared<build_matrix>();
863 B_nbr->set_size(C.recv.count(), 0, false);
864 B_nbr->ptr[0] = 0;
865
866 for (size_t k = 0; k < nrecv; ++k) {
867 ptrdiff_t beg = C.recv.ptr[k];
868 ptrdiff_t end = C.recv.ptr[k + 1];
869
870 recv_ptr_req[k] = comm.doIReceive(&B_nbr->ptr[beg + 1], end - beg, C.recv.nbr[k], tag_ptr);
871 }
872
873 ARCCORE_ALINA_TIC("MPI Wait");
874 comm.waitAll(recv_ptr_req);
875 ARCCORE_ALINA_TOC("MPI Wait");
876
877 B_nbr->set_nonzeros(B_nbr->scan_row_sizes(), need_values);
878
879 for (size_t k = 0; k < nrecv; ++k) {
880 ptrdiff_t rbeg = C.recv.ptr[k];
881 ptrdiff_t rend = C.recv.ptr[k + 1];
882
883 ptrdiff_t cbeg = B_nbr->ptr[rbeg];
884 ptrdiff_t cend = B_nbr->ptr[rend];
885
886 recv_col_req[k] = comm.doIReceive(&B_nbr->col[cbeg], cend - cbeg, C.recv.nbr[k], tag_col);
887
888 if (need_values)
889 recv_val_req[k] = comm.doIReceive(&B_nbr->val[cbeg], cend - cbeg, C.recv.nbr[k], tag_val);
890 }
891
892 ARCCORE_ALINA_TIC("MPI Wait");
893 comm.waitAll(send_ptr_req);
894 comm.waitAll(send_col_req);
895 comm.waitAll(recv_col_req);
896
897 if (need_values) {
898 comm.waitAll(send_val_req);
899 comm.waitAll(recv_val_req);
900 }
901 ARCCORE_ALINA_TOC("MPI Wait");
902
903 ARCCORE_ALINA_TOC("remote_rows");
904 return B_nbr;
905}
906
907/*---------------------------------------------------------------------------*/
908/*---------------------------------------------------------------------------*/
909
910template <class Backend>
911std::shared_ptr<DistributedMatrix<Backend>>
913{
914 typedef typename Backend::value_type value_type;
915 using build_matrix = Backend::matrix;
916 typedef typename Backend::col_type col_type;
917 ARCCORE_ALINA_TIC("product");
918
919 const CommunicationPattern<Backend>& Acp = A.cpat();
920
921 build_matrix& A_loc = *A.local();
922 build_matrix& A_rem = *A.remote();
923 build_matrix& B_loc = *B.local();
924 build_matrix& B_rem = *B.remote();
925
926 ptrdiff_t A_rows = A.loc_rows();
927 ptrdiff_t B_cols = B.loc_cols();
928
929 ptrdiff_t B_beg = B.loc_col_shift();
930 ptrdiff_t B_end = B_beg + B_cols;
931
932 auto b_nbr = remote_rows(Acp, B);
933 build_matrix& B_nbr = *b_nbr;
934
935 // Build mapping from global to local column numbers in the remote part of
936 // the product matrix.
937 std::vector<col_type> rem_cols(B_rem.nbNonZero() + B_nbr.nbNonZero());
938
939 std::copy(B_nbr.col.data(), B_nbr.col.data() + B_nbr.nbNonZero(),
940 std::copy(B_rem.col.data(), B_rem.col.data() + B_rem.nbNonZero(), rem_cols.begin()));
941
942 std::sort(rem_cols.begin(), rem_cols.end());
943 rem_cols.erase(std::unique(rem_cols.begin(), rem_cols.end()), rem_cols.end());
944
945 ptrdiff_t n_rem_cols = 0;
946 std::unordered_map<ptrdiff_t, int> rem_idx(2 * rem_cols.size());
947 for (ptrdiff_t c : rem_cols) {
948 if (c >= B_beg && c < B_end)
949 continue;
950 rem_idx[c] = n_rem_cols++;
951 }
952
953 // Build the product.
954 auto c_loc = std::make_shared<build_matrix>();
955 auto c_rem = std::make_shared<build_matrix>();
956
957 build_matrix& C_loc = *c_loc;
958 build_matrix& C_rem = *c_rem;
959
960 C_loc.set_size(A_rows, B_cols, false);
961 C_rem.set_size(A_rows, 0, false);
962
963 C_loc.ptr[0] = 0;
964 C_rem.ptr[0] = 0;
965
966 ARCCORE_ALINA_TIC("analyze");
967 arccoreParallelFor(0, A_rows, ForLoopRunInfo{}, [&](Int32 begin, Int32 size) {
968 std::vector<ptrdiff_t> loc_marker(B_end - B_beg, -1);
969 std::vector<ptrdiff_t> rem_marker(n_rem_cols, -1);
970
971 for (ptrdiff_t ia = begin; ia < (begin + size); ++ia) {
972 ptrdiff_t loc_cols = 0;
973 ptrdiff_t rem_cols = 0;
974
975 for (ptrdiff_t ja = A_loc.ptr[ia], ea = A_loc.ptr[ia + 1]; ja < ea; ++ja) {
976 ptrdiff_t ca = A_loc.col[ja];
977
978 for (ptrdiff_t jb = B_loc.ptr[ca], eb = B_loc.ptr[ca + 1]; jb < eb; ++jb) {
979 ptrdiff_t cb = B_loc.col[jb];
980
981 if (loc_marker[cb] != ia) {
982 loc_marker[cb] = ia;
983 ++loc_cols;
984 }
985 }
986
987 for (ptrdiff_t jb = B_rem.ptr[ca], eb = B_rem.ptr[ca + 1]; jb < eb; ++jb) {
988 ptrdiff_t cb = rem_idx[B_rem.col[jb]];
989
990 if (rem_marker[cb] != ia) {
991 rem_marker[cb] = ia;
992 ++rem_cols;
993 }
994 }
995 }
996
997 for (ptrdiff_t ja = A_rem.ptr[ia], ea = A_rem.ptr[ia + 1]; ja < ea; ++ja) {
998 ptrdiff_t ca = Acp.local_index(A_rem.col[ja]);
999
1000 for (ptrdiff_t jb = B_nbr.ptr[ca], eb = B_nbr.ptr[ca + 1]; jb < eb; ++jb) {
1001 ptrdiff_t cb = B_nbr.col[jb];
1002
1003 if (cb >= B_beg && cb < B_end) {
1004 cb -= B_beg;
1005
1006 if (loc_marker[cb] != ia) {
1007 loc_marker[cb] = ia;
1008 ++loc_cols;
1009 }
1010 }
1011 else {
1012 cb = rem_idx[cb];
1013
1014 if (rem_marker[cb] != ia) {
1015 rem_marker[cb] = ia;
1016 ++rem_cols;
1017 }
1018 }
1019 }
1020 }
1021
1022 C_loc.ptr[ia + 1] = loc_cols;
1023 C_rem.ptr[ia + 1] = rem_cols;
1024 }
1025 });
1026 ARCCORE_ALINA_TOC("analyze");
1027
1028 C_loc.set_nonzeros(C_loc.scan_row_sizes());
1029 C_rem.set_nonzeros(C_rem.scan_row_sizes());
1030
1031 ARCCORE_ALINA_TIC("compute");
1032 arccoreParallelFor(0, A_rows, ForLoopRunInfo{}, [&](Int32 begin, Int32 size) {
1033 std::vector<ptrdiff_t> loc_marker(B_end - B_beg, -1);
1034 std::vector<ptrdiff_t> rem_marker(n_rem_cols, -1);
1035
1036 for (ptrdiff_t ia = begin; ia < (begin + size); ++ia) {
1037 ptrdiff_t loc_beg = C_loc.ptr[ia];
1038 ptrdiff_t rem_beg = C_rem.ptr[ia];
1039 ptrdiff_t loc_end = loc_beg;
1040 ptrdiff_t rem_end = rem_beg;
1041
1042 for (ptrdiff_t ja = A_loc.ptr[ia], ea = A_loc.ptr[ia + 1]; ja < ea; ++ja) {
1043 ptrdiff_t ca = A_loc.col[ja];
1044 value_type va = A_loc.val[ja];
1045
1046 for (ptrdiff_t jb = B_loc.ptr[ca], eb = B_loc.ptr[ca + 1]; jb < eb; ++jb) {
1047 ptrdiff_t cb = B_loc.col[jb];
1048 value_type vb = B_loc.val[jb];
1049
1050 if (loc_marker[cb] < loc_beg) {
1051 loc_marker[cb] = loc_end;
1052
1053 C_loc.col[loc_end] = cb;
1054 C_loc.val[loc_end] = va * vb;
1055
1056 ++loc_end;
1057 }
1058 else {
1059 C_loc.val[loc_marker[cb]] += va * vb;
1060 }
1061 }
1062
1063 for (ptrdiff_t jb = B_rem.ptr[ca], eb = B_rem.ptr[ca + 1]; jb < eb; ++jb) {
1064 ptrdiff_t gb = B_rem.col[jb];
1065 ptrdiff_t cb = rem_idx[gb];
1066 value_type vb = B_rem.val[jb];
1067
1068 if (rem_marker[cb] < rem_beg) {
1069 rem_marker[cb] = rem_end;
1070
1071 C_rem.col[rem_end] = gb;
1072 C_rem.val[rem_end] = va * vb;
1073
1074 ++rem_end;
1075 }
1076 else {
1077 C_rem.val[rem_marker[cb]] += va * vb;
1078 }
1079 }
1080 }
1081
1082 for (ptrdiff_t ja = A_rem.ptr[ia], ea = A_rem.ptr[ia + 1]; ja < ea; ++ja) {
1083 ptrdiff_t ca = Acp.local_index(A_rem.col[ja]);
1084 value_type va = A_rem.val[ja];
1085
1086 for (ptrdiff_t jb = B_nbr.ptr[ca], eb = B_nbr.ptr[ca + 1]; jb < eb; ++jb) {
1087 ptrdiff_t gb = B_nbr.col[jb];
1088 value_type vb = B_nbr.val[jb];
1089
1090 if (gb >= B_beg && gb < B_end) {
1091 ptrdiff_t cb = gb - B_beg;
1092
1093 if (loc_marker[cb] < loc_beg) {
1094 loc_marker[cb] = loc_end;
1095
1096 C_loc.col[loc_end] = cb;
1097 C_loc.val[loc_end] = va * vb;
1098
1099 ++loc_end;
1100 }
1101 else {
1102 C_loc.val[loc_marker[cb]] += va * vb;
1103 }
1104 }
1105 else {
1106 ptrdiff_t cb = rem_idx[gb];
1107
1108 if (rem_marker[cb] < rem_beg) {
1109 rem_marker[cb] = rem_end;
1110
1111 C_rem.col[rem_end] = gb;
1112 C_rem.val[rem_end] = va * vb;
1113
1114 ++rem_end;
1115 }
1116 else {
1117 C_rem.val[rem_marker[cb]] += va * vb;
1118 }
1119 }
1120 }
1121 }
1122 }
1123 });
1124 ARCCORE_ALINA_TOC("compute");
1125 ARCCORE_ALINA_TOC("product");
1126
1127 return std::make_shared<DistributedMatrix<Backend>>(A.comm(), c_loc, c_rem);
1128}
1129
1130/*---------------------------------------------------------------------------*/
1131/*---------------------------------------------------------------------------*/
1132
1133template <class Backend, class T>
1134void scale(DistributedMatrix<Backend>& A, T s)
1135{
1136 using build_matrix = Backend::matrix;
1137
1138 build_matrix& A_loc = *A.local();
1139 build_matrix& A_rem = *A.remote();
1140
1141 ptrdiff_t n = A_loc.nbRow();
1142
1143 arccoreParallelFor(0, n, ForLoopRunInfo{}, [&](Int32 begin, Int32 size) {
1144 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
1145 for (ptrdiff_t j = A_loc.ptr[i], e = A_loc.ptr[i + 1]; j < e; ++j)
1146 A_loc.val[j] *= s;
1147 for (ptrdiff_t j = A_rem.ptr[i], e = A_rem.ptr[i + 1]; j < e; ++j)
1148 A_rem.val[j] *= s;
1149 }
1150 });
1151}
1152
1153/*---------------------------------------------------------------------------*/
1154/*---------------------------------------------------------------------------*/
1155
1156template <class Backend>
1157void sort_rows(DistributedMatrix<Backend>& A)
1158{
1159 sort_rows(*A.local());
1160 sort_rows(*A.remote());
1161}
1162
1163/*---------------------------------------------------------------------------*/
1164/*---------------------------------------------------------------------------*/
1165
1166} // namespace Arcane::Alina
1167
1168/*---------------------------------------------------------------------------*/
1169/*---------------------------------------------------------------------------*/
1170
1171namespace Arcane::Alina::backend
1172{
1173
1174/*---------------------------------------------------------------------------*/
1175/*---------------------------------------------------------------------------*/
1176
1177template <class Backend>
1179{
1180 static size_t get(const DistributedMatrix<Backend>& A)
1181 {
1182 return A.loc_rows();
1183 }
1184};
1185
1186template <class Backend, class Alpha, class Vec1, class Beta, class Vec2>
1187struct spmv_impl<Alpha, DistributedMatrix<Backend>, Vec1, Beta, Vec2>
1188{
1189 static void apply(Alpha alpha,
1191 const Vec1& x, Beta beta, Vec2& y)
1192 {
1193 A.mul(alpha, x, beta, y);
1194 }
1195};
1196
1197template <class Backend, class Vec1, class Vec2, class Vec3>
1198struct residual_impl<DistributedMatrix<Backend>, Vec1, Vec2, Vec3>
1199{
1200 static void apply(const Vec1& rhs,
1202 const Vec2& x, Vec3& r)
1203 {
1204 A.residual(rhs, x, r);
1205 }
1206};
1207
1208/*---------------------------------------------------------------------------*/
1209/*---------------------------------------------------------------------------*/
1210}
1211
1212/*---------------------------------------------------------------------------*/
1213/*---------------------------------------------------------------------------*/
1214
1215namespace Arcane::Alina
1216{
1217
1218/*---------------------------------------------------------------------------*/
1219/*---------------------------------------------------------------------------*/
1220
1221// Diagonal of the matrix
1222template <class Backend>
1223std::shared_ptr<numa_vector<typename Backend::value_type>>
1224diagonal(const DistributedMatrix<Backend>& A, bool invert = false)
1225{
1226 return diagonal(*A.local(), invert);
1227}
1228
1229/*---------------------------------------------------------------------------*/
1230/*---------------------------------------------------------------------------*/
1231
1232// Estimate spectral radius of the matrix.
1233template <bool scale, class Backend>
1234typename math::scalar_of<typename Backend::value_type>::type
1235spectral_radius(const DistributedMatrix<Backend>& A, int power_iters = 0)
1236{
1237 ARCCORE_ALINA_TIC("spectral radius");
1238 typedef typename Backend::value_type value_type;
1239 typedef typename math::rhs_of<value_type>::type rhs_type;
1240 typedef typename math::scalar_of<value_type>::type scalar_type;
1241 typedef CSRMatrix<value_type> build_matrix;
1242
1243 AlinaCommunicator comm = A.comm();
1244
1245 const build_matrix& A_loc = *A.local();
1246 const build_matrix& A_rem = *A.remote();
1247 const CommunicationPattern<Backend>& C = A.cpat();
1248
1249 const ptrdiff_t n = A_loc.nbRow();
1250 scalar_type radius = 0;
1251
1252 // TODO: Use the code in CSRMatrixOperation.h
1253 if (power_iters <= 0) {
1254 arccoreParallelFor(0, n, ForLoopRunInfo{}, [&](Int32 begin, Int32 size) {
1255 scalar_type emax = 0;
1256 value_type dia = math::identity<value_type>();
1257
1258 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
1259 scalar_type s = 0;
1260
1261 for (ptrdiff_t j = A_loc.ptr[i], e = A_loc.ptr[i + 1]; j < e; ++j) {
1262 ptrdiff_t c = A_loc.col[j];
1263 value_type v = A_loc.val[j];
1264
1265 s += math::norm(v);
1266
1267 if (scale && c == i)
1268 dia = v;
1269 }
1270
1271 for (ptrdiff_t j = A_rem.ptr[i], e = A_rem.ptr[i + 1]; j < e; ++j)
1272 s += math::norm(A_rem.val[j]);
1273
1274 if (scale)
1275 s *= math::norm(math::inverse(dia));
1276
1277 emax = std::max(emax, s);
1278 }
1279
1281 });
1282 }
1283 else {
1284 numa_vector<rhs_type> b0(n, false), b1(n, false);
1285 numa_vector<ptrdiff_t> rem_col(A_rem.nbNonZero(), false);
1286
1287 // Fill the initial vector with random values.
1288 // Also extract the inverted matrix diagonal values.
1289 std::atomic<scalar_type> atomic_b0_loc_norm = {};
1290
1291 arccoreParallelFor(0, n, ForLoopRunInfo{}, [&](Int32 begin, Int32 size) {
1292 const int tid = TaskFactory::currentTaskThreadIndex();
1293 const int nt = ConcurrencyBase::maxAllowedThread();
1294
1295 std::mt19937 rng(comm.size * nt + tid);
1296 std::uniform_real_distribution<scalar_type> rnd(-1, 1);
1297
1298 scalar_type t_norm = 0;
1299
1300 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
1301 rhs_type v = math::constant<rhs_type>(rnd(rng));
1302
1303 b0[i] = v;
1304 t_norm += math::norm(math::inner_product(v, v));
1305
1306 for (ptrdiff_t j = A_rem.ptr[i], e = A_rem.ptr[i + 1]; j < e; ++j) {
1307 rem_col[j] = C.local_index(A_rem.col[j]);
1308 }
1309 }
1310
1311 // GG: Not reproducible
1312 atomic_b0_loc_norm += t_norm;
1313 });
1314 scalar_type b0_loc_norm = atomic_b0_loc_norm;
1315 scalar_type b0_norm = comm.reduceSum(b0_loc_norm);
1316
1317 // Normalize b0
1318 b0_norm = 1 / sqrt(b0_norm);
1319 arccoreParallelFor(0, n, ForLoopRunInfo{}, [&](Int32 begin, Int32 size) {
1320 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
1321 b0[i] = b0_norm * b0[i];
1322 }
1323 });
1324
1325 std::vector<rhs_type> b0_send(C.send.count());
1326 std::vector<rhs_type> b0_recv(C.recv.count());
1327
1328 for (size_t i = 0, m = C.send.count(); i < m; ++i)
1329 b0_send[i] = b0[C.send.col[i]];
1330 C.exchange(b0_send.data(), b0_recv.data());
1331
1332 for (int iter = 0; iter < power_iters;) {
1333 // b1 = (D * A) * b0
1334 // b1_norm = ||b1||
1335 // radius = <b1,b0>
1336
1337 std::atomic<scalar_type> atomic_b1_loc_norm = 0;
1338 std::atomic<scalar_type> atomic_loc_radius = 0;
1339
1340 arccoreParallelFor(0, n, ForLoopRunInfo{}, [&](Int32 begin, Int32 size) {
1341 scalar_type t_norm = 0;
1342 scalar_type t_radi = 0;
1343 value_type dia = math::identity<value_type>();
1344
1345 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
1346 rhs_type s = math::zero<rhs_type>();
1347
1348 for (ptrdiff_t j = A_loc.ptr[i], e = A_loc.ptr[i + 1]; j < e; ++j) {
1349 ptrdiff_t c = A_loc.col[j];
1350 value_type v = A_loc.val[j];
1351 if (scale && c == i)
1352 dia = v;
1353 s += v * b0[c];
1354 }
1355
1356 for (ptrdiff_t j = A_rem.ptr[i], e = A_rem.ptr[i + 1]; j < e; ++j)
1357 s += A_rem.val[j] * b0_recv[rem_col[j]];
1358
1359 if (scale)
1360 s = math::inverse(dia) * s;
1361
1362 t_norm += math::norm(math::inner_product(s, s));
1363 t_radi += math::norm(math::inner_product(s, b0[i]));
1364
1365 b1[i] = s;
1366 }
1367
1368 {
1369 // GG: Not reproducible
1370 atomic_b1_loc_norm += t_norm;
1371 atomic_loc_radius += t_radi;
1372 }
1373 });
1374 scalar_type b1_loc_norm = atomic_b1_loc_norm;
1375 scalar_type loc_radius = atomic_loc_radius;
1376
1377 radius = comm.reduceSum(loc_radius);
1378
1379 if (++iter < power_iters) {
1380 scalar_type b1_norm;
1381 b1_norm = comm.reduceSum(b1_loc_norm);
1382
1383 // b0 = b1 / b1_norm
1384 b1_norm = 1 / sqrt(b1_norm);
1385 arccoreParallelFor(0, n, ForLoopRunInfo{}, [&](Int32 begin, Int32 size) {
1386 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
1387 b0[i] = b1_norm * b1[i];
1388 }
1389 });
1390
1391 for (size_t i = 0, m = C.send.count(); i < m; ++i)
1392 b0_send[i] = b0[C.send.col[i]];
1393 C.exchange(b0_send.data(), b0_recv.data());
1394 }
1395 }
1396 }
1397 ARCCORE_ALINA_TOC("spectral radius");
1398
1399 return radius < 0 ? static_cast<scalar_type>(2) : radius;
1400}
1401
1402/*---------------------------------------------------------------------------*/
1403/*---------------------------------------------------------------------------*/
1404
1405} // namespace Arcane::Alina
1406
1407/*---------------------------------------------------------------------------*/
1408/*---------------------------------------------------------------------------*/
1409
1410#endif
Call to handle communication pattern.
Distributed Matrix using message passing.
NUMA-aware vector container.
Definition NumaVector.h:42
T & back()
Last element of the array.
static Int32 maxAllowedThread()
Maximum number of allowed threads for multi-threading.
Loop execution information.
Matrix class, to be used by user.
static Int32 currentTaskThreadIndex()
Index (between 0 and nbAllowedThread()-1) of the thread executing the current task.
1D data vector with value semantics (STL style).
Vector class, to be used by user.
__host__ __device__ DataType doAtomic(DataType *ptr, ValueType value)
Applies the atomic operation Operation to the value at address ptr with the value value.
C void mpAllToAll(IMessagePassingMng *pm, Span< const char > send_buf, Span< char > recv_buf, Int32 count)
apfloat sqrt(apfloat v)
Square root of v.
Definition MathApfloat.h:69
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.
Convenience wrapper around MPI_Comm.
UniqueArray< T > exclusive_sum(T n) const
Exclusive sum over mpi communicator.
Implementation for residual error compuatation.
Implementation for function returning the number of rows in a matrix.
Implementation for matrix-vector product.