Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge remote-tracking branch 'origin/libdw2'
[simgrid.git] / examples / smpi / energy / se.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <mpi.h>
4 #include <smpi/smpi.h>
5
6 int main(int argc, char *argv[])
7 {
8   int rank, pstates;
9   int i;
10   char buf[1024];
11   char *s;
12   size_t sz, x;
13
14   if (MPI_Init(&argc, &argv) != MPI_SUCCESS) {
15     printf("MPI initialization failed!\n");
16     exit(EXIT_FAILURE);
17   }
18
19   MPI_Comm_rank(MPI_COMM_WORLD, &rank);   /* Get id of this process */
20
21   pstates = smpi_get_host_nb_pstates();
22
23   s = buf;
24   sz = sizeof buf;
25   x = snprintf(s, sz,
26                "[%.6f] [rank %d] Pstates: %d; Powers: %.0f",
27                MPI_Wtime(), rank, pstates, smpi_get_host_power_peak_at(0));
28   if (x < sz) {
29     s += x;
30     sz -= x;
31   } else
32     sz = 0;
33   for (i = 1; i < pstates; i++) {
34     x = snprintf(s, sz, ", %.0f", smpi_get_host_power_peak_at(i));
35     if (x < sz) {
36       s += x;
37       sz -= x;
38     } else
39       sz = 0;
40   }
41   printf("%s%s\n", buf, (sz ? "" : " [...]"));
42
43   for (i = 0; i < pstates; i++) {
44     smpi_set_host_power_peak_at(i);
45     printf("[%.6f] [rank %d] Current pstate: %d; Current power: %.0f\n",
46            MPI_Wtime(), rank, i, smpi_get_host_current_power_peak());
47
48     SMPI_SAMPLE_FLOPS(1e9) {
49       /* imagine here some code running for 1e9 flops... */
50     }
51
52     printf("[%.6f] [rank %d] Energy consumed: %g Joules.\n",
53            MPI_Wtime(), rank, smpi_get_host_consumed_energy());
54   }
55
56   return MPI_Finalize();
57 }