forked from realstolz/polymer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
numa-BFS-async-pipe.C
411 lines (345 loc) · 11.5 KB
/
numa-BFS-async-pipe.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
/*
* This code is part of the project "NUMA-aware Graph-structured Analytics"
*
*
* Copyright (C) 2014 Institute of Parallel And Distributed Systems (IPADS), Shanghai Jiao Tong University
* All rights reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more about this software, visit:
*
* http://ipads.se.sjtu.edu.cn/projects/polymer.html
*
*/
#include "polymer.h"
#include "parallel.h"
#include <numa.h>
#include <sys/mman.h>
#include <pthread.h>
using namespace std;
int CORES_PER_NODE = 6;
volatile int shouldStart = 0;
vertices *Frontier;
intT *parents_global;
pthread_barrier_t barr;
pthread_barrier_t global_barr;
pthread_barrier_t timerBarr;
intT vPerNode = 0;
int numOfNode = 0;
bool needResult = false;
struct BFS_F {
intT* Parents;
BFS_F(intT* _Parents) : Parents(_Parents) {}
inline void *nextPrefetchAddr(intT index) {
return NULL;
}
inline bool update (intT s, intT d) { //Update
if(Parents[d] == -1) { Parents[d] = s; return 1; }
else return 0;
}
inline bool updateAtomic (intT s, intT d){ //atomic version of Update
return (CAS(&Parents[d],(intT)-1,s));
}
inline void vertUpdate(intT v) {
return;
}
//cond function checks if vertex has been visited yet
inline bool cond (intT d) { return (Parents[d] == -1); }
};
struct BFS_worker_arg {
void *GA;
int tid;
int numOfNode;
intT rangeLow;
intT rangeHi;
intT start;
};
struct BFS_subworker_arg {
void *GA;
int tid;
int subTid;
intT startPos;
intT endPos;
intT rangeLow;
intT rangeHi;
intT *parents_ptr;
pthread_barrier_t *global_barr;
pthread_barrier_t *node_barr;
pthread_barrier_t *node_barr2;
LocalFrontier *localFrontier;
};
template <class vertex>
void *BFSSubWorker(void *arg) {
BFS_subworker_arg *my_arg = (BFS_subworker_arg *)arg;
graph<vertex> &GA = *(graph<vertex> *)my_arg->GA;
const intT n = GA.n;
int tid = my_arg->tid;
int subTid = my_arg->subTid;
pthread_barrier_t *local_barr = my_arg->node_barr;
pthread_barrier_t *local_barr2 = my_arg->node_barr2;
pthread_barrier_t *global_barr = my_arg->global_barr;
LocalFrontier *output = my_arg->localFrontier;
intT *parents = my_arg->parents_ptr;
int currIter = 0;
intT rangeLow = my_arg->rangeLow;
intT rangeHi = my_arg->rangeHi;
intT start = my_arg->startPos;
intT end = my_arg->endPos;
intT numVisited = 0;
Subworker_Partitioner subworker(CORES_PER_NODE);
subworker.tid = tid;
subworker.subTid = subTid;
subworker.dense_start = start;
subworker.dense_end = end;
subworker.global_barr = global_barr;
subworker.local_barr = my_arg->node_barr2;
pthread_barrier_wait(local_barr);
if (subTid == 0)
Frontier->calculateNumOfNonZero(tid);
pthread_barrier_wait(global_barr);
struct timeval startT, endT;
struct timezone tz = {0, 0};
while(!Frontier->isEmpty() || currIter == 0){ //loop until frontier is empty
currIter++;
if (tid + subTid == 0) {
numVisited += Frontier->numNonzeros();
printf("num of non zeros: %" PRIintT "\n", Frontier->numNonzeros());
}
if (subTid == 0) {
//{parallel_for(intT i=output->startID;i<output->endID;i++) output->setBit(i, false);}
}
//pthread_barrier_wait(global_barr);
//apply edgemap
gettimeofday(&startT, &tz);
//edgeMap(GA, Frontier, BFS_F(parents), output, GA.n/20, DENSE_FORWARD, false, true, subworker);
//vertexCounter(GA, output, tid, subTid, CORES_PER_NODE);
edgeMapSparseAsyncPipe(GA, Frontier, BFS_F(parents), output, subworker);
if (subTid == 0) {
pthread_barrier_wait(global_barr);
switchFrontier(tid, Frontier, output); //set new frontier
} else {
output = Frontier->getFrontier(tid);
pthread_barrier_wait(global_barr);
}
if (subworker.isSubMaster()) {
Frontier->calculateNumOfNonZero(tid);
}
pthread_barrier_wait(global_barr);
gettimeofday(&endT, &tz);
double timeStart = ((double)startT.tv_sec) + ((double)startT.tv_usec) / 1000000.0;
double timeEnd = ((double)endT.tv_sec) + ((double)endT.tv_usec) / 1000000.0;
double mapTime = timeEnd - timeStart;
if (tid + subTid == 0) {
printf("edge map time: %lf\n", mapTime);
}
break;
}
if (tid + subTid == 0) {
cout << "Vertices visited = " << numVisited << "\n";
cout << "Finished in " << currIter << " iterations\n";
}
pthread_barrier_wait(local_barr);
return NULL;
}
template <class vertex>
void *BFSWorker(void *arg) {
BFS_worker_arg *my_arg = (BFS_worker_arg *)arg;
graph<vertex> &GA = *(graph<vertex> *)my_arg->GA;
int tid = my_arg->tid;
char nodeString[10];
sprintf(nodeString, "%d", tid);
struct bitmask *nodemask = numa_parse_nodestring(nodeString);
numa_bind(nodemask);
intT rangeLow = my_arg->rangeLow;
intT rangeHi = my_arg->rangeHi;
graph<vertex> localGraph = graphFilter(GA, rangeLow, rangeHi);
while (shouldStart == 0);
const intT n = GA.n;
int numOfT = my_arg->numOfNode;
intT blockSize = rangeHi - rangeLow;
intT *parents = parents_global;
for (intT i = rangeLow; i < rangeHi; i++) {
parents[i] = -1;
}
bool *frontier = (bool *)numa_alloc_local(sizeof(bool) * blockSize);
for(intT i=0;i<blockSize;i++) frontier[i] = false;
LocalFrontier *current = new LocalFrontier(frontier, rangeLow, rangeHi);
if (tid == 0)
Frontier = new vertices(numOfT);
pthread_barrier_wait(&barr);
Frontier->registerFrontier(tid, current);
pthread_barrier_wait(&barr);
if (tid == 0) {
Frontier->calculateOffsets();
Frontier->setBit(my_arg->start, true);
parents[my_arg->start] = my_arg->start;
}
if (my_arg->start >= rangeLow && my_arg->start < rangeHi) {
current->m = 1;
current->outEdgesCount = GA.V[my_arg->start].getOutDegree();
}
bool *next = (bool *)numa_alloc_local(sizeof(bool) * blockSize);
for (intT i = 0; i < blockSize; i++) next[i] = false;
LocalFrontier *output = new LocalFrontier(next, rangeLow, rangeHi);
intT sizeOfShards[CORES_PER_NODE];
partitionByDegree(GA, CORES_PER_NODE, sizeOfShards, sizeof(intT), true);
intT startPos = 0;
pthread_barrier_t localBarr;
pthread_barrier_init(&localBarr, NULL, CORES_PER_NODE+1);
pthread_barrier_t localBarr2;
pthread_barrier_init(&localBarr2, NULL, CORES_PER_NODE);
current->localQueue = (AsyncChunk **)malloc(sizeof(AsyncChunk *) * GA.n);
pthread_barrier_wait(&barr);
/*
if (tid == 0)
Frontier->toSparse();
*/
/*
current->s = (intT *)malloc(sizeof(intT) * GA.n);
current->s[0] = my_arg->start;
current->m = 1;
current->head = 0;
current->tail = 1;
*/
current->head = 0;
LocalFrontier *toInsert = Frontier->frontiers[(tid+1) % Frontier->numOfNodes];
if (my_arg->start >= rangeLow && my_arg->start < rangeHi) {
AsyncChunk *firstChunk = newChunk(64);
firstChunk->m = 1;
firstChunk->s[0] = my_arg->start;
toInsert->localQueue[0] = firstChunk;
toInsert->insertTail = 1;
toInsert->tail = 1;
printf("start vert %" PRIintT " from %d, %p\n", my_arg->start, tid, current->localQueue);
} else {
toInsert->insertTail = 0;
toInsert->tail = 0;
}
pthread_barrier_wait(&timerBarr);
pthread_t subTids[CORES_PER_NODE];
for (int i = 0; i < CORES_PER_NODE; i++) {
BFS_subworker_arg *arg = (BFS_subworker_arg *)malloc(sizeof(BFS_subworker_arg));
arg->GA = (void *)(&localGraph);
arg->tid = tid;
arg->subTid = i;
arg->rangeLow = rangeLow;
arg->rangeHi = rangeHi;
arg->parents_ptr = parents;
arg->node_barr = &localBarr;
arg->node_barr2 = &localBarr2;
arg->global_barr = &global_barr;
arg->localFrontier = output;
arg->startPos = startPos;
arg->endPos = startPos + sizeOfShards[i];
startPos = arg->endPos;
pthread_create(&subTids[i], NULL, BFSSubWorker<vertex>, (void *)arg);
}
pthread_barrier_wait(&localBarr);
//computation of subworkers
pthread_barrier_wait(&localBarr);
pthread_barrier_wait(&barr);
return NULL;
}
struct PR_Hash_F {
int shardNum;
intT vertPerShard;
intT n;
PR_Hash_F(intT _n, int _shardNum):n(_n), shardNum(_shardNum), vertPerShard(_n / _shardNum){}
inline intT hashFunc(intT index) {
if (index >= shardNum * vertPerShard) {
return index;
}
intT idxOfShard = index % shardNum;
intT idxInShard = index / shardNum;
return (idxOfShard * vertPerShard + idxInShard);
}
inline intT hashBackFunc(intT index) {
if (index >= shardNum * vertPerShard) {
return index;
}
intT idxOfShard = index / vertPerShard;
intT idxInShard = index % vertPerShard;
return (idxOfShard + idxInShard * shardNum);
}
};
template <class vertex>
void BFS(intT start, graph<vertex> &GA) {
numOfNode = 8;//numa_num_configured_nodes();
int numOfCpu = numa_num_configured_cpus();
CORES_PER_NODE = 1;//numOfCpu / numOfNode;
vPerNode = GA.n / numOfNode;
pthread_barrier_init(&barr, NULL, numOfNode);
pthread_barrier_init(&global_barr, NULL, numOfNode * CORES_PER_NODE);
pthread_barrier_init(&timerBarr, NULL, numOfNode+1);
intT sizeArr[numOfNode];
PR_Hash_F hasher(GA.n, numOfNode);
graphHasher(GA, hasher);
partitionByDegree(GA, numOfNode, sizeArr, sizeof(intT));
parents_global = (intT *)mapDataArray(numOfNode, sizeArr, sizeof(intT));
printf("start create %d threads\n", numOfNode);
pthread_t tids[numOfNode];
intT prev = 0;
for (int i = 0; i < numOfNode; i++) {
BFS_worker_arg *arg = (BFS_worker_arg *)malloc(sizeof(BFS_worker_arg));
arg->GA = (void *)(&GA);
arg->tid = i;
arg->numOfNode = numOfNode;
arg->rangeLow = prev;
arg->rangeHi = prev + sizeArr[i];
arg->start = hasher.hashFunc(start);
prev = prev + sizeArr[i];
pthread_create(&tids[i], NULL, BFSWorker<vertex>, (void *)arg);
}
shouldStart = 1;
pthread_barrier_wait(&timerBarr);
//nextTime("Graph Partition");
startTime();
printf("all created\n");
for (int i = 0; i < numOfNode; i++) {
pthread_join(tids[i], NULL);
}
nextTime("BFS");
if (needResult) {
intT counter = 0;
for (intT i = 0; i < GA.n; i++) {
if (parents_global[i] != -1)
counter++;
}
printf("Vert visited: %" PRIintT "\n", counter);
}
}
int parallel_main(int argc, char* argv[]) {
char* iFile;
bool binary = false;
bool symmetric = false;
intT start = 0;
if(argc > 1) iFile = argv[1];
if(argc > 2) start = atoi(argv[2]);
if(argc > 3) if((string) argv[3] == (string) "-result") needResult = true;
//pass -s flag if graph is already symmetric
if(argc > 4) if((string) argv[4] == (string) "-s") symmetric = true;
//pass -b flag if using binary file (also need to pass 2nd arg for now)
if(argc > 5) if((string) argv[5] == (string) "-b") binary = true;
if(symmetric) {
graph<symmetricVertex> G =
readGraph<symmetricVertex>(iFile,symmetric,binary); //symmetric graph
BFS((intT)start,G);
G.del();
} else {
graph<asymmetricVertex> G =
readGraph<asymmetricVertex>(iFile,symmetric,binary); //asymmetric graph
BFS((intT)start,G);
G.del();
}
}