Coupled Communications: Sendrecv
MPI also provides a pair of functions that combine a blocking send and receive into a single API call. This is useful for swap communications, where messages must be exchanged between processes; or for chain communications, where messages are being passed down the line. There are two versions of these combined calls, which differ in the number of buffers.
MPI_Sendrecv
MPI_Sendrecv requires the specification of two buffers, one for the send component and one for the receive component. The C prototype for this call is shown below. The call combines the full specification of both a send and a receive call, so the argument list is very long and has been broken into multiple lines for readability. Note that the source and the destination do not need to be the same.
int MPI_Sendrecv ( \
const void *sendbuf, int sendcount, \
MPI_Datatype sendtype, int dest, int sendtag, \
void *recvbuf, int recvcount, MPI_Datatype recvtype, \
int source, int recvtag, \
MPI_Comm comm, MPI_Status *status)
Input parameters for MPI_Sendrecv:
- *sendbuf
- The address of the send buffer.
- sendcount
- Number of elements in the send buffer.
- sendtype
- Data type of the send buffer.
- dest
- Destination process number (rank number).
- sendtag
- Sent message tag.
- *recvbuf
- The memory address of the receiving buffer.
- recvcount
- Number of elements in the receive buffer.
- recvtype
- Data type of the receiving buffer.
- source
- Source process rank number.
- recvtag
- Receiving message tag.
- comm
- MPI communicator.
- *status
- Status of object.
MPI_Sendrecv_replace
MPI_Sendrecv_replace requires the specification of only one buffer, as it will be reused by the receive following the send. This means that the received message ultimately overwrites the sent message. The system handles any buffering that may be needed. This is useful for one-time requests where the received message does not need to be understood in the context of the sent message. If the sent message is needed for further processing, then the MPI_Sendrecv should be used instead.
int MPI_Sendrecv_replace ( \
void *buf, int count, MPI_Datatype datatype, \
int dest, int sendtag, \
int source, int recvtag, \
MPI_Comm comm, MPI_Status *status)
Evidently, the above routine requires the count and datatype of the sent and received messages to be identical. If any of the arguments are unclear, refer to the blocking syntax page for details.