413 : _index_maps({p.index_map(0),
414 std::make_shared<common::IndexMap>(p.column_index_map())}),
415 _block_mode(mode), _bs({p.block_size(0), p.block_size(1)}),
416 _data(p.num_nonzeros() * _bs[0] * _bs[1], 0),
417 _cols(p.graph().first.begin(), p.graph().first.end()),
418 _row_ptr(p.graph().second.begin(), p.graph().second.end()),
424 for (
int i = 0; i < 2; ++i)
426 const auto im = _index_maps[i];
427 const int size_local = im->size_local() * _bs[i];
428 std::span ghost_i = im->ghosts();
429 std::vector<std::int64_t> ghosts;
430 const std::vector<int> ghost_owner_i(im->owners().begin(),
432 std::vector<int> src_rank;
433 for (std::size_t j = 0; j < ghost_i.size(); ++j)
435 for (
int k = 0; k < _bs[i]; ++k)
437 ghosts.push_back(ghost_i[j] * _bs[i] + k);
438 src_rank.push_back(ghost_owner_i[j]);
441 const std::array<std::vector<int>, 2> src_dest0
442 = {std::vector(_index_maps[i]->src().begin(),
443 _index_maps[i]->src().end()),
444 std::vector(_index_maps[i]->dest().begin(),
445 _index_maps[i]->dest().end())};
446 _index_maps[i] = std::make_shared<common::IndexMap>(
447 _index_maps[i]->comm(), size_local, src_dest0, ghosts, src_rank);
452 column_container_type new_cols;
453 new_cols.reserve(_data.size());
454 rowptr_container_type new_row_ptr = {0};
455 new_row_ptr.reserve(_row_ptr.size() * _bs[0]);
456 std::span<const std::int32_t> num_diag_nnz = p.off_diagonal_offsets();
457 for (std::size_t i = 0; i < _row_ptr.size() - 1; ++i)
460 for (
int q0 = 0; q0 < _bs[0]; ++q0)
462 _off_diagonal_offset.push_back(new_row_ptr.back()
463 + num_diag_nnz[i] * _bs[1]);
464 for (
auto j = _row_ptr[i]; j < _row_ptr[i + 1]; ++j)
466 for (
int q1 = 0; q1 < _bs[1]; ++q1)
467 new_cols.push_back(_cols[j] * _bs[1] + q1);
469 new_row_ptr.push_back(new_cols.size());
473 _row_ptr = new_row_ptr;
480 std::span<const std::int32_t> num_diag_nnz = p.off_diagonal_offsets();
481 _off_diagonal_offset.reserve(num_diag_nnz.size());
482 std::ranges::transform(num_diag_nnz, _row_ptr,
483 std::back_inserter(_off_diagonal_offset),
488 const std::array local_size
489 = {_index_maps[0]->size_local(), _index_maps[1]->size_local()};
491 = {_index_maps[0]->local_range(), _index_maps[1]->local_range()};
492 std::span ghosts1 = _index_maps[1]->ghosts();
494 std::span ghosts0 = _index_maps[0]->ghosts();
495 std::span src_ranks = _index_maps[0]->src();
496 std::span dest_ranks = _index_maps[0]->dest();
500 MPI_Dist_graph_create_adjacent(_index_maps[0]->comm(), dest_ranks.size(),
501 dest_ranks.data(), MPI_UNWEIGHTED,
502 src_ranks.size(), src_ranks.data(),
503 MPI_UNWEIGHTED, MPI_INFO_NULL,
false, &comm);
508 _ghost_row_to_rank.reserve(_index_maps[0]->owners().
size());
509 for (
int r : _index_maps[0]->owners())
511 auto it = std::ranges::lower_bound(src_ranks, r);
512 assert(it != src_ranks.end() and *it == r);
513 int pos = std::distance(src_ranks.begin(), it);
514 _ghost_row_to_rank.push_back(pos);
518 std::vector<std::int32_t> data_per_proc(src_ranks.size(), 0);
519 for (std::size_t i = 0; i < _ghost_row_to_rank.size(); ++i)
521 assert(_ghost_row_to_rank[i] < data_per_proc.size());
522 std::size_t pos = local_size[0] + i;
523 data_per_proc[_ghost_row_to_rank[i]] += _row_ptr[pos + 1] - _row_ptr[pos];
527 _val_send_disp.resize(src_ranks.size() + 1, 0);
528 std::partial_sum(data_per_proc.begin(), data_per_proc.end(),
529 std::next(_val_send_disp.begin()));
532 std::vector<std::int64_t> ghost_index_data(2 * _val_send_disp.back());
534 std::vector<int> insert_pos = _val_send_disp;
535 for (std::size_t i = 0; i < _ghost_row_to_rank.size(); ++i)
537 const int rank = _ghost_row_to_rank[i];
538 int row_id = local_size[0] + i;
539 for (
int j = _row_ptr[row_id]; j < _row_ptr[row_id + 1]; ++j)
542 const std::int32_t idx_pos = 2 * insert_pos[
rank];
545 ghost_index_data[idx_pos] = ghosts0[i];
546 if (std::int32_t col_local = _cols[j]; col_local < local_size[1])
547 ghost_index_data[idx_pos + 1] = col_local + local_range[1][0];
549 ghost_index_data[idx_pos + 1] = ghosts1[col_local - local_size[1]];
551 insert_pos[
rank] += 1;
557 std::vector<std::int64_t> ghost_index_array;
558 std::vector<int> recv_disp;
560 std::vector<int> send_sizes;
561 std::ranges::transform(data_per_proc, std::back_inserter(send_sizes),
562 [](
auto x) {
return 2 * x; });
564 std::vector<int> recv_sizes(dest_ranks.size());
565 send_sizes.reserve(1);
566 recv_sizes.reserve(1);
567 MPI_Neighbor_alltoall(send_sizes.data(), 1, MPI_INT, recv_sizes.data(), 1,
568 MPI_INT, _comm.comm());
571 std::vector<int> send_disp = {0};
572 std::partial_sum(send_sizes.begin(), send_sizes.end(),
573 std::back_inserter(send_disp));
575 std::partial_sum(recv_sizes.begin(), recv_sizes.end(),
576 std::back_inserter(recv_disp));
578 ghost_index_array.resize(recv_disp.back());
579 MPI_Neighbor_alltoallv(ghost_index_data.data(), send_sizes.data(),
580 send_disp.data(), MPI_INT64_T,
581 ghost_index_array.data(), recv_sizes.data(),
582 recv_disp.data(), MPI_INT64_T, _comm.comm());
587 _val_recv_disp.resize(recv_disp.size());
588 const int bs2 = _bs[0] * _bs[1];
589 std::ranges::transform(recv_disp, _val_recv_disp.begin(),
590 [&bs2](
auto d) { return bs2 * d / 2; });
591 std::ranges::transform(_val_send_disp, _val_send_disp.begin(),
592 [&bs2](
auto d) { return d * bs2; });
595 std::vector<std::pair<std::int64_t, std::int32_t>> global_to_local;
596 global_to_local.reserve(ghosts1.size());
597 for (std::int64_t idx : ghosts1)
598 global_to_local.push_back({idx, global_to_local.size() + local_size[1]});
599 std::ranges::sort(global_to_local);
603 for (std::size_t i = 0; i < ghost_index_array.size(); i += 2)
606 const std::int32_t local_row = ghost_index_array[i] -
local_range[0][0];
607 assert(local_row >= 0 and local_row < local_size[0]);
610 std::int32_t local_col = ghost_index_array[i + 1] -
local_range[1][0];
611 if (local_col < 0 or local_col >= local_size[1])
613 auto it = std::ranges::lower_bound(
614 global_to_local, std::pair(ghost_index_array[i + 1], -1),
615 [](
auto& a,
auto& b) {
return a.first < b.first; });
616 assert(it != global_to_local.end()
617 and it->first == ghost_index_array[i + 1]);
618 local_col = it->second;
620 auto cit0 = std::next(_cols.begin(), _row_ptr[local_row]);
621 auto cit1 = std::next(_cols.begin(), _row_ptr[local_row + 1]);
624 auto cit = std::lower_bound(cit0, cit1, local_col);
626 assert(*cit == local_col);
627 std::size_t d = std::distance(_cols.begin(), cit);
628 _unpack_pos.push_back(d);
657 const std::int32_t local_size0 = _index_maps[0]->size_local();
658 const std::int32_t num_ghosts0 = _index_maps[0]->num_ghosts();
659 const int bs2 = _bs[0] * _bs[1];
662 std::vector<int> insert_pos = _val_send_disp;
663 _ghost_value_data.resize(_val_send_disp.back());
664 for (
int i = 0; i < num_ghosts0; ++i)
666 const int rank = _ghost_row_to_rank[i];
670 const std::int32_t val_pos = insert_pos[rank];
671 std::copy(std::next(_data.data(), _row_ptr[local_size0 + i] * bs2),
672 std::next(_data.data(), _row_ptr[local_size0 + i + 1] * bs2),
673 std::next(_ghost_value_data.begin(), val_pos));
675 += bs2 * (_row_ptr[local_size0 + i + 1] - _row_ptr[local_size0 + i]);
678 _ghost_value_data_in.resize(_val_recv_disp.back());
681 std::vector<int> val_send_count(_val_send_disp.size() - 1);
682 std::adjacent_difference(std::next(_val_send_disp.begin()),
683 _val_send_disp.end(), val_send_count.begin());
685 std::vector<int> val_recv_count(_val_recv_disp.size() - 1);
686 std::adjacent_difference(std::next(_val_recv_disp.begin()),
687 _val_recv_disp.end(), val_recv_count.begin());
689 int status = MPI_Ineighbor_alltoallv(
690 _ghost_value_data.data(), val_send_count.data(), _val_send_disp.data(),
692 val_recv_count.data(), _val_recv_disp.data(),
694 assert(status == MPI_SUCCESS);