DOLFINx 0.10.0.0
DOLFINx C++ interface
Loading...
Searching...
No Matches
MatrixCSR.h
1// Copyright (C) 2021-2022 Garth N. Wells and Chris N. Richardson
2//
3// This file is part of DOLFINx (https://www.fenicsproject.org)
4//
5// SPDX-License-Identifier: LGPL-3.0-or-later
6
7#pragma once
8
9#include "SparsityPattern.h"
10#include "Vector.h"
11#include "matrix_csr_impl.h"
12#include <algorithm>
13#include <dolfinx/common/IndexMap.h>
14#include <dolfinx/common/MPI.h>
15#include <dolfinx/graph/AdjacencyList.h>
16#include <mpi.h>
17#include <numeric>
18#include <span>
19#include <utility>
20#include <vector>
21
22namespace dolfinx::la
23{
25enum class BlockMode : int
26{
27 compact = 0,
32};
33
48template <class Scalar, class Container = std::vector<Scalar>,
49 class ColContainer = std::vector<std::int32_t>,
50 class RowPtrContainer = std::vector<std::int64_t>>
52{
53 static_assert(std::is_same_v<typename Container::value_type, Scalar>);
54
55public:
57 using value_type = Scalar;
58
60 using container_type = Container;
61
63 using column_container_type = ColContainer;
64
66 using rowptr_container_type = RowPtrContainer;
67
68 static_assert(std::is_same_v<value_type, typename container_type::value_type>,
69 "Scalar type and container value type must be the same.");
70
94 template <int BS0 = 1, int BS1 = 1>
96 {
97 if ((BS0 != _bs[0] and BS0 > 1 and _bs[0] > 1)
98 or (BS1 != _bs[1] and BS1 > 1 and _bs[1] > 1))
99 {
100 throw std::runtime_error(
101 "Cannot insert blocks of different size than matrix block size");
102 }
103
104 return [&](std::span<const std::int32_t> rows,
105 std::span<const std::int32_t> cols,
106 std::span<const value_type> data) -> int
107 {
108 this->set<BS0, BS1>(data, rows, cols);
109 return 0;
110 };
111 }
112
136 template <int BS0 = 1, int BS1 = 1>
138 {
139 if ((BS0 != _bs[0] and BS0 > 1 and _bs[0] > 1)
140 or (BS1 != _bs[1] and BS1 > 1 and _bs[1] > 1))
141 {
142 throw std::runtime_error(
143 "Cannot insert blocks of different size than matrix block size");
144 }
145
146 return [&](std::span<const std::int32_t> rows,
147 std::span<const std::int32_t> cols,
148 std::span<const value_type> data) -> int
149 {
150 this->add<BS0, BS1>(data, rows, cols);
151 return 0;
152 };
153 }
154
178 MatrixCSR(const SparsityPattern& p, BlockMode mode = BlockMode::compact);
179
182 MatrixCSR(MatrixCSR&& A) = default;
183
187 void set(value_type x) { std::ranges::fill(_data, x); }
188
205 template <int BS0, int BS1>
206 void set(std::span<const value_type> x, std::span<const std::int32_t> rows,
207 std::span<const std::int32_t> cols)
208 {
209 auto set_fn = [](value_type& y, const value_type& x) { y = x; };
210
211 std::int32_t num_rows
212 = _index_maps[0]->size_local() + _index_maps[0]->num_ghosts();
213 assert(x.size() == rows.size() * cols.size() * BS0 * BS1);
214 if (_bs[0] == BS0 and _bs[1] == BS1)
215 {
216 impl::insert_csr<BS0, BS1>(_data, _cols, _row_ptr, x, rows, cols, set_fn,
217 num_rows);
218 }
219 else if (_bs[0] == 1 and _bs[1] == 1)
220 {
221 // Set blocked data in a regular CSR matrix (_bs[0]=1, _bs[1]=1)
222 // with correct sparsity
223 impl::insert_blocked_csr<BS0, BS1>(_data, _cols, _row_ptr, x, rows, cols,
224 set_fn, num_rows);
225 }
226 else
227 {
228 assert(BS0 == 1 and BS1 == 1);
229 // Set non-blocked data in a blocked CSR matrix (BS0=1, BS1=1)
230 impl::insert_nonblocked_csr(_data, _cols, _row_ptr, x, rows, cols, set_fn,
231 num_rows, _bs[0], _bs[1]);
232 }
233 }
234
250 template <int BS0 = 1, int BS1 = 1>
251 void add(std::span<const value_type> x, std::span<const std::int32_t> rows,
252 std::span<const std::int32_t> cols)
253 {
254 auto add_fn = [](value_type& y, const value_type& x) { y += x; };
255
256 assert(x.size() == rows.size() * cols.size() * BS0 * BS1);
257 if (_bs[0] == BS0 and _bs[1] == BS1)
258 {
259 impl::insert_csr<BS0, BS1>(_data, _cols, _row_ptr, x, rows, cols, add_fn,
260 _row_ptr.size());
261 }
262 else if (_bs[0] == 1 and _bs[1] == 1)
263 {
264 // Add blocked data to a regular CSR matrix (_bs[0]=1, _bs[1]=1)
265 impl::insert_blocked_csr<BS0, BS1>(_data, _cols, _row_ptr, x, rows, cols,
266 add_fn, _row_ptr.size());
267 }
268 else
269 {
270 assert(BS0 == 1 and BS1 == 1);
271 // Add non-blocked data to a blocked CSR matrix (BS0=1, BS1=1)
272 impl::insert_nonblocked_csr(_data, _cols, _row_ptr, x, rows, cols, add_fn,
273 _row_ptr.size(), _bs[0], _bs[1]);
274 }
275 }
276
278 std::int32_t num_owned_rows() const { return _index_maps[0]->size_local(); }
279
281 std::int32_t num_all_rows() const { return _row_ptr.size() - 1; }
282
292 std::vector<value_type> to_dense() const
293 {
294 const std::size_t nrows = num_all_rows();
295 const std::size_t ncols = _index_maps[1]->size_global();
296 std::vector<value_type> A(nrows * ncols * _bs[0] * _bs[1], 0.0);
297 for (std::size_t r = 0; r < nrows; ++r)
298 {
299 for (std::int32_t j = _row_ptr[r]; j < _row_ptr[r + 1]; ++j)
300 {
301 for (int i0 = 0; i0 < _bs[0]; ++i0)
302 {
303 for (int i1 = 0; i1 < _bs[1]; ++i1)
304 {
305 std::array<std::int32_t, 1> local_col{_cols[j]};
306 std::array<std::int64_t, 1> global_col{0};
307 _index_maps[1]->local_to_global(local_col, global_col);
308 A[(r * _bs[1] + i0) * ncols * _bs[0] + global_col[0] * _bs[1] + i1]
309 = _data[j * _bs[0] * _bs[1] + i0 * _bs[1] + i1];
310 }
311 }
312 }
313 }
314
315 return A;
316 }
317
325 {
328 }
329
338 {
339 const std::int32_t local_size0 = _index_maps[0]->size_local();
340 const std::int32_t num_ghosts0 = _index_maps[0]->num_ghosts();
341 const int bs2 = _bs[0] * _bs[1];
342
343 // For each ghost row, pack and send values to send to neighborhood
344 std::vector<int> insert_pos = _val_send_disp;
345 _ghost_value_data.resize(_val_send_disp.back());
346 for (int i = 0; i < num_ghosts0; ++i)
347 {
348 const int rank = _ghost_row_to_rank[i];
349
350 // Get position in send buffer to place data to send to this
351 // neighbour
352 const std::int32_t val_pos = insert_pos[rank];
353 std::copy(std::next(_data.data(), _row_ptr[local_size0 + i] * bs2),
354 std::next(_data.data(), _row_ptr[local_size0 + i + 1] * bs2),
355 std::next(_ghost_value_data.begin(), val_pos));
356 insert_pos[rank]
357 += bs2 * (_row_ptr[local_size0 + i + 1] - _row_ptr[local_size0 + i]);
358 }
359
360 _ghost_value_data_in.resize(_val_recv_disp.back());
361
362 // Compute data sizes for send and receive from displacements
363 std::vector<int> val_send_count(_val_send_disp.size() - 1);
364 std::adjacent_difference(std::next(_val_send_disp.begin()),
365 _val_send_disp.end(), val_send_count.begin());
366
367 std::vector<int> val_recv_count(_val_recv_disp.size() - 1);
368 std::adjacent_difference(std::next(_val_recv_disp.begin()),
369 _val_recv_disp.end(), val_recv_count.begin());
370
371 int status = MPI_Ineighbor_alltoallv(
372 _ghost_value_data.data(), val_send_count.data(), _val_send_disp.data(),
373 dolfinx::MPI::mpi_t<value_type>, _ghost_value_data_in.data(),
374 val_recv_count.data(), _val_recv_disp.data(),
375 dolfinx::MPI::mpi_t<value_type>, _comm.comm(), &_request);
376 dolfinx::MPI::check_error(_comm.comm(), status);
377 }
378
385 {
386 int status = MPI_Wait(&_request, MPI_STATUS_IGNORE);
387 dolfinx::MPI::check_error(_comm.comm(), status);
388
389 _ghost_value_data.clear();
390 _ghost_value_data.shrink_to_fit();
391
392 // Add to local rows
393 const int bs2 = _bs[0] * _bs[1];
394 assert(_ghost_value_data_in.size() == _unpack_pos.size() * bs2);
395 for (std::size_t i = 0; i < _unpack_pos.size(); ++i)
396 for (int j = 0; j < bs2; ++j)
397 _data[_unpack_pos[i] * bs2 + j] += _ghost_value_data_in[i * bs2 + j];
398
399 _ghost_value_data_in.clear();
400 _ghost_value_data_in.shrink_to_fit();
401
402 // Set ghost row data to zero
403 const std::int32_t local_size0 = _index_maps[0]->size_local();
404 std::fill(std::next(_data.begin(), _row_ptr[local_size0] * bs2),
405 _data.end(), 0);
406 }
407
410 double squared_norm() const
411 {
412 const std::size_t num_owned_rows = _index_maps[0]->size_local();
413 const int bs2 = _bs[0] * _bs[1];
414 assert(num_owned_rows < _row_ptr.size());
415 double norm_sq_local = std::accumulate(
416 _data.cbegin(),
417 std::next(_data.cbegin(), _row_ptr[num_owned_rows] * bs2), double(0),
418 [](auto norm, value_type y) { return norm + std::norm(y); });
419 double norm_sq;
420 MPI_Allreduce(&norm_sq_local, &norm_sq, 1, MPI_DOUBLE, MPI_SUM,
421 _comm.comm());
422 return norm_sq;
423 }
424
436
444 std::shared_ptr<const common::IndexMap> index_map(int dim) const
445 {
446 return _index_maps.at(dim);
447 }
448
451 container_type& values() { return _data; }
452
455 const container_type& values() const { return _data; }
456
459 const rowptr_container_type& row_ptr() const { return _row_ptr; }
460
463 const column_container_type& cols() const { return _cols; }
464
473 {
474 return _off_diagonal_offset;
475 }
476
479 std::array<int, 2> block_size() const { return _bs; }
480
481private:
482 // Maps for the distribution of the ows and columns
483 std::array<std::shared_ptr<const common::IndexMap>, 2> _index_maps;
484
485 // Block mode (compact or expanded)
486 BlockMode _block_mode;
487
488 // Block sizes
489 std::array<int, 2> _bs;
490
491 // Matrix data
492 container_type _data;
494 rowptr_container_type _row_ptr;
495
496 // Start of off-diagonal (unowned columns) on each row
497 rowptr_container_type _off_diagonal_offset;
498
499 // Neighborhood communicator (ghost->owner communicator for rows)
500 dolfinx::MPI::Comm _comm;
501
502 // -- Precomputed data for scatter_rev/update
503
504 // Request in non-blocking communication
505 MPI_Request _request;
506
507 // Position in _data to add received data
508 std::vector<int> _unpack_pos;
509
510 // Displacements for alltoall for each neighbor when sending and
511 // receiving
512 std::vector<int> _val_send_disp, _val_recv_disp;
513
514 // Ownership of each row, by neighbor (for the neighbourhood defined
515 // on _comm)
516 std::vector<int> _ghost_row_to_rank;
517
518 // Temporary stores for data during non-blocking communication
519 container_type _ghost_value_data;
520 container_type _ghost_value_data_in;
521};
522//-----------------------------------------------------------------------------
523template <class U, class V, class W, class X>
525 : _index_maps({p.index_map(0),
526 std::make_shared<common::IndexMap>(p.column_index_map())}),
527 _block_mode(mode), _bs({p.block_size(0), p.block_size(1)}),
528 _data(p.num_nonzeros() * _bs[0] * _bs[1], 0),
529 _cols(p.graph().first.begin(), p.graph().first.end()),
530 _row_ptr(p.graph().second.begin(), p.graph().second.end()),
531 _comm(MPI_COMM_NULL)
532{
533 if (_block_mode == BlockMode::expanded)
534 {
535 // Rebuild IndexMaps
536 for (int i = 0; i < 2; ++i)
537 {
538 const auto im = _index_maps[i];
539 const std::int32_t size_local = im->size_local() * _bs[i];
540 std::span ghost_i = im->ghosts();
541 std::vector<std::int64_t> ghosts;
542 const std::vector<int> ghost_owner_i(im->owners().begin(),
543 im->owners().end());
544 std::vector<int> src_rank;
545 for (std::size_t j = 0; j < ghost_i.size(); ++j)
546 {
547 for (int k = 0; k < _bs[i]; ++k)
548 {
549 ghosts.push_back(ghost_i[j] * _bs[i] + k);
550 src_rank.push_back(ghost_owner_i[j]);
551 }
552 }
553
554 std::array<std::vector<int>, 2> src_dest0
555 = {std::vector(_index_maps[i]->src().begin(),
556 _index_maps[i]->src().end()),
557 std::vector(_index_maps[i]->dest().begin(),
558 _index_maps[i]->dest().end())};
559 _index_maps[i] = std::make_shared<common::IndexMap>(
560 _index_maps[i]->comm(), size_local, src_dest0, ghosts, src_rank);
561 }
562
563 // Convert sparsity pattern and set _bs to 1
564
565 column_container_type new_cols;
566 new_cols.reserve(_data.size());
567 rowptr_container_type new_row_ptr{0};
568 new_row_ptr.reserve(_row_ptr.size() * _bs[0]);
569 std::span<const std::int32_t> num_diag_nnz = p.off_diagonal_offsets();
570 for (std::size_t i = 0; i < _row_ptr.size() - 1; ++i)
571 {
572 // Repeat row _bs[0] times
573 for (int q0 = 0; q0 < _bs[0]; ++q0)
574 {
575 _off_diagonal_offset.push_back(new_row_ptr.back()
576 + num_diag_nnz[i] * _bs[1]);
577 for (auto j = _row_ptr[i]; j < _row_ptr[i + 1]; ++j)
578 {
579 for (int q1 = 0; q1 < _bs[1]; ++q1)
580 new_cols.push_back(_cols[j] * _bs[1] + q1);
581 }
582 new_row_ptr.push_back(new_cols.size());
583 }
584 }
585 _cols = new_cols;
586 _row_ptr = new_row_ptr;
587 _bs[0] = 1;
588 _bs[1] = 1;
589 }
590 else
591 {
592 // Compute off-diagonal offset for each row (compact)
593 std::span<const std::int32_t> num_diag_nnz = p.off_diagonal_offsets();
594 _off_diagonal_offset.reserve(num_diag_nnz.size());
595 std::ranges::transform(num_diag_nnz, _row_ptr,
596 std::back_inserter(_off_diagonal_offset),
597 std::plus{});
598 }
599
600 // Some short-hand
601 const std::array local_size
602 = {_index_maps[0]->size_local(), _index_maps[1]->size_local()};
603 const std::array local_range
604 = {_index_maps[0]->local_range(), _index_maps[1]->local_range()};
605 std::span ghosts1 = _index_maps[1]->ghosts();
606
607 std::span ghosts0 = _index_maps[0]->ghosts();
608 std::span src_ranks = _index_maps[0]->src();
609 std::span dest_ranks = _index_maps[0]->dest();
610
611 // Create neighbourhood communicator (owner <- ghost)
612 MPI_Comm comm;
613 MPI_Dist_graph_create_adjacent(_index_maps[0]->comm(), dest_ranks.size(),
614 dest_ranks.data(), MPI_UNWEIGHTED,
615 src_ranks.size(), src_ranks.data(),
616 MPI_UNWEIGHTED, MPI_INFO_NULL, false, &comm);
617 _comm = dolfinx::MPI::Comm(comm, false);
618
619 // Build map from ghost row index position to owning (neighborhood)
620 // rank
621 _ghost_row_to_rank.reserve(_index_maps[0]->owners().size());
622 for (int r : _index_maps[0]->owners())
623 {
624 auto it = std::ranges::lower_bound(src_ranks, r);
625 assert(it != src_ranks.end() and *it == r);
626 std::size_t pos = std::distance(src_ranks.begin(), it);
627 _ghost_row_to_rank.push_back(pos);
628 }
629
630 // Compute size of data to send to each neighbor
631 std::vector<std::int32_t> data_per_proc(src_ranks.size(), 0);
632 for (std::size_t i = 0; i < _ghost_row_to_rank.size(); ++i)
633 {
634 assert(_ghost_row_to_rank[i] < (int)data_per_proc.size());
635 std::size_t pos = local_size[0] + i;
636 data_per_proc[_ghost_row_to_rank[i]] += _row_ptr[pos + 1] - _row_ptr[pos];
637 }
638
639 // Compute send displacements
640 _val_send_disp.resize(src_ranks.size() + 1, 0);
641 std::partial_sum(data_per_proc.begin(), data_per_proc.end(),
642 std::next(_val_send_disp.begin()));
643
644 // For each ghost row, pack and send indices to neighborhood
645 std::vector<std::int64_t> ghost_index_data(2 * _val_send_disp.back());
646 {
647 std::vector<int> insert_pos = _val_send_disp;
648 for (std::size_t i = 0; i < _ghost_row_to_rank.size(); ++i)
649 {
650 const int rank = _ghost_row_to_rank[i];
651 std::int32_t row_id = local_size[0] + i;
652 for (int j = _row_ptr[row_id]; j < _row_ptr[row_id + 1]; ++j)
653 {
654 // Get position in send buffer
655 const std::int32_t idx_pos = 2 * insert_pos[rank];
656
657 // Pack send data (row, col) as global indices
658 ghost_index_data[idx_pos] = ghosts0[i];
659 if (std::int32_t col_local = _cols[j]; col_local < local_size[1])
660 ghost_index_data[idx_pos + 1] = col_local + local_range[1][0];
661 else
662 ghost_index_data[idx_pos + 1] = ghosts1[col_local - local_size[1]];
663
664 insert_pos[rank] += 1;
665 }
666 }
667 }
668
669 // Communicate data with neighborhood
670 std::vector<std::int64_t> ghost_index_array;
671 std::vector<int> recv_disp;
672 {
673 std::vector<int> send_sizes;
674 std::ranges::transform(data_per_proc, std::back_inserter(send_sizes),
675 [](auto x) { return 2 * x; });
676
677 std::vector<int> recv_sizes(dest_ranks.size());
678 send_sizes.reserve(1);
679 recv_sizes.reserve(1);
680 MPI_Neighbor_alltoall(send_sizes.data(), 1, MPI_INT, recv_sizes.data(), 1,
681 MPI_INT, _comm.comm());
682
683 // Build send/recv displacement
684 std::vector<int> send_disp{0};
685 std::partial_sum(send_sizes.begin(), send_sizes.end(),
686 std::back_inserter(send_disp));
687 recv_disp = {0};
688 std::partial_sum(recv_sizes.begin(), recv_sizes.end(),
689 std::back_inserter(recv_disp));
690
691 ghost_index_array.resize(recv_disp.back());
692 MPI_Neighbor_alltoallv(ghost_index_data.data(), send_sizes.data(),
693 send_disp.data(), MPI_INT64_T,
694 ghost_index_array.data(), recv_sizes.data(),
695 recv_disp.data(), MPI_INT64_T, _comm.comm());
696 }
697
698 // Store receive displacements for future use, when transferring
699 // data values
700 _val_recv_disp.resize(recv_disp.size());
701 const int bs2 = _bs[0] * _bs[1];
702 std::ranges::transform(recv_disp, _val_recv_disp.begin(),
703 [&bs2](auto d) { return bs2 * d / 2; });
704 std::ranges::transform(_val_send_disp, _val_send_disp.begin(),
705 [&bs2](auto d) { return d * bs2; });
706
707 // Global-to-local map for ghost columns
708 std::vector<std::pair<std::int64_t, std::int32_t>> global_to_local;
709 global_to_local.reserve(ghosts1.size());
710 for (std::int64_t idx : ghosts1)
711 global_to_local.push_back({idx, global_to_local.size() + local_size[1]});
712 std::ranges::sort(global_to_local);
713
714 // Compute location in which data for each index should be stored
715 // when received
716 for (std::size_t i = 0; i < ghost_index_array.size(); i += 2)
717 {
718 // Row must be on this process
719 const std::int32_t local_row = ghost_index_array[i] - local_range[0][0];
720 assert(local_row >= 0 and local_row < local_size[0]);
721
722 // Column may be owned or unowned
723 std::int32_t local_col = ghost_index_array[i + 1] - local_range[1][0];
724 if (local_col < 0 or local_col >= local_size[1])
725 {
726 auto it = std::ranges::lower_bound(
727 global_to_local, std::pair(ghost_index_array[i + 1], -1),
728 [](auto& a, auto& b) { return a.first < b.first; });
729 assert(it != global_to_local.end()
730 and it->first == ghost_index_array[i + 1]);
731 local_col = it->second;
732 }
733 auto cit0 = std::next(_cols.begin(), _row_ptr[local_row]);
734 auto cit1 = std::next(_cols.begin(), _row_ptr[local_row + 1]);
735
736 // Find position of column index and insert data
737 auto cit = std::lower_bound(cit0, cit1, local_col);
738 assert(cit != cit1);
739 assert(*cit == local_col);
740 std::size_t d = std::distance(_cols.begin(), cit);
741 _unpack_pos.push_back(d);
742 }
743}
744//-----------------------------------------------------------------------------
745
746// The matrix A is distributed across P processes by blocks of rows:
747// A = | A_0 |
748// | A_1 |
749// | ... |
750// | A_P-1 |
751//
752// Each submatrix A_i is owned by a single process "i" and can be further
753// decomposed into diagonal (Ai[0]) and off diagonal (Ai[1]) blocks:
754// Ai = |Ai[0] Ai[1]|
755//
756// If A is square, the diagonal block Ai[0] is also square and contains
757// only owned columns and rows. The block Ai[1] contains ghost columns
758// (unowned dofs).
759
760// Likewise, a local vector x can be decomposed into owned and ghost blocks:
761// xi = | x[0] |
762// | x[1] |
763//
764// So the product y = Ax can be computed into two separate steps:
765// y[0] = |Ai[0] Ai[1]| | x[0] | = Ai[0] x[0] + Ai[1] x[1]
766// | x[1] |
767//
770template <typename Scalar, typename V, typename W, typename X>
773{
774 // start communication (update ghosts)
776
777 const std::int32_t nrowslocal = num_owned_rows();
778 std::span<const std::int64_t> Arow_ptr(row_ptr().data(), nrowslocal + 1);
779 std::span<const std::int32_t> Acols(cols().data(), Arow_ptr[nrowslocal]);
780 std::span<const std::int64_t> Aoff_diag_offset(off_diag_offset().data(),
781 nrowslocal);
782 std::span<const Scalar> Avalues(values().data(), Arow_ptr[nrowslocal]);
783
784 std::span<const Scalar> _x = x.array();
785 std::span<Scalar> _y = y.mutable_array();
786
787 std::span<const std::int64_t> Arow_begin(Arow_ptr.data(), nrowslocal);
788 std::span<const std::int64_t> Arow_end(Arow_ptr.data() + 1, nrowslocal);
789
790 // First stage: spmv - diagonal
791 // yi[0] += Ai[0] * xi[0]
792 if (_bs[1] == 1)
793 {
794 impl::spmv<Scalar, 1>(Avalues, Arow_begin, Aoff_diag_offset, Acols, _x, _y,
795 _bs[0], 1);
796 }
797 else
798 {
799 impl::spmv<Scalar, -1>(Avalues, Arow_begin, Aoff_diag_offset, Acols, _x, _y,
800 _bs[0], _bs[1]);
801 }
802
803 // finalize ghost update
804 x.scatter_fwd_end();
805
806 // Second stage: spmv - off-diagonal
807 // yi[0] += Ai[1] * xi[1]
808 if (_bs[1] == 1)
809 {
810 impl::spmv<Scalar, 1>(Avalues, Aoff_diag_offset, Arow_end, Acols, _x, _y,
811 _bs[0], 1);
812 }
813 else
814 {
815 impl::spmv<Scalar, -1>(Avalues, Aoff_diag_offset, Arow_end, Acols, _x, _y,
816 _bs[0], _bs[1]);
817 }
818}
819
820} // namespace dolfinx::la
A duplicate MPI communicator and manage lifetime of the communicator.
Definition MPI.h:42
const container_type & values() const
Definition MatrixCSR.h:455
std::shared_ptr< const common::IndexMap > index_map(int dim) const
Index maps for the row and column space.
Definition MatrixCSR.h:444
const rowptr_container_type & off_diag_offset() const
Definition MatrixCSR.h:472
void set(std::span< const value_type > x, std::span< const std::int32_t > rows, std::span< const std::int32_t > cols)
Set values in the matrix.
Definition MatrixCSR.h:206
RowPtrContainer rowptr_container_type
Row pointer container type.
Definition MatrixCSR.h:66
void scatter_rev_end()
End transfer of ghost row data to owning ranks.
Definition MatrixCSR.h:384
container_type & values()
Definition MatrixCSR.h:451
auto mat_add_values()
Insertion functor for adding values to a matrix. It is typically used in finite element assembly func...
Definition MatrixCSR.h:137
void add(std::span< const value_type > x, std::span< const std::int32_t > rows, std::span< const std::int32_t > cols)
Accumulate values in the matrix.
Definition MatrixCSR.h:251
std::int32_t num_owned_rows() const
Number of local rows excluding ghost rows.
Definition MatrixCSR.h:278
MatrixCSR(const SparsityPattern &p, BlockMode mode=BlockMode::compact)
Create a distributed matrix.
Definition MatrixCSR.h:524
ColContainer column_container_type
Column index container type.
Definition MatrixCSR.h:63
MatrixCSR(MatrixCSR &&A)=default
void mult(Vector< value_type > &x, Vector< value_type > &y)
Compute the product y += Ax.
Definition MatrixCSR.h:771
double squared_norm() const
Compute the Frobenius norm squared across all processes.
Definition MatrixCSR.h:410
void scatter_rev()
Transfer ghost row data to the owning ranks accumulating received values on the owned rows,...
Definition MatrixCSR.h:324
Container container_type
Matrix entries container type.
Definition MatrixCSR.h:60
Scalar value_type
Scalar type.
Definition MatrixCSR.h:57
void scatter_rev_begin()
Begin transfer of ghost row data to owning ranks, where it will be accumulated into existing owned ro...
Definition MatrixCSR.h:337
const column_container_type & cols() const
Definition MatrixCSR.h:463
void set(value_type x)
Set all non-zero local entries to a value including entries in ghost rows.
Definition MatrixCSR.h:187
std::array< int, 2 > block_size() const
Definition MatrixCSR.h:479
std::int32_t num_all_rows() const
Number of local rows including ghost rows.
Definition MatrixCSR.h:281
const rowptr_container_type & row_ptr() const
Definition MatrixCSR.h:459
std::vector< value_type > to_dense() const
Copy to a dense matrix.
Definition MatrixCSR.h:292
auto mat_set_values()
Insertion functor for setting values in a matrix. It is typically used in finite element assembly fun...
Definition MatrixCSR.h:95
Definition SparsityPattern.h:26
Definition Vector.h:32
void scatter_fwd_begin()
Definition Vector.h:85
std::span< const value_type > array() const
Get local part of the vector (const version)
Definition Vector.h:189
void scatter_fwd_end()
Definition Vector.h:104
std::span< value_type > mutable_array()
Get local part of the vector.
Definition Vector.h:195
MPI_Datatype mpi_t
Retrieves the MPI data type associated to the provided type.
Definition MPI.h:280
void check_error(MPI_Comm comm, int code)
Check MPI error code. If the error code is not equal to MPI_SUCCESS, then std::abort is called.
Definition MPI.cpp:80
int size(MPI_Comm comm)
Definition MPI.cpp:72
int rank(MPI_Comm comm)
Return process rank for the communicator.
Definition MPI.cpp:64
constexpr std::array< std::int64_t, 2 > local_range(int rank, std::int64_t N, int size)
Return local range for the calling process, partitioning the global [0, N - 1] range across all ranks...
Definition MPI.h:89
Linear algebra interface.
Definition sparsitybuild.h:15
BlockMode
Modes for representing block structured matrices.
Definition MatrixCSR.h:26
@ expanded
Definition MatrixCSR.h:29
auto norm(const V &x, Norm type=Norm::l2)
Definition Vector.h:267