Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / teshsuite / smpi / macro-sample / macro-sample.c
1 /* Copyright (c) 2009-2023. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 /* This example should be instructive to learn about SMPI_SAMPLE_LOCAL and
8    SMPI_SAMPLE_GLOBAL macros for execution sampling */
9
10 #include <stdio.h>
11 #include <mpi.h>
12
13 static double compute(double d0)
14 {
15   double d = d0;
16   for (int j = 0; j < 100 * 1000 * 1000; j++) { /* 100 kflop */
17     if (d < 100000) {
18       d = d * d;
19     } else {
20       d = 2;
21     }
22   }
23   return d;
24 }
25
26 int main(int argc, char *argv[])
27 {
28   int n;
29   int rank;
30   MPI_Init(&argc, &argv);
31   int verbose = argc <= 1;
32   MPI_Comm_size(MPI_COMM_WORLD, &n);
33   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
34   double d = 2.0;
35     /* I want no more than n + 1 benchs (thres < 0) */
36   SMPI_SAMPLE_GLOBAL(int i = 0, i < 5, i++, n + 1, -1){
37       if (verbose)
38         fprintf(stderr, "(%12.6f) [rank:%d]", MPI_Wtime(), rank);
39       else
40         fprintf(stderr, "(0)");
41       fprintf(stderr, " Run the first computation. It's globally benched, "
42               "and I want no more than %d benchmarks (thres<0)\n", n + 1);
43       d = compute(2.0);
44   }
45
46   //tagged version, should differentiate between two different calls to the same kernel and run calibration even on the second one
47   for (int tag=0; tag < 4; tag++){
48     char ctag [12];
49     //run twice with the same tag, test should skip 1 and 3, as they were already benched.
50     sprintf(ctag, "%d", tag - tag%2);
51     SMPI_SAMPLE_GLOBAL_TAG(int i = 0, i < 500, i++, 2, 0.1, ctag){
52         if (verbose)
53           fprintf(stderr, "(%12.6f) [rank:%d]", MPI_Wtime(), rank);
54         else
55           fprintf(stderr, "(0)");
56         fprintf(stderr, " Run the computation %d with tag %d\n", tag, tag- tag%2);
57         d = compute(2.0);
58     }
59   }
60
61   n = 0;
62   //Use 0 as max iter, but one will always be performed by design.
63   SMPI_SAMPLE_LOCAL (int i = 0, i < 5, i++,0, 0.1){
64       if (verbose || n < 2) {
65         n++;
66         if (verbose)
67           fprintf(stderr, "(%12.6f)", MPI_Wtime());
68         else
69           fprintf(stderr, "(1)");
70         fprintf(stderr,
71                 " [rank:%d] Run the second (locally benched) computation. It's locally benched, and I want the "
72                 "standard error to go below 0.1 second (count is not >0)\n", rank);
73       }
74       d = compute(d);
75   }
76
77   if (verbose)
78     fprintf(stderr, "(%12.6f) [rank:%d] The result of the computation is: %f\n", MPI_Wtime(), rank, d);
79   else
80     fprintf(stderr, "(2) [rank:%d] Done.\n", rank);
81
82   MPI_Finalize();
83   return 0;
84 }