1 /* A simple example ping-pong program to test MPI_Send and MPI_Recv */
3 /* Copyright (c) 2009-2023. 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 static void test_opts(int argc, char* const argv[])
16 while ((ret = getopt(argc, argv, "s")) >= 0) {
21 printf("(smpi_)getopt failed ! \n");
25 int main(int argc, char *argv[])
28 const int tag2 = 43; /* Message tag */
33 int err = MPI_Init(&argc, &argv); /* Initialize MPI */
35 /* test getopt function */
36 test_opts(argc, argv);
38 if (err != MPI_SUCCESS) {
39 printf("MPI initialization failed!\n");
42 MPI_Comm_size(MPI_COMM_WORLD, &size); /* Get nr of tasks */
43 MPI_Comm_rank(MPI_COMM_WORLD, &rank); /* Get id of this process */
45 printf("run this program with exactly 2 processes (-np 2)\n");
50 printf("\n *** Ping-pong test (MPI_Send/MPI_Recv) ***\n\n");
53 /* start ping-pong tests between several pairs */
54 for (int pivot = 0; pivot < size - 1; pivot++) {
56 printf("\n== pivot=%d : pingpong [%d] <--> [%d]\n", pivot, pivot, pivot + 1);
59 printf("[%d] About to send 1st message '%d' to process [%d]\n", rank, msg, dst);
60 MPI_Send(&msg, 1, MPI_INT, dst, tag1, MPI_COMM_WORLD);
62 MPI_Recv(&msg, 1, MPI_INT, dst, tag2, MPI_COMM_WORLD, &status); /* Receive a message */
63 printf("[%d] Received reply message '%d' from process [%d]\n", rank, msg, dst);
65 if ((pivot + 1) == rank) {
67 MPI_Recv(&msg, 1, MPI_INT, src, tag1, MPI_COMM_WORLD, &status); /* Receive a message */
68 printf("[%d] Received 1st message '%d' from process [%d]\n", rank, msg, src);
70 printf("[%d] increment message's value to '%d'\n", rank, msg);
71 printf("[%d] About to send back message '%d' to process [%d]\n", rank, msg, src);
72 MPI_Send(&msg, 1, MPI_INT, src, tag2, MPI_COMM_WORLD);