1 /* A simple example pingpong pogram to test MPI_Send and MPI_Recv */
3 /* Copyright (c) 2009-2010, 2012-2014. 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 */
21 err = MPI_Init(&argc, &argv); /* Initialize MPI */
22 if (err != MPI_SUCCESS) {
23 printf("MPI initialization failed!\n");
26 MPI_Comm_size(MPI_COMM_WORLD, &size); /* Get nr of tasks */
27 MPI_Comm_rank(MPI_COMM_WORLD, &rank); /* Get id of this process */
29 printf("run this program with exactly 2 processes (-np 2)\n");
34 printf("\n *** Ping-pong test (MPI_Send/MPI_Recv) ***\n\n");
37 /* start pingpong tests between several pairs */
38 for (pivot = 0; pivot < size - 1; pivot++) {
40 printf("\n== pivot=%d : pingpong [%d] <--> [%d]\n", pivot, pivot, pivot + 1);
43 printf("[%d] About to send 1st message '%d' to process [%d]\n", rank, msg, dst);
44 MPI_Send(&msg, 1, MPI_INT, dst, tag1, MPI_COMM_WORLD);
46 MPI_Recv(&msg, 1, MPI_INT, dst, tag2, MPI_COMM_WORLD, &status); /* Receive a message */
47 printf("[%d] Received reply message '%d' from process [%d]\n", rank, msg, dst);
49 if ((pivot + 1) == rank) {
51 MPI_Recv(&msg, 1, MPI_INT, src, tag1, MPI_COMM_WORLD, &status); /* Receive a message */
52 printf("[%d] Received 1st message '%d' from process [%d]\n", rank, msg, src);
54 printf("[%d] increment message's value to '%d'\n", rank, msg);
55 printf("[%d] About to send back message '%d' to process [%d]\n", rank, msg, src);
56 MPI_Send(&msg, 1, MPI_INT, src, tag2, MPI_COMM_WORLD);