Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into mc
[simgrid.git] / teshsuite / smpi / compute / compute3.c
1 /* Copyright (c) 2009-2014. 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   int j;
17   for (j = 0; j < 100 * 1000 * 1000; j++) { /* 100 kflop */
18     if (d < 100000) {
19       d = d * d;
20     } else {
21       d = 2;
22     }
23   }
24   return d;
25 }
26
27 int main(int argc, char *argv[])
28 {
29   int verbose;
30   int i, n;
31   double d;
32   MPI_Init(&argc, &argv);
33   verbose = argc <= 1;
34   MPI_Comm_size(MPI_COMM_WORLD, &n);
35   d = 2.0;
36   for (i = 0; i < 5; i++) {
37     /* I want no more than n + 1 benchs (thres < 0) */
38     SMPI_SAMPLE_GLOBAL(n + 1, -1) {
39       if (verbose)
40         fprintf(stderr, "(%12.6f) [rank:%d]", MPI_Wtime(), smpi_process_index());
41       else
42         fprintf(stderr, "(0)");
43       fprintf(stderr, " Run the first computation. It's globally benched, "
44               "and I want no more than %d benchmarks (thres<0)\n", n + 1);
45       d = compute(2.0);
46      }
47   }
48
49   n = 0;
50   for (i = 0; i < 5; i++) {
51     /* I want the standard error to go below 0.1 second.
52      * Two tests at least will be run (count is not > 0) */
53     SMPI_SAMPLE_LOCAL(0, 0.1) {
54       if (verbose || n++ < 2) {
55         if (verbose)
56           fprintf(stderr, "(%12.6f)", MPI_Wtime());
57         else
58           fprintf(stderr, "(1)");
59         fprintf(stderr,
60                 " [rank:%d] Run the first (locally benched) computation. "
61                 "It's locally benched, and I want the standard error to go "
62                 "below 0.1 second (count is not >0)\n", smpi_process_index());
63       }
64       d = compute(d);
65      }
66   }
67
68   if (verbose)
69     fprintf(stderr, "(%12.6f) [rank:%d] The result of the computation is: %f\n",
70             MPI_Wtime(), smpi_process_index(), d);
71   else
72     fprintf(stderr, "(2) [rank:%d] Done.\n", smpi_process_index());
73
74   MPI_Finalize();
75   return 0;
76 }