291 std::array<std::int64_t, 2> shape,
292 std::int64_t rank_offset)
294 assert(rank_offset >= 0 or x.empty());
295 using T =
typename std::remove_reference_t<typename U::value_type>;
299 assert(x.size() % shape[1] == 0);
300 const std::int32_t shape0_local = x.size() / shape[1];
302 spdlog::debug(
"Sending data to post offices (distribute_to_postoffice)");
305 std::vector<int> row_to_dest(shape0_local);
306 for (std::int32_t i = 0; i < shape0_local; ++i)
309 row_to_dest[i] = dest;
314 std::vector<std::array<std::int32_t, 2>> dest_to_index;
315 dest_to_index.reserve(shape0_local);
316 for (std::int32_t i = 0; i < shape0_local; ++i)
318 std::size_t idx = i + rank_offset;
320 dest_to_index.push_back({dest, i});
322 std::ranges::sort(dest_to_index);
326 std::vector<int> dest;
327 std::vector<std::int32_t> num_items_per_dest,
328 pos_to_neigh_rank(shape0_local, -1);
330 auto it = dest_to_index.begin();
331 while (it != dest_to_index.end())
333 const int neigh_rank = dest.size();
336 dest.push_back((*it)[0]);
340 = std::find_if(it, dest_to_index.end(),
341 [r = dest.back()](
auto& idx) { return idx[0] != r; });
344 num_items_per_dest.push_back(std::distance(it, it1));
347 for (
auto e = it; e != it1; ++e)
348 pos_to_neigh_rank[(*e)[1]] = neigh_rank;
358 "Number of neighbourhood source ranks in distribute_to_postoffice: {}",
359 static_cast<int>(src.size()));
363 int err = MPI_Dist_graph_create_adjacent(
364 comm, src.size(), src.data(), MPI_UNWEIGHTED, dest.size(), dest.data(),
365 MPI_UNWEIGHTED, MPI_INFO_NULL,
false, &neigh_comm);
369 std::vector<std::int32_t> send_disp{0};
370 std::partial_sum(num_items_per_dest.begin(), num_items_per_dest.end(),
371 std::back_inserter(send_disp));
374 std::vector<T> send_buffer_data(shape[1] * send_disp.back());
375 std::vector<std::int64_t> send_buffer_index(send_disp.back());
377 std::vector<std::int32_t> send_offsets = send_disp;
378 for (std::int32_t i = 0; i < shape0_local; ++i)
380 if (
int neigh_dest = pos_to_neigh_rank[i]; neigh_dest != -1)
382 std::size_t pos = send_offsets[neigh_dest];
383 send_buffer_index[pos] = i + rank_offset;
384 std::copy_n(std::next(x.begin(), i * shape[1]), shape[1],
385 std::next(send_buffer_data.begin(), shape[1] * pos));
386 ++send_offsets[neigh_dest];
393 std::vector<int> num_items_recv(src.size());
394 num_items_per_dest.reserve(1);
395 num_items_recv.reserve(1);
396 err = MPI_Neighbor_alltoall(num_items_per_dest.data(), 1, MPI_INT,
397 num_items_recv.data(), 1, MPI_INT, neigh_comm);
401 std::vector<std::int32_t> recv_disp(num_items_recv.size() + 1, 0);
402 std::partial_sum(num_items_recv.begin(), num_items_recv.end(),
403 std::next(recv_disp.begin()));
406 std::vector<std::int64_t> recv_buffer_index(recv_disp.back());
407 err = MPI_Neighbor_alltoallv(
408 send_buffer_index.data(), num_items_per_dest.data(), send_disp.data(),
409 MPI_INT64_T, recv_buffer_index.data(), num_items_recv.data(),
410 recv_disp.data(), MPI_INT64_T, neigh_comm);
414 MPI_Datatype compound_type;
416 MPI_Type_commit(&compound_type);
417 std::vector<T> recv_buffer_data(shape[1] * recv_disp.back());
418 err = MPI_Neighbor_alltoallv(
419 send_buffer_data.data(), num_items_per_dest.data(), send_disp.data(),
420 compound_type, recv_buffer_data.data(), num_items_recv.data(),
421 recv_disp.data(), compound_type, neigh_comm);
423 err = MPI_Type_free(&compound_type);
425 err = MPI_Comm_free(&neigh_comm);
428 spdlog::debug(
"Completed send data to post offices.");
432 std::vector<std::int32_t> index_local(recv_buffer_index.size());
433 std::ranges::transform(recv_buffer_index, index_local.begin(),
434 [r0](
auto idx) { return idx - r0; });
436 return {index_local, recv_buffer_data};
442 const U& x, std::array<std::int64_t, 2> shape,
443 std::int64_t rank_offset)
445 assert(rank_offset >= 0 or x.empty());
446 using T =
typename std::remove_reference_t<typename U::value_type>;
449 assert(shape[1] > 0);
453 assert(x.size() % shape[1] == 0);
454 const std::int64_t shape0_local = x.size() / shape[1];
461 comm, x, {shape[0], shape[1]}, rank_offset);
462 assert(post_indices.size() == post_x.size() / shape[1]);
468 std::vector<std::tuple<int, std::int64_t, std::int32_t>> src_to_index;
469 for (std::size_t i = 0; i < indices.size(); ++i)
471 std::size_t idx = indices[i];
473 src_to_index.push_back({src, idx, i});
475 std::ranges::sort(src_to_index);
479 std::vector<std::int32_t> num_items_per_src;
480 std::vector<int> src;
482 auto it = src_to_index.begin();
483 while (it != src_to_index.end())
485 src.push_back(std::get<0>(*it));
487 = std::find_if(it, src_to_index.end(), [r = src.back()](
auto& idx)
488 { return std::get<0>(idx) != r; });
489 num_items_per_src.push_back(std::distance(it, it1));
496 const std::vector<int> dest
499 "Neighbourhood destination ranks from post office in "
500 "distribute_data (rank, num dests, num dests/mpi_size): {}, {}, {}",
501 rank,
static_cast<int>(dest.size()),
502 static_cast<double>(dest.size()) /
size);
506 MPI_Comm neigh_comm0;
507 int err = MPI_Dist_graph_create_adjacent(
508 comm, dest.size(), dest.data(), MPI_UNWEIGHTED, src.size(), src.data(),
509 MPI_UNWEIGHTED, MPI_INFO_NULL,
false, &neigh_comm0);
513 std::vector<int> num_items_recv(dest.size());
514 num_items_per_src.reserve(1);
515 num_items_recv.reserve(1);
516 err = MPI_Neighbor_alltoall(num_items_per_src.data(), 1, MPI_INT,
517 num_items_recv.data(), 1, MPI_INT, neigh_comm0);
521 std::vector<std::int32_t> send_disp{0};
522 std::partial_sum(num_items_per_src.begin(), num_items_per_src.end(),
523 std::back_inserter(send_disp));
524 std::vector<std::int32_t> recv_disp = {0};
525 std::partial_sum(num_items_recv.begin(), num_items_recv.end(),
526 std::back_inserter(recv_disp));
530 assert(send_disp.back() == (
int)src_to_index.size());
531 std::vector<std::int64_t> send_buffer_index(src_to_index.size());
532 std::ranges::transform(src_to_index, send_buffer_index.begin(),
533 [](
auto x) { return std::get<1>(x); });
536 std::vector<std::int64_t> recv_buffer_index(recv_disp.back());
537 err = MPI_Neighbor_alltoallv(
538 send_buffer_index.data(), num_items_per_src.data(), send_disp.data(),
539 MPI_INT64_T, recv_buffer_index.data(), num_items_recv.data(),
540 recv_disp.data(), MPI_INT64_T, neigh_comm0);
543 err = MPI_Comm_free(&neigh_comm0);
552 const std::array<std::int64_t, 2> postoffice_range
554 std::vector<std::int32_t> post_indices_map(
555 postoffice_range[1] - postoffice_range[0], -1);
556 for (std::size_t i = 0; i < post_indices.size(); ++i)
558 assert(post_indices[i] < (
int)post_indices_map.size());
559 post_indices_map[post_indices[i]] = i;
563 std::vector<T> send_buffer_data(shape[1] * recv_disp.back());
564 for (std::size_t p = 0; p < recv_disp.size() - 1; ++p)
566 int offset = recv_disp[p];
567 for (std::int32_t i = recv_disp[p]; i < recv_disp[p + 1]; ++i)
569 std::int64_t index = recv_buffer_index[i];
570 if (index >= rank_offset and index < (rank_offset + shape0_local))
573 std::int32_t local_index = index - rank_offset;
574 std::copy_n(std::next(x.begin(), shape[1] * local_index), shape[1],
575 std::next(send_buffer_data.begin(), shape[1] * offset));
580 auto local_index = index - postoffice_range[0];
581 std::int32_t pos = post_indices_map[local_index];
583 std::copy_n(std::next(post_x.begin(), shape[1] * pos), shape[1],
584 std::next(send_buffer_data.begin(), shape[1] * offset));
591 err = MPI_Dist_graph_create_adjacent(
592 comm, src.size(), src.data(), MPI_UNWEIGHTED, dest.size(), dest.data(),
593 MPI_UNWEIGHTED, MPI_INFO_NULL,
false, &neigh_comm0);
596 MPI_Datatype compound_type0;
598 MPI_Type_commit(&compound_type0);
600 std::vector<T> recv_buffer_data(shape[1] * send_disp.back());
601 err = MPI_Neighbor_alltoallv(
602 send_buffer_data.data(), num_items_recv.data(), recv_disp.data(),
603 compound_type0, recv_buffer_data.data(), num_items_per_src.data(),
604 send_disp.data(), compound_type0, neigh_comm0);
607 err = MPI_Type_free(&compound_type0);
609 err = MPI_Comm_free(&neigh_comm0);
612 std::vector<std::int32_t> index_pos_to_buffer(indices.size(), -1);
613 for (std::size_t i = 0; i < src_to_index.size(); ++i)
614 index_pos_to_buffer[std::get<2>(src_to_index[i])] = i;
617 std::vector<T> x_new(shape[1] * indices.size());
618 for (std::size_t i = 0; i < indices.size(); ++i)
620 const std::int64_t index = indices[i];
621 if (index >= rank_offset and index < (rank_offset + shape0_local))
624 auto local_index = index - rank_offset;
625 std::copy_n(std::next(x.begin(), shape[1] * local_index), shape[1],
626 std::next(x_new.begin(), shape[1] * i));
634 auto local_index = index - postoffice_range[0];
635 std::int32_t pos = post_indices_map[local_index];
637 std::copy_n(std::next(post_x.begin(), shape[1] * pos), shape[1],
638 std::next(x_new.begin(), shape[1] * i));
643 std::int32_t pos = index_pos_to_buffer[i];
645 std::copy_n(std::next(recv_buffer_data.begin(), shape[1] * pos),
646 shape[1], std::next(x_new.begin(), shape[1] * i));