-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
135 lines (115 loc) · 3.64 KB
/
main.cpp
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <string>
#include <cassert>
#include <cstring>
#include <cstdio>
#include <sched.h>
#include <sys/sysinfo.h>
#include <mpi.h>
#include <omp.h>
void affinity_to_string(cpu_set_t *mask, char *buffer) {
int nproc = get_nprocs();
int last_zero = -1;
for (int i = 0; i <= nproc; i++) {
if (i == nproc || !CPU_ISSET(i, mask)) {
if (i > last_zero + 1) {
// stripe found
if (i > last_zero + 2) {
// more than one
sprintf(buffer + strlen(buffer), " %d-%d", last_zero + 1, i - 1);
} else {
// only one
sprintf(buffer + strlen(buffer), " %d", last_zero + 1);
}
}
last_zero = i;
}
}
}
int main(int argc, char *argv[]) {
bool call_gnuplot = false;
if (argc > 1 && strcmp(argv[1], "-p") == 0) {
call_gnuplot = true;
};
MPI_Init(&argc, &argv);
int size, rank;
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0) {
printf("MPI Size: %d\n", size);
}
MPI_Barrier(MPI_COMM_WORLD);
// get thread affinity
int num_threads = omp_get_max_threads();
cpu_set_t *all_masks = new cpu_set_t[num_threads + 1];
#pragma omp parallel for schedule(static, 1)
for (int i = 0; i < num_threads; i++) {
CPU_ZERO(&all_masks[i + 1]);
assert(pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t),
&all_masks[i + 1]) == 0);
}
// get process affinity
CPU_ZERO(&all_masks[0]);
assert(sched_getaffinity(0, sizeof(cpu_set_t), &all_masks[0]) == 0);
cpu_set_t *root_buffer = NULL;
if (rank == 0) {
root_buffer = new cpu_set_t[(num_threads + 1) * size];
memset(root_buffer, 0, sizeof(cpu_set_t) * (num_threads + 1) * size);
}
MPI_Gather(all_masks, sizeof(cpu_set_t) * (num_threads + 1), MPI_CHAR,
root_buffer, sizeof(cpu_set_t) * (num_threads + 1), MPI_CHAR, 0,
MPI_COMM_WORLD);
if (rank == 0) {
char buffer[1024];
FILE *fp = fopen("affinity.gnuplot", "w");
assert(fp);
fprintf(fp, "set terminal png\n");
fprintf(fp, "set title \"Thread affinity map\"\n");
fprintf(fp, "unset key\n");
fprintf(fp, "set palette rgbformula -7,2,-7\n");
fprintf(fp, "unset colorbox\n");
fprintf(fp, "$map << EOD\n");
int nproc = get_nprocs();
for (int i = 0; i < nproc; i++) {
if (i % (nproc / 4) == 0) {
fprintf(fp, ",%d", i);
} else {
fprintf(fp, ",");
}
}
for (int i = 0; i < size; i++) {
cpu_set_t *process_affinity = &root_buffer[i * (num_threads + 1)];
buffer[0] = 0;
affinity_to_string(process_affinity, buffer);
printf("Rank %d Process: %s\n", i, buffer);
for (int j = 1; j <= num_threads; j++) {
cpu_set_t *thread_affinity = &root_buffer[i * (num_threads + 1) + j];
if (j == 1) {
fprintf(fp, "\nRank %d", i);
} else {
fprintf(fp, "\n");
}
for (int i = 0; i < nproc; i++) {
fprintf(fp, ", %d", CPU_ISSET(i, thread_affinity) != 0);
}
buffer[0] = 0;
affinity_to_string(&root_buffer[i * (num_threads + 1) + j], buffer);
printf("Rank %d Thread %d: %s\n", i, j, buffer);
}
}
fprintf(fp, "\nEOD\n");
fprintf(fp, "set datafile separator comma\n");
fprintf(
fp,
"plot '$map' matrix rowheaders columnheaders using 1:2:3 with image\n");
fprintf(fp, "set datafile separator");
fclose(fp);
delete[] root_buffer;
if (call_gnuplot) {
int res = system("gnuplot affinity.gnuplot > affinity.png");
printf("Called gnuplot with result %d\n", res);
}
}
delete[] all_masks;
MPI_Finalize();
return 0;
}