1 /* A simple example ping-pong program to test MPI_Send and MPI_Recv */
3 /* Copyright (c) 2009-2021. 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 /* This test performs a simple pingpong between 2 processes
13 Each process calls smpi_execute or smpi_execute_flops */
15 int main(int argc, char *argv[])
18 const int tag2 = 43; /* Message tag */
23 int err = MPI_Init(&argc, &argv); /* Initialize MPI */
25 if (err != MPI_SUCCESS) {
26 printf("MPI initialization failed!\n");
29 MPI_Comm_size(MPI_COMM_WORLD, &size); /* Get nr of tasks */
30 MPI_Comm_rank(MPI_COMM_WORLD, &rank); /* Get id of this process */
32 printf("run this program with exactly 2 processes (-np 2)\n");
37 printf("\n *** Ping-pong test (MPI_Send/MPI_Recv) ***\n\n");
39 /* start ping-pong tests between several pairs */
41 printf("[%d] About to send 1st message '%d' to process [%d]\n", rank, msg, dst);
42 MPI_Send(&msg, 1, MPI_INT, dst, tag1, MPI_COMM_WORLD);
44 /* Inject five seconds of fake computation time */
45 /* We are in a public file, not internal to simgrid, so _benched flavour is preferred, as it protects against accidental skip */
46 /* smpi_execute_benched here is mostly equivalent to sleep, which is intercepted by smpi and turned into smpi_sleep */
47 /* Difference with sleep is only for energy consumption */
48 smpi_execute_benched(5.0);
50 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, msg, dst);
54 MPI_Recv(&msg, 1, MPI_INT, src, tag1, MPI_COMM_WORLD, &status); /* Receive a message */
55 printf("[%d] Received 1st message '%d' from process [%d]\n", rank, msg, src);
58 /* Inject 762.96 Mflops of computation time - Host Jupiter is 76.296Mf per second, so this should amount to 10s */
59 /* We are in a public file, not internal to simgrid, so _benched flavour is preferred, as it protects against accidental skip */
60 smpi_execute_flops_benched(762960000);
62 printf("[%d] After a nap, increment message's value to '%d'\n", rank, msg);
63 printf("[%d] About to send back message '%d' to process [%d]\n", rank, msg, src);
64 MPI_Send(&msg, 1, MPI_INT, src, tag2, MPI_COMM_WORLD);