Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add simple example using smpi_execute_* calls inside MPI code to simulate computation.
[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 (0 == rank) {
37     printf("\n    *** Ping-pong test (MPI_Send/MPI_Recv) ***\n\n");
38   }
39
40   /* start ping-pong tests between several pairs */
41   if ( rank == 0) {
42     int dst = 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);
45
46     /* Inject five seconds of fake computation time */
47     /* We are in a public file, not internal to simgrid, so _benched flavour is preferred, as it protects against accidental skip */
48     /* smpi_execute_benched here is mostly equivalent to sleep, which is intercepted by smpi and turned into smpi_sleep */
49     /* Difference with sleep is only for energy consumption */
50     smpi_execute_benched(5.0);
51
52     MPI_Recv(&msg, 1, MPI_INT, dst, tag2, MPI_COMM_WORLD, &status);     /* Receive a message */
53     printf("[%d] Received reply message '%d' from process [%d]\n", rank, msg, dst);
54   } else {
55     int src = 0;
56     MPI_Recv(&msg, 1, MPI_INT, src, tag1, MPI_COMM_WORLD, &status);     /* Receive a message */
57     printf("[%d] Received 1st message '%d' from process [%d]\n", rank, msg, src);
58     msg++;
59
60     /* Inject 762.96 Mflops of computation time - Host Jupiter is 76.296Mf per second, so this should amount to 10s */
61     /* We are in a public file, not internal to simgrid, so _benched flavour is preferred, as it protects against accidental skip */
62     smpi_execute_flops_benched(762960000);
63
64     printf("[%d] After a nap, increment message's value to  '%d'\n", rank, msg);
65     printf("[%d] About to send back message '%d' to process [%d]\n", rank, msg, src);
66     MPI_Send(&msg, 1, MPI_INT, src, tag2, MPI_COMM_WORLD);
67   }
68   MPI_Finalize();
69   return 0;
70 }