forked from fredhohman/atlas-algorithm
/
parallelkcore.cpp
476 lines (417 loc) · 14.7 KB
/
parallelkcore.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
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#include <iostream>
#include <fstream>
#include <algorithm>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <omp.h>
#define DEBUG 1
// A struct to represent an edge in the edge list
struct edge {
unsigned int src;
unsigned int tgt;
};
struct graph {
unsigned int NODENUM;
unsigned int EDGENUM;
unsigned int *start_indices;
unsigned int *end_indices;
edge *edgeList;
}g;
// Utility function to print a given array
template <class T>
void printArray(T *arr, unsigned int n) {
for(unsigned int i = 0; i < n; i++) {
std::cout<<arr[i]<<" ";
}
std::cout<<"\n";
}
long long currentTimeMilliS = 0;
long long currentTimeStamp() {
struct timeval te;
gettimeofday(&te, NULL); // get current time
long long milliseconds = te.tv_sec*1000LL + te.tv_usec/1000; // calculate milliseconds
return milliseconds;
}
void reset() {
currentTimeMilliS = currentTimeStamp();
}
long long getTimeElapsed() {
long long newTime = currentTimeStamp();
long long timeElapsed = newTime - currentTimeMilliS;
currentTimeMilliS = newTime;
return timeElapsed;
}
// Memory maps input file
void createMemoryMap(char *fileName) {
unsigned int binFile = open(fileName, O_RDWR);
long fileSizeInByte;
struct stat sizeResults;
assert(stat(fileName, &sizeResults) == 0);
fileSizeInByte = sizeResults.st_size;
g.edgeList = (edge *)mmap(NULL, fileSizeInByte, PROT_READ | PROT_WRITE, MAP_SHARED, binFile, 0);
close(binFile);
}
// In-memory edge list instead of memory mapped file
void createInMemoryEdgeList(char *fileName) {
g.edgeList = new edge[g.EDGENUM];
std::ifstream is;
is.open(fileName, std::ios::in | std::ios::binary);
unsigned int src, tgt;
unsigned int updatedEdgeNum = g.EDGENUM;
for(unsigned int i = 0; i < g.EDGENUM; i++) {
is.read((char *)(&src), sizeof(unsigned int));
is.read((char *)(&tgt), sizeof(unsigned int));
assert(src >= 0 && src <= g.NODENUM);
assert(tgt >= 0 && tgt <= g.NODENUM);
(g.edgeList + i)->src = src;
(g.edgeList + i)->tgt = tgt;
}
is.close();
}
void doubleAndReverseGraph(char *inputFile, char *outputFile) {
std::ifstream is;
is.open(inputFile, std::ios::in | std::ios::binary);
std::ofstream os;
os.open(outputFile, std::ios::out | std::ios::binary | std::ios::app);
unsigned int src, tgt;
unsigned int updatedEdgeNum = g.EDGENUM;
for(unsigned int i = 0; i < g.EDGENUM; i++) {
is.read((char *)(&src), sizeof(unsigned int));
is.read((char *)(&tgt), sizeof(unsigned int));
src = htonl(src);
tgt = htonl(tgt);
assert(src >= 0 && src <= g.NODENUM);
assert(tgt >= 0 && tgt <= g.NODENUM);
// Removes self loops
if(src != tgt) {
os.write((char *)&src, sizeof(unsigned int));
os.write((char *)&tgt, sizeof(unsigned int));
}
else {
updatedEdgeNum--;
}
}
is.seekg(0, std::ios::beg);
for(unsigned int i = 0; i < g.EDGENUM; i++) {
is.read((char *)(&src), sizeof(unsigned int));
is.read((char *)(&tgt), sizeof(unsigned int));
src = htonl(src);
tgt = htonl(tgt);
assert(src >= 0 && src <= g.NODENUM);
assert(tgt >= 0 && tgt <= g.NODENUM);
// Removes self loops
if(src != tgt) {
os.write((char *)(&tgt), sizeof(unsigned int));
os.write((char *)(&src), sizeof(unsigned int));
}
}
g.EDGENUM = updatedEdgeNum;
g.EDGENUM *= 2;
is.close();
os.close();
}
bool compareEdges(const edge& a, const edge& b) {
if(a.src < b.src)
return true;
if(a.src == b.src) {
if(a.tgt < b.tgt)
return true;
}
return false;
}
// Compares indices according to their corresponding edges
int compareByEdges(const void * a, const void * b) {
if ((g.edgeList + *(unsigned int *)a)->src < (g.edgeList + *(unsigned int *)b)->src)
return -1;
if ((g.edgeList + *(unsigned int *)a)->src == (g.edgeList + *(unsigned int *)b)->src){
if ((g.edgeList + *(unsigned int *)a)->tgt < (g.edgeList + *(unsigned int *)b)->tgt)
return -1;
if ((g.edgeList + *(unsigned int *)a)->tgt == (g.edgeList + *(unsigned int *)b)->tgt)
return 0;
if ((g.edgeList + *(unsigned int *)a)->tgt > (g.edgeList + *(unsigned int *)b)->tgt)
return 1;
}
if ((g.edgeList + *(unsigned int *)a)->src > (g.edgeList + *(unsigned int *)b)->src)
return 1;
}
// Formats the graph by sorting it and tracing original indices in the graph
void formatGraph(unsigned int *originalIndices) {
unsigned int *indices = new unsigned int[g.EDGENUM];
for(unsigned int i = 0; i < g.EDGENUM; i++) {
indices[i] = i;
}
qsort(indices, g.EDGENUM, sizeof(unsigned int), compareByEdges);
for(unsigned int i = 0; i < g.EDGENUM; i++) {
originalIndices[indices[i]] = i;
}
std::sort(g.edgeList, g.edgeList + g.EDGENUM, compareEdges);
//qsort(g.edgeList, g.EDGENUM, sizeof(edge), compareByEdges);
delete [] indices;
}
// Finds the start and end indices of each node in the graph
void findStartAndEndIndices() {
g.start_indices = new unsigned int[g.NODENUM + 1];
g.end_indices = new unsigned int[g.NODENUM + 1];
std::fill_n(g.start_indices, g.NODENUM + 1, 0);
std::fill_n(g.end_indices, g.NODENUM + 1, 0);
unsigned int i;
unsigned int old = g.edgeList->src;
g.start_indices[old] = 0;
for(i = 0; i < g.EDGENUM; i++) {
if((g.edgeList + i)->src != old) {
g.end_indices[old] = i - 1;
old = (g.edgeList + i)->src;
g.start_indices[old] = i;
}
}
g.end_indices[old] = i - 1;
}
bool isGraphEmpty(unsigned int *edgeLabels) {
for(unsigned int i = 0; i < g.EDGENUM; i++) {
if(edgeLabels[i] == -1)
return false;
}
return true;
}
// Computes the degree of each node in the graph
void findDegree(unsigned int *edgeLabels, float *degree) {
std::fill_n(degree, g.NODENUM + 1, 0);
for(unsigned int i = 0; i < g.EDGENUM; i++) {
// If edge hasn't been deleted yet. An edge is considered deleted
// when it has been labeled.
if(edgeLabels[i] == -1) {
degree[(g.edgeList + i)->src]++;
degree[(g.edgeList + i)->tgt]++;
}
}
for(unsigned int i = 0; i < g.NODENUM + 1; i++) {
degree[i] /= 2;
}
}
void scan(unsigned int *deg, unsigned int level, unsigned int* curr, long *currTailPtr) {
// Size of cache line
const long BUFFER_SIZE_BYTES = 2048;
const long BUFFER_SIZE = BUFFER_SIZE_BYTES/sizeof(unsigned int);
unsigned int buff[BUFFER_SIZE];
long index = 0;
#pragma omp for schedule(static)
for (unsigned int i = 0; i < g.NODENUM + 1; i++) {
if (deg[i] == level) {
buff[index] = i;
index++;
if (index >= BUFFER_SIZE) {
long tempIdx = __sync_fetch_and_add(currTailPtr, BUFFER_SIZE);
for (long j = 0; j < BUFFER_SIZE; j++) {
curr[tempIdx+j] = buff[j];
}
index = 0;
}
}
}
if (index > 0) {
long tempIdx = __sync_fetch_and_add(currTailPtr, index);
for (long j = 0; j < index; j++)
curr[tempIdx+j] = buff[j];
}
#pragma omp barrier
}
void processSubLevel(unsigned int *curr, long currTail,
unsigned int *deg, unsigned int *edgeLabels, unsigned int level, unsigned int *next, long *nextTailPtr) {
// Size of cache line
const long BUFFER_SIZE_BYTES = 2048;
const long BUFFER_SIZE = BUFFER_SIZE_BYTES/sizeof(unsigned int);
unsigned int buff[BUFFER_SIZE];
long index = 0;
#pragma omp for schedule(static)
for (long i = 0; i < currTail; i++) {
unsigned int v = curr[i];
// Do nothing if node doesn't exist in the graph
if(g.start_indices[v] == 0 && g.end_indices[v] == 0) {
;
}
else {
//For all neighbors of vertex v
for (unsigned int j = g.start_indices[v]; j <= g.end_indices[v]; j++) {
if(edgeLabels[j] == -1) {
unsigned int u = (g.edgeList + j)->tgt;
if (deg[u] > level) {
unsigned int du = __sync_fetch_and_sub(°[u], 1);
if (du == (level+1)) {
buff[index] = u;
index++;
if (index >= BUFFER_SIZE) {
long tempIdx = __sync_fetch_and_add(nextTailPtr, BUFFER_SIZE);
for(long bufIdx = 0; bufIdx < BUFFER_SIZE; bufIdx++)
next [tempIdx + bufIdx] = buff[bufIdx];
index = 0;
}
}
}
}
}
}
}
if (index > 0) {
long tempIdx = __sync_fetch_and_add(nextTailPtr, index);;
for (long bufIdx = 0; bufIdx < index; bufIdx++)
next [tempIdx + bufIdx] = buff[bufIdx];
}
#pragma omp barrier
#pragma omp for schedule(static)
for (long i = 0; i < *nextTailPtr; i++) {
unsigned int u = next[i];
if (deg[u] != level)
deg[u] = level;
}
#pragma omp barrier
}
//ParK to compute k core decomposition in parallel
void parKCore(unsigned int *deg, unsigned int *edgeLabels) {
unsigned int *curr = new unsigned int[g.NODENUM];
unsigned int *next = new unsigned int[g.NODENUM];
long currTail = 0;
long nextTail = 0;
#pragma omp parallel
{
unsigned int tid = omp_get_thread_num();
long todo = g.NODENUM;
unsigned int level = 0;
while (todo > 0) {
scan(deg, level, curr, &currTail);
while (currTail > 0) {
todo = todo - currTail;
processSubLevel(curr, currTail, deg, edgeLabels, level, next, &nextTail);
if (tid == 0) {
unsigned int *tempCurr = curr;
curr = next;
next = tempCurr;
currTail = nextTail;
nextTail = 0;
}
#pragma omp barrier
}
level = level + 1;
#pragma omp barrier
}
}
delete [] curr;
delete [] next;
}
void labelEdgesAndUpdateDegree(unsigned int peel, bool *isFinalNode, float *degree, unsigned int *edgeLabels) {
for(unsigned int i = 0; i < g.EDGENUM; i++) {
unsigned int src = (g.edgeList + i)->src;
unsigned int tgt = (g.edgeList + i)->tgt;
if(isFinalNode[src] && isFinalNode[tgt] && edgeLabels[i] == -1) {
edgeLabels[i] = peel;
degree[src] -= 0.5;
degree[tgt] -= 0.5;
}
}
}
void labelAndDeletePeelOneEdges(float *degree, unsigned int *edgeLabels) {
float *tmp = new float[g.NODENUM + 1];
std::copy(degree, degree + g.NODENUM + 1, tmp);
for(unsigned int i = 0; i < g.EDGENUM; i++) {
unsigned int src = (g.edgeList + i)->src;
unsigned int tgt = (g.edgeList + i)->tgt;
if(edgeLabels[i] == -1) {
if(tmp[src] == 1 || tmp[tgt] == 1) {
edgeLabels[i] = 1;
degree[src] -= 0.5;
degree[tgt] -= 0.5;
}
}
}
delete [] tmp;
}
void writeToFile(unsigned int *edgeIndices, unsigned int *edgeLabels) {
std::ofstream outputFile;
outputFile.open("graph-decomposition.csv");
for(unsigned int i = 0; i < g.EDGENUM; i++) {
outputFile<<(g.edgeList + edgeIndices[i])->src<<","<<(g.edgeList + edgeIndices[i])->tgt<<","<<edgeLabels[i]<<"\n";
}
outputFile.close();
}
void writeMetaData(unsigned int NODENUM, unsigned int EDGENUM, long long preprocessingTime, long long algorithmTime) {
std::ofstream outputFile;
outputFile.open("graph-decomposition-info.json");
outputFile<<"{\n";
outputFile<<"\"vertices\":"<<NODENUM<<",\n";
outputFile<<"\"edges\":"<<EDGENUM<<",\n";
outputFile<<"\"preprocessing-time\":"<<preprocessingTime<<",\n";
outputFile<<"\"algorithm-time\":"<<algorithmTime<<"\n}";
outputFile.close();
}
int main(int argc, char *argv[]) {
char *tmpFile = "tmp.bin";
remove(tmpFile);
g.EDGENUM = atoi(argv[2]);
g.NODENUM = atoi(argv[3]);
reset();
doubleAndReverseGraph(argv[1], tmpFile);
if(DEBUG)
std::cout<<"DOUBLED AND REVERSED GRAPH\n";
unsigned int *originalIndices = new unsigned int[g.EDGENUM];
unsigned int *edgeLabels = new unsigned int[g.EDGENUM];
std::fill_n(edgeLabels, g.EDGENUM, -1);
createMemoryMap(tmpFile);
if(DEBUG)
std::cout<<"CREATED MEMORY MAP\n";
formatGraph(originalIndices);
long long preprocessingTime = getTimeElapsed();
reset();
if(DEBUG)
std::cout<<"FORMATTED GRAPH\n";
findStartAndEndIndices();
if(DEBUG)
std::cout<<"START AND END INDICES COMPUTED\n";
float *degree = new float[g.NODENUM + 1];
findDegree(edgeLabels, degree);
unsigned int *core = new unsigned int[g.NODENUM + 1];
while(!isGraphEmpty(edgeLabels)) {
std::copy(degree, degree + g.NODENUM + 1, core);
parKCore(core, edgeLabels);
unsigned int mc = *std::max_element(core, core + g.NODENUM + 1);
if(DEBUG)
std::cout<<"CURRENT MAXIMUM CORE : "<<mc<<"\n";
bool *isFinalNode = new bool[g.NODENUM + 1];
std::fill_n(isFinalNode, g.NODENUM + 1, false);
for(unsigned int i = 0; i <= g.NODENUM; i++) {
if(core[i] == mc) {
isFinalNode[i] = true;
}
}
labelEdgesAndUpdateDegree(mc, isFinalNode, degree, edgeLabels);
delete [] isFinalNode;
}
g.EDGENUM /= 2;
unsigned int *originalLabels = new unsigned int[g.EDGENUM];
if(DEBUG)
std::cout<<"RECONSTRUCTING ORIGINAL LABELS\n";
for(unsigned int i = 0; i < g.EDGENUM; i++) {
originalLabels[i] = edgeLabels[originalIndices[i]];
}
long long algorithmTime = getTimeElapsed();
writeToFile(originalIndices, originalLabels);
writeMetaData(atoi(argv[3]), atoi(argv[2]), preprocessingTime, algorithmTime);
remove(tmpFile);
delete [] core;
delete [] degree;
delete [] g.start_indices;
delete [] g.end_indices;
delete [] edgeLabels;
delete [] originalLabels;
delete [] originalIndices;
return 0;
}