314                         std::array<std::int64_t, 2> shape,
 
  315                         std::int64_t rank_offset)
 
  317  assert(rank_offset >= 0 or x.empty());
 
  318  using T = 
typename std::remove_reference_t<typename U::value_type>;
 
  322  assert(x.size() % shape[1] == 0);
 
  323  const std::int32_t shape0_local = x.size() / shape[1];
 
  325  spdlog::debug(
"Sending data to post offices (distribute_to_postoffice)");
 
  328  std::vector<int> row_to_dest(shape0_local);
 
  329  for (std::int32_t i = 0; i < shape0_local; ++i)
 
  332    row_to_dest[i] = dest;
 
  337  std::vector<std::array<std::int32_t, 2>> dest_to_index;
 
  338  dest_to_index.reserve(shape0_local);
 
  339  for (std::int32_t i = 0; i < shape0_local; ++i)
 
  341    std::size_t idx = i + rank_offset;
 
  343      dest_to_index.push_back({dest, i});
 
  345  std::ranges::sort(dest_to_index);
 
  349  std::vector<int> dest;
 
  350  std::vector<std::int32_t> num_items_per_dest,
 
  351      pos_to_neigh_rank(shape0_local, -1);
 
  353    auto it = dest_to_index.begin();
 
  354    while (it != dest_to_index.end())
 
  356      const int neigh_rank = dest.size();
 
  359      dest.push_back((*it)[0]);
 
  363          = std::find_if(it, dest_to_index.end(),
 
  364                         [r = dest.back()](
auto& idx) { return idx[0] != r; });
 
  367      num_items_per_dest.push_back(std::distance(it, it1));
 
  370      for (
auto e = it; e != it1; ++e)
 
  371        pos_to_neigh_rank[(*e)[1]] = neigh_rank;
 
  381      "Number of neighbourhood source ranks in distribute_to_postoffice: {}",
 
  382      static_cast<int>(src.size()));
 
  386  int err = MPI_Dist_graph_create_adjacent(
 
  387      comm, src.size(), src.data(), MPI_UNWEIGHTED, dest.size(), dest.data(),
 
  388      MPI_UNWEIGHTED, MPI_INFO_NULL, 
false, &neigh_comm);
 
  392  std::vector<std::int32_t> send_disp{0};
 
  393  std::partial_sum(num_items_per_dest.begin(), num_items_per_dest.end(),
 
  394                   std::back_inserter(send_disp));
 
  397  std::vector<T> send_buffer_data(shape[1] * send_disp.back());
 
  398  std::vector<std::int64_t> send_buffer_index(send_disp.back());
 
  400    std::vector<std::int32_t> send_offsets = send_disp;
 
  401    for (std::int32_t i = 0; i < shape0_local; ++i)
 
  403      if (
int neigh_dest = pos_to_neigh_rank[i]; neigh_dest != -1)
 
  405        std::size_t pos = send_offsets[neigh_dest];
 
  406        send_buffer_index[pos] = i + rank_offset;
 
  407        std::copy_n(std::next(x.begin(), i * shape[1]), shape[1],
 
  408                    std::next(send_buffer_data.begin(), shape[1] * pos));
 
  409        ++send_offsets[neigh_dest];
 
  416  std::vector<int> num_items_recv(src.size());
 
  417  num_items_per_dest.reserve(1);
 
  418  num_items_recv.reserve(1);
 
  419  err = MPI_Neighbor_alltoall(num_items_per_dest.data(), 1, MPI_INT,
 
  420                              num_items_recv.data(), 1, MPI_INT, neigh_comm);
 
  424  std::vector<std::int32_t> recv_disp(num_items_recv.size() + 1, 0);
 
  425  std::partial_sum(num_items_recv.begin(), num_items_recv.end(),
 
  426                   std::next(recv_disp.begin()));
 
  429  std::vector<std::int64_t> recv_buffer_index(recv_disp.back());
 
  430  err = MPI_Neighbor_alltoallv(
 
  431      send_buffer_index.data(), num_items_per_dest.data(), send_disp.data(),
 
  432      MPI_INT64_T, recv_buffer_index.data(), num_items_recv.data(),
 
  433      recv_disp.data(), MPI_INT64_T, neigh_comm);
 
  437  MPI_Datatype compound_type;
 
  439  MPI_Type_commit(&compound_type);
 
  440  std::vector<T> recv_buffer_data(shape[1] * recv_disp.back());
 
  441  err = MPI_Neighbor_alltoallv(
 
  442      send_buffer_data.data(), num_items_per_dest.data(), send_disp.data(),
 
  443      compound_type, recv_buffer_data.data(), num_items_recv.data(),
 
  444      recv_disp.data(), compound_type, neigh_comm);
 
  446  err = MPI_Type_free(&compound_type);
 
  448  err = MPI_Comm_free(&neigh_comm);
 
  451  spdlog::debug(
"Completed send data to post offices.");
 
  455  std::vector<std::int32_t> index_local(recv_buffer_index.size());
 
  456  std::ranges::transform(recv_buffer_index, index_local.begin(),
 
  457                         [r0](
auto idx) { return idx - r0; });
 
  459  return {index_local, recv_buffer_data};
 
 
  465                           const U& x, std::array<std::int64_t, 2> shape,
 
  466                           std::int64_t rank_offset)
 
  468  assert(rank_offset >= 0 or x.empty());
 
  469  using T = 
typename std::remove_reference_t<typename U::value_type>;
 
  472  assert(shape[1] > 0);
 
  476  assert(x.size() % shape[1] == 0);
 
  477  const std::int64_t shape0_local = x.size() / shape[1];
 
  484      comm, x, {shape[0], shape[1]}, rank_offset);
 
  485  assert(post_indices.size() == post_x.size() / shape[1]);
 
  491  std::vector<std::tuple<int, std::int64_t, std::int32_t>> src_to_index;
 
  492  for (std::size_t i = 0; i < indices.size(); ++i)
 
  494    std::size_t idx = indices[i];
 
  496      src_to_index.push_back({src, idx, i});
 
  498  std::ranges::sort(src_to_index);
 
  502  std::vector<std::int32_t> num_items_per_src;
 
  503  std::vector<int> src;
 
  505    auto it = src_to_index.begin();
 
  506    while (it != src_to_index.end())
 
  508      src.push_back(std::get<0>(*it));
 
  510          = std::find_if(it, src_to_index.end(), [r = src.back()](
auto& idx)
 
  511                         { return std::get<0>(idx) != r; });
 
  512      num_items_per_src.push_back(std::distance(it, it1));
 
  519  const std::vector<int> dest
 
  522      "Neighbourhood destination ranks from post office in " 
  523      "distribute_data (rank, num dests, num dests/mpi_size): {}, {}, {}",
 
  524      rank, 
static_cast<int>(dest.size()),
 
  525      static_cast<double>(dest.size()) / 
size);
 
  529  MPI_Comm neigh_comm0;
 
  530  int err = MPI_Dist_graph_create_adjacent(
 
  531      comm, dest.size(), dest.data(), MPI_UNWEIGHTED, src.size(), src.data(),
 
  532      MPI_UNWEIGHTED, MPI_INFO_NULL, 
false, &neigh_comm0);
 
  536  std::vector<int> num_items_recv(dest.size());
 
  537  num_items_per_src.reserve(1);
 
  538  num_items_recv.reserve(1);
 
  539  err = MPI_Neighbor_alltoall(num_items_per_src.data(), 1, MPI_INT,
 
  540                              num_items_recv.data(), 1, MPI_INT, neigh_comm0);
 
  544  std::vector<std::int32_t> send_disp{0};
 
  545  std::partial_sum(num_items_per_src.begin(), num_items_per_src.end(),
 
  546                   std::back_inserter(send_disp));
 
  547  std::vector<std::int32_t> recv_disp = {0};
 
  548  std::partial_sum(num_items_recv.begin(), num_items_recv.end(),
 
  549                   std::back_inserter(recv_disp));
 
  553  assert(send_disp.back() == (
int)src_to_index.size());
 
  554  std::vector<std::int64_t> send_buffer_index(src_to_index.size());
 
  555  std::ranges::transform(src_to_index, send_buffer_index.begin(),
 
  556                         [](
auto x) { return std::get<1>(x); });
 
  559  std::vector<std::int64_t> recv_buffer_index(recv_disp.back());
 
  560  err = MPI_Neighbor_alltoallv(
 
  561      send_buffer_index.data(), num_items_per_src.data(), send_disp.data(),
 
  562      MPI_INT64_T, recv_buffer_index.data(), num_items_recv.data(),
 
  563      recv_disp.data(), MPI_INT64_T, neigh_comm0);
 
  566  err = MPI_Comm_free(&neigh_comm0);
 
  575  const std::array<std::int64_t, 2> postoffice_range
 
  577  std::vector<std::int32_t> post_indices_map(
 
  578      postoffice_range[1] - postoffice_range[0], -1);
 
  579  for (std::size_t i = 0; i < post_indices.size(); ++i)
 
  581    assert(post_indices[i] < (
int)post_indices_map.size());
 
  582    post_indices_map[post_indices[i]] = i;
 
  586  std::vector<T> send_buffer_data(shape[1] * recv_disp.back());
 
  587  for (std::size_t p = 0; p < recv_disp.size() - 1; ++p)
 
  589    int offset = recv_disp[p];
 
  590    for (std::int32_t i = recv_disp[p]; i < recv_disp[p + 1]; ++i)
 
  592      std::int64_t index = recv_buffer_index[i];
 
  593      if (index >= rank_offset and index < (rank_offset + shape0_local))
 
  596        std::int32_t local_index = index - rank_offset;
 
  597        std::copy_n(std::next(x.begin(), shape[1] * local_index), shape[1],
 
  598                    std::next(send_buffer_data.begin(), shape[1] * offset));
 
  603        auto local_index = index - postoffice_range[0];
 
  604        std::int32_t pos = post_indices_map[local_index];
 
  606        std::copy_n(std::next(post_x.begin(), shape[1] * pos), shape[1],
 
  607                    std::next(send_buffer_data.begin(), shape[1] * offset));
 
  614  err = MPI_Dist_graph_create_adjacent(
 
  615      comm, src.size(), src.data(), MPI_UNWEIGHTED, dest.size(), dest.data(),
 
  616      MPI_UNWEIGHTED, MPI_INFO_NULL, 
false, &neigh_comm0);
 
  619  MPI_Datatype compound_type0;
 
  621  MPI_Type_commit(&compound_type0);
 
  623  std::vector<T> recv_buffer_data(shape[1] * send_disp.back());
 
  624  err = MPI_Neighbor_alltoallv(
 
  625      send_buffer_data.data(), num_items_recv.data(), recv_disp.data(),
 
  626      compound_type0, recv_buffer_data.data(), num_items_per_src.data(),
 
  627      send_disp.data(), compound_type0, neigh_comm0);
 
  630  err = MPI_Type_free(&compound_type0);
 
  632  err = MPI_Comm_free(&neigh_comm0);
 
  635  std::vector<std::int32_t> index_pos_to_buffer(indices.size(), -1);
 
  636  for (std::size_t i = 0; i < src_to_index.size(); ++i)
 
  637    index_pos_to_buffer[std::get<2>(src_to_index[i])] = i;
 
  640  std::vector<T> x_new(shape[1] * indices.size());
 
  641  for (std::size_t i = 0; i < indices.size(); ++i)
 
  643    const std::int64_t index = indices[i];
 
  644    if (index >= rank_offset and index < (rank_offset + shape0_local))
 
  647      auto local_index = index - rank_offset;
 
  648      std::copy_n(std::next(x.begin(), shape[1] * local_index), shape[1],
 
  649                  std::next(x_new.begin(), shape[1] * i));
 
  657        auto local_index = index - postoffice_range[0];
 
  658        std::int32_t pos = post_indices_map[local_index];
 
  660        std::copy_n(std::next(post_x.begin(), shape[1] * pos), shape[1],
 
  661                    std::next(x_new.begin(), shape[1] * i));
 
  666        std::int32_t pos = index_pos_to_buffer[i];
 
  668        std::copy_n(std::next(recv_buffer_data.begin(), shape[1] * pos),
 
  669                    shape[1], std::next(x_new.begin(), shape[1] * i));