Example MPI Message Sending and Receiving

A message is an array of elements of a given data type. MPI supports all the basic data types and allows a more elaborate application to construct new data types at runtime. A message is sent to a specific process and is marked by a tag (integer value) specified by the user. Tags are used to distinguish between different message types a process might send/receive. In the sample code above, the tag is used to distinguish between work and termination messages.

Sending messages

MPI_Send(buffer, count, datatype, destination, tag, MPI_COMM_WORLD);

Receiving messages

A receiving process specifies the tag and the rank of the sending process. MPI_ANY_TAG and MPI_ANY_SOURCE may be used optionally to receive a message of any tag and from any sending process.

MPI_Recv(buffer, maxcount, datatype, source, tag, MPI_COMM_WORLD, &status);

Information about the received message is returned in a status variable. The received message tag is status. MPI_TAG and the rank of the sending process is status. MPI_SOURCE. Another function, not used in the sample code, returns the number of data type elements received. It is used when the number of elements received might be smaller than maxcount.

MPI_Get_count(&status, datatype, &nelements);

With these few functions, you are ready to program almost any application. There are many other, more exotic functions in MPI, but all can be built upon those presented here so far.

Copyright 2002-2014 ClusterVision BV