724MatrixCSR<U, V, W, X>::MatrixCSR(
const SparsityType& p,
BlockMode mode)
725 : _index_maps({p.index_map(0), p.index_map(1)}), _block_mode(mode),
726 _bs({p.block_size(0), p.block_size(1)}),
727 _data(p.graph().first.size() * _bs[0] * _bs[1], 0),
728 _cols(p.graph().first.begin(), p.graph().first.end()),
729 _row_ptr(p.graph().second.begin(), p.graph().second.end()),
735 for (
int i = 0; i < 2; ++i)
737 auto im = _index_maps[i];
738 std::int32_t size_local = im->size_local() * _bs[i];
739 std::span ghost_i = im->ghosts();
740 std::vector<std::int64_t> ghosts;
741 const std::vector<int> ghost_owner_i(im->owners().begin(),
743 std::vector<int> src_rank;
744 for (std::size_t j = 0; j < ghost_i.size(); ++j)
746 for (
int k = 0; k < _bs[i]; ++k)
748 ghosts.push_back(ghost_i[j] * _bs[i] + k);
749 src_rank.push_back(ghost_owner_i[j]);
753 std::array<std::vector<int>, 2> src_dest0
754 = {std::vector(_index_maps[i]->src().begin(),
755 _index_maps[i]->src().end()),
756 std::vector(_index_maps[i]->dest().begin(),
757 _index_maps[i]->dest().end())};
758 _index_maps[i] = std::make_shared<common::IndexMap>(
759 _index_maps[i]->comm(), size_local, src_dest0, ghosts, src_rank);
764 column_container_type new_cols;
765 new_cols.reserve(_data.size());
766 rowptr_container_type new_row_ptr{0};
767 new_row_ptr.reserve(_row_ptr.size() * _bs[0]);
768 std::span<const std::int32_t> num_diag_nnz = p.off_diagonal_offsets();
769 for (std::size_t i = 0; i < _row_ptr.size() - 1; ++i)
772 for (
int q0 = 0; q0 < _bs[0]; ++q0)
774 _off_diagonal_offset.push_back(new_row_ptr.back()
775 + num_diag_nnz[i] * _bs[1]);
776 for (
auto j = _row_ptr[i]; j < _row_ptr[i + 1]; ++j)
778 for (
int q1 = 0; q1 < _bs[1]; ++q1)
779 new_cols.push_back(_cols[j] * _bs[1] + q1);
781 new_row_ptr.push_back(new_cols.size());
785 _row_ptr = new_row_ptr;
792 std::span<const std::int32_t> num_diag_nnz = p.off_diagonal_offsets();
793 _off_diagonal_offset.reserve(num_diag_nnz.size());
794 std::ranges::transform(num_diag_nnz, _row_ptr,
795 std::back_inserter(_off_diagonal_offset),
800 std::array local_size
801 = {_index_maps[0]->size_local(), _index_maps[1]->size_local()};
803 = {_index_maps[0]->local_range(), _index_maps[1]->local_range()};
804 std::span ghosts1 = _index_maps[1]->ghosts();
806 std::span ghosts0 = _index_maps[0]->ghosts();
807 std::span src_ranks = _index_maps[0]->src();
808 std::span dest_ranks = _index_maps[0]->dest();
812 MPI_Dist_graph_create_adjacent(_index_maps[0]->comm(), dest_ranks.size(),
813 dest_ranks.data(), MPI_UNWEIGHTED,
814 src_ranks.size(), src_ranks.data(),
815 MPI_UNWEIGHTED, MPI_INFO_NULL,
false, &comm);
816 _comm = dolfinx::MPI::Comm(comm,
false);
820 _ghost_row_to_rank.reserve(_index_maps[0]->owners().
size());
821 for (
int r : _index_maps[0]->owners())
823 auto it = std::ranges::lower_bound(src_ranks, r);
824 assert(it != src_ranks.end() and *it == r);
825 std::size_t pos = std::ranges::distance(src_ranks.begin(), it);
826 _ghost_row_to_rank.push_back(pos);
830 std::vector<std::int32_t> data_per_proc(src_ranks.size(), 0);
831 for (std::size_t i = 0; i < _ghost_row_to_rank.size(); ++i)
833 assert(_ghost_row_to_rank[i] < (
int)data_per_proc.size());
834 std::size_t pos = local_size[0] + i;
835 data_per_proc[_ghost_row_to_rank[i]] += _row_ptr[pos + 1] - _row_ptr[pos];
839 _val_send_disp.resize(src_ranks.size() + 1, 0);
840 std::partial_sum(data_per_proc.begin(), data_per_proc.end(),
841 std::next(_val_send_disp.begin()));
844 std::vector<std::int64_t> ghost_index_data(2 * _val_send_disp.back());
846 std::vector<int> insert_pos = _val_send_disp;
847 for (std::size_t i = 0; i < _ghost_row_to_rank.size(); ++i)
849 int rank = _ghost_row_to_rank[i];
850 std::int32_t row_id = local_size[0] + i;
851 for (
int j = _row_ptr[row_id]; j < _row_ptr[row_id + 1]; ++j)
854 std::int32_t idx_pos = 2 * insert_pos[
rank];
857 ghost_index_data[idx_pos] = ghosts0[i];
858 if (std::int32_t col_local = _cols[j]; col_local < local_size[1])
859 ghost_index_data[idx_pos + 1] = col_local + local_range[1][0];
861 ghost_index_data[idx_pos + 1] = ghosts1[col_local - local_size[1]];
863 insert_pos[
rank] += 1;
869 std::vector<std::int64_t> ghost_index_array;
870 std::vector<int> recv_disp;
872 std::vector<int> send_sizes;
873 std::ranges::transform(data_per_proc, std::back_inserter(send_sizes),
874 [](
auto x) {
return 2 * x; });
876 std::vector<int> recv_sizes(dest_ranks.size());
877 send_sizes.reserve(1);
878 recv_sizes.reserve(1);
879 MPI_Neighbor_alltoall(send_sizes.data(), 1, MPI_INT, recv_sizes.data(), 1,
880 MPI_INT, _comm.
comm());
883 std::vector<int> send_disp{0};
884 std::partial_sum(send_sizes.begin(), send_sizes.end(),
885 std::back_inserter(send_disp));
887 std::partial_sum(recv_sizes.begin(), recv_sizes.end(),
888 std::back_inserter(recv_disp));
890 ghost_index_array.resize(recv_disp.back());
891 MPI_Neighbor_alltoallv(ghost_index_data.data(), send_sizes.data(),
892 send_disp.data(), MPI_INT64_T,
893 ghost_index_array.data(), recv_sizes.data(),
894 recv_disp.data(), MPI_INT64_T, _comm.
comm());
899 _val_recv_disp.resize(recv_disp.size());
900 int bs2 = _bs[0] * _bs[1];
901 std::ranges::transform(recv_disp, _val_recv_disp.begin(),
902 [&bs2](
auto d) { return bs2 * d / 2; });
903 std::ranges::transform(_val_send_disp, _val_send_disp.begin(),
904 [&bs2](
auto d) { return d * bs2; });
907 std::vector<std::pair<std::int64_t, std::int32_t>> global_to_local;
908 global_to_local.reserve(ghosts1.size());
909 for (std::int64_t idx : ghosts1)
910 global_to_local.push_back({idx, global_to_local.size() + local_size[1]});
911 std::ranges::sort(global_to_local);
915 for (std::size_t i = 0; i < ghost_index_array.size(); i += 2)
918 std::int32_t local_row = ghost_index_array[i] -
local_range[0][0];
919 assert(local_row >= 0 and local_row < local_size[0]);
922 std::int32_t local_col = ghost_index_array[i + 1] -
local_range[1][0];
923 if (local_col < 0 or local_col >= local_size[1])
925 auto it = std::ranges::lower_bound(
926 global_to_local, std::pair(ghost_index_array[i + 1], -1),
927 [](
auto a,
auto b) {
return a.first < b.first; });
928 assert(it != global_to_local.end()
929 and it->first == ghost_index_array[i + 1]);
930 local_col = it->second;
932 auto cit0 = std::next(_cols.begin(), _row_ptr[local_row]);
933 auto cit1 = std::next(_cols.begin(), _row_ptr[local_row + 1]);
936 auto cit = std::lower_bound(cit0, cit1, local_col);
938 assert(*cit == local_col);
939 std::size_t d = std::ranges::distance(_cols.begin(), cit);
940 _unpack_pos.push_back(d);
943 _unpack_pos.shrink_to_fit();
979 std::span<const std::int64_t> Arow_ptr(
row_ptr().data(), nrowslocal + 1);
980 std::span<const std::int32_t> Acols(
cols().data(), Arow_ptr[nrowslocal]);
981 std::span<const std::int64_t> Aoff_diag_offset(
off_diag_offset().data(),
983 std::span<const Scalar> Avalues(
values().data(),
984 Arow_ptr[nrowslocal] * _bs[0] * _bs[1]);
986 std::span<const Scalar> _x = x.
array();
987 std::span<Scalar> _y = y.
array();
989 std::span<const std::int64_t> Arow_begin(Arow_ptr.data(), nrowslocal);
990 std::span<const std::int64_t> Arow_end(Arow_ptr.data() + 1, nrowslocal);
996 impl::spmv<Scalar>(Avalues, Arow_begin, Aoff_diag_offset, Acols, _x, _y,
997 _bs[0], std::integral_constant<int, 1>{});
999 else if (_bs[1] == 2)
1001 impl::spmv<Scalar>(Avalues, Arow_begin, Aoff_diag_offset, Acols, _x, _y,
1002 _bs[0], std::integral_constant<int, 2>{});
1004 else if (_bs[1] == 3)
1006 impl::spmv<Scalar>(Avalues, Arow_begin, Aoff_diag_offset, Acols, _x, _y,
1007 _bs[0], std::integral_constant<int, 3>{});
1011 impl::spmv<Scalar>(Avalues, Arow_begin, Aoff_diag_offset, Acols, _x, _y,
1022 impl::spmv<Scalar>(Avalues, Aoff_diag_offset, Arow_end, Acols, _x, _y,
1023 _bs[0], std::integral_constant<int, 1>{});
1025 else if (_bs[1] == 2)
1027 impl::spmv<Scalar>(Avalues, Aoff_diag_offset, Arow_end, Acols, _x, _y,
1028 _bs[0], std::integral_constant<int, 2>{});
1030 else if (_bs[1] == 3)
1032 impl::spmv<Scalar>(Avalues, Aoff_diag_offset, Arow_end, Acols, _x, _y,
1033 _bs[0], std::integral_constant<int, 3>{});
1037 impl::spmv<Scalar>(Avalues, Aoff_diag_offset, Arow_end, Acols, _x, _y,
1049 std::span<const std::int64_t> Arow_ptr(
row_ptr().data(), nrowslocal + 1);
1050 std::span<const std::int32_t> Acols(
cols().data(), Arow_ptr[nrowslocal]);
1051 std::span<const std::int64_t> Aoff_diag_offset(
off_diag_offset().data(),
1053 std::span<const Scalar> Avalues(
values().data(),
1054 Arow_ptr[nrowslocal] * _bs[0] * _bs[1]);
1056 std::span<const Scalar> _x = x.
array();
1057 std::span<Scalar> _y = y.
array();
1059 std::span<const std::int64_t> Arow_begin(Arow_ptr.data(), nrowslocal);
1060 std::span<const std::int64_t> Arow_end(Arow_ptr.data() + 1, nrowslocal);
1065 std::int32_t ncolslocal =
index_map(1)->size_local();
1066 std::fill(std::next(_y.begin(), ncolslocal * _bs[1]), _y.end(), Scalar(0));
1069 impl::spmvT<Scalar>(Avalues, Aoff_diag_offset, Arow_end, Acols, _x, _y,
1070 _bs[0], std::integral_constant<int, 1>{});
1072 else if (_bs[1] == 2)
1074 impl::spmvT<Scalar>(Avalues, Aoff_diag_offset, Arow_end, Acols, _x, _y,
1075 _bs[0], std::integral_constant<int, 2>{});
1077 else if (_bs[1] == 3)
1079 impl::spmvT<Scalar>(Avalues, Aoff_diag_offset, Arow_end, Acols, _x, _y,
1080 _bs[0], std::integral_constant<int, 3>{});
1084 impl::spmvT<Scalar>(Avalues, Aoff_diag_offset, Arow_end, Acols, _x, _y,
1092 impl::spmvT<Scalar>(Avalues, Arow_begin, Aoff_diag_offset, Acols, _x, _y,
1093 _bs[0], std::integral_constant<int, 1>{});
1095 else if (_bs[1] == 2)
1097 impl::spmvT<Scalar>(Avalues, Arow_begin, Aoff_diag_offset, Acols, _x, _y,
1098 _bs[0], std::integral_constant<int, 2>{});
1100 else if (_bs[1] == 3)
1102 impl::spmvT<Scalar>(Avalues, Arow_begin, Aoff_diag_offset, Acols, _x, _y,
1103 _bs[0], std::integral_constant<int, 3>{});
1107 impl::spmvT<Scalar>(Avalues, Arow_begin, Aoff_diag_offset, Acols, _x, _y,