1 /* A simple example pingpong pogram to test MPI_Send and MPI_Recv */
3 /* Copyright (c) 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
12 int main(int argc, char *argv[])
14 const int tag1 = 42, tag2 = 43; /* Message tag */
22 err = MPI_Init(&argc, &argv); /* Initialize MPI */
23 if (err != MPI_SUCCESS) {
24 printf("MPI initialization failed!\n");
27 err = MPI_Comm_size(MPI_COMM_WORLD, &size); /* Get nr of tasks */
28 err = MPI_Comm_rank(MPI_COMM_WORLD, &rank); /* Get id of this process */
30 printf("run this program with exactly 2 processes (-np 2)\n");
35 printf("\n *** Ping-pong test (MPI_Send/MPI_Recv) ***\n\n");
38 /* start pingpong tests between several pairs */
39 for (pivot = 0; pivot < size - 1; pivot++) {
42 printf("\n== pivot=%d : pingpong [%d] <--> [%d]\n", pivot, pivot,
46 printf("[%d] About to send 1st message '%d' to process [%d] \n",
48 err = MPI_Send(&msg, 1, MPI_INT, dst, tag1, MPI_COMM_WORLD);
50 err = MPI_Recv(&msg, 1, MPI_INT, dst, tag2, MPI_COMM_WORLD, &status); /* Receive a message */
51 printf("[%d] Received reply message '%d' from process [%d] \n", rank,
55 if ((pivot + 1) == rank) {
57 err = MPI_Recv(&msg, 1, MPI_INT, src, tag1, MPI_COMM_WORLD, &status); /* Receive a message */
58 printf("[%d] Received 1st message '%d' from process [%d] \n", rank,
61 printf("[%d] increment message's value to '%d'\n", rank, msg);
62 printf("[%d] About to send back message '%d' to process [%d] \n",
64 err = MPI_Send(&msg, 1, MPI_INT, src, tag2, MPI_COMM_WORLD);
67 err = MPI_Finalize(); /* Terminate MPI */