Example #1a:
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);Example #1a is a do-nothing program that initializes itself legally, and refers to the the ``all'' communicator, and prints a message. It terminates itself legally too. This example does not imply that MPI supports printf-like communication itself.(void)printf ("Process %d size %d\n", me, size); ... MPI_Finalize(); }
Example #1b (supposing that size is even):
main(int argc, char **argv) { int me, size; int SOME_TAG = 0; ... MPI_Init(&argc, &argv);Example #1b schematically illustrates message exchanges between ``even'' and ``odd'' processes in the ``all'' communicator.MPI_Comm_rank(MPI_COMM_WORLD, &me); /* local */ MPI_Comm_size(MPI_COMM_WORLD, &size); /* local */
if((me % 2) == 0) { /* send unless highest-numbered process */ if((me + 1) < size) MPI_Send(..., me + 1, SOME_TAG, MPI_COMM_WORLD); } else MPI_Recv(..., me - 1, SOME_TAG, MPI_COMM_WORLD);
... MPI_Finalize(); }