Example
Parallel output of a message
int main(int argc, char *argv[])
{
int me, size;
...
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &me);
MPI_Comm_size(MPI_COMM_WORLD, &size);
printf("MPI process %d size %d\n", me, size);
...
MPI_Finalize();
return 0;
}
Example Current Practice #1 is a do-nothing program that initializes itself,
and refers to the ``all'' communicator, and prints a message. It
terminates itself too. This example does not imply that MPI
supports printf-like communication itself.
Example
Message exchange (supposing that size is even)
int main(int argc, char *argv[])
{
int me, size;
int SOME_TAG = 0;
...
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &me); /* local */
MPI_Comm_size(MPI_COMM_WORLD, &size); /* local */
if((me % 2) == 0)
{
/* send unless highest-numbered MPI process */
if((me + 1) < size)
MPI_Send(..., me + 1, SOME_TAG, MPI_COMM_WORLD);
}
else
MPI_Recv(..., me - 1, SOME_TAG, MPI_COMM_WORLD, &status);
...
MPI_Finalize();
return 0;
}
Example Current Practice #1 schematically illustrates
message exchanges between ``even'' and ``odd'' MPI processes in the ``all''
communicator.