-
Notifications
You must be signed in to change notification settings - Fork 11
/
pr_numa.c
428 lines (362 loc) · 12.3 KB
/
pr_numa.c
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/***** Implements NUMA partitioning for PR and corresponding computation
Has non NUMA aware code and this should be used to benchmark
benefit of NUMA-Awareness
***/
#include "random.h"
#include <numa.h>
#include "parallel_ligra.h"
#include "bitmap.h"
#include <cilk/reducer_opadd.h>
#include "barrier.h"
#define ROOT 0
#define NUMA_PART 2
#define DAMPING_FACTOR 0.85
#define PAGESIZE 4096
float* curr_rank;
float* prev_rank;
uint32_t* degree;
float one_over_n;
struct per_thread_task {
uint32_t* task_array;
uint32_t curr;
};
uint32_t part_offsets[NUMA_PART + 1];
uint32_t* edge_part_degree[NUMA_PART];
uint64_t* edge_part_offsets[NUMA_PART];
struct edge* edge_array_numa[NUMA_PART];
struct per_thread_task per_thread_tasks[ALGO_NB_THREADS];
#define ALPHA 4
void prnuma_reset(struct node*) {
}
int getNuma_node(uint32_t v_id) {
for(int i = 0; i < NUMA_PART; i++) {
int end = (i == NUMA_PART -1 ? NB_NODES - 1 : part_offsets[i+1]);
if(v_id >= part_offsets[i] && v_id <= end) return i;
}
}
template <class E>
struct getNumaPartDst { uint32_t operator() (E a) {
getNuma_node(a.dst);
}
};
template <class E>
struct getNumaPartSrc { uint32_t operator() (E a) {
getNuma_node(a.src);
}
};
uint32_t edges_per_node[NUMA_PART];
uint32_t* curr_work;
struct x_barrier x_sync;
float adding_constant;
void prnuma_construct(){
uint64_t part_start, part_stop;
init_barrier(&x_sync, ALGO_NB_THREADS);
free(edge_array_out);
rdtscll(part_start);
one_over_n = 1.0/(float)NB_NODES;
if(!numa){
prev_rank = (float*) malloc(NB_NODES * sizeof(float));
curr_rank = (float*) malloc(NB_NODES * sizeof(float));
degree = (uint32_t*) malloc(NB_NODES * sizeof(uint32_t));
parallel_for(uint32_t i = 0; i < NB_NODES; i++) {
prev_rank[i] = 0.15;
curr_rank[i] = 0.0;
degree[i] = nodes[i].nb_out_edges;
}
return;
}
part_offsets[0] = 0;
uint32_t remained = nb_edges ; //+ NB_NODES * ALPHA;
for(int i = 0; i < NUMA_PART ; i++) {
uint32_t remained_part = NUMA_PART - i;
uint32_t expected_chunk = remained / remained_part;
if(remained_part == 1)
part_offsets[i+1] = NB_NODES;
else {
uint32_t edges_in = 0;
for( uint32_t v = part_offsets[i]; v < NB_NODES; v++) {
edges_in += nodes[v].nb_in_edges;// + ALPHA;
if (edges_in > expected_chunk) {
part_offsets[i+1] = v;
break;
}
}
part_offsets[i+1] = (part_offsets[i+1]) / PAGESIZE * PAGESIZE;
}
for (uint32_t v = part_offsets[i]; v < part_offsets[i+1]; v++) {
remained -= nodes[v].nb_in_edges; /// + ALPHA;
}
}
char* array = (char*) mmap(NULL, sizeof(float) * NB_NODES, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
char* array2 = (char*) mmap(NULL, sizeof(float) * NB_NODES, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
char* array3 = (char*) mmap(NULL, sizeof(uint32_t) * NB_NODES, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
int num_part = numa_num_configured_nodes();
printf("NUMA partitions: %d\n", num_part);
char* curr_work_t = (char*) mmap(NULL, sizeof(uint32_t) * NUMA_PART, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
for(int i = 0; i < num_part; i++) {
int size = sizeof(uint32_t);
int page_size = 4096;
size = size < page_size ? page_size : (size % page_size == 0) ? size : size + size % page_size;
printf("Size %d\n", size);
numa_tonode_memory(curr_work_t + i * sizeof(uint32_t), size, i);
uint32_t end;
if(i == num_part - 1) end = NB_NODES;
else end = part_offsets[i+1];
size = sizeof(float) * (end - part_offsets[i]);
size = size < 4096 ? 4096 : (size % 4096 == 0) ? size : size + size % 4096;
numa_tonode_memory(array + sizeof(float) * part_offsets[i], size, i);
numa_tonode_memory(array2 + sizeof(float) * part_offsets[i], size, i);
size = sizeof(uint32_t) * (end - part_offsets[i]);
size = size < 4096 ? 4096 : (size % 4096 == 0) ? size : size + size % 4096;
numa_tonode_memory(array3 + sizeof(uint32_t) * part_offsets[i], size, i);
}
curr_rank = (float*) array;
prev_rank = (float*) array2;
degree = (uint32_t*) array3;
curr_work = (uint32_t*) curr_work_t;
for(uint32_t i = 0 ; i < NUMA_PART; i++) {
edge_part_degree[i] = (uint32_t*) numa_alloc_onnode(NB_NODES * sizeof(uint32_t), i);
assert(NULL!= edge_part_degree[i]);
parallel_for(uint32_t v = 0; v < NB_NODES; v++)
edge_part_degree[i][v] = 0;
edge_part_offsets[i] = (uint64_t*) numa_alloc_onnode(NB_NODES * sizeof(uint64_t), i);
assert(NULL != edge_part_offsets[i]);
parallel_for (uint32_t v = 0; v < NB_NODES; v++)
edge_part_offsets[i][v] = nb_edges;
}
printf("Allocated offsets and degree\n");
parallel_for(int i = 0; i < NB_NODES; i++) {
degree[i] = nodes[i].nb_out_edges;
for(uint32_t dst_idx = 0; dst_idx < nodes[i].nb_in_edges; dst_idx++) {
uint32_t dst = edge_array_in[nodes[i].incoming_edges + dst_idx].dst;
assert(dst < NB_NODES);
int node = getNuma_node(i);
assert(node < NUMA_PART);
edge_part_degree[node][i]++;
}
}
printf("Counted degrees\n");
for(int i = 0; i < NUMA_PART; i++) {
edge_part_offsets[i][0] = 0;
}
parallel_for(uint32_t i = 0; i < NUMA_PART; i++) {
for(uint32_t v = 0; v < NB_NODES; v++) {
edges_per_node[i] += edge_part_degree[i][v];
if(v > 0) edge_part_offsets[i][v] = edge_part_offsets[i][v-1] + edge_part_degree[i][v-1];
}
edge_array_numa[i] = (struct edge*) numa_alloc_onnode(edges_per_node[i] * sizeof(struct edge), i);
assert(NULL != edge_array_numa[i]);
}
//this has to be replace with radix sort
parallel_for(uint32_t i = 0; i < NB_NODES; i++) {
//do the actual edge copy
int edge_idx[NUMA_PART];
for(int n = 0; n < NUMA_PART; n++)
edge_idx[n] = 0;
for(uint32_t idx = 0; idx < nodes[i].nb_in_edges; idx++) {
int node = getNuma_node(i); // can be placed by dst, but for PR this has better load balance
//edge_array_in[nodes[i].incoming_edges + idx].dst);
assert(node < NUMA_PART);
struct edge* e = &edge_array_numa[node][edge_part_offsets[node][i] + edge_idx[node]];
e->dst = edge_array_in[nodes[i].incoming_edges + idx].dst;
assert(e->dst < NB_NODES);
edge_idx[node]++;
}
}
free(edge_array_in);
parallel_for(uint32_t i = 0; i < NB_NODES; i++) {
prev_rank[i] = 0.15;
curr_rank[i] = 0.0;
}
rdtscll(part_stop);
printf("Partitioning time %.3f s", (float)(part_stop - part_start)/ (double)get_cpu_freq());
}
uint32_t total =0;
int iterations = 0;
uint32_t work_done = 0;
void get_work(uint32_t *start, uint32_t* stop, uint32_t max) {
int increment =512;
if(work_done + increment > max)
increment = (max - work_done) / ALGO_NB_THREADS;
if(increment == 0)
increment = 1;
*start = __sync_fetch_and_add(&work_done, increment);
*stop = *start + increment;
if(*stop > max) *stop = max;
if(*start >=max ) { *start = *stop ; return; }
}
int stolen[ALGO_NB_THREADS];
void get_numa_work(uint32_t* start, uint32_t* stop, uint32_t max, int node) {
int increment = 64;
if(curr_work[node] + increment > max) {
increment = (max -curr_work[node] ) / ALGO_NB_THREADS;
}
if(increment == 0)
increment = 1;
*start = __sync_fetch_and_add(&curr_work[node], increment);
*stop = *start + increment;
if(*stop > max) *stop = max;
if(*start >=max ) { *start = *stop ; return; }
return;
}
int has_work(int node, uint32_t max){
return *(volatile uint32_t*)&curr_work[node] < max;
}
void* process_interleaved(void* c ){
uint32_t num = NB_NODES/ ALGO_NB_THREADS;
int tid = (long)c;
if(tid != 0) {
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(tid, &mask);
sched_setaffinity(gettid(), sizeof(mask), &mask);
}
uint32_t start, stop;
begin:
wait_b(&x_sync);
uint32_t idx;
get_work(&start, &stop, NB_NODES);
do {
for(;start < stop; start++) {
curr_rank[start] = 0;
for(uint32_t idx = 0; idx < nodes[start].nb_in_edges; idx++) {
struct edge *e = &edge_array_in[nodes[start].incoming_edges + idx];
curr_rank[start] += prev_rank[e->dst] / (float)degree[e->dst];
}
curr_rank[start] = adding_constant + DAMPING_FACTOR * curr_rank[start];
}
get_work(&start, &stop, NB_NODES);
}while (start != stop);
start = tid * num;
wait_b(&x_sync);
if(tid != 0) goto begin;
}
void* process_numa(void* c) {
int tid = (long)c;
if(tid != 0) {
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(tid, &mask);
sched_setaffinity(gettid(), sizeof(mask), &mask);
}
uint32_t thread_id, node;
uint32_t idx;
uint64_t my_start, my_stop;
thread_id = tid;
node = numa_node_of_cpu(thread_id);
uint32_t start, stop;
begin:
wait_b(&x_sync);
uint32_t buf_size = part_offsets[node+1] - part_offsets[node];
get_numa_work(&start, &stop, buf_size, node);
do{
start += part_offsets[node];
stop += part_offsets[node];
for(;start < stop; start++) {
curr_rank[start] = 0.0;
for(uint32_t e_idx = 0; e_idx < edge_part_degree[node][start]; e_idx++) {
uint32_t dst_idx = edge_array_numa[node][edge_part_offsets[node][start] + e_idx].dst;
curr_rank[start] += prev_rank[dst_idx] / (float)degree[dst_idx];
}
/* No need to steal on own node because we changed partitioning compared to BFS to achieve better performance
If edges of vertex go to different NUMA nodes this has to be uncommented
*/
/*****
for(int n = 0; n < NUMA_PART; n++) {
if(n == node) continue;
for(uint32_t e_idx = 0; e_idx < edge_part_degree[n][start]; e_idx++) {
uint32_t dst_idx = edge_array_numa[n][edge_part_offsets[n][start] + e_idx].dst;
curr_rank[start] += prev_rank[dst_idx] / (float)degree[dst_idx];
}
} ****/
curr_rank[start] = adding_constant + DAMPING_FACTOR * curr_rank[start];
}
get_numa_work(&start, &stop, buf_size, node);
} while(start != stop);
for(uint32_t n_steal = 0; n_steal < NUMA_PART; n_steal++) { //If done steal from cores on other nodes
if(n_steal == node) continue;
buf_size = part_offsets[n_steal + 1] - part_offsets[n_steal];
get_numa_work(&start, &stop, buf_size, node);
do{
start += part_offsets[n_steal];
stop += part_offsets[n_steal];
for(;start < stop; start++) {
curr_rank[start] = 0.0;
for(uint32_t e_idx = 0; e_idx < edge_part_degree[n_steal][start]; e_idx++) {
uint32_t dst_idx = edge_array_numa[n_steal][edge_part_offsets[n_steal][start] + e_idx].dst;
curr_rank[start] += prev_rank[dst_idx] / (float)degree[dst_idx];
}
for(int n = 0; n < NUMA_PART; n++) {
if(n == n_steal) continue;
for(uint32_t e_idx = 0; e_idx < edge_part_degree[n][start]; e_idx++) {
uint32_t dst_idx = edge_array_numa[n][edge_part_offsets[n][start] + e_idx].dst;
curr_rank[start] += prev_rank[dst_idx] / (float)degree[dst_idx];
}
}
curr_rank[start] = adding_constant + DAMPING_FACTOR * curr_rank[start];
}
get_numa_work(&start, &stop, buf_size, n_steal);
} while(start != stop);
}
wait_b(&x_sync);
if(tid != 0) goto begin;
}
inline void prnuma(struct node* nodes){
if(numa)
parallel_for(int i = 0; i < NUMA_PART; i++)
curr_work[i] = 0;
uint64_t t_start, t_stop;
adding_constant = (1 - DAMPING_FACTOR) * 1 / (float) NB_NODES;
__cilkrts_end_cilk(); //pthreads allow better thread placemenet
pthread_t threads[ALGO_NB_THREADS];
for(int i = 1; i < ALGO_NB_THREADS; i++){
if(numa)
pthread_create(&threads[i],NULL, process_numa, (void*)i);
else
pthread_create(&threads[i],NULL, process_interleaved, (void*)i);
}
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(0, &mask);
sched_setaffinity(gettid(), sizeof(mask), &mask);
rdtscll(t_start);
while(iterations++ < 10 ) {
int threads_per_node = ALGO_NB_THREADS / NUMA_PART;
if(numa) {
process_numa(0);
for(int i = 0; i < NUMA_PART; i++)
curr_work[i] = 0;
}
else {
process_interleaved(0);
work_done = 0;
}
printf("Done with iter %d\n", iterations);
std::swap(curr_rank,prev_rank);
}
rdtscll(t_stop);
for(int i = 1; i < ALGO_NB_THREADS; i++){
pthread_cancel(threads[i]);
}
printf("Algo time %.3f \n" , (double)(t_stop - t_start)/(double)get_cpu_freq());
}
void prnuma_destruct(){
if(numa) {
for(int i = 0; i < NUMA_PART; i++) {
numa_free(edge_array_numa[i], edges_per_node[i] * sizeof(uint32_t));
numa_free(edge_part_offsets[i], NB_NODES * sizeof(uint32_t) );
numa_free(edge_part_degree[i], NB_NODES * sizeof(uint32_t) );
}
for(int i = 0; i < ALGO_NB_THREADS; i++){
numa_free(per_thread_tasks[i].task_array, sizeof(uint32_t) * 1024);
}
}
else {
free(degree);
free(curr_rank);
free(prev_rank);
}
}
struct algo_func current_algo = {
.reset = prnuma_reset, .main = prnuma, .construct = prnuma_construct, .destruct = prnuma_destruct
};