20#include <kassert/kassert.hpp>
46 template <
typename...>
typename DefaultContainerType = std::vector,
47 template <
typename,
template <
typename...>
typename>
49class Communicator :
public Plugins<Communicator<DefaultContainerType, Plugins...>, DefaultContainerType>... {
53 template <
typename...
Args>
102 other._owns_mpi_comm =
false;
222 "invalid root rank " <<
new_root <<
" in communicator of size " <<
size()
232 "invalid root rank " <<
new_root <<
" in communicator of size " <<
size()
322 template <
typename Ranks>
324 static_assert(std::is_same_v<typename Ranks::value_type, int>,
"Ranks must be of type int");
327 "The set of ranks to include in the new subcommunicator must not be empty."
361 KASSERT(
rank_ranges.size() > 0
ull,
"The set of ranks to include in the new subcommunicator must not be empty.");
364 "The ranks to include in the new subcommunicator must contain own rank."
404 return other_comm.convert_rank_to_communicator(rank, *
this);
442 return rank <
size();
451 mpi_error_hook_impl<Plugins...>(error_code,
callee);
466 template <
typename...
Args>
469 template <
typename...
Args>
472 template <
typename...
Args>
475 template <
typename...
Args>
478 template <
typename...
Args>
481 template <
typename...
Args>
484 template <
typename...
Args>
487 template <
typename...
Args>
490 template <
typename...
Args>
493 template <
typename...
Args>
508 template <
typename...
Args>
511 template <
typename...
Args>
514 template <
typename...
Args>
526 template <
typename...
Args>
529 template <
typename...
Args>
532 template <
typename...
Args>
535 template <
typename...
Args>
538 template <
typename...
Args>
541 template <
typename...
Args>
544 template <
typename...
Args>
547 template <
typename...
Args>
550 template <
typename...
Args>
553 template <
typename...
Args>
556 template <
typename...
Args>
559 template <
typename...
Args>
562 template <
typename...
Args>
565 template <
typename...
Args>
568 template <
typename...
Args>
571 template <
typename...
Args>
574 template <
typename...
Args>
583 template <
typename...
Args>
586 template <
typename...
Args>
589 template <
typename Value>
605 size_t get_mpi_size(MPI_Comm comm)
const {
606 THROWING_KASSERT(comm != MPI_COMM_NULL,
"communicator must be initialized with a valid MPI communicator");
609 MPI_Comm_size(comm, &
size);
610 return asserting_cast<size_t>(
size);
615 template <
typename,
template <
typename...>
typename>
617 template <
typename,
template <
typename...>
typename>
618 typename... RemainingPlugins>
619 void mpi_error_hook_impl(
int const error_code, std::string
const& callee)
const {
620 using PluginType = Plugin<
Communicator<DefaultContainerType, Plugins...>, DefaultContainerType>;
621 if constexpr (has_member_mpi_error_handler_v<PluginType, int, std::string const&>) {
622 static_cast<PluginType const&
>(*this).mpi_error_handler(error_code, callee);
624 if constexpr (
sizeof...(RemainingPlugins) == 0) {
625 mpi_error_hook_impl<void>(error_code, callee);
627 mpi_error_hook_impl<RemainingPlugins...>(error_code, callee);
632 template <
typename =
void>
633 void mpi_error_hook_impl(
int const error_code, std::string
const& callee)
const {
668inline size_t world_rank() {
669 return comm_world().
rank();
675inline int world_rank_signed() {
682inline size_t world_size() {
683 return comm_world().
size();
689inline int world_size_signed() {
Helper functions that make casts safer.
Wrapper for MPI communicator providing access to rank() and size() of the communicator....
Definition communicator.hpp:49
size_t _rank
Rank of the MPI process in this communicator.
Definition communicator.hpp:638
Communicator create_subcommunicators(RankRanges const &rank_ranges) const
Create (sub-)communicators using a sparse representation for the ranks contained in the subcommunicat...
Definition communicator.hpp:360
Communicator(Communicator &&other)
Move constructor.
Definition communicator.hpp:93
size_t rank_shifted_cyclic(int const distance) const
Computes a rank that is some ranks apart from this MPI thread's rank modulo the communicator's size.
Definition communicator.hpp:428
bool is_valid_rank(size_t const rank) const
Checks if a rank is a valid rank for this communicator, i.e., if the rank is in [0,...
Definition communicator.hpp:441
bool is_same_on_all_ranks(Value const &value) const
Checks if all ranks provide the same value to this collective.
Definition is_same_on_all_ranks.hpp:41
MPI_Comm _comm
Corresponding MPI communicator.
Definition communicator.hpp:640
int convert_rank_from_communicator(int const rank, Communicator const &other_comm) const
Convert a rank from another communicator to the rank in this communicator.
Definition communicator.hpp:403
void abort(int errorcode=1) const
Terminates MPI execution environment (on all processes in this Communicator). Beware of MPI implement...
Definition communicator.hpp:142
size_t rank_shifted_checked(int const distance) const
Computes a rank that is distance ranks away from this MPI thread's current rank and checks if this is...
Definition communicator.hpp:415
Communicator()
Default constructor not specifying any MPI communicator and using MPI_COMM_WORLD by default.
Definition communicator.hpp:57
void root(int const new_root)
Set a new root for MPI operations that require a root.
Definition communicator.hpp:219
MPI_Comm disown_mpi_communicator()
Disowns the wrapped MPI_Comm, i.e. it will not be freed in the destructor.
Definition communicator.hpp:198
Communicator split(int const color, int const key=0) const
Split the communicator in different colors.
Definition communicator.hpp:274
size_t rank() const
Rank of the current MPI process in the communicator as size_t.
Definition communicator.hpp:155
bool is_root() const
Check if this rank is the root rank.
Definition communicator.hpp:265
int _default_tag
Default tag value used in point to point communication.
Definition communicator.hpp:643
size_t _size
Number of MPI processes in this communicator.
Definition communicator.hpp:639
virtual ~Communicator()
Destructor that frees the contained MPI_Comm if it is owned by the Communicator.
Definition communicator.hpp:106
int convert_rank_to_communicator(int const rank, Communicator const &other_comm) const
Convert a rank from this communicator to the rank in another communicator.
Definition communicator.hpp:389
Communicator split_to_shared_memory() const
Split the communicator into NUMA nodes.
Definition communicator.hpp:300
bool is_root(size_t const root) const
Check if this rank is the root rank.
Definition communicator.hpp:259
MPI_Comm mpi_communicator() const
MPI communicator corresponding to this communicator.
Definition communicator.hpp:192
int rank_signed() const
Rank of the current MPI process in the communicator as int.
Definition communicator.hpp:149
size_t root() const
Default root for MPI operations that require a root as size_t.
Definition communicator.hpp:239
void root(size_t const new_root)
Set a new root for MPI operations that require a root.
Definition communicator.hpp:229
Communicator create_subcommunicators(Ranks const &ranks_in_own_group) const
Create subcommunicators.
Definition communicator.hpp:323
Communicator & operator=(Communicator &&other)
Move assignment operator.
Definition communicator.hpp:114
int root_signed() const
Default root for MPI operations that require a root as int.
Definition communicator.hpp:245
bool is_root(int const root) const
Check if this rank is the root rank.
Definition communicator.hpp:252
size_t _root
Default root for MPI operations that require a root.
Definition communicator.hpp:642
Communicator(MPI_Comm comm, int root, bool take_ownership=false)
Constructor where an MPI communicator and the default root have to be specified.
Definition communicator.hpp:68
size_t size() const
Number of MPI processes in this communicator as size_t.
Definition communicator.hpp:168
Communicator & operator=(Communicator const &other)
Copy assignment operator. Behaves according to the copy constructor.
Definition communicator.hpp:121
int default_tag() const
Default tag used in point to point communication. The initial value is 0.
Definition communicator.hpp:213
int size_signed() const
Number of MPI processes in this communicator as int.
Definition communicator.hpp:161
CommunicatorComparisonResult compare(Communicator const &other_comm) const
Compare this communicator with another given communicator. Uses MPI_Comm_compare internally.
Definition communicator.hpp:379
bool is_valid_rank(int const rank) const
Checks if a rank is a valid rank for this communicator, i.e., if the rank is in [0,...
Definition communicator.hpp:435
bool _owns_mpi_comm
Definition communicator.hpp:645
size_t num_numa_nodes() const
Number of NUMA nodes (different shared memory regions) in this communicator. This operation is expens...
Definition num_numa_nodes.hpp:11
std::string processor_name() const
Get this 'processor's' name using MPI_Get_processor_name.
Definition communicator.hpp:180
void mpi_error_hook(int const error_code, std::string const &callee) const
If error_code != MPI_SUCCESS, searchs the plugins for a public mpi_error_handler(const int error_code...
Definition communicator.hpp:449
void mpi_error_default_handler(int const error_code, std::string const &function_name) const
Default MPI error callback. Depending on KASSERT_EXCEPTION_MODE either throws a MpiErrorException if ...
Definition communicator.hpp:457
Group group() const
Return the group associated with this communicator.
Definition communicator.hpp:306
Communicator split_by_type(int const type) const
Split the communicator by the specified type (e.g., shared memory)
Definition communicator.hpp:284
void default_tag(int const default_tag)
Set a new default tag used in point to point communication. The initial value is 0.
Definition communicator.hpp:204
Communicator(Communicator const &other)
Copy constructor that duplicates the MPI_Comm and takes ownership of the newly created one in the cop...
Definition communicator.hpp:82
void swap(Communicator &other)
Swaps the Communicator with another Communicator.
Definition communicator.hpp:129
Communicator(MPI_Comm comm, bool take_ownership=false)
Constructor where an MPI communicator has to be specified.
Definition communicator.hpp:62
Wrapper for MPI functions that don't require a communicator. If the template parameter init_finalize_...
Definition environment.hpp:52
A group of MPI processes.
Definition group.hpp:36
STL-compatible allocator for requesting memory using the builtin MPI allocator.
Definition allocator.hpp:32
The exception type used when an MPI call did not return MPI_SUCCESS.
Definition error_handling.hpp:47
RankRanges encapsulate multiple rank ranges which are used in functions like MPI_Group_range_incl/exc...
Definition rank_ranges.hpp:36
Wrapper for MPI functions that don't require a communicator.
An abstraction around MPI_Group.
auto gather(Args... args) const
Wrapper for MPI_Gather.
Definition gather.hpp:80
auto exscan_inplace(Args... args) const
Wrapper for the in-place version of Communicator::exscan().
Definition exscan.hpp:247
auto ibarrier(Args... args) const
Perform a non-blocking barrier synchronization on this communicator using MPI_Ibarrier....
Definition ibarrier.hpp:47
auto scan_single(Args... args) const
Wrapper for MPI_Scan for single elements.
Definition scan.hpp:278
auto exscan_single(Args... args) const
Wrapper for MPI_exscan for single elements.
Definition exscan.hpp:377
auto gatherv(Args... args) const
Wrapper for MPI_Gatherv.
Definition gather.hpp:222
auto allgather_inplace(Args... args) const
Wrapper for the in-place version of MPI_Allgather.
Definition allgather.hpp:209
void barrier(Args... args) const
Perform a MPI_Barrier on this communicator.
Definition barrier.hpp:34
auto bcast(Args... args) const
Wrapper for MPI_Bcast.
Definition bcast.hpp:75
auto scan_inplace(Args... args) const
Wrapper for the in-place version of Communicator::scan().
Definition scan.hpp:190
auto scatter(Args... args) const
Definition scatter.hpp:86
auto scan(Args... args) const
Definition scan.hpp:76
auto reduce(Args... args) const
Definition reduce.hpp:76
auto allreduce_inplace(Args... args) const
Wrapper for the in-place version of Communicator::allreduce().
Definition allreduce.hpp:195
auto reduce_single(Args... args) const
Wrapper for MPI_Reduce.
Definition reduce.hpp:194
auto allreduce(Args... args) const
Wrapper for MPI_Allreduce; which is semantically a reduction followed by a broadcast.
Definition allreduce.hpp:75
auto bcast_single(Args... args) const
Wrapper for MPI_Bcast.
Definition bcast.hpp:246
auto allgather(Args... args) const
Wrapper for MPI_Allgather.
Definition allgather.hpp:78
auto alltoall(Args... args) const
Wrapper for MPI_Alltoall.
Definition alltoall.hpp:86
auto allgatherv(Args... args) const
Wrapper for MPI_Allgatherv.
Definition allgather.hpp:326
auto exscan(Args... args) const
Wrapper for MPI_Exscan.
Definition exscan.hpp:84
auto alltoall_inplace(Args... args) const
Wrapper for the in-place version of Communicator::alltoall.
Definition alltoall.hpp:225
auto alltoallv(Args... args) const
Wrapper for MPI_Alltoallv.
Definition alltoall.hpp:335
auto scatterv(Args... args) const
Wrapper for MPI_Scatterv.
Definition scatter.hpp:328
auto iallreduce(Args... args) const
Wrapper for MPI_Iallreduce.
Definition iallreduce.hpp:76
auto scatter_single(Args... args) const
Definition scatter.hpp:242
auto allreduce_single(Args... args) const
Wrapper for MPI_Allreduce; which is semantically a reduction followed by a broadcast.
Definition allreduce.hpp:278
auto ibsend(Args... args) const
Convenience wrapper for MPI_Ibsend. Calls kamping::Communicator::isend() with the appropriate send mo...
Definition isend.hpp:207
auto issend(Args... args) const
Convenience wrapper for MPI_Issend. Calls kamping::Communicator::isend() with the appropriate send mo...
Definition isend.hpp:219
void bsend(Args... args) const
Convenience wrapper for MPI_Bsend. Calls kamping::Communicator::send() with the appropriate send mode...
Definition send.hpp:183
void send(Args... args) const
Definition send.hpp:68
auto isend(Args... args) const
Definition isend.hpp:80
auto iprobe(Args... args) const
Definition iprobe.hpp:61
void rsend(Args... args) const
Convenience wrapper for MPI_Rsend. Calls kamping::Communicator::send() with the appropriate send mode...
Definition send.hpp:207
auto probe(Args... args) const
Definition probe.hpp:58
auto recv(Args... args) const
Definition recv.hpp:83
auto recv_single(Args... args) const
Convience wrapper for receiving single values via MPI_Recv.
Definition recv.hpp:221
auto try_recv(Args... args) const
Definition try_recv.hpp:80
auto irecv(Args... args) const
Definition irecv.hpp:85
void ssend(Args... args) const
Convenience wrapper for MPI_Ssend. Calls kamping::Communicator::send() with the appropriate send mode...
Definition send.hpp:195
auto irsend(Args... args) const
Convenience wrapper for MPI_Irsend. Calls kamping::Communicator::isend() with the appropriate send mo...
Definition isend.hpp:231
#define KAMPING_MAKE_HAS_MEMBER(Member)
Macro for generating has_member_xxx and has_member_xxx_v templates. They return true if the type give...
Definition has_member.hpp:91
Wrapper for MPI constants.
CommunicatorComparisonResult
Wraps the possible results when calling MPI_Comm_compare on two MPI communicators comm1 and comm2.
Definition mpi_constants.hpp:22
Utility that maps C++ types to types that can be understood by MPI.
Definitions for builtin MPI operations.
Factory methods for buffer wrappers.
Defines utility classes for communicator creation using range based ranks descriptions.
An unused template parameter.
Definition named_parameters.hpp:36