Logo AND Algorithmique Numérique Distribuée

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