Sending and receiving of messages by processes is the basic MPI communication mechanism. The basic point-to-point communication operations are send and receive. Their use is illustrated in the example below.
#include "mpi.h"
int main( int argc, char *argv[])
{
char message[20];
int myrank;
MPI_Status status;
MPI_Init( &argc, &argv );
MPI_Comm_rank( MPI_COMM_WORLD, &myrank );
if (myrank == 0) /* code for process zero */
{
strcpy(message,"Hello, there");
MPI_Send(message, strlen(message)+1, MPI_CHAR, 1, 99, MPI_COMM_WORLD);
}
else if (myrank == 1) /* code for process one */
{
MPI_Recv(message, 20, MPI_CHAR, 0, 99, MPI_COMM_WORLD, &status);
printf("received :%s:\n", message);
}
MPI_Finalize();
return 0;
}
In this example, process zero (myrank = 0) sends a message to process one
using the
send operation MPI_SEND. The
operation specifies a send buffer in the sender memory from which the
message data is taken. In the example above, the send buffer consists of the
storage containing the variable message in the memory of process zero.
The location, size and type of the send buffer are specified by the first three
parameters of the send operation. The message sent will contain the 13
characters of this variable.
In addition, the send operation associates an envelope with the
message. This envelope specifies the message destination and contains
distinguishing information that can be used by the receive operation to
select a particular message.
The last three parameters of the send operation, along with the rank of the
sender,
specify the envelope for the message sent.
Process one (myrank = 1) receives this message with the
receive operation MPI_RECV.
The message to be received is selected according to the value of its
envelope, and the message data is stored into the
receive buffer.
In the example above, the receive buffer consists of the storage
containing the string message in the memory of process one.
The first three parameters of the receive operation specify the location, size
and type of the receive buffer. The next three
parameters are used for selecting the incoming message. The last parameter is
used to return information on the message just received.
The next sections describe the blocking send and receive operations. We discuss send, receive, blocking communication semantics, type matching requirements, type conversion in heterogeneous environments, and more general communication modes. Nonblocking communication is addressed next, followed by probing and canceling a message, channel-like constructs and send-receive operations, ending with a description of the ``dummy'' process, MPI_PROC_NULL.