630MatrixCSR<U, V, W, X>::MatrixCSR(
const SparsityType& p,
BlockMode mode)
631 : _index_maps({p.index_map(0), p.index_map(1)}), _block_mode(mode),
632 _bs({p.block_size(0), p.block_size(1)}),
633 _data(p.graph().first.size() * _bs[0] * _bs[1], 0),
634 _cols(p.graph().first.begin(), p.graph().first.end()),
635 _row_ptr(p.graph().second.begin(), p.graph().second.end()),
641 for (
int i = 0; i < 2; ++i)
643 auto im = _index_maps[i];
644 std::int32_t size_local = im->size_local() * _bs[i];
645 std::span ghost_i = im->ghosts();
646 std::vector<std::int64_t> ghosts;
647 const std::vector<int> ghost_owner_i(im->owners().begin(),
649 std::vector<int> src_rank;
650 for (std::size_t j = 0; j < ghost_i.size(); ++j)
652 for (
int k = 0; k < _bs[i]; ++k)
654 ghosts.push_back(ghost_i[j] * _bs[i] + k);
655 src_rank.push_back(ghost_owner_i[j]);
659 std::array<std::vector<int>, 2> src_dest0
660 = {std::vector(_index_maps[i]->src().begin(),
661 _index_maps[i]->src().end()),
662 std::vector(_index_maps[i]->dest().begin(),
663 _index_maps[i]->dest().end())};
664 _index_maps[i] = std::make_shared<common::IndexMap>(
665 _index_maps[i]->comm(), size_local, src_dest0, ghosts, src_rank);
670 column_container_type new_cols;
671 new_cols.reserve(_data.size());
672 rowptr_container_type new_row_ptr{0};
673 new_row_ptr.reserve(_row_ptr.size() * _bs[0]);
674 std::span<const std::int32_t> num_diag_nnz = p.off_diagonal_offsets();
675 for (std::size_t i = 0; i < _row_ptr.size() - 1; ++i)
678 for (
int q0 = 0; q0 < _bs[0]; ++q0)
680 _off_diagonal_offset.push_back(new_row_ptr.back()
681 + num_diag_nnz[i] * _bs[1]);
682 for (
auto j = _row_ptr[i]; j < _row_ptr[i + 1]; ++j)
684 for (
int q1 = 0; q1 < _bs[1]; ++q1)
685 new_cols.push_back(_cols[j] * _bs[1] + q1);
687 new_row_ptr.push_back(new_cols.size());
691 _row_ptr = new_row_ptr;
698 std::span<const std::int32_t> num_diag_nnz = p.off_diagonal_offsets();
699 _off_diagonal_offset.reserve(num_diag_nnz.size());
700 std::ranges::transform(num_diag_nnz, _row_ptr,
701 std::back_inserter(_off_diagonal_offset),
706 std::array local_size
707 = {_index_maps[0]->size_local(), _index_maps[1]->size_local()};
709 = {_index_maps[0]->local_range(), _index_maps[1]->local_range()};
710 std::span ghosts1 = _index_maps[1]->ghosts();
712 std::span ghosts0 = _index_maps[0]->ghosts();
713 std::span src_ranks = _index_maps[0]->src();
714 std::span dest_ranks = _index_maps[0]->dest();
718 MPI_Dist_graph_create_adjacent(_index_maps[0]->comm(), dest_ranks.size(),
719 dest_ranks.data(), MPI_UNWEIGHTED,
720 src_ranks.size(), src_ranks.data(),
721 MPI_UNWEIGHTED, MPI_INFO_NULL,
false, &comm);
722 _comm = dolfinx::MPI::Comm(comm,
false);
726 _ghost_row_to_rank.reserve(_index_maps[0]->owners().
size());
727 for (
int r : _index_maps[0]->owners())
729 auto it = std::ranges::lower_bound(src_ranks, r);
730 assert(it != src_ranks.end() and *it == r);
731 std::size_t pos = std::ranges::distance(src_ranks.begin(), it);
732 _ghost_row_to_rank.push_back(pos);
736 std::vector<std::int32_t> data_per_proc(src_ranks.size(), 0);
737 for (std::size_t i = 0; i < _ghost_row_to_rank.size(); ++i)
739 assert(_ghost_row_to_rank[i] < (
int)data_per_proc.size());
740 std::size_t pos = local_size[0] + i;
741 data_per_proc[_ghost_row_to_rank[i]] += _row_ptr[pos + 1] - _row_ptr[pos];
745 _val_send_disp.resize(src_ranks.size() + 1, 0);
746 std::partial_sum(data_per_proc.begin(), data_per_proc.end(),
747 std::next(_val_send_disp.begin()));
750 std::vector<std::int64_t> ghost_index_data(2 * _val_send_disp.back());
752 std::vector<int> insert_pos = _val_send_disp;
753 for (std::size_t i = 0; i < _ghost_row_to_rank.size(); ++i)
755 int rank = _ghost_row_to_rank[i];
756 std::int32_t row_id = local_size[0] + i;
757 for (
int j = _row_ptr[row_id]; j < _row_ptr[row_id + 1]; ++j)
760 std::int32_t idx_pos = 2 * insert_pos[
rank];
763 ghost_index_data[idx_pos] = ghosts0[i];
764 if (std::int32_t col_local = _cols[j]; col_local < local_size[1])
765 ghost_index_data[idx_pos + 1] = col_local + local_range[1][0];
767 ghost_index_data[idx_pos + 1] = ghosts1[col_local - local_size[1]];
769 insert_pos[
rank] += 1;
775 std::vector<std::int64_t> ghost_index_array;
776 std::vector<int> recv_disp;
778 std::vector<int> send_sizes;
779 std::ranges::transform(data_per_proc, std::back_inserter(send_sizes),
780 [](
auto x) {
return 2 * x; });
782 std::vector<int> recv_sizes(dest_ranks.size());
783 send_sizes.reserve(1);
784 recv_sizes.reserve(1);
785 MPI_Neighbor_alltoall(send_sizes.data(), 1, MPI_INT, recv_sizes.data(), 1,
786 MPI_INT, _comm.comm());
789 std::vector<int> send_disp{0};
790 std::partial_sum(send_sizes.begin(), send_sizes.end(),
791 std::back_inserter(send_disp));
793 std::partial_sum(recv_sizes.begin(), recv_sizes.end(),
794 std::back_inserter(recv_disp));
796 ghost_index_array.resize(recv_disp.back());
797 MPI_Neighbor_alltoallv(ghost_index_data.data(), send_sizes.data(),
798 send_disp.data(), MPI_INT64_T,
799 ghost_index_array.data(), recv_sizes.data(),
800 recv_disp.data(), MPI_INT64_T, _comm.comm());
805 _val_recv_disp.resize(recv_disp.size());
806 int bs2 = _bs[0] * _bs[1];
807 std::ranges::transform(recv_disp, _val_recv_disp.begin(),
808 [&bs2](
auto d) { return bs2 * d / 2; });
809 std::ranges::transform(_val_send_disp, _val_send_disp.begin(),
810 [&bs2](
auto d) { return d * bs2; });
813 std::vector<std::pair<std::int64_t, std::int32_t>> global_to_local;
814 global_to_local.reserve(ghosts1.size());
815 for (std::int64_t idx : ghosts1)
816 global_to_local.push_back({idx, global_to_local.size() + local_size[1]});
817 std::ranges::sort(global_to_local);
821 for (std::size_t i = 0; i < ghost_index_array.size(); i += 2)
824 std::int32_t local_row = ghost_index_array[i] -
local_range[0][0];
825 assert(local_row >= 0 and local_row < local_size[0]);
828 std::int32_t local_col = ghost_index_array[i + 1] -
local_range[1][0];
829 if (local_col < 0 or local_col >= local_size[1])
831 auto it = std::ranges::lower_bound(
832 global_to_local, std::pair(ghost_index_array[i + 1], -1),
833 [](
auto a,
auto b) {
return a.first < b.first; });
834 assert(it != global_to_local.end()
835 and it->first == ghost_index_array[i + 1]);
836 local_col = it->second;
838 auto cit0 = std::next(_cols.begin(), _row_ptr[local_row]);
839 auto cit1 = std::next(_cols.begin(), _row_ptr[local_row + 1]);
842 auto cit = std::lower_bound(cit0, cit1, local_col);
844 assert(*cit == local_col);
845 std::size_t d = std::ranges::distance(_cols.begin(), cit);
846 _unpack_pos.push_back(d);
849 _unpack_pos.shrink_to_fit();
885 std::span<const std::int64_t> Arow_ptr(
row_ptr().data(), nrowslocal + 1);
886 std::span<const std::int32_t> Acols(
cols().data(), Arow_ptr[nrowslocal]);
887 std::span<const std::int64_t> Aoff_diag_offset(
off_diag_offset().data(),
889 std::span<const Scalar> Avalues(
values().data(),
890 Arow_ptr[nrowslocal] * _bs[0] * _bs[1]);
892 std::span<const Scalar> _x = x.
array();
893 std::span<Scalar> _y = y.
array();
895 std::span<const std::int64_t> Arow_begin(Arow_ptr.data(), nrowslocal);
896 std::span<const std::int64_t> Arow_end(Arow_ptr.data() + 1, nrowslocal);
902 impl::spmv<Scalar>(Avalues, Arow_begin, Aoff_diag_offset, Acols, _x, _y,
903 _bs[0], std::integral_constant<int, 1>{});
905 else if (_bs[1] == 2)
907 impl::spmv<Scalar>(Avalues, Arow_begin, Aoff_diag_offset, Acols, _x, _y,
908 _bs[0], std::integral_constant<int, 2>{});
910 else if (_bs[1] == 3)
912 impl::spmv<Scalar>(Avalues, Arow_begin, Aoff_diag_offset, Acols, _x, _y,
913 _bs[0], std::integral_constant<int, 3>{});
917 impl::spmv<Scalar>(Avalues, Arow_begin, Aoff_diag_offset, Acols, _x, _y,
928 impl::spmv<Scalar>(Avalues, Aoff_diag_offset, Arow_end, Acols, _x, _y,
929 _bs[0], std::integral_constant<int, 1>{});
931 else if (_bs[1] == 2)
933 impl::spmv<Scalar>(Avalues, Aoff_diag_offset, Arow_end, Acols, _x, _y,
934 _bs[0], std::integral_constant<int, 2>{});
936 else if (_bs[1] == 3)
938 impl::spmv<Scalar>(Avalues, Aoff_diag_offset, Arow_end, Acols, _x, _y,
939 _bs[0], std::integral_constant<int, 3>{});
943 impl::spmv<Scalar>(Avalues, Aoff_diag_offset, Arow_end, Acols, _x, _y,
955 std::span<const std::int64_t> Arow_ptr(
row_ptr().data(), nrowslocal + 1);
956 std::span<const std::int32_t> Acols(
cols().data(), Arow_ptr[nrowslocal]);
957 std::span<const std::int64_t> Aoff_diag_offset(
off_diag_offset().data(),
959 std::span<const Scalar> Avalues(
values().data(),
960 Arow_ptr[nrowslocal] * _bs[0] * _bs[1]);
962 std::span<const Scalar> _x = x.
array();
963 std::span<Scalar> _y = y.
array();
965 std::span<const std::int64_t> Arow_begin(Arow_ptr.data(), nrowslocal);
966 std::span<const std::int64_t> Arow_end(Arow_ptr.data() + 1, nrowslocal);
971 std::int32_t ncolslocal =
index_map(1)->size_local();
972 std::fill(std::next(_y.begin(), ncolslocal * _bs[1]), _y.end(), Scalar(0));
975 impl::spmvT<Scalar>(Avalues, Aoff_diag_offset, Arow_end, Acols, _x, _y,
976 _bs[0], std::integral_constant<int, 1>{});
978 else if (_bs[1] == 2)
980 impl::spmvT<Scalar>(Avalues, Aoff_diag_offset, Arow_end, Acols, _x, _y,
981 _bs[0], std::integral_constant<int, 2>{});
983 else if (_bs[1] == 3)
985 impl::spmvT<Scalar>(Avalues, Aoff_diag_offset, Arow_end, Acols, _x, _y,
986 _bs[0], std::integral_constant<int, 3>{});
990 impl::spmvT<Scalar>(Avalues, Aoff_diag_offset, Arow_end, Acols, _x, _y,
998 impl::spmvT<Scalar>(Avalues, Arow_begin, Aoff_diag_offset, Acols, _x, _y,
999 _bs[0], std::integral_constant<int, 1>{});
1001 else if (_bs[1] == 2)
1003 impl::spmvT<Scalar>(Avalues, Arow_begin, Aoff_diag_offset, Acols, _x, _y,
1004 _bs[0], std::integral_constant<int, 2>{});
1006 else if (_bs[1] == 3)
1008 impl::spmvT<Scalar>(Avalues, Arow_begin, Aoff_diag_offset, Acols, _x, _y,
1009 _bs[0], std::integral_constant<int, 3>{});
1013 impl::spmvT<Scalar>(Avalues, Arow_begin, Aoff_diag_offset, Acols, _x, _y,