MPI objects¶
MPI objects come in a non-owning form (*_view), which is cheap to copy and never frees the object, and an owning,
move-only form that frees it on destruction. Every function that takes an MPI object also accepts the native handle
(e.g. MPI_Comm) and your own wrapper types; see Customization points and MPI objects.
With MPI_ERRORS_RETURN set on a communicator, errors are thrown as mpi_error; the default
MPI_ERRORS_ARE_FATAL terminates the program.
Environment¶
-
class environment¶
RAII wrapper that calls
MPI_Init_threadon construction andMPI_Finalizeon destruction.Construct one instance at the top of
main. The destructor guards against double-finalization. This class is non-copyable and non-movable: it owns global MPI state.int main(int argc, char** argv) { kamping::v2::environment env(argc, argv); // single-threaded kamping::v2::environment env(argc, argv, ThreadLevel::multiple); // threaded variant }
Defined in header
<kamping/v2/environment.hpp>
Communicators¶
-
class comm_view¶
Non-owning wrapper around an
MPI_Comm.Satisfies
convertible_to_mpi_handle<MPI_Comm>. Does not free the communicator on destruction — use the owningcommfor that. Provides read-only accessors and.dup()/.split()viacomm_accessors.Defined in header
<kamping/v2/comm.hpp>-
comm_view(MPI_Comm c) noexcept¶
Construct from a raw
MPI_Comm. The communicator must outlive this view.
-
int rank() const¶
- Returns:
The rank of the calling process in this communicator.
-
int size() const¶
- Returns:
The number of processes in this communicator.
-
group group() const¶
Extract the group associated with this communicator.
The returned
groupis owned by the caller.- Throws:
mpi_error– ifMPI_Comm_groupfails.
-
comm dup() const¶
Collective: duplicate this communicator (MPI_Comm_dup).
All processes in the communicator must call this collectively. Delegates to the free function
kamping::v2::dup.- Throws:
mpi_error– ifMPI_Comm_dupfails.
-
comm split(int color, int key = 0) const¶
Collective: split this communicator by color and key (MPI_Comm_split).
Processes with the same
colorform a sub-communicator ordered bykey. PassMPI_UNDEFINEDascolorto opt out — result isMPI_COMM_NULL. Delegates to the free functionkamping::v2::split.- Throws:
mpi_error– ifMPI_Comm_splitfails.
-
comm_view(MPI_Comm c) noexcept¶
-
class comm¶
Owning RAII wrapper for
MPI_Comm.Move-only. Calls
MPI_Comm_freeon destruction unless the stored handle isMPI_COMM_NULL.Invariant: only communicators created by the caller (via
MPI_Comm_dup,MPI_Comm_split,MPI_Comm_create_from_group, etc.) should be passed tofrom_native. Never adoptMPI_COMM_WORLDorMPI_COMM_SELF.Satisfies
convertible_to_mpi_handle<MPI_Comm>.Defined in header
<kamping/v2/comm.hpp>-
comm() noexcept = default¶
(1) -
comm(comm &&o) noexcept¶
(2) (1) Default-construct a null communicator (holds
MPI_COMM_NULL).Useful as an output parameter for the free functions
dup()/split():comm out; kamping::v2::dup(source, out);
(2) Move constructor — transfers ownership; moved-from holds
MPI_COMM_NULL.
-
comm &operator=(comm &&o) noexcept¶
Move assignment — frees the current handle then transfers ownership.
-
~comm() noexcept¶
Free the communicator (unless moved from or null).
-
void free()¶
Explicitly free the communicator.
After this call
*thisholdsMPI_COMM_NULL— the destructor becomes a no-op. Use this instead of relying on the destructor when you need to handle errors fromMPI_Comm_free.The handle is exchanged to
MPI_COMM_NULLbefore the MPI call so the destructor cannot double-free even if this throws.- Throws:
mpi_error– ifMPI_Comm_freefails.
-
MPI_Comm release() noexcept¶
Relinquish ownership; returns the raw
MPI_Commhandle.Leaves
*thisasMPI_COMM_NULL. The caller is responsible for callingMPI_Comm_freeon the returned handle.
-
operator comm_view() const noexcept¶
Implicit conversion to a non-owning view.
Allows passing a
commwherever acomm_viewis expected without an explicit cast. The view borrows; it does not extend lifetime.
-
int rank() const¶
- Returns:
The rank of the calling process in this communicator.
-
int size() const¶
- Returns:
The number of processes in this communicator.
-
group group() const¶
Extract the group associated with this communicator.
The returned
groupis owned by the caller.- Throws:
mpi_error– ifMPI_Comm_groupfails.
-
comm dup() const¶
Collective: duplicate this communicator (MPI_Comm_dup).
All processes in the communicator must call this collectively. Delegates to the free function
kamping::v2::dup.- Throws:
mpi_error– ifMPI_Comm_dupfails.
-
comm split(int color, int key = 0) const¶
Collective: split this communicator by color and key (MPI_Comm_split).
Processes with the same
colorform a sub-communicator ordered bykey. PassMPI_UNDEFINEDascolorto opt out — result isMPI_COMM_NULL. Delegates to the free functionkamping::v2::split.- Throws:
mpi_error– ifMPI_Comm_splitfails.
-
static comm from_native(MPI_Comm c) noexcept¶
Adopt an already-created
MPI_Comm(takes ownership).Do not pass predefined communicators (
MPI_COMM_WORLD,MPI_COMM_SELF).
-
template<convertible_to_mpi_handle<MPI_Group> Group, convertible_to_mpi_handle<MPI_Info> Info = MPI_Info>
static comm from_group(Group const &g, std::string_view tag = "", Info info = MPI_INFO_NULL)¶ Collective: create a new communicator from a group.
Calls
MPI_Comm_create_from_group. All processes ingmust call this collectively with the sametag.- Template parameters:
Group– Any type satisfyingconvertible_to_mpi_handle<MPI_Group>: rawMPI_Group,group,group_view, or any third-party wrapper.Info– Any type satisfyingconvertible_to_mpi_handle<MPI_Info>.
- Parameters:
g– Group defining the new communicator’s process set.tag– Application-defined string tag (must match across all callers).info– MPI info hints (defaults toMPI_INFO_NULL).
- Throws:
mpi_error– if the MPI call fails.
-
comm() noexcept = default¶
The free functions dup and split do the same as the members, and can also write into an existing comm:
-
template<convertible_to_mpi_handle<MPI_Comm> Comm = MPI_Comm, convertible_to_mpi_handle_ptr<MPI_Comm> NewComm = MPI_Comm*>
void dup(Comm const &c, NewComm &new_comm)¶
(1) -
template<convertible_to_mpi_handle<MPI_Comm> Comm = MPI_Comm>
comm dup(Comm const &c)¶
(2) (1) Collective: duplicate a communicator (MPI_Comm_dup).
(2) Collective: duplicate a communicator, returning an owning
comm.- Template parameters:
Comm– Any type satisfyingconvertible_to_mpi_handle<MPI_Comm>.NewComm– Any type satisfyingconvertible_to_mpi_handle_ptr<MPI_Comm>.
- Parameters:
c– Input communicator.new_comm– Output: receives the duplicated communicator handle.
- Throws:
mpi_error– ifMPI_Comm_dupfails.
Defined in header
<kamping/v2/comm.hpp>
-
template<convertible_to_mpi_handle<MPI_Comm> Comm = MPI_Comm, convertible_to_mpi_handle_ptr<MPI_Comm> NewComm = MPI_Comm*>
void split(Comm const &c, int color, int key, NewComm &new_comm)¶
(1) -
template<convertible_to_mpi_handle<MPI_Comm> Comm = MPI_Comm>
comm split(Comm const &c, int color, int key = 0)¶
(2) (1) Collective: split a communicator by color and key (MPI_Comm_split).
Processes with the same
colorform a sub-communicator, ordered bykey. PassMPI_UNDEFINEDascolorto opt out — the output handle is set toMPI_COMM_NULLfor that process.(2) Collective: split a communicator, returning an owning
comm.- Template parameters:
Comm– Any type satisfyingconvertible_to_mpi_handle<MPI_Comm>.NewComm– Any type satisfyingconvertible_to_mpi_handle_ptr<MPI_Comm>.
- Parameters:
c– Input communicator.new_comm– Output: receives the new sub-communicator handle.
- Throws:
mpi_error– ifMPI_Comm_splitfails.
Defined in header
<kamping/v2/comm.hpp>
Groups¶
-
class group_view¶
Non-owning wrapper around an
MPI_Group.Satisfies
convertible_to_mpi_handle<MPI_Group>. Does not free the group on destruction — use the owninggroupfor that.Defined in header
<kamping/v2/group.hpp>-
explicit group_view(MPI_Group g) noexcept¶
Construct from a raw
MPI_Group. The group must outlive this view.
-
int size() const¶
- Returns:
Number of processes in the group.
-
std::optional<int> rank() const¶
- Returns:
Rank of the calling process in this group, or
std::nulloptif the calling process is not a member (MPI_UNDEFINED).
-
bool contains_self() const¶
- Returns:
trueif the calling process is a member of this group.
-
GroupEquality compare(group_view other) const¶
Compare two groups.
- Returns:
GroupEquality::Identicalif same object,Similarif same processes in different order,Unequalotherwise.
-
explicit group_view(MPI_Group g) noexcept¶
-
class group¶
Owning RAII wrapper for
MPI_Group.Move-only. Calls
MPI_Group_freeon destruction unless the stored handle isMPI_GROUP_EMPTY(the predefined empty group, which must not be freed).Satisfies
convertible_to_mpi_handle<MPI_Group>.Defined in header
<kamping/v2/group.hpp>-
group() noexcept = default¶
(1) -
group(group &&o) noexcept¶
(2) (1) Default-construct (holds
MPI_GROUP_EMPTY).(2) Move constructor — transfers ownership; moved-from holds
MPI_GROUP_EMPTY.
-
group &operator=(group &&o) noexcept¶
Move assignment — frees the current handle then transfers ownership.
-
~group() noexcept¶
Free the group (unless it is
MPI_GROUP_EMPTYor moved from).
-
MPI_Group release() noexcept¶
Relinquish ownership; returns the raw
MPI_Grouphandle.Leaves
*thisasMPI_GROUP_EMPTY. The caller is responsible for callingMPI_Group_freeon the returned handle.
-
operator group_view() const noexcept¶
Implicit conversion to a non-owning view.
Allows passing a
groupwherever agroup_viewis expected without an explicit cast. The view borrows; it does not extend lifetime.
-
int size() const¶
- Returns:
Number of processes in the group.
-
std::optional<int> rank() const¶
- Returns:
Rank of the calling process in this group, or
std::nulloptif the calling process is not a member (MPI_UNDEFINED).
-
bool contains_self() const¶
- Returns:
trueif the calling process is a member of this group.
-
GroupEquality compare(group_view other) const¶
Compare two groups.
- Returns:
GroupEquality::Identicalif same object,Similarif same processes in different order,Unequalotherwise.
-
static group empty() noexcept¶
Return an owning wrapper around
MPI_GROUP_EMPTY.The predefined empty group is never freed —
free_if_validskips it.
-
static group from_native(MPI_Group g) noexcept¶
Adopt an already-created
MPI_Grouphandle (takes ownership).Use when you have called an MPI routine that returns an
MPI_Groupand you want RAII management. Do not passMPI_GROUP_EMPTY(it is never freed anyway, but usinggroup::empty()is clearer).
-
group() noexcept = default¶
Status¶
-
class status¶
Owning wrapper around
MPI_Status.All fields are undefined until the object is passed to (or filled by) an MPI communication function. Satisfies both
convertible_to_mpi_handle<MPI_Status>andconvertible_to_mpi_handle_ptr<MPI_Status>.Defined in header
<kamping/v2/status.hpp>-
status()¶
(1) -
explicit status(MPI_Status s)¶
(2) (1) Default-construct. All fields are undefined until filled by MPI.
(2) Construct from an existing
MPI_Status.
-
MPI_Status &native()¶
(1) -
MPI_Status const &native() const¶
(2) - Returns:
(1) Reference to the underlying
MPI_Status(escape hatch).(2) Const reference to the underlying
MPI_Status(escape hatch).
-
operator status_view() noexcept¶
Implicit conversion to a non-owning view.
Allows passing a
statuswherever astatus_viewis expected without an explicit cast. The view borrows; it does not extend lifetime.
-
operator const_status_view() const noexcept¶
Implicit conversion to a read-only, non-owning view.
-
int source() const¶
- Returns:
The source rank.
-
int tag() const¶
- Returns:
The tag.
-
std::optional<int> count(MPI_Datatype datatype) const¶
(1) -
template<typename T>
requires kamping::types::is_builtin_type_v<T>
std::optional<int> count() const¶
(2) - Template parameters:
T– A builtin MPI-mappable type.
- Parameters:
datatype– The MPI datatype.
- Returns:
(1) The number of top-level elements of the given datatype in the message. In cases where MPI_Get_count would return MPI_UNDEFINED,
std::nulloptis returned.(2) The number of top-level elements of type
Tin the message. In cases where MPI_Get_count would return MPI_UNDEFINED,std::nulloptis returned.
-
status()¶
-
class status_view¶
Non-owning view over an existing
MPI_Status.Satisfies
convertible_to_mpi_handle_ptr<MPI_Status>so it can be passed directly to operations that accept a status out-parameter.Defined in header
<kamping/v2/status.hpp>-
explicit status_view(MPI_Status *status)¶
Construct from a pointer to an existing MPI_Status. Must not be null.
-
MPI_Status *native() const¶
- Returns:
Pointer to the underlying
MPI_Status(escape hatch).
-
int source() const¶
- Returns:
The source rank.
-
int tag() const¶
- Returns:
The tag.
-
std::optional<int> count(MPI_Datatype datatype) const¶
(1) -
template<typename T>
requires kamping::types::is_builtin_type_v<T>
std::optional<int> count() const¶
(2) - Template parameters:
T– A builtin MPI-mappable type.
- Parameters:
datatype– The MPI datatype.
- Returns:
(1) The number of top-level elements of the given datatype in the message. In cases where MPI_Get_count would return MPI_UNDEFINED,
std::nulloptis returned.(2) The number of top-level elements of type
Tin the message. In cases where MPI_Get_count would return MPI_UNDEFINED,std::nulloptis returned.
-
explicit status_view(MPI_Status *status)¶
-
class const_status_view¶
Read-only, non-owning view over an existing
MPI_Status.Provides the read accessors (
source(),tag(),count()) for a status that must not be written, e.g. an element of aconst status_vector. Satisfiesconvertible_to_mpi_handle<MPI_Status>but, being read-only, notconvertible_to_mpi_handle_ptr<MPI_Status>.Defined in header
<kamping/v2/status.hpp>-
explicit const_status_view(MPI_Status const *status)¶
(1) -
const_status_view(status_view view) noexcept¶
(2) (1) Construct from a pointer to an existing MPI_Status. Must not be null.
(2) Implicit conversion from a mutable view.
-
MPI_Status const *native() const¶
- Returns:
Pointer to the underlying
MPI_Status(escape hatch).
-
int source() const¶
- Returns:
The source rank.
-
int tag() const¶
- Returns:
The tag.
-
std::optional<int> count(MPI_Datatype datatype) const¶
(1) -
template<typename T>
requires kamping::types::is_builtin_type_v<T>
std::optional<int> count() const¶
(2) - Template parameters:
T– A builtin MPI-mappable type.
- Parameters:
datatype– The MPI datatype.
- Returns:
(1) The number of top-level elements of the given datatype in the message. In cases where MPI_Get_count would return MPI_UNDEFINED,
std::nulloptis returned.(2) The number of top-level elements of type
Tin the message. In cases where MPI_Get_count would return MPI_UNDEFINED,std::nulloptis returned.
-
explicit const_status_view(MPI_Status const *status)¶
-
class status_vector¶
Growable array of MPI statuses that mirrors
std::vector, but whose element access yieldsstatus_views (orconst_status_views, through a const vector) instead of rawMPI_Statuses.The statuses are stored contiguously, so the whole array can be passed as the statuses argument of the multiple-completion functions (
waitall,waitsome, …) without copying; it exposes them viampi_handle_range()rather thandata(), since its elements as a range are views, notMPI_Statuses.request_vectorrequests;status_vectorstatuses(requests.size()); waitall(requests, statuses); int source = statuses[0].source();Unlike
request_vector, a status is a plain value with no MPI resource attached, sostatus_vectoris copyable and dropping elements needs no completion. Views are invalidated by reallocation, as withstd::vector.Defined in header
<kamping/v2/status.hpp>-
explicit status_vector(size_type n)¶
Constructs
nstatuses. Their fields are undefined until filled in by MPI.
-
void reserve(size_type n)¶
Reserves storage for
nstatuses. Invalidates all views if it reallocates.
-
void resize(size_type n)¶
Resizes to
nstatuses. Invalidates all views if it reallocates.
-
status_view operator[](size_type i) noexcept¶
(1) -
const_status_view operator[](size_type i) const noexcept¶
(2) - Returns:
(1) A view of the status at index
i. Precondition:i < size().(2) A read-only view of the status at index
i. Precondition:i < size().
-
explicit status_vector(size_type n)¶
Info objects¶
-
class info_view¶
Non-owning view over an existing
MPI_Info.Does not free the handle on destruction. Use for
MPI_INFO_NULL, handles borrowed from the MPI implementation, or anywhere lifetimes are managed externally.Satisfies
convertible_to_mpi_handle<MPI_Info>so it can be passed directly to any operation that accepts an info argument.Defined in header
<kamping/v2/info.hpp>-
explicit info_view(MPI_Info info) noexcept¶
Wrap
infowithout taking ownership. The handle must outlive this view.
-
int nkeys() const¶
- Returns:
Number of key–value pairs.
-
std::string nth_key(int n) const¶
Return the
n-thkey (0-indexed), null-truncated at the first\0.
-
std::optional<std::string> get(std::string_view key) const¶
(1) -
template<typename T>
std::optional<T> get(std::string_view key) const¶
(2) (1) Look up
key; returnsstd::nulloptif the key is absent.(2) Type-safe get. Requires
info_value_traits<T>to be specialized.
-
bool contains(std::string_view key) const¶
- Returns:
trueifkeyis present.
-
void set(std::string_view key, std::string_view value)¶
(1) -
template<typename T>
requires (!std::convertible_to<T, std::string_view>)
void set(std::string_view key, T const &value)¶
(2) (1) Set
keytovalue(creates or overwrites).(2) Type-safe set. Requires
info_value_traits<T>to be specialized.Note: this overload is excluded when
Tis already implicitly convertible tostd::string_view— the non-template overload above is preferred then.
-
void erase(std::string_view key)¶
Remove
key.- Throws:
mpi_error– if the MPI call fails (including when the key is absent).
-
entry_iterator begin() const¶
Iterator to the first key–value pair.
-
entry_sentinel end() const¶
Sentinel marking the end of iteration.
-
auto entries() const¶
Lazy range of all (key, value) pairs.
Returns a
std::ranges::subrangethat can be passed directly to range algorithms and supports structured bindings in range-for:for (auto [key, val] : my_info.entries()) { ... } auto n = std::ranges::distance(my_info.entries());
-
explicit info_view(MPI_Info info) noexcept¶
-
class info¶
Owning RAII wrapper for
MPI_Info.Calls
MPI_Info_createon construction andMPI_Info_freeon destruction. Move-only: if you need an independent copy, calldup()explicitly.Satisfies
convertible_to_mpi_handle<MPI_Info>so it can be passed directly to any operation that accepts an info argument.Defined in header
<kamping/v2/info.hpp>-
info()¶
(1) -
info(info &&other) noexcept¶
(2) (1) Create a new empty info object via
MPI_Info_create.(2) Move constructor — transfers ownership; the moved-from object holds
MPI_INFO_NULL.- Throws:
mpi_error– if the MPI call fails.
-
info &operator=(info &&other) noexcept¶
Move assignment — frees the current handle then transfers ownership.
-
~info() noexcept¶
Free the info object (unless moved from).
-
void free()¶
Explicitly free the info object.
After this call
*thisholdsMPI_INFO_NULL— the destructor becomes a no-op. Use this instead of relying on the destructor when you need to handle errors fromMPI_Info_free.The handle is exchanged to
MPI_INFO_NULLbefore the MPI call so the destructor cannot double-free even if this throws.- Throws:
mpi_error– ifMPI_Info_freefails.
-
MPI_Info release() noexcept¶
Relinquish ownership; returns the raw
MPI_Infohandle.Leaves
*thisasMPI_INFO_NULL. The caller is responsible for callingMPI_Info_freeon the returned handle.
-
operator info_view() const noexcept¶
Implicit conversion to a non-owning view.
Allows passing an
infowherever aninfo_viewis expected without an explicit cast. The view borrows; it does not extend lifetime.
-
int nkeys() const¶
- Returns:
Number of key–value pairs.
-
std::string nth_key(int n) const¶
Return the
n-thkey (0-indexed), null-truncated at the first\0.
-
std::optional<std::string> get(std::string_view key) const¶
(1) -
template<typename T>
std::optional<T> get(std::string_view key) const¶
(2) (1) Look up
key; returnsstd::nulloptif the key is absent.(2) Type-safe get. Requires
info_value_traits<T>to be specialized.
-
bool contains(std::string_view key) const¶
- Returns:
trueifkeyis present.
-
void set(std::string_view key, std::string_view value)¶
(1) -
template<typename T>
requires (!std::convertible_to<T, std::string_view>)
void set(std::string_view key, T const &value)¶
(2) (1) Set
keytovalue(creates or overwrites).(2) Type-safe set. Requires
info_value_traits<T>to be specialized.Note: this overload is excluded when
Tis already implicitly convertible tostd::string_view— the non-template overload above is preferred then.
-
void erase(std::string_view key)¶
Remove
key.- Throws:
mpi_error– if the MPI call fails (including when the key is absent).
-
entry_iterator begin() const¶
Iterator to the first key–value pair.
-
entry_sentinel end() const¶
Sentinel marking the end of iteration.
-
auto entries() const¶
Lazy range of all (key, value) pairs.
Returns a
std::ranges::subrangethat can be passed directly to range algorithms and supports structured bindings in range-for:for (auto [key, val] : my_info.entries()) { ... } auto n = std::ranges::distance(my_info.entries());
-
info()¶
-
template<typename T>
struct info_value_traits¶ Defined in header
<mpi/info.hpp>
Sessions¶
-
class session_view¶
Non-owning wrapper around an
MPI_Session.Does not finalize the session on destruction — use the owning
sessionfor that.Defined in header
<kamping/v2/session.hpp>-
explicit session_view(MPI_Session s) noexcept¶
Construct from a raw
MPI_Session. The session must outlive this view.
-
std::optional<group> group_from_pset(std::string_view pset_name) const¶
Create an owned group from a named process set.
Calls
MPI_Group_from_session_pset. Returnsstd::nulloptif the calling process is not a member of the named process set — MPI sets the output group toMPI_GROUP_EMPTYin that case. Returns the owned group otherwise.This is a local (non-collective) operation.
- Parameters:
pset_name– Name of the process set (e.g.,"mpi://WORLD").
- Throws:
mpi_error– if the MPI call fails (e.g., unknown pset name).
- Returns:
The owned group, or
std::nulloptif this process is not a member.
-
template<convertible_to_mpi_handle<MPI_Info> Info = MPI_Info>
std::optional<comm> comm_from_pset(std::string_view pset_name, std::string_view tag = "", Info info = MPI_INFO_NULL) const¶ Collective: create a communicator from a named process set.
Convenience shortcut for:
auto g = s.group_from_pset(pset_name); if (g) auto c = comm::from_group(*g, tag, info);
Returns
std::nulloptif the calling process is not a member of the pset. All processes that are members must call this collectively with the sametag. Processes that receivestd::nulloptmust not call this.- Template parameters:
Info– Any type satisfyingconvertible_to_mpi_handle<MPI_Info>: rawMPI_Info,info,info_view, or any third-party wrapper.
- Parameters:
pset_name– Name of the process set (e.g.,"mpi://WORLD").tag– Application-defined string tag (must match across all callers).info– MPI info hints (defaults toMPI_INFO_NULL).
- Throws:
mpi_error– if either MPI call fails.
- Returns:
The new communicator, or
std::nulloptif this process is not a member.
-
template<convertible_to_mpi_handle<MPI_Info> Info = MPI_Info>
pset_range<Info> psets(Info info = MPI_INFO_NULL) const¶ Enumerate all process set names visible to this session.
Returns a lazy input range.
MPI_Session_get_num_psetsis called once when iteration begins; each increment fetches the next name on demand viaMPI_Session_get_nth_pset. No names are loaded until iteration starts.infois stored by value inside the returned range, so owning wrappers are kept alive for the range’s lifetime.- Template parameters:
Info– Any type satisfyingconvertible_to_mpi_handle<MPI_Info>.
- Parameters:
info– Optional MPI info hints (defaults toMPI_INFO_NULL).
-
explicit session_view(MPI_Session s) noexcept¶
-
class session¶
Owning RAII wrapper for
MPI_Session(MPI-4 sessions model).Move-only. Calls
MPI_Session_finalizeon destruction unless the stored handle isMPI_SESSION_NULLor MPI has already been finalized.Unlike
kamping::v2::environment, a session does not requireMPI_Initto have been called — the sessions model allows MPI to be initialized via sessions alone. When used alongside the traditional model, the session should be finalized beforeMPI_Finalize.namespace psets = kamping::v2::psets; kamping::v2::session s; // Create a comm directly from a pset name (most concise): auto c = s.comm_from_pset(psets::world); // Or work through the group explicitly: auto g = s.group_from_pset(psets::world); // std::optional<group> if (g) { auto c2 = comm::from_group(*g, "my-tag"); }
Defined in header
<kamping/v2/session.hpp>-
session()¶
(1) -
session(session &&o) noexcept¶
(2) (1) Initialize a session with default info and the return-errors error handler.
(2) Move constructor — transfers ownership; moved-from holds
MPI_SESSION_NULL.- Throws:
mpi_error– ifMPI_Session_initfails.
-
session &operator=(session &&o) noexcept¶
Move assignment — finalizes the current session then transfers ownership.
-
~session() noexcept¶
Finalize the session unless already done or MPI is already finalized.
-
void finalize()¶
Explicitly finalize the session.
After this call
*thisholdsMPI_SESSION_NULL— the destructor becomes a no-op. Use this instead of relying on the destructor when you need to handle errors fromMPI_Session_finalize.The handle is exchanged to
MPI_SESSION_NULLbefore the MPI call so the destructor cannot double-free even if this throws.- Throws:
mpi_error– ifMPI_Session_finalizefails.
-
MPI_Session release() noexcept¶
Relinquish ownership; returns the raw
MPI_Sessionhandle.Leaves
*thisasMPI_SESSION_NULL. The caller is responsible for callingMPI_Session_finalizeon the returned handle.
-
operator session_view() const noexcept¶
Implicit conversion to a non-owning view.
-
std::optional<group> group_from_pset(std::string_view pset_name) const¶
Create an owned group from a named process set.
Calls
MPI_Group_from_session_pset. Returnsstd::nulloptif the calling process is not a member of the named process set — MPI sets the output group toMPI_GROUP_EMPTYin that case. Returns the owned group otherwise.This is a local (non-collective) operation.
- Parameters:
pset_name– Name of the process set (e.g.,"mpi://WORLD").
- Throws:
mpi_error– if the MPI call fails (e.g., unknown pset name).
- Returns:
The owned group, or
std::nulloptif this process is not a member.
-
template<convertible_to_mpi_handle<MPI_Info> Info = MPI_Info>
std::optional<comm> comm_from_pset(std::string_view pset_name, std::string_view tag = "", Info info = MPI_INFO_NULL) const¶ Collective: create a communicator from a named process set.
Convenience shortcut for:
auto g = s.group_from_pset(pset_name); if (g) auto c = comm::from_group(*g, tag, info);
Returns
std::nulloptif the calling process is not a member of the pset. All processes that are members must call this collectively with the sametag. Processes that receivestd::nulloptmust not call this.- Template parameters:
Info– Any type satisfyingconvertible_to_mpi_handle<MPI_Info>: rawMPI_Info,info,info_view, or any third-party wrapper.
- Parameters:
pset_name– Name of the process set (e.g.,"mpi://WORLD").tag– Application-defined string tag (must match across all callers).info– MPI info hints (defaults toMPI_INFO_NULL).
- Throws:
mpi_error– if either MPI call fails.
- Returns:
The new communicator, or
std::nulloptif this process is not a member.
-
template<convertible_to_mpi_handle<MPI_Info> Info = MPI_Info>
pset_range<Info> psets(Info info = MPI_INFO_NULL) const¶ Enumerate all process set names visible to this session.
Returns a lazy input range.
MPI_Session_get_num_psetsis called once when iteration begins; each increment fetches the next name on demand viaMPI_Session_get_nth_pset. No names are loaded until iteration starts.infois stored by value inside the returned range, so owning wrappers are kept alive for the range’s lifetime.- Template parameters:
Info– Any type satisfyingconvertible_to_mpi_handle<MPI_Info>.
- Parameters:
info– Optional MPI info hints (defaults toMPI_INFO_NULL).
-
session()¶
Windows¶
-
class win_view¶
Non-owning wrapper around an
MPI_Win.Satisfies
convertible_to_mpi_handle<MPI_Win>. Does not free the window on destruction — use the owningwinfor that. Provides.group()/.fence()viawin_accessors.Defined in header
<kamping/v2/win.hpp>-
win_view(MPI_Win w) noexcept¶
Construct from a raw
MPI_Win. The window must outlive this view.
-
group group() const¶
Extract the group of processes sharing this window.
The returned
groupis owned by the caller.- Throws:
mpi_error– ifMPI_Win_get_groupfails.
-
void fence(int assertion = 0) const¶
Collective: synchronize an active-target epoch (
MPI_Win_fence).Delegates to the free function
kamping::v2::fence.- Throws:
mpi_error– ifMPI_Win_fencefails.
-
win_view(MPI_Win w) noexcept¶
-
class win¶
Owning RAII wrapper for
MPI_Win.Move-only. Calls
MPI_Win_freeon destruction unless the stored handle isMPI_WIN_NULL. Freeing a window is collective, so all processes of the window’s group must destroy (orfree) it at the same point.Satisfies
convertible_to_mpi_handle<MPI_Win>andconvertible_to_mpi_handle_ptr<MPI_Win>.Defined in header
<kamping/v2/win.hpp>-
win() noexcept = default¶
(1) -
win(win &&o) noexcept¶
(2) (1) Default-construct a null window (holds
MPI_WIN_NULL).Useful as an output parameter for the Level 1
win_allocate().(2) Move constructor — transfers ownership; moved-from holds
MPI_WIN_NULL.
-
win &operator=(win &&o) noexcept¶
Move assignment — frees the current window then transfers ownership.
-
~win() noexcept¶
Free the window (unless moved from or null).
-
void free()¶
Explicitly free the window.
After this call
*thisholdsMPI_WIN_NULL— the destructor becomes a no-op. Use this instead of relying on the destructor when you need to handle errors fromMPI_Win_free.The handle is exchanged to
MPI_WIN_NULLbefore the MPI call so the destructor cannot double-free even if this throws.- Throws:
mpi_error– ifMPI_Win_freefails.
-
MPI_Win release() noexcept¶
Relinquish ownership; returns the raw
MPI_Winhandle.Leaves
*thisasMPI_WIN_NULL. The caller is responsible for callingMPI_Win_freeon the returned handle.
-
operator win_view() const noexcept¶
Implicit conversion to a non-owning view.
The view borrows; it does not extend lifetime.
-
group group() const¶
Extract the group of processes sharing this window.
The returned
groupis owned by the caller.- Throws:
mpi_error– ifMPI_Win_get_groupfails.
-
void fence(int assertion = 0) const¶
Collective: synchronize an active-target epoch (
MPI_Win_fence).Delegates to the free function
kamping::v2::fence.- Throws:
mpi_error– ifMPI_Win_fencefails.
-
win() noexcept = default¶
-
template<typename T, convertible_to_mpi_handle<MPI_Comm> Comm = MPI_Comm, convertible_to_mpi_handle_ptr<MPI_Win> NewWin = MPI_Win*, convertible_to_mpi_handle<MPI_Info> Info = MPI_Info>
requires std::is_trivially_copyable_v<T>
void win_allocate(std::size_t count, Comm const &c, T *&base, NewWin &new_win, Info const &info = MPI_INFO_NULL)¶
(1) -
template<typename T, convertible_to_mpi_handle<MPI_Comm> Comm = MPI_Comm, convertible_to_mpi_handle<MPI_Info> Info = MPI_Info>
requires std::is_trivially_copyable_v<T>
std::pair<win, std::span<T>> win_allocate(std::size_t count, Comm const &c, Info const &info = MPI_INFO_NULL)¶
(2) (1) Collective: allocate a window exposing
countelements ofTon each process (MPI_Win_allocate).The displacement unit is
sizeof(T), so target displacements count elements ofT. The memory is owned by MPI, valid until the window is freed, and uninitialized.(2) Collective: allocate a window exposing
countelements ofTon each process, returning an owningwin.See the Level 1 overload for the semantics.
- Template parameters:
Comm– Any type satisfyingconvertible_to_mpi_handle<MPI_Comm>.NewWin– Any type satisfyingconvertible_to_mpi_handle_ptr<MPI_Win>.Info– Any type satisfyingconvertible_to_mpi_handle<MPI_Info>.
- Parameters:
count– Number of elements ofTexposed by this process (may differ between processes).c– The communicator whose processes share the window.base– Output: receives the address of this process’s local memory.new_win– Output: receives the window handle.info– Info hints (defaults toMPI_INFO_NULL).
- Throws:
mpi_error– ifMPI_Win_allocatefails.
- Returns:
The window and this process’s local, uninitialized part of it.
Defined in header
<kamping/v2/win.hpp>
-
template<convertible_to_mpi_handle<MPI_Win> Win = MPI_Win>
void fence(Win const &w, int assertion = 0)¶ Collective: synchronize an active-target epoch (
MPI_Win_fence).- Template parameters:
Win– Any type satisfyingconvertible_to_mpi_handle<MPI_Win>.
- Parameters:
w– The window.assertion– Bitwise OR ofMPI_MODE_*assertions (MPI_MODE_NOPRECEDE,MPI_MODE_NOSUCCEED, …).
- Throws:
mpi_error– ifMPI_Win_fencefails.
Defined in header
<kamping/v2/win.hpp>
Caching state on MPI objects¶
See Caching state on MPI objects.
-
template<attribute_kind Kind, typename T>
requires std::is_nothrow_destructible_v<T>
class cache¶ Owning cache of one
Tper MPI object, attached to the object as an attribute.The cache owns its values; each object carries a non-owning attribute pointing to its value, which makes lookup O(1). A value is destroyed when
its object is freed (
MPI_Comm_free,MPI_Win_free,MPI_Type_free), via the attribute’s delete callback,it is removed with
erase, orthe cache is destroyed, which removes all remaining values in the order they were created.
Each cache has its own keyval, so independent caches on the same object do not interfere, and no global state is involved: the cache works the same with the World and the Sessions model. Where no explicit owner is available (stateless interfaces, e.g. behind a C API), use
keyval<Kind, T>, whose values are owned by the objects instead.Collective use. If creating or destroying a
Tis collective (e.g. a window on a communicator), thenget_or_create,eraseand the destructor are collective too: all processes must call them for the same objects in the same order. Destroying the cache in creation order makes its cleanup consistent across processes. The cache must be destroyed before MPI is finalized.The delete callback runs inside MPI, so
T’s destructor must not throw. Duplicated objects (MPI_Comm_dup,MPI_Type_dup) do not inherit cached values.- Template parameters:
Kind– The kind of MPI object, one of the tags inattribute.T– The cached value type. Need not be movable: it is constructed in place from the factory’s result.
Defined in header
<kamping/v2/cache.hpp>-
cache()¶
(1) -
cache(cache &&other) noexcept = default¶
(2) (1) Creates an empty cache with its own keyval.
(2) Takes over
other’s values. A moved-from cache may only be destroyed or assigned to.- Throws:
mpi_error– if the keyval cannot be created.
-
~cache()¶
Destroys all remaining values in creation order and frees the keyval (see class documentation).
-
template<convertible_to_mpi_handle<handle_type> Obj, std::invocable F>
requires std::same_as<std::invoke_result_t<F>, T>
T &get_or_create(Obj const &obj, F &&make)¶ Returns the value cached for
obj, creating it withmake()if there is none.- Parameters:
obj– The MPI object, as a raw handle or any wrapper convertible to it.make– Called only on a miss; must return aTby value, which is constructed in place.
- Throws:
mpi_error– if an attribute call fails; nothing is cached in that case.
- Returns:
The cached value; stays valid until it is destroyed (see class documentation).
-
template<convertible_to_mpi_handle<handle_type> Obj>
T *find(Obj const &obj)¶
(1) -
template<convertible_to_mpi_handle<handle_type> Obj>
T const *find(Obj const &obj) const¶
(2) - Throws:
mpi_error– if the attribute lookup fails.
- Returns:
(1) The value cached for
obj, ornullptrif there is none.(2) The value cached for
obj, ornullptrif there is none. The cache owns its values, so a const cache gives read-only access to them.
-
template<convertible_to_mpi_handle<handle_type> Obj>
bool erase(Obj const &obj)¶ Destroys the value cached for
obj, if any.- Throws:
mpi_error– if an attribute call fails.
- Returns:
trueif a value was destroyed.
-
std::size_t size() const noexcept¶
- Returns:
The number of cached values.
-
bool empty() const noexcept¶
- Returns:
trueif no values are cached.
-
template<attribute_kind Kind, typename T>
requires std::is_nothrow_destructible_v<T>
class keyval¶ A typed keyval: caches one
Tper MPI object, owned by the object’s attribute.Each value lives as long as its attribute: it is destroyed when its object is freed (
MPI_Comm_free,MPI_Win_free,MPI_Type_free) or when it is removed witherase. Destroying the keyval only frees the key: values already attached stay alive until their objects are freed, but can no longer be looked up. Values on objects that are never freed (e.g.MPI_COMM_WORLD) are therefore never destroyed.This suits stateless interfaces, where nothing but the MPI object is available to hold the cached state (e.g. behind a C API). When an explicit owner is available, prefer
cache<Kind, T>, which also destroys its values when it is destroyed.Collective use. If creating or destroying a
Tis collective (e.g. a window on a communicator), thenget_or_createanderaseare collective too, and freeing the object runs the destructor of its value.Lifetime of the key. A keyval must be destroyed while MPI is usable. For a keyval in static storage, which is destroyed after
MPI_Finalize, the destructor skips freeing the key once MPI has been finalized (World model). In the Sessions model this cannot be detected; destroy the keyval before the last session is finalized.Constness. A keyval is a key, not a container: it does not own the values, so
constdoes not apply to them, andfindon a const keyval still returns a mutable value (like a conststd::spanorstd::unique_ptr).cache<Kind, T>, which owns its values, propagatesconstto them instead.The delete callback runs inside MPI, so
T’s destructor must not throw. Duplicated objects (MPI_Comm_dup,MPI_Type_dup) do not inherit cached values.- Template parameters:
Kind– The kind of MPI object, one of the tags inattribute.T– The cached value type. Need not be movable: it is constructed in place from the factory’s result.
Defined in header
<kamping/v2/cache.hpp>-
using handle_type = typename Kind::handle_type¶
The raw MPI handle type of the objects carrying values.
-
keyval()¶
(1) -
keyval(keyval &&other) noexcept¶
(2) (1) Creates a new key.
(2) Takes over
other’s key;otheris left without one.- Throws:
mpi_error– if the key cannot be created.
-
~keyval()¶
Frees the key; attached values stay alive until their objects are freed (see class documentation).
-
template<convertible_to_mpi_handle<handle_type> Obj, std::invocable F>
requires std::same_as<std::invoke_result_t<F>, T>
T &get_or_create(Obj const &obj, F &&make)¶ Returns the value attached to
obj, creating it withmake()if there is none.- Parameters:
obj– The MPI object, as a raw handle or any wrapper convertible to it.make– Called only on a miss; must return aTby value, which is constructed in place.
- Throws:
mpi_error– if an attribute call fails; nothing is attached in that case.
- Returns:
The attached value; stays valid until
objis freed or the value is erased.
-
template<convertible_to_mpi_handle<handle_type> Obj>
T *find(Obj const &obj) const¶ - Throws:
mpi_error– if the attribute lookup fails.
- Returns:
The value attached to
obj, ornullptrif there is none. Mutable even through a const keyval, which does not own the values (see class documentation).
-
template<convertible_to_mpi_handle<handle_type> Obj>
bool erase(Obj const &obj)¶ Destroys the value attached to
obj, if any.- Throws:
mpi_error– if an attribute call fails.
- Returns:
trueif a value was destroyed.
Errors¶
-
struct mpi_error¶
Exception thrown by the MPI wrappers when an MPI call returns an error code other than
MPI_SUCCESS.Errors are only reported this way if the communicator’s error handler returns them, e.g.
MPI_ERRORS_RETURN; with the defaultMPI_ERRORS_ARE_FATALMPI aborts before the wrapper sees the error.Defined in header
<kamping/v2/error.hpp>-
int error_code() const¶
- Returns:
The MPI error code.
-
int error_class() const¶
- Returns:
The MPI error class of the error code (
MPI_Error_class).
-
std::string error_string() const¶
- Returns:
The description of the error code (
MPI_Error_string).
-
char const *what() const noexcept final¶
- Returns:
The description of the error code, computed on first use.
-
int error_code() const¶