...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
boost::mpi::communicator — A communicator that permits communication and synchronization among a set of processes.
// In header: <boost/mpi/communicator.hpp> class communicator { public: // construct/copy/destruct communicator(); communicator(const MPI_Comm &, comm_create_kind); communicator(const communicator &, const boost::mpi::group &); // public member functions int rank() const; int size() const; boost::mpi::group group() const; template<typename T> void send(int, int, const T &) const; template<typename T> void send(int, int, const skeleton_proxy< T > &) const; template<typename T> void send(int, int, const T *, int) const; void send(int, int) const; template<typename T> status recv(int, int, T &) const; template<typename T> status recv(int, int, const skeleton_proxy< T > &) const; template<typename T> status recv(int, int, skeleton_proxy< T > &) const; template<typename T> status recv(int, int, T *, int) const; status recv(int, int) const; template<typename T> request isend(int, int, const T &) const; template<typename T> request isend(int, int, const skeleton_proxy< T > &) const; template<typename T> request isend(int, int, const T *, int) const; request isend(int, int) const; template<typename T> request irecv(int, int, T &) const; template<typename T> request irecv(int, int, T *, int) const; request irecv(int, int) const; status probe(int = any_source, int = any_tag) const; optional< status > iprobe(int = any_source, int = any_tag) const; void barrier() const; operator bool() const; operator MPI_Comm() const; communicator split(int) const; communicator split(int, int) const; optional< intercommunicator > as_intercommunicator() const; optional< graph_communicator > as_graph_communicator() const; bool has_cartesian_topology() const; void abort(int) const; };
The communicator
class abstracts a set of communicating processes in MPI. All of the processes that belong to a certain communicator can determine the size of the communicator, their rank within the communicator, and communicate with any other processes in the communicator.
communicator
public
construct/copy/destructcommunicator();
Build a new Boost.MPI communicator for MPI_COMM_WORLD
.
Constructs a Boost.MPI communicator that attaches to MPI_COMM_WORLD
. This is the equivalent of constructing with (MPI_COMM_WORLD, comm_attach).
communicator(const MPI_Comm & comm, comm_create_kind kind);
Build a new Boost.MPI communicator based on the MPI communicator comm
.
comm
may be any valid MPI communicator. If comm
is MPI_COMM_NULL, an empty communicator (that cannot be used for communication) is created and the kind
parameter is ignored. Otherwise, the kind
parameters determines how the Boost.MPI communicator will be related to comm:
If kind
is comm_duplicate
, duplicate comm
to create a new communicator. This new communicator will be freed when the Boost.MPI communicator (and all copies of it) is destroyed. This option is only permitted if comm
is a valid MPI intracommunicator or if the underlying MPI implementation supports MPI 2.0 (which supports duplication of intercommunicators).
If kind
is comm_take_ownership
, take ownership of comm
. It will be freed automatically when all of the Boost.MPI communicators go out of scope. This option must not be used when comm
is MPI_COMM_WORLD.
If kind
is comm_attach
, this Boost.MPI communicator will reference the existing MPI communicator comm
but will not free comm
when the Boost.MPI communicator goes out of scope. This option should only be used when the communicator is managed by the user or MPI library (e.g., MPI_COMM_WORLD).
communicator(const communicator & comm, const boost::mpi::group & subgroup);
Build a new Boost.MPI communicator based on a subgroup of another MPI communicator.
This routine will construct a new communicator containing all of the processes from communicator comm
that are listed within the group subgroup
. Equivalent to MPI_Comm_create
.
Parameters: |
|
communicator
public member functionsint rank() const;Determine the rank of the executing process in a communicator.
This routine is equivalent to MPI_Comm_rank
.
Returns: |
The rank of the process in the communicator, which will be a value in [0, size()) |
int size() const;Determine the number of processes in a communicator.
This routine is equivalent to MPI_Comm_size
.
Returns: |
The number of processes in the communicator. |
boost::mpi::group group() const;
This routine constructs a new group whose members are the processes within this communicator. Equivalent to calling MPI_Comm_group
.
template<typename T> void send(int dest, int tag, const T & value) const;Send data to another process.
This routine executes a potentially blocking send with tag tag
to the process with rank dest
. It can be received by the destination process with a matching recv
call.
The given value
must be suitable for transmission over MPI. There are several classes of types that meet these requirements:
Types with mappings to MPI data types: If is_mpi_datatype<T>
is convertible to mpl::true_
, then value
will be transmitted using the MPI data type get_mpi_datatype<T>()
. All primitive C++ data types that have MPI equivalents, e.g., int
, float
, char
, double
, etc., have built-in mappings to MPI data types. You may turn a Serializable type with fixed structure into an MPI data type by specializing
for your type.is_mpi_datatype
Serializable types: Any type that provides the serialize()
functionality required by the Boost.Serialization library can be transmitted and received.
Packed archives and skeletons: Data that has been packed into an
or the skeletons of data that have been backed into an mpi::packed_oarchive
can be transmitted, but will be received as mpi::packed_skeleton_oarchive
and mpi::packed_iarchive
, respectively, to allow the values (or skeletons) to be extracted by the destination process.mpi::packed_skeleton_iarchive
Content: Content associated with a previously-transmitted skeleton can be transmitted by send
and received by recv
. The receiving process may only receive content into the content of a value that has been constructed with the matching skeleton.
For types that have mappings to an MPI data type (including the concent of a type), an invocation of this routine will result in a single MPI_Send call. For variable-length data, e.g., serialized types and packed archives, two messages will be sent via MPI_Send: one containing the length of the data and the second containing the data itself. Note that the transmission mode for variable-length data is an implementation detail that is subject to change.
Parameters: |
|
template<typename T> void send(int dest, int tag, const skeleton_proxy< T > & proxy) const;Send the skeleton of an object.
This routine executes a potentially blocking send with tag tag
to the process with rank dest
. It can be received by the destination process with a matching recv
call. This variation on send
will be used when a send of a skeleton is explicitly requested via code such as:
* comm.send(dest, tag, skeleton(object)); *
The semantics of this routine are equivalent to that of sending a
storing the skeleton of the packed_skeleton_oarchive
object
.
Parameters: |
|
template<typename T> void send(int dest, int tag, const T * values, int n) const;Send an array of values to another process.
This routine executes a potentially blocking send of an array of data with tag tag
to the process with rank dest
. It can be received by the destination process with a matching array recv
call.
If T
is an MPI datatype, an invocation of this routine will be mapped to a single call to MPI_Send, using the datatype get_mpi_datatype<T>()
.
Parameters: |
|
void send(int dest, int tag) const;Send a message to another process without any data.
This routine executes a potentially blocking send of a message to another process. The message contains no extra data, and can therefore only be received by a matching call to recv()
.
Parameters: |
|
template<typename T> status recv(int source, int tag, T & value) const;Receive data from a remote process.
This routine blocks until it receives a message from the process source
with the given tag
. The type T
of the value
must be suitable for transmission over MPI, which includes serializable types, types that can be mapped to MPI data types (including most built-in C++ types), packed MPI archives, skeletons, and content associated with skeletons; see the documentation of send
for a complete description.
Parameters: |
|
||||||
Returns: |
Information about the received message. |
template<typename T> status recv(int source, int tag, const skeleton_proxy< T > & proxy) const;Receive a skeleton from a remote process.
This routine blocks until it receives a message from the process source
with the given tag
containing a skeleton.
Parameters: |
|
||||||
Returns: |
Information about the received message. |
template<typename T> status recv(int source, int tag, skeleton_proxy< T > & proxy) const;Receive a skeleton from a remote process.
This routine blocks until it receives a message from the process source
with the given tag
containing a skeleton.
Parameters: |
|
||||||
Returns: |
Information about the received message. |
template<typename T> status recv(int source, int tag, T * values, int n) const;Receive an array of values from a remote process.
This routine blocks until it receives an array of values from the process source
with the given tag
. If the type T
is
Parameters: |
|
||||||||
Returns: |
Information about the received message. |
||||||||
Throws: |
std::range_error if the message to be received contains more than n values. |
status recv(int source, int tag) const;Receive a message from a remote process without any data.
This routine blocks until it receives a message from the process source
with the given tag
.
Parameters: |
|
||||
Returns: |
Information about the received message. |
template<typename T> request isend(int dest, int tag, const T & value) const;Send a message to a remote process without blocking.
The isend
method is functionality identical to the send
method and transmits data in the same way, except that isend
will not block while waiting for the data to be transmitted. Instead, a request object will be immediately returned, allowing one to query the status of the communication or wait until it has completed.
Parameters: |
|
||||||
Returns: |
a |
template<typename T> request isend(int dest, int tag, const skeleton_proxy< T > & proxy) const;Send the skeleton of an object without blocking.
This routine is functionally identical to the send
method for
objects except that skeleton_proxy
isend
will not block while waiting for the data to be transmitted. Instead, a request object will be immediately returned, allowing one to query the status of the communication or wait until it has completed.
The semantics of this routine are equivalent to a non-blocking send of a
storing the skeleton of the packed_skeleton_oarchive
object
.
Parameters: |
|
||||||
Returns: |
a |
template<typename T> request isend(int dest, int tag, const T * values, int n) const;Send an array of values to another process without blocking.
This routine is functionally identical to the send
method for arrays except that isend
will not block while waiting for the data to be transmitted. Instead, a request object will be immediately returned, allowing one to query the status of the communication or wait until it has completed.
Parameters: |
|
||||||||
Returns: |
a |
request isend(int dest, int tag) const;Send a message to another process without any data without blocking.
This routine is functionally identical to the send
method for sends with no data, except that isend
will not block while waiting for the message to be transmitted. Instead, a request object will be immediately returned, allowing one to query the status of the communication or wait until it has completed.
Parameters: |
|
||||
Returns: |
a |
template<typename T> request irecv(int source, int tag, T & value) const;Prepare to receive a message from a remote process.
The irecv
method is functionally identical to the recv
method and receive data in the same way, except that irecv
will not block while waiting for data to be transmitted. Instead, it immediately returns a request object that allows one to query the status of the receive or wait until it has completed.
Parameters: |
|
||||||
Returns: |
a |
template<typename T> request irecv(int source, int tag, T * values, int n) const;Initiate receipt of an array of values from a remote process.
This routine initiates a receive operation for an array of values transmitted by process source
with the given tag
.
Parameters: |
|
||||||||
Returns: |
a |
request irecv(int source, int tag) const;Initiate receipt of a message from a remote process that carries no data.
This routine initiates a receive operation for a message from process source
with the given tag
that carries no data.
Parameters: |
|
||||
Returns: |
a |
status probe(int source = any_source, int tag = any_tag) const;Waits until a message is available to be received.
This operation waits until a message matching (source
, tag
) is available to be received. It then returns information about that message. The functionality is equivalent to MPI_Probe
. To check if a message is available without blocking, use iprobe
.
Parameters: |
|
||||
Returns: |
Returns information about the first message that matches the given criteria. |
optional< status > iprobe(int source = any_source, int tag = any_tag) const;Determine if a message is available to be received.
This operation determines if a message matching (source
, tag
) is available to be received. If so, it returns information about that message; otherwise, it returns immediately with an empty optional. The functionality is equivalent to MPI_Iprobe
. To wait until a message is available, use wait
.
Parameters: |
|
||||
Returns: |
If a matching message is available, returns information about that message. Otherwise, returns an empty |
void barrier() const;Wait for all processes within a communicator to reach the barrier.
This routine is a collective operation that blocks each process until all processes have entered it, then releases all of the processes "simultaneously". It is equivalent to MPI_Barrier
.
operator bool() const;Determine if this communicator is valid for communication.
Evaluates true
in a boolean context if this communicator is valid for communication, i.e., does not represent MPI_COMM_NULL. Otherwise, evaluates false
.
operator MPI_Comm() const;Access the MPI communicator associated with a Boost.MPI communicator.
This routine permits the implicit conversion from a Boost.MPI communicator to an MPI communicator.
Returns: |
The associated MPI communicator. |
communicator split(int color) const;
Split the communicator into multiple, disjoint communicators each of which is based on a particular color. This is a collective operation that returns a new communicator that is a subgroup of this
. This routine is functionally equivalent to MPI_Comm_split
.
Parameters: |
|
||
Returns: |
A new communicator containing all of the processes in |
communicator split(int color, int key) const;
Split the communicator into multiple, disjoint communicators each of which is based on a particular color. This is a collective operation that returns a new communicator that is a subgroup of this
. This routine is functionally equivalent to MPI_Comm_split
.
Parameters: |
|
||||
Returns: |
A new communicator containing all of the processes in |
optional< intercommunicator > as_intercommunicator() const;
Determine if the communicator is in fact an intercommunicator and, if so, return that intercommunicator.
Returns: |
an |
optional< graph_communicator > as_graph_communicator() const;
Determine if the communicator has a graph topology and, if so, return that
. Even though the communicators have different types, they refer to the same underlying communication space and can be used interchangeably for communication.graph_communicator
Returns: |
an |
bool has_cartesian_topology() const;
Determines whether this communicator has a Cartesian topology.
void abort(int errcode) const;
Abort all tasks in the group of this communicator.
Makes a "best attempt" to abort all of the tasks in the group of this communicator. Depending on the underlying MPI implementation, this may either abort the entire program (and possibly return errcode
to the environment) or only abort some processes, allowing the others to continue. Consult the documentation for your MPI implementation. This is equivalent to a call to MPI_Abort
Parameters: |
|
||
Returns: |
Will not return. |