642 const U& x, std::array<std::int64_t, 2> shape,
643 std::int64_t rank_offset)
645 assert(rank_offset >= 0 or x.empty());
646 using T = std::ranges::range_value_t<U>;
649 assert(shape[1] > 0);
653 assert(x.size() % shape[1] == 0);
654 const std::int64_t shape0_local = x.size() / shape[1];
662 auto [send_dest, num_items_per_send_dest, pos_to_neigh_rank]
663 = impl::postoffice_plan(
size,
rank, shape0_local, shape[0], rank_offset);
668 std::vector<std::tuple<int, std::int64_t, std::int32_t>> src_to_index;
669 for (std::size_t i = 0; i < indices.size(); ++i)
671 std::int64_t idx = indices[i];
672 if (idx >= rank_offset and idx < rank_offset + shape0_local)
675 src_to_index.push_back({src, idx, i});
681 std::vector<std::int32_t> perm(src_to_index.size());
682 std::iota(perm.begin(), perm.end(), 0);
684 {
return std::get<0>(src_to_index[i]); });
685 std::vector<std::tuple<int, std::int64_t, std::int32_t>> sorted(
686 src_to_index.size());
687 for (std::size_t i = 0; i < perm.size(); ++i)
688 sorted[i] = src_to_index[perm[i]];
689 src_to_index = std::move(sorted);
694 std::vector<std::int32_t> num_items_per_src;
695 std::vector<int> src;
697 auto it = src_to_index.begin();
698 while (it != src_to_index.end())
700 src.push_back(std::get<0>(*it));
701 auto it1 = std::ranges::find_if(it, src_to_index.end(),
702 [r = src.back()](
auto& idx)
703 { return std::get<0>(idx) != r; });
704 num_items_per_src.push_back(std::ranges::distance(it, it1));
714 comm, send_dest,
static_cast<int>(tag::consensus_nbx), src,
715 static_cast<int>(tag::consensus_nbx) + 1);
717 "Neighbourhood destination ranks from post office in "
718 "distribute_data (rank, num dests, num dests/mpi_size): {}, {}, {}",
719 rank, dest.size(),
static_cast<double>(dest.size()) /
size);
723 auto [post_indices, post_x] = impl::postoffice_exchange(
724 comm, x, {shape[0], shape[1]}, rank_offset, send_dest,
725 num_items_per_send_dest, pos_to_neigh_rank, post_src);
726 assert(post_indices.size() == post_x.size() / shape[1]);
730 MPI_Comm neigh_comm0;
731 int err = MPI_Dist_graph_create_adjacent(
732 comm, dest.size(), dest.data(), MPI_UNWEIGHTED, src.size(), src.data(),
733 MPI_UNWEIGHTED, MPI_INFO_NULL,
false, &neigh_comm0);
737 std::vector<int> num_items_recv(dest.size());
738 num_items_per_src.reserve(1);
739 num_items_recv.reserve(1);
740 err = MPI_Neighbor_alltoall(num_items_per_src.data(), 1, MPI_INT,
741 num_items_recv.data(), 1, MPI_INT, neigh_comm0);
745 std::vector<std::int32_t> send_disp{0};
746 std::partial_sum(num_items_per_src.begin(), num_items_per_src.end(),
747 std::back_inserter(send_disp));
748 std::vector<std::int32_t> recv_disp = {0};
749 std::partial_sum(num_items_recv.begin(), num_items_recv.end(),
750 std::back_inserter(recv_disp));
754 assert(send_disp.back() ==
static_cast<int>(src_to_index.size()));
755 std::vector<std::int64_t> send_buffer_index(src_to_index.size());
756 std::ranges::transform(src_to_index, send_buffer_index.begin(),
757 [](
auto x) { return std::get<1>(x); });
760 std::vector<std::int64_t> recv_buffer_index(recv_disp.back());
761 err = MPI_Neighbor_alltoallv(
762 send_buffer_index.data(), num_items_per_src.data(), send_disp.data(),
763 MPI_INT64_T, recv_buffer_index.data(), num_items_recv.data(),
764 recv_disp.data(), MPI_INT64_T, neigh_comm0);
767 err = MPI_Comm_free(&neigh_comm0);
776 const std::array<std::int64_t, 2> postoffice_range
778 std::vector<std::int32_t> post_indices_map(
779 postoffice_range[1] - postoffice_range[0], -1);
780 for (std::size_t i = 0; i < post_indices.size(); ++i)
782 assert(post_indices[i] <
static_cast<int>(post_indices_map.size()));
783 post_indices_map[post_indices[i]] = i;
787 std::vector<T> send_buffer_data(shape[1] * recv_disp.back());
788 for (std::int32_t i = 0; i < recv_disp.back(); ++i)
790 std::int64_t index = recv_buffer_index[i];
791 if (index >= rank_offset and index < (rank_offset + shape0_local))
794 std::int32_t local_index = index - rank_offset;
795 std::copy_n(std::next(x.begin(), shape[1] * local_index), shape[1],
796 std::next(send_buffer_data.begin(), shape[1] * i));
801 std::int64_t local_index = index - postoffice_range[0];
802 std::int32_t pos = post_indices_map[local_index];
804 std::copy_n(std::next(post_x.begin(), shape[1] * pos), shape[1],
805 std::next(send_buffer_data.begin(), shape[1] * i));
809 err = MPI_Dist_graph_create_adjacent(
810 comm, src.size(), src.data(), MPI_UNWEIGHTED, dest.size(), dest.data(),
811 MPI_UNWEIGHTED, MPI_INFO_NULL,
false, &neigh_comm0);
814 MPI_Datatype compound_type0;
816 MPI_Type_commit(&compound_type0);
818 std::vector<T> recv_buffer_data(shape[1] * send_disp.back());
819 err = MPI_Neighbor_alltoallv(
820 send_buffer_data.data(), num_items_recv.data(), recv_disp.data(),
821 compound_type0, recv_buffer_data.data(), num_items_per_src.data(),
822 send_disp.data(), compound_type0, neigh_comm0);
825 err = MPI_Type_free(&compound_type0);
827 err = MPI_Comm_free(&neigh_comm0);
830 std::vector<std::int32_t> index_pos_to_buffer(indices.size(), -1);
831 for (std::size_t i = 0; i < src_to_index.size(); ++i)
832 index_pos_to_buffer[std::get<2>(src_to_index[i])] = i;
835 std::vector<T> x_new(shape[1] * indices.size());
836 for (std::size_t i = 0; i < indices.size(); ++i)
838 const std::int64_t index = indices[i];
839 if (index >= rank_offset and index < (rank_offset + shape0_local))
842 std::int64_t local_index = index - rank_offset;
843 std::copy_n(std::next(x.begin(), shape[1] * local_index), shape[1],
844 std::next(x_new.begin(), shape[1] * i));
846 else if (std::int32_t pos = index_pos_to_buffer[i]; pos != -1)
851 std::copy_n(std::next(recv_buffer_data.begin(), shape[1] * pos), shape[1],
852 std::next(x_new.begin(), shape[1] * i));
857 std::int64_t local_index = index - postoffice_range[0];
858 std::int32_t bag_pos = post_indices_map[local_index];
859 assert(bag_pos != -1);
860 std::copy_n(std::next(post_x.begin(), shape[1] * bag_pos), shape[1],
861 std::next(x_new.begin(), shape[1] * i));