Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add/update copyright notices.
[simgrid.git] / examples / smpi / energy / se.c
1 /* Copyright (c) 2013-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 #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
20   if (MPI_Init(&argc, &argv) != MPI_SUCCESS) {
21     printf("MPI initialization failed!\n");
22     exit(EXIT_FAILURE);
23   }
24
25   MPI_Comm_rank(MPI_COMM_WORLD, &rank);   /* Get id of this process */
26
27   pstates = smpi_get_host_nb_pstates();
28
29   s = buf;
30   sz = sizeof buf;
31   x = snprintf(s, sz,
32                "[%.6f] [rank %d] Pstates: %d; Powers: %.0f",
33                MPI_Wtime(), rank, pstates, smpi_get_host_power_peak_at(0));
34   if (x < sz) {
35     s += x;
36     sz -= x;
37   } else
38     sz = 0;
39   for (i = 1; i < pstates; i++) {
40     x = snprintf(s, sz, ", %.0f", smpi_get_host_power_peak_at(i));
41     if (x < sz) {
42       s += x;
43       sz -= x;
44     } else
45       sz = 0;
46   }
47   printf("%s%s\n", buf, (sz ? "" : " [...]"));
48
49   for (i = 0; i < pstates; i++) {
50     smpi_set_host_power_peak_at(i);
51     printf("[%.6f] [rank %d] Current pstate: %d; Current power: %.0f\n",
52            MPI_Wtime(), rank, i, smpi_get_host_current_power_peak());
53
54     SMPI_SAMPLE_FLOPS(1e9) {
55       /* imagine here some code running for 1e9 flops... */
56     }
57
58     printf("[%.6f] [rank %d] Energy consumed: %g Joules.\n",
59            MPI_Wtime(), rank, smpi_get_host_consumed_energy());
60   }
61
62   return MPI_Finalize();
63 }