DOLFINx 0.12.0.0
DOLFINx C++
Loading...
Searching...
No Matches
MPI.h
1// Copyright (C) 2007-2023 Magnus Vikstrøm, Garth N. Wells and Paul T. Kühner
2//
3// This file is part of DOLFINx (https://www.fenicsproject.org)
4//
5// SPDX-License-Identifier: LGPL-3.0-or-later
6
7#pragma once
8
9#include "Timer.h"
10#include "local_range.h"
11#include "log.h"
12#include "sort.h"
13#include "types.h"
14#include <algorithm>
15#include <array>
16#include <cassert>
17#include <complex>
18#include <concepts>
19#include <cstdint>
20#include <format>
21#include <iterator>
22#include <numeric>
23#include <ranges>
24#include <span>
25#include <stdexcept>
26#include <tuple>
27#include <utility>
28#include <vector>
29
30#define MPICH_IGNORE_CXX_SEEK 1
31#include <mpi.h>
32
34namespace dolfinx::MPI
35{
37enum class tag : int
38{
39 consensus_pcx = 1200,
40 consensus_nbx = 1202,
41};
42
45class Comm
46{
47public:
49 explicit Comm(MPI_Comm comm, bool duplicate = true);
50
52 Comm(const Comm& comm) noexcept;
53
55 Comm(Comm&& comm) noexcept;
56
58 ~Comm();
59
60 // Copy assignment (deleted). MPI_Comm_dup is collective; assigning into
61 // a live Comm would hide a collective dup and free behind `=`.
62 Comm& operator=(const Comm& comm) = delete;
63
65 Comm& operator=(Comm&& comm) noexcept;
66
68 MPI_Comm comm() const noexcept;
69
70private:
71 // MPI communicator
72 MPI_Comm _comm;
73};
74
76int rank(MPI_Comm comm);
77
80int size(MPI_Comm comm);
81
89void check_error(MPI_Comm comm, int code) noexcept;
90
97constexpr int index_owner(int size, std::size_t index, std::size_t N)
98{
99 assert(index < N);
100
101 // Compute number of items per rank and remainder
102 const std::size_t n = N / size;
103 const std::size_t r = N % size;
104
105 if (index < r * (n + 1))
106 {
107 // First r ranks own n + 1 indices
108 return index / (n + 1);
109 }
110 else
111 {
112 // Remaining ranks own n indices
113 return r + (index - r * (n + 1)) / n;
114 }
115}
116
143std::vector<int> compute_graph_edges_pcx(MPI_Comm comm,
144 std::span<const int> edges);
145
173std::vector<int>
174compute_graph_edges_nbx(MPI_Comm comm, std::span<const int> edges,
175 int tag = static_cast<int>(tag::consensus_nbx));
176
201std::pair<std::vector<int>, std::vector<int>>
202compute_graph_edges_nbx(MPI_Comm comm, std::span<const int> edges0, int tag0,
203 std::span<const int> edges1, int tag1);
204
223template <std::ranges::contiguous_range U>
224std::pair<std::vector<std::int32_t>, std::vector<std::ranges::range_value_t<U>>>
225distribute_to_postoffice(MPI_Comm comm, const U& x,
226 std::array<std::int64_t, 2> shape,
227 std::int64_t rank_offset);
228
250template <std::ranges::contiguous_range U>
251std::vector<std::ranges::range_value_t<U>>
252distribute_from_postoffice(MPI_Comm comm, std::span<const std::int64_t> indices,
253 const U& x, std::array<std::int64_t, 2> shape,
254 std::int64_t rank_offset);
255
275template <std::ranges::contiguous_range U>
276std::vector<std::ranges::range_value_t<U>>
277distribute_data(MPI_Comm comm0, std::span<const std::int64_t> indices,
278 MPI_Comm comm1, const U& x, int shape1);
279
283template <typename T>
284MPI_Datatype mpi_datatype()
285{
286 if constexpr (std::same_as<T, float>)
287 return MPI_FLOAT;
288 else if constexpr (std::same_as<T, double>)
289 return MPI_DOUBLE;
290 else if constexpr (std::same_as<T, std::complex<float>>)
291 return MPI_C_FLOAT_COMPLEX;
292 else if constexpr (std::same_as<T, std::complex<double>>)
293 return MPI_C_DOUBLE_COMPLEX;
294 else if constexpr (std::same_as<T, std::int8_t>)
295 return MPI_INT8_T;
296 else if constexpr (std::same_as<T, std::int16_t>)
297 return MPI_INT16_T;
298 else if constexpr (std::same_as<T, std::int32_t>)
299 return MPI_INT32_T;
300 else if constexpr (std::same_as<T, std::int64_t>)
301 return MPI_INT64_T;
302 else if constexpr (std::same_as<T, std::uint8_t>)
303 return MPI_UINT8_T;
304 else if constexpr (std::same_as<T, std::uint16_t>)
305 return MPI_UINT16_T;
306 else if constexpr (std::same_as<T, std::uint32_t>)
307 return MPI_UINT32_T;
308 else
309 {
310 static_assert(std::same_as<T, std::uint64_t>,
311 "No MPI datatype registered for this type.");
312 return MPI_UINT64_T;
313 }
314}
316
319template <typename T>
320MPI_Datatype mpi_t = mpi_datatype<T>();
321
338template <typename T>
340{
341public:
344 explicit Datatype(int count)
345 {
346 // Not error checked, matching the other datatype creation sites in
347 // the library: these are local calls, and under the default
348 // MPI_ERRORS_ARE_FATAL handler a failure aborts before a return code
349 // is visible. There is also no communicator here to abort on --
350 // MPI_COMM_SELF would abort this rank alone and hang the rest.
351 if (count > 1)
352 {
353 MPI_Type_contiguous(count, mpi_t<T>, &_type);
354 MPI_Type_commit(&_type);
355 }
356 }
357
358 // Copy constructor (deleted)
359 Datatype(const Datatype& type) = delete;
360
362 Datatype(Datatype&& type) noexcept : _type(type._type)
363 {
364 type._type = MPI_DATATYPE_NULL;
365 }
366
369 {
370 if (_type != MPI_DATATYPE_NULL)
371 MPI_Type_free(&_type);
372 }
373
374 // Copy assignment (deleted)
375 Datatype& operator=(const Datatype& type) = delete;
376
379 {
380 if (_type != MPI_DATATYPE_NULL)
381 MPI_Type_free(&_type);
382 _type = type._type;
383 type._type = MPI_DATATYPE_NULL;
384 return *this;
385 }
386
389 MPI_Datatype type() const noexcept
390 {
391 return _type == MPI_DATATYPE_NULL ? mpi_t<T> : _type;
392 }
393
394private:
395 // Created contiguous type, or MPI_DATATYPE_NULL if none was created
396 MPI_Datatype _type = MPI_DATATYPE_NULL;
397};
398
415{
416public:
418 Request() = default;
419
421 Request(const Request&) noexcept {}
422
425 : _request(std::exchange(request._request, MPI_REQUEST_NULL))
426 {
427 }
428
430 ~Request() = default;
431
433 Request& operator=(const Request&) noexcept
434 {
435 _request = MPI_REQUEST_NULL;
436 return *this;
437 }
438
441 {
442 _request = std::exchange(request._request, MPI_REQUEST_NULL);
443 return *this;
444 }
445
448 MPI_Request& request() noexcept { return _request; }
449
452 const MPI_Request& request() const noexcept { return _request; }
453
454private:
455 MPI_Request _request = MPI_REQUEST_NULL;
456};
457
458//---------------------------------------------------------------------------
459namespace impl
460{
490std::tuple<std::vector<int>, std::vector<std::int32_t>,
491 std::vector<std::int32_t>>
492postoffice_plan(int size, int rank, std::int32_t shape0_local,
493 std::int64_t shape0, std::int64_t rank_offset);
494
514template <std::ranges::contiguous_range U>
515std::pair<std::vector<std::int32_t>, std::vector<std::ranges::range_value_t<U>>>
516postoffice_exchange(MPI_Comm comm, const U& x,
517 std::array<std::int64_t, 2> shape, std::int64_t rank_offset,
518 std::span<const int> dest,
519 std::span<const std::int32_t> num_items_per_dest0,
520 std::span<const std::int32_t> pos_to_neigh_rank,
521 std::span<const int> src)
522{
523 using T = std::ranges::range_value_t<U>;
524
525 const int size = dolfinx::MPI::size(comm);
526 const int rank = dolfinx::MPI::rank(comm);
527 assert(x.size() % shape[1] == 0);
528 const std::int32_t shape0_local = x.size() / shape[1];
529
530 // Create neighbourhood communicator for sending data to post offices
531 MPI_Comm neigh_comm;
532 int err = MPI_Dist_graph_create_adjacent(
533 comm, src.size(), src.data(), MPI_UNWEIGHTED, dest.size(), dest.data(),
534 MPI_UNWEIGHTED, MPI_INFO_NULL, false, &neigh_comm);
535 dolfinx::MPI::check_error(comm, err);
536
537 // Compute send displacements
538 std::vector<std::int32_t> num_items_per_dest(num_items_per_dest0.begin(),
539 num_items_per_dest0.end());
540 std::vector<std::int32_t> send_disp{0};
541 std::partial_sum(num_items_per_dest.begin(), num_items_per_dest.end(),
542 std::back_inserter(send_disp));
543
544 // Pack send buffers
545 std::vector<T> send_buffer_data(shape[1] * send_disp.back());
546 std::vector<std::int64_t> send_buffer_index(send_disp.back());
547 {
548 std::vector<std::int32_t> send_offsets = send_disp;
549 for (std::int32_t i = 0; i < shape0_local; ++i)
550 {
551 if (int neigh_dest = pos_to_neigh_rank[i]; neigh_dest != -1)
552 {
553 std::size_t pos = send_offsets[neigh_dest];
554 send_buffer_index[pos] = i + rank_offset;
555 std::copy_n(std::next(x.begin(), i * shape[1]), shape[1],
556 std::next(send_buffer_data.begin(), shape[1] * pos));
557 ++send_offsets[neigh_dest];
558 }
559 }
560 }
561
562 // Send number of items to post offices (destination) that I will be
563 // sending
564 std::vector<int> num_items_recv(src.size());
565 num_items_per_dest.reserve(1);
566 num_items_recv.reserve(1);
567 err = MPI_Neighbor_alltoall(num_items_per_dest.data(), 1, MPI_INT,
568 num_items_recv.data(), 1, MPI_INT, neigh_comm);
569 dolfinx::MPI::check_error(comm, err);
570
571 // Prepare receive displacement and buffers
572 std::vector<std::int32_t> recv_disp(num_items_recv.size() + 1, 0);
573 std::partial_sum(num_items_recv.begin(), num_items_recv.end(),
574 std::next(recv_disp.begin()));
575
576 // Send/receive global indices
577 std::vector<std::int64_t> recv_buffer_index(recv_disp.back());
578 err = MPI_Neighbor_alltoallv(
579 send_buffer_index.data(), num_items_per_dest.data(), send_disp.data(),
580 MPI_INT64_T, recv_buffer_index.data(), num_items_recv.data(),
581 recv_disp.data(), MPI_INT64_T, neigh_comm);
582 dolfinx::MPI::check_error(comm, err);
583
584 // Send/receive data (x)
585 MPI_Datatype compound_type;
586 MPI_Type_contiguous(shape[1], dolfinx::MPI::mpi_t<T>, &compound_type);
587 MPI_Type_commit(&compound_type);
588 std::vector<T> recv_buffer_data(shape[1] * recv_disp.back());
589 err = MPI_Neighbor_alltoallv(
590 send_buffer_data.data(), num_items_per_dest.data(), send_disp.data(),
591 compound_type, recv_buffer_data.data(), num_items_recv.data(),
592 recv_disp.data(), compound_type, neigh_comm);
593 dolfinx::MPI::check_error(comm, err);
594 err = MPI_Type_free(&compound_type);
595 dolfinx::MPI::check_error(comm, err);
596 err = MPI_Comm_free(&neigh_comm);
597 dolfinx::MPI::check_error(comm, err);
598
599 // Convert to local indices
600 const std::int64_t r0 = common::local_range(rank, shape[0], size)[0];
601 std::vector<std::int32_t> index_local(recv_buffer_index.size());
602 std::ranges::transform(recv_buffer_index, index_local.begin(),
603 [r0](std::int64_t idx) { return idx - r0; });
604
605 return {index_local, recv_buffer_data};
606}
607} // namespace impl
608
609template <std::ranges::contiguous_range U>
610std::pair<std::vector<std::int32_t>, std::vector<std::ranges::range_value_t<U>>>
611distribute_to_postoffice(MPI_Comm comm, const U& x,
612 std::array<std::int64_t, 2> shape,
613 std::int64_t rank_offset)
614{
615 assert(rank_offset >= 0 or x.empty());
616 assert(x.size() % shape[1] == 0);
617 const std::int32_t shape0_local = x.size() / shape[1];
618
619 logger().debug("Sending data to post offices (distribute_to_postoffice)");
620
621 const int size = dolfinx::MPI::size(comm);
622 const int rank = dolfinx::MPI::rank(comm);
623 auto [dest, num_items_per_dest, pos_to_neigh_rank]
624 = impl::postoffice_plan(size, rank, shape0_local, shape[0], rank_offset);
625
626 // Determine source ranks
627 const std::vector<int> src = MPI::compute_graph_edges_nbx(comm, dest);
628 logger().info(
629 "Number of neighbourhood source ranks in distribute_to_postoffice: {}",
630 src.size());
631
632 auto result
633 = impl::postoffice_exchange(comm, x, shape, rank_offset, dest,
634 num_items_per_dest, pos_to_neigh_rank, src);
635 logger().debug("Completed send data to post offices.");
636 return result;
637}
638//---------------------------------------------------------------------------
639template <std::ranges::contiguous_range U>
640std::vector<std::ranges::range_value_t<U>>
641distribute_from_postoffice(MPI_Comm comm, std::span<const std::int64_t> indices,
642 const U& x, std::array<std::int64_t, 2> shape,
643 std::int64_t rank_offset)
644{
645 assert(rank_offset >= 0 or x.empty());
646 using T = std::ranges::range_value_t<U>;
647
648 common::Timer timer("Distribute row-wise data (scalable)");
649 assert(shape[1] > 0);
650
651 const int size = dolfinx::MPI::size(comm);
652 const int rank = dolfinx::MPI::rank(comm);
653 assert(x.size() % shape[1] == 0);
654 const std::int64_t shape0_local = x.size() / shape[1];
655
656 // 0. Send x data to/from post offices, and 1. determine which post
657 // office ranks hold the data I need (indices) -- these are
658 // independent local computations, so the two NBX consensus
659 // rounds they each need (below) are run concurrently in a single
660 // overlapped round rather than back-to-back.
661
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);
664
665 // Build (src, global index, position) for each entry in 'indices'
666 // not held locally, then sort. Locally-held entries are read
667 // directly below -- skipping them here avoids a wasted round trip.
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)
670 {
671 std::int64_t idx = indices[i];
672 if (idx >= rank_offset and idx < rank_offset + shape0_local)
673 continue;
674 if (int src = dolfinx::MPI::index_owner(size, idx, shape[0]); src != rank)
675 src_to_index.push_back({src, idx, i});
676 }
677
678 // Radix sort on the rank alone (not the full tuple) -- only
679 // grouping by rank matters below; order within a group doesn't.
680 {
681 std::vector<std::int32_t> perm(src_to_index.size());
682 std::iota(perm.begin(), perm.end(), 0);
683 dolfinx::radix_sort(perm, [&src_to_index](std::int32_t i)
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);
690 }
691
692 // Build list of neighbour src ranks and count number of items (rows
693 // of x) to receive from each src post office (by neighbourhood rank)
694 std::vector<std::int32_t> num_items_per_src;
695 std::vector<int> src;
696 {
697 auto it = src_to_index.begin();
698 while (it != src_to_index.end())
699 {
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));
705 it = it1;
706 }
707 }
708
709 // Determine, in one overlapped NBX round, (0) the post office ranks
710 // that hold data for me to receive (post office send round) and (1)
711 // the 'delivery' destination ranks that want data from me (my
712 // request round)
713 auto [post_src, dest] = dolfinx::MPI::compute_graph_edges_nbx(
714 comm, send_dest, static_cast<int>(tag::consensus_nbx), src,
715 static_cast<int>(tag::consensus_nbx) + 1);
716 logger().info(
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);
720
721 // Send receive x data to post office (only for rows that need to be
722 // communicated)
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]);
727
728 // Create neighbourhood communicator for sending data to post offices
729 // (src), and receiving data form my send my post office
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);
734 dolfinx::MPI::check_error(comm, err);
735
736 // Communicate number of requests to each source
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);
742 dolfinx::MPI::check_error(comm, err);
743
744 // Prepare send/receive displacements
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));
751
752 // Pack my requested indices (global) in send buffer ready to send to
753 // post offices
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); });
758
759 // Prepare the receive buffer
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);
765 dolfinx::MPI::check_error(comm, err);
766
767 err = MPI_Comm_free(&neigh_comm0);
768 dolfinx::MPI::check_error(comm, err);
769
770 // 2. Send data (rows of x) from post office back to requesting ranks
771 // (transpose of the preceding communication pattern operation)
772
773 // Build map from local index to post_indices position. Set to -1 for
774 // data that was already on this rank and was therefore was not
775 // sent/received via a postoffice.
776 const std::array<std::int64_t, 2> postoffice_range
777 = common::local_range(rank, shape[0], size);
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)
781 {
782 assert(post_indices[i] < static_cast<int>(post_indices_map.size()));
783 post_indices_map[post_indices[i]] = i;
784 }
785
786 // Build send buffer
787 std::vector<T> send_buffer_data(shape[1] * recv_disp.back());
788 for (std::int32_t i = 0; i < recv_disp.back(); ++i)
789 {
790 std::int64_t index = recv_buffer_index[i];
791 if (index >= rank_offset and index < (rank_offset + shape0_local))
792 {
793 // I already had this index before any communication
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));
797 }
798 else
799 {
800 // Take from my 'post bag'
801 std::int64_t local_index = index - postoffice_range[0];
802 std::int32_t pos = post_indices_map[local_index];
803 assert(pos != -1);
804 std::copy_n(std::next(post_x.begin(), shape[1] * pos), shape[1],
805 std::next(send_buffer_data.begin(), shape[1] * i));
806 }
807 }
808
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);
812 dolfinx::MPI::check_error(comm, err);
813
814 MPI_Datatype compound_type0;
815 MPI_Type_contiguous(shape[1], dolfinx::MPI::mpi_t<T>, &compound_type0);
816 MPI_Type_commit(&compound_type0);
817
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);
823 dolfinx::MPI::check_error(comm, err);
824
825 err = MPI_Type_free(&compound_type0);
826 dolfinx::MPI::check_error(comm, err);
827 err = MPI_Comm_free(&neigh_comm0);
828 dolfinx::MPI::check_error(comm, err);
829
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;
833
834 // Extra data to return
835 std::vector<T> x_new(shape[1] * indices.size());
836 for (std::size_t i = 0; i < indices.size(); ++i)
837 {
838 const std::int64_t index = indices[i];
839 if (index >= rank_offset and index < (rank_offset + shape0_local))
840 {
841 // Had data from the start in x
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));
845 }
846 else if (std::int32_t pos = index_pos_to_buffer[i]; pos != -1)
847 {
848 // In my received post: index_pos_to_buffer[i] != -1 iff
849 // index_owner would say this rank isn't the owner -- avoids
850 // recomputing it.
851 std::copy_n(std::next(recv_buffer_data.begin(), shape[1] * pos), shape[1],
852 std::next(x_new.begin(), shape[1] * i));
853 }
854 else
855 {
856 // In my post office bag
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));
862 }
863 }
864
865 return x_new;
866}
867//---------------------------------------------------------------------------
868template <std::ranges::contiguous_range U>
869std::vector<std::ranges::range_value_t<U>>
870distribute_data(MPI_Comm comm0, std::span<const std::int64_t> indices,
871 MPI_Comm comm1, const U& x, int shape1)
872{
873 if (shape1 <= 0)
874 throw std::invalid_argument("distribute_data: shape1 must be positive");
875 if (x.size() % shape1 != 0)
876 {
877 throw std::invalid_argument(
878 "distribute_data: x.size() must be a multiple of shape1");
879 }
880 const std::int64_t shape0_local = x.size() / shape1;
881
882 // A rank outside comm1 must hold no data. Check this collectively before
883 // the later comm0/comm1 collectives to avoid leaving other ranks blocked.
884 {
885 int invalid_local = (comm1 == MPI_COMM_NULL and !x.empty()) ? 1 : 0;
886 int invalid = 0;
887 int err
888 = MPI_Allreduce(&invalid_local, &invalid, 1, MPI_INT, MPI_MAX, comm0);
889 dolfinx::MPI::check_error(comm0, err);
890 if (invalid)
891 throw std::invalid_argument("Non-empty data on null MPI communicator");
892 }
893
894 std::int64_t shape0 = 0;
895 int err
896 = MPI_Allreduce(&shape0_local, &shape0, 1, MPI_INT64_T, MPI_SUM, comm0);
897 dolfinx::MPI::check_error(comm0, err);
898
899#ifndef NDEBUG
900 {
901 int invalid_local = !std::ranges::all_of(indices, [shape0](std::int64_t i)
902 { return i >= 0 and i < shape0; });
903 int invalid = 0;
904 err = MPI_Allreduce(&invalid_local, &invalid, 1, MPI_INT, MPI_MAX, comm0);
905 dolfinx::MPI::check_error(comm0, err);
906 if (invalid)
907 {
908 throw std::out_of_range(
909 std::format("distribute_data: index outside the global row range "
910 "[0, {}) of the distributed data.",
911 shape0));
912 }
913 }
914#endif
915
916 std::int64_t rank_offset = -1;
917 if (comm1 != MPI_COMM_NULL)
918 {
919 rank_offset = 0;
920 err = MPI_Exscan(&shape0_local, &rank_offset, 1,
921 dolfinx::MPI::mpi_t<std::int64_t>, MPI_SUM, comm1);
922 dolfinx::MPI::check_error(comm1, err);
923 }
924
925 return distribute_from_postoffice(comm0, indices, x, {shape0, shape1},
926 rank_offset);
927}
928//---------------------------------------------------------------------------
929
930} // namespace dolfinx::MPI
Comm(MPI_Comm comm, bool duplicate=true)
Duplicate communicator and wrap duplicate.
Definition MPI.cpp:21
MPI_Comm comm() const noexcept
Return the underlying MPI_Comm object.
Definition MPI.cpp:71
An MPI datatype for count contiguous values of type T, and manage its lifetime.
Definition MPI.h:340
Datatype & operator=(Datatype &&type) noexcept
Move assignment.
Definition MPI.h:378
MPI_Datatype type() const noexcept
The datatype to pass to MPI.
Definition MPI.h:389
Datatype(Datatype &&type) noexcept
Move constructor.
Definition MPI.h:362
Datatype(int count)
Create a datatype for count contiguous values.
Definition MPI.h:344
~Datatype()
Destructor (frees the datatype, if one was created).
Definition MPI.h:368
Request()=default
Create a null request.
Request & operator=(Request &&request) noexcept
Move assignment.
Definition MPI.h:440
Request & operator=(const Request &) noexcept
Copy assignment. Leaves this with no request in flight.
Definition MPI.h:433
MPI_Request & request() noexcept
The handle to pass to MPI.
Definition MPI.h:448
Request(Request &&request) noexcept
Move constructor.
Definition MPI.h:424
Request(const Request &) noexcept
Copy constructor. The copy has no request in flight.
Definition MPI.h:421
~Request()=default
Destructor.
const MPI_Request & request() const noexcept
The handle.
Definition MPI.h:452
Timer for measuring and logging elapsed time durations.
Definition Timer.h:41
MPI support functionality.
Definition MPI.h:35
MPI_Datatype mpi_t
Retrieves the MPI data type associated to the provided type.
Definition MPI.h:320
void check_error(MPI_Comm comm, int code) noexcept
Check MPI error code. If the error code is not equal to MPI_SUCCESS, then std::abort is called.
Definition MPI.cpp:89
std::vector< std::ranges::range_value_t< U > > distribute_from_postoffice(MPI_Comm comm, std::span< const std::int64_t > indices, const U &x, std::array< std::int64_t, 2 > shape, std::int64_t rank_offset)
Fetch rows of a distributed row-major array via their post office ranks.
Definition MPI.h:641
std::vector< int > compute_graph_edges_nbx(MPI_Comm comm, std::span< const int > edges, int tag=static_cast< int >(tag::consensus_nbx))
Determine incoming graph edges using the NBX consensus algorithm.
Definition MPI.cpp:294
constexpr int index_owner(int size, std::size_t index, std::size_t N)
Return which rank owns index in global range [0, N - 1] (inverse of MPI::local_range).
Definition MPI.h:97
std::pair< std::vector< std::int32_t >, std::vector< std::ranges::range_value_t< U > > > distribute_to_postoffice(MPI_Comm comm, const U &x, std::array< std::int64_t, 2 > shape, std::int64_t rank_offset)
Send row data to its 'post office' rank.
Definition MPI.h:611
std::vector< int > compute_graph_edges_pcx(MPI_Comm comm, std::span< const int > edges)
Determine incoming graph edges using the PCX consensus algorithm.
Definition MPI.cpp:104
int size(MPI_Comm comm)
Definition MPI.cpp:81
std::vector< std::ranges::range_value_t< U > > distribute_data(MPI_Comm comm0, std::span< const std::int64_t > indices, MPI_Comm comm1, const U &x, int shape1)
Distribute rows of a row-major array to the ranks that require them, via the post office pattern.
Definition MPI.h:870
int rank(MPI_Comm comm)
Return process rank for the communicator.
Definition MPI.cpp:73
tag
MPI communication tags.
Definition MPI.h:38
constexpr std::array< std::int64_t, 2 > local_range(int index, std::int64_t N, int size)
Partition a global range [0, N - 1] across callers into non-overlapping sub-partitions of almost equa...
Definition local_range.h:26
constexpr void radix_sort(R &&range, P proj={})
Sort a range with radix sorting algorithm. The bucket size is determined by the number of bits to sor...
Definition sort.h:81
auto logger() -> spdlog::logger &
Dolfinx's logger.
Definition log.cpp:24