Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Combine identical conditions.
[simgrid.git] / examples / smpi / simple-execute / simple-execute.c
1 /* A simple example ping-pong program to test MPI_Send and MPI_Recv */
2
3 /* Copyright (c) 2009-2021. The SimGrid Team.
4  * All rights reserved.                                                     */
5
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. */
8
9 #include <stdio.h>
10 #include <mpi.h>
11
12 /* This test performs a simple pingpong between 2 processes
13    Each process calls smpi_execute or smpi_execute_flops */
14
15 int main(int argc, char *argv[])
16 {
17   const int tag1 = 42;
18   const int tag2 = 43; /* Message tag */
19   int size;
20   int rank;
21   int msg = 99;
22   MPI_Status status;
23   int err = MPI_Init(&argc, &argv); /* Initialize MPI */
24
25   if (err != MPI_SUCCESS) {
26     printf("MPI initialization failed!\n");
27     exit(1);
28   }
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 */
31   if (size != 2) {
32     printf("run this program with exactly 2 processes (-np 2)\n");
33     MPI_Finalize();
34     exit(0);
35   }
36   if (rank == 0) {
37     printf("\n    *** Ping-pong test (MPI_Send/MPI_Recv) ***\n\n");
38
39     /* start ping-pong tests between several pairs */
40     int dst = 1;
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);
43
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);
49
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);
52   } else {
53     int src = 0;
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);
56     msg++;
57
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);
61
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);
65   }
66   MPI_Finalize();
67   return 0;
68 }