Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[Energy] Fix broken commit c6fc463d75e4daa67ddf9c460cbc473b1aa9d6ba
[simgrid.git] / examples / smpi / energy / energy.c
1 /* Copyright (c) 2013-2015. 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 #include <stdio.h>
8 #include <stdlib.h>
9 #include <mpi.h>
10 #include <smpi/smpi.h>
11
12 int main(int argc, char *argv[])
13 {
14   int rank;
15   int i;
16   char buf[1024];
17
18   int err = MPI_Init(&argc, &argv);
19   if (err != MPI_SUCCESS) {
20     fprintf(stderr, "MPI_init failed: %d\n", err);
21     exit(EXIT_FAILURE);
22   }
23
24   err = MPI_Comm_rank(MPI_COMM_WORLD, &rank);   /* Get id of this process */
25   if (err != MPI_SUCCESS) {
26     fprintf(stderr, "MPI_Comm_rank failed: %d", err);
27     MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
28     exit(EXIT_FAILURE);
29   }
30
31   int pstates = smpi_get_host_nb_pstates();
32
33   char *s = buf;
34   size_t sz = sizeof buf;
35   size_t x = snprintf(s, sz,
36                "[%.6f] [rank %d] Pstates: %d; Powers: %.0f",
37                MPI_Wtime(), rank, pstates, smpi_get_host_power_peak_at(0));
38   if (x < sz) {
39     s += x;
40     sz -= x;
41   } else
42     sz = 0;
43   for (i = 1; i < pstates; i++) {
44     x = snprintf(s, sz, ", %.0f", smpi_get_host_power_peak_at(i));
45     if (x < sz) {
46       s += x;
47       sz -= x;
48     } else
49       sz = 0;
50   }
51   fprintf(stderr, "%s%s\n", buf, (sz ? "" : " [...]"));
52
53   for (i = 0; i < pstates; i++) {
54     smpi_set_host_pstate(i);
55     fprintf(stderr, "[%.6f] [rank %d] Current pstate: %d; Current power: %.0f\n",
56             MPI_Wtime(), rank, i, smpi_get_host_current_power_peak());
57
58     SMPI_SAMPLE_FLOPS(1e9) {
59       /* imagine here some code running for 1e9 flops... */
60     }
61
62     fprintf(stderr, "[%.6f] [rank %d] Energy consumed: %g Joules.\n",
63             MPI_Wtime(), rank, smpi_get_host_consumed_energy());
64   }
65
66   err = MPI_Finalize();
67   if (err != MPI_SUCCESS) {
68     fprintf(stderr, "MPI_Finalize failed: %d\n", err);
69     MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
70     exit(EXIT_FAILURE);
71   }
72
73   return EXIT_SUCCESS;
74 }