409 _block_mode(mode), _bs({p.block_size(0), p.block_size(1)}),
410 _data(p.num_nonzeros() * _bs[0] * _bs[1], 0),
411 _cols(p.graph().first.begin(), p.graph().first.end()),
412 _row_ptr(p.graph().second.begin(), p.graph().second.end()),
418 for (
int i = 0; i < 2; ++i)
420 const auto im = _index_maps[i];
421 const int size_local = im->size_local() * _bs[i];
422 std::span ghost_i = im->ghosts();
423 std::vector<std::int64_t> ghosts;
424 const std::vector<int> ghost_owner_i(im->owners().begin(),
426 std::vector<int> src_rank;
427 for (std::size_t j = 0; j < ghost_i.size(); ++j)
429 for (
int k = 0; k < _bs[i]; ++k)
431 ghosts.push_back(ghost_i[j] * _bs[i] + k);
432 src_rank.push_back(ghost_owner_i[j]);
435 const std::array<std::vector<int>, 2> src_dest0
436 = {std::vector(_index_maps[i]->src().begin(),
437 _index_maps[i]->src().end()),
438 std::vector(_index_maps[i]->dest().begin(),
439 _index_maps[i]->dest().end())};
440 _index_maps[i] = std::make_shared<common::IndexMap>(
441 _index_maps[i]->comm(), size_local, src_dest0, ghosts, src_rank);
446 column_container_type new_cols;
447 new_cols.reserve(_data.size());
448 rowptr_container_type new_row_ptr = {0};
449 new_row_ptr.reserve(_row_ptr.size() * _bs[0]);
450 std::span<const std::int32_t> num_diag_nnz = p.off_diagonal_offsets();
451 for (std::size_t i = 0; i < _row_ptr.size() - 1; ++i)
454 for (
int q0 = 0; q0 < _bs[0]; ++q0)
456 _off_diagonal_offset.push_back(new_row_ptr.back()
457 + num_diag_nnz[i] * _bs[1]);
458 for (
auto j = _row_ptr[i]; j < _row_ptr[i + 1]; ++j)
460 for (
int q1 = 0; q1 < _bs[1]; ++q1)
461 new_cols.push_back(_cols[j] * _bs[1] + q1);
463 new_row_ptr.push_back(new_cols.size());
467 _row_ptr = new_row_ptr;
474 std::span<const std::int32_t> num_diag_nnz = p.off_diagonal_offsets();
475 _off_diagonal_offset.reserve(num_diag_nnz.size());
476 std::transform(num_diag_nnz.begin(), num_diag_nnz.end(), _row_ptr.begin(),
477 std::back_inserter(_off_diagonal_offset), std::plus{});
481 const std::array local_size
482 = {_index_maps[0]->size_local(), _index_maps[1]->size_local()};
484 = {_index_maps[0]->local_range(), _index_maps[1]->local_range()};
485 std::span ghosts1 = _index_maps[1]->ghosts();
487 std::span ghosts0 = _index_maps[0]->ghosts();
488 std::span src_ranks = _index_maps[0]->src();
489 std::span dest_ranks = _index_maps[0]->dest();
493 MPI_Dist_graph_create_adjacent(_index_maps[0]->comm(), dest_ranks.size(),
494 dest_ranks.data(), MPI_UNWEIGHTED,
495 src_ranks.size(), src_ranks.data(),
496 MPI_UNWEIGHTED, MPI_INFO_NULL,
false, &comm);
501 _ghost_row_to_rank.reserve(_index_maps[0]->owners().
size());
502 for (
int r : _index_maps[0]->owners())
504 auto it = std::lower_bound(src_ranks.begin(), src_ranks.end(), r);
505 assert(it != src_ranks.end() and *it == r);
506 int pos = std::distance(src_ranks.begin(), it);
507 _ghost_row_to_rank.push_back(pos);
511 std::vector<std::int32_t> data_per_proc(src_ranks.size(), 0);
512 for (std::size_t i = 0; i < _ghost_row_to_rank.size(); ++i)
514 assert(_ghost_row_to_rank[i] < data_per_proc.size());
515 std::size_t pos = local_size[0] + i;
516 data_per_proc[_ghost_row_to_rank[i]] += _row_ptr[pos + 1] - _row_ptr[pos];
520 _val_send_disp.resize(src_ranks.size() + 1, 0);
521 std::partial_sum(data_per_proc.begin(), data_per_proc.end(),
522 std::next(_val_send_disp.begin()));
525 std::vector<std::int64_t> ghost_index_data(2 * _val_send_disp.back());
527 std::vector<int> insert_pos = _val_send_disp;
528 for (std::size_t i = 0; i < _ghost_row_to_rank.size(); ++i)
530 const int rank = _ghost_row_to_rank[i];
531 int row_id = local_size[0] + i;
532 for (
int j = _row_ptr[row_id]; j < _row_ptr[row_id + 1]; ++j)
535 const std::int32_t idx_pos = 2 * insert_pos[
rank];
538 ghost_index_data[idx_pos] = ghosts0[i];
539 if (std::int32_t col_local = _cols[j]; col_local < local_size[1])
540 ghost_index_data[idx_pos + 1] = col_local + local_range[1][0];
542 ghost_index_data[idx_pos + 1] = ghosts1[col_local - local_size[1]];
544 insert_pos[
rank] += 1;
550 std::vector<std::int64_t> ghost_index_array;
551 std::vector<int> recv_disp;
553 std::vector<int> send_sizes;
554 std::transform(data_per_proc.begin(), data_per_proc.end(),
555 std::back_inserter(send_sizes),
556 [](
auto x) { return 2 * x; });
558 std::vector<int> recv_sizes(dest_ranks.size());
559 send_sizes.reserve(1);
560 recv_sizes.reserve(1);
561 MPI_Neighbor_alltoall(send_sizes.data(), 1, MPI_INT, recv_sizes.data(), 1,
562 MPI_INT, _comm.comm());
565 std::vector<int> send_disp = {0};
566 std::partial_sum(send_sizes.begin(), send_sizes.end(),
567 std::back_inserter(send_disp));
569 std::partial_sum(recv_sizes.begin(), recv_sizes.end(),
570 std::back_inserter(recv_disp));
572 ghost_index_array.resize(recv_disp.back());
573 MPI_Neighbor_alltoallv(ghost_index_data.data(), send_sizes.data(),
574 send_disp.data(), MPI_INT64_T,
575 ghost_index_array.data(), recv_sizes.data(),
576 recv_disp.data(), MPI_INT64_T, _comm.comm());
581 _val_recv_disp.resize(recv_disp.size());
582 const int bs2 = _bs[0] * _bs[1];
583 std::transform(recv_disp.begin(), recv_disp.end(), _val_recv_disp.begin(),
584 [&bs2](
auto d) { return bs2 * d / 2; });
585 std::transform(_val_send_disp.begin(), _val_send_disp.end(),
586 _val_send_disp.begin(), [&bs2](
auto d) { return d * bs2; });
589 std::vector<std::pair<std::int64_t, std::int32_t>> global_to_local;
590 global_to_local.reserve(ghosts1.size());
591 for (std::int64_t idx : ghosts1)
592 global_to_local.push_back({idx, global_to_local.size() + local_size[1]});
593 std::sort(global_to_local.begin(), global_to_local.end());
597 for (std::size_t i = 0; i < ghost_index_array.size(); i += 2)
600 const std::int32_t local_row = ghost_index_array[i] -
local_range[0][0];
601 assert(local_row >= 0 and local_row < local_size[0]);
604 std::int32_t local_col = ghost_index_array[i + 1] -
local_range[1][0];
605 if (local_col < 0 or local_col >= local_size[1])
607 auto it = std::lower_bound(global_to_local.begin(), global_to_local.end(),
608 std::pair(ghost_index_array[i + 1], -1),
610 { return a.first < b.first; });
611 assert(it != global_to_local.end()
612 and it->first == ghost_index_array[i + 1]);
613 local_col = it->second;
615 auto cit0 = std::next(_cols.begin(), _row_ptr[local_row]);
616 auto cit1 = std::next(_cols.begin(), _row_ptr[local_row + 1]);
619 auto cit = std::lower_bound(cit0, cit1, local_col);
621 assert(*cit == local_col);
622 std::size_t d = std::distance(_cols.begin(), cit);
623 _unpack_pos.push_back(d);
650 const std::int32_t local_size0 = _index_maps[0]->size_local();
651 const std::int32_t num_ghosts0 = _index_maps[0]->num_ghosts();
652 const int bs2 = _bs[0] * _bs[1];
655 std::vector<int> insert_pos = _val_send_disp;
656 _ghost_value_data.resize(_val_send_disp.back());
657 for (
int i = 0; i < num_ghosts0; ++i)
659 const int rank = _ghost_row_to_rank[i];
663 const std::int32_t val_pos = insert_pos[rank];
664 std::copy(std::next(_data.data(), _row_ptr[local_size0 + i] * bs2),
665 std::next(_data.data(), _row_ptr[local_size0 + i + 1] * bs2),
666 std::next(_ghost_value_data.begin(), val_pos));
668 += bs2 * (_row_ptr[local_size0 + i + 1] - _row_ptr[local_size0 + i]);
671 _ghost_value_data_in.resize(_val_recv_disp.back());
674 std::vector<int> val_send_count(_val_send_disp.size() - 1);
675 std::adjacent_difference(std::next(_val_send_disp.begin()),
676 _val_send_disp.end(), val_send_count.begin());
678 std::vector<int> val_recv_count(_val_recv_disp.size() - 1);
679 std::adjacent_difference(std::next(_val_recv_disp.begin()),
680 _val_recv_disp.end(), val_recv_count.begin());
682 int status = MPI_Ineighbor_alltoallv(
683 _ghost_value_data.data(), val_send_count.data(), _val_send_disp.data(),
684 dolfinx::MPI::mpi_type<value_type>(), _ghost_value_data_in.data(),
685 val_recv_count.data(), _val_recv_disp.data(),
686 dolfinx::MPI::mpi_type<value_type>(), _comm.comm(), &_request);
687 assert(status == MPI_SUCCESS);