-
Notifications
You must be signed in to change notification settings - Fork 2
/
power.hpp
83 lines (65 loc) · 1.98 KB
/
power.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <mpi.h>
#include <unistd.h>
namespace power {
static double read_pm_file(const std::string &fname) {
double result = 0.;
std::ifstream fid(fname.c_str());
fid >> result;
//std::cout << fname << " :: " << result << std::endl;
return result;
}
static double device_energy(void) {
return read_pm_file("/sys/cray/pm_counters/accel_energy");
}
static double energy() {
return read_pm_file("/sys/cray/pm_counters/energy");
}
static double device_power() {
return read_pm_file("/sys/cray/pm_counters/accel_power");
}
static double power() {
return read_pm_file("/sys/cray/pm_counters/power");
}
static int num_nodes() {
// find out the number of nodes
char *ptr = std::getenv("SLURM_JOB_NUM_NODES");
if(ptr) {
return atoi(ptr);
}
else {
return -1;
}
}
// returns -1 if unable to determine number of nodes
static int ranks_per_node() {
const int maxlen = 512;
char name[maxlen];
// check whether MPI has been initialized
int is_initialized = 0;
MPI_Initialized(&is_initialized);
if(!is_initialized) {
std::cerr << "ERROR : MPI not initialized" << std::endl;
return -1;
}
// get MPI information
int rank, size;
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
// get hostname for this node
int result = gethostname(name, maxlen);
if(result) return -1;
// get integer index for this node, by stripping off first 3 characters
// on cray systems all compute nodes have hostname set as
// nid#######
int node = atoi(name+3);
// gather list of node identifiers
std::vector<int> node_ids(size);
MPI_Allgather(&node, 1, MPI_INT, &node_ids[0], 1, MPI_INT, MPI_COMM_WORLD);
// count the number of mpi ranks that are on the same node as this rank
return std::count(node_ids.begin(), node_ids.end(), node);
}
} // namespace power