-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvisor.c
More file actions
304 lines (274 loc) · 9.69 KB
/
advisor.c
File metadata and controls
304 lines (274 loc) · 9.69 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include "tackle.h"
static void show_workers_config(tackle_opts_t *p_tackle_opts, worker_opts_t *p_workers)
{
printf("Advisor Recommendations:\n");
for (int i = 0; i < p_tackle_opts->num_workers; i++) {
printf("WID[%d]:\n", i);
if (p_workers[i].num_threads) {
if (p_tackle_opts->inter_op > 1) {
printf("\tTotal Threads = %d\n", p_workers[i].num_threads*p_tackle_opts->inter_op);
}
printf("\tOMP_NUM_THREADS = %s\n", p_workers[i].num_threads_str);
printf("\tOMP Affinity = %s\n", p_workers[i].affinity_str);
printf("\tTaskset CPU(s) = %s\n", p_workers[i].cpus_str);
printf("\tNUMA membind = %s\n", p_workers[i].numa_str);
} else {
printf("\tOMP Threads = 0\n");
}
fflush(0);
}
}
static int populate_workers(machine_info_t *p_mach_info,
tackle_opts_t *p_tackle_opts,
worker_opts_t *p_workers)
{
int total_cpus;
int *cpus_list = NULL;
#if defined (TACKLE_USE_LOGICAL_CORES_IN_OMP)
total_cpus = p_mach_info->total_log_cores;
cpus_list = p_mach_info->core_siblings;
#else
total_cpus = p_mach_info->total_phy_cores;
cpus_list = (int *)malloc(sizeof(int) * total_cpus);
if (cpus_list == NULL) {
printf("ERROR: Memory allocation failed in Affinity Advisor\n");
return 1;
}
for (int i = 0; i < total_cpus; i++) {
cpus_list[i] =
p_mach_info->core_siblings[i * p_mach_info->threads_per_core];
}
#endif
bool use_log_cores = false;
#if defined (TACKLE_USE_LOGICAL_CORES_IN_TASKSET) && \
!defined (TACKLE_USE_LOGICAL_CORES_IN_OMP)
use_log_cores = ((p_mach_info->is_ht) ? true : false);
#endif
for (int i = 0, count = 0; i < p_tackle_opts->num_workers; i++) {
int numa_node;
bool multiple_numa_nodes = false;
int worker_nthrs = p_workers[i].num_threads * p_tackle_opts->inter_op;
char log_cpus_str[TACKLE_MAX_BUFFER_LENGTH];
int sloc = 0;
for (int t = 0, sloc2 = 0, cpu_id_start, cpu_id_end;
t < worker_nthrs && count < total_cpus; t++, count++) {
if (t == 0) {
numa_node = map_cpu_to_numa_node(cpus_list[count], p_mach_info);
cpu_id_start = cpus_list[count];
if (worker_nthrs == 1) {
sloc += sprintf(p_workers[i].cpus_str, "%d", cpu_id_start);
if (use_log_cores) {
sprintf(log_cpus_str, "%d", get_ht_sibling(cpu_id_start, p_mach_info));
}
}
#if !defined (TACKLE_USE_SHORT_CPU_LIST)
if (t < worker_nthrs - 1) {
sloc += sprintf(p_workers[i].cpus_str + sloc, "%d,", cpu_id_start);
if (use_log_cores) {
sloc2 += sprintf(log_cpus_str + sloc2, "%d,", get_ht_sibling(cpu_id_start, p_mach_info));
}
}
#endif
continue;
}
if (map_cpu_to_numa_node(cpus_list[count], p_mach_info) != numa_node) {
multiple_numa_nodes = true;
}
#if defined (TACKLE_USE_SHORT_CPU_LIST)
if (cpus_list[count] - cpus_list[count-1] != 1) {
cpu_id_end = cpus_list[count-1];
if (cpu_id_start == cpu_id_end) {
sloc += sprintf(p_workers[i].cpus_str + sloc, "%d", cpu_id_start);
if (use_log_cores) {
sloc2 += sprintf(log_cpus_str + sloc2, "%d",
get_ht_sibling(cpu_id_start, p_mach_info));
}
} else {
sloc += sprintf(p_workers[i].cpus_str + sloc, "%d-%d",
cpu_id_start, cpu_id_end);
if (use_log_cores) {
sloc2 += sprintf(log_cpus_str + sloc2, "%d-%d",
get_ht_sibling(cpu_id_start, p_mach_info),
get_ht_sibling(cpu_id_end, p_mach_info));
}
}
if (t < worker_nthrs) {
sloc += sprintf(p_workers[i].cpus_str + sloc, ",");
if (use_log_cores) {
sloc2 += sprintf(log_cpus_str + sloc2, ",");
}
}
cpu_id_start = cpus_list[count];
}
if (t == worker_nthrs - 1) {
cpu_id_end = cpus_list[count];
if (cpu_id_start == cpu_id_end) {
sloc += sprintf(p_workers[i].cpus_str + sloc, "%d", cpu_id_start);
if (use_log_cores) {
sloc2 += sprintf(log_cpus_str + sloc2, "%d",
get_ht_sibling(cpu_id_start, p_mach_info));
}
} else {
sloc += sprintf(p_workers[i].cpus_str + sloc, "%d-%d",
cpu_id_start, cpu_id_end);
if (use_log_cores) {
sloc2 += sprintf(log_cpus_str + sloc2, "%d-%d",
get_ht_sibling(cpu_id_start, p_mach_info),
get_ht_sibling(cpu_id_end, p_mach_info));
}
}
}
#else
if (t == worker_nthrs - 1) {
sloc += sprintf(p_workers[i].cpus_str + sloc, "%d", cpus_list[count]);
if (use_log_cores) {
sloc2 += sprintf(log_cpus_str + sloc2, "%d",
get_ht_sibling(cpus_list[count], p_mach_info));
}
} else {
sloc += sprintf(p_workers[i].cpus_str + sloc, "%d,", cpus_list[count]);
if (use_log_cores) {
sloc2 += sprintf(log_cpus_str + sloc2, "%d,",
get_ht_sibling(cpus_list[count], p_mach_info));
}
}
#endif
}
if (worker_nthrs > 0) {
sprintf(p_workers[i].num_threads_str, "%d", p_workers[i].num_threads);
sprintf(p_workers[i].affinity_str,
"granularity=fine,explicit,proclist=[%s]", p_workers[i].cpus_str);
if (use_log_cores) {
sprintf(p_workers[i].cpus_str+sloc, ",%s", log_cpus_str);
}
if (multiple_numa_nodes) {
sprintf(p_workers[i].numa_str, "-l");
} else {
sprintf(p_workers[i].numa_str, "-m%d", numa_node);
}
}
}
if (cpus_list != p_mach_info->core_siblings) {
free(cpus_list);
}
return 0;
}
static int initialize_workers(machine_info_t *p_mach_info,
tackle_opts_t *p_tackle_opts,
worker_opts_t *p_workers)
{
int nthrs_tail, nthrs_per_worker, num_workers_with_tail, total_nthrs;
int num_workers = p_tackle_opts->num_workers;
int inter_op = p_tackle_opts->inter_op;
bool user_set_nthrs = true;
bool user_set_aff = true;
int total_cpus;
#if defined (TACKLE_USE_LOGICAL_CORES_IN_OMP)
total_cpus = p_mach_info->total_log_cores;
#else
total_cpus = p_mach_info->total_phy_cores;
#endif
// Init all workers to empty
for (int i = 0; i < num_workers; i++) {
*p_workers[i].num_threads_str = '\0';
*p_workers[i].affinity_str = '\0';
*p_workers[i].cpus_str = '\0';
*p_workers[i].numa_str = '\0';
}
for (int i = 0; i < num_workers; i++) {
if (p_tackle_opts->omp_num_threads[i]) {
p_workers[i].num_threads = p_tackle_opts->omp_num_threads[i] / inter_op;
sprintf(p_workers[i].num_threads_str, "%d", p_workers[i].num_threads);
if (p_tackle_opts->omp_affinity[i]) {
strcpy(p_workers[i].affinity_str, p_tackle_opts->omp_affinity[i]);
} else {
user_set_aff = false;
}
} else {
user_set_nthrs = false;
user_set_aff = false;
}
if (!user_set_nthrs && !user_set_aff) {
break;
}
}
if (user_set_nthrs && user_set_aff) {
printf(
"Advisor Warning: User specified number of workers, number of"
" threads per worker and corresponding affinity. There is nothing"
" to advice, skipping advisor!\n");
fflush(0);
return 1;
}
if (user_set_nthrs) {
total_nthrs = 0;
for (int i = 0; i < num_workers; i++) {
total_nthrs += p_workers[i].num_threads * inter_op;
}
if (total_nthrs > total_cpus) {
printf(
"Advisor Warning: User specified number of workers and threads"
" leads to system over-subscription. Reducing number of threads\n");
fflush(0);
} else {
return 0;
}
}
nthrs_per_worker = total_cpus / (num_workers * inter_op);
for (int i = 0; i < num_workers; i++) {
p_workers[i].num_threads = nthrs_per_worker;
}
nthrs_tail = total_cpus - (nthrs_per_worker * num_workers * inter_op);
num_workers_with_tail = nthrs_tail / inter_op;
#ifdef TACKLE_HETERO_WORKERS
for (int i = 0; i < num_workers_with_tail; i++) {
p_workers[i].num_threads++;
}
#endif
if (num_workers > total_cpus) {
printf(
"Advisor Warning: The number of available cores (%d) are less than"
" requested number of total workers (%d). %d workers will have zero"
" threads\n",
total_cpus, num_workers, num_workers - total_cpus);
fflush(0);
return 0;
}
if (num_workers_with_tail) {
#ifdef TACKLE_HETERO_WORKERS
printf(
"Advisor Warning: The number of available cores (%d) cannot be"
" evenly distributed to requested number of workers (%d)\n"
" Distributing %d tail cores across %d workers."
" This leads to heterogenous workers!\n",
total_cpus, num_workers, nthrs_tail, num_workers_with_tail);
#else
printf(
"Advisor Warning: TACKLE is compiled to not have hetero workers,"
" but the current settings leads to hetero workers"
" and %d cores will not be used.\n",
nthrs_tail);
#endif
fflush(0);
}
return 0;
}
int affinity_advisor(machine_info_t *p_mach_info, tackle_opts_t *p_tackle_opts,
worker_opts_t *p_workers)
{
int status;
if (p_mach_info == NULL || p_tackle_opts == NULL || p_workers == NULL) {
printf("ERROR: Affinity Advisor initialization failed\n");
return 1;
}
status = initialize_workers(p_mach_info, p_tackle_opts, p_workers);
if (status) {
return 0;
}
status = populate_workers(p_mach_info, p_tackle_opts, p_workers);
if (status) {
return 1;
}
show_workers_config(p_tackle_opts, p_workers);
return 0;
}