forked from NVSL/blaze
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbc_sync.cpp
More file actions
236 lines (185 loc) · 6.07 KB
/
bc_sync.cpp
File metadata and controls
236 lines (185 loc) · 6.07 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
#include <iostream>
#include <string>
#include <queue>
#include <deque>
#include <math.h>
#include "llvm/Support/CommandLine.h"
#include "galois/Galois.h"
#include "galois/Bag.h"
#include "Type.h"
#include "Graph.h"
#include "atomics.h"
#include "Array.h"
#include "Util.h"
#include "EdgeMap.h"
#include "boilerplate.h"
#include "Runtime.h"
#include "VertexMap.h"
#include "PageRank.h"
namespace cll = llvm::cl;
using namespace blaze;
static cll::opt<unsigned int>
startNode("startNode",
cll::desc("Node to start search from (default value 0)"),
cll::init(0));
static cll::opt<std::string>
inIndexFilename("inIndexFilename", cll::desc("<in index file>"), cll::Required);
static cll::list<std::string>
inAdjFilenames("inAdjFilenames", cll::desc("<in adj files>"), cll::OneOrMore);
struct Node {
float num_paths;
float dependencies;
float inverse_num_paths;
bool visited;
};
struct BC_F : public EDGEMAP_F<uint32_t> {
Array<float>& num_paths;
Bitmap& visited;
BC_F(Array<float>& n, Bitmap& v): num_paths(n), visited(v) {}
inline bool update(VID src, VID dst) {
float oldV = num_paths[dst];
num_paths[dst] += num_paths[src];
return oldV == 0.0;
}
inline bool updateAtomic(VID src, VID dst) {
float oldV, newV;
do {
oldV = num_paths[dst];
newV = oldV + num_paths[src];
} while (!compare_and_swap(num_paths[dst], oldV, newV));
return oldV == 0.0;
}
inline bool cond(VID dst) {
return !visited.get_bit(dst);
}
};
struct BC_Back_F : public EDGEMAP_F<uint32_t> {
Array<float>& dependencies;
Bitmap& visited;
BC_Back_F(Array<float>& d, Bitmap& v): dependencies(d), visited(v) {}
inline bool update(VID src, VID dst) {
float oldV = dependencies[dst];
dependencies[dst] += dependencies[src];
return oldV == 0.0;
}
inline bool updateAtomic(VID src, VID dst) {
float oldV, newV;
do {
oldV = dependencies[dst];
newV = oldV + dependencies[src];
} while (!compare_and_swap(dependencies[dst], oldV, newV));
return oldV == 0.0;
}
inline bool cond(VID dst) {
return !visited.get_bit(dst);
}
};
struct BC_Vertex_F {
Bitmap& visited;
BC_Vertex_F(Bitmap& v): visited(v) {}
inline bool operator() (const VID& node) {
visited.set_bit_atomic(node);
return 1;
}
};
struct BC_Back_Vertex_F {
Array<float>& dependencies;
Array<float>& inverse_num_paths;
Bitmap& visited;
BC_Back_Vertex_F(Array<float>& d, Array<float>& i, Bitmap& v)
: dependencies(d), inverse_num_paths(i), visited(v) {}
inline bool operator() (const VID& node) {
visited.set_bit_atomic(node);
dependencies[node] += inverse_num_paths[node];
return 1;
}
};
void printTopBC(Array<float>& dependencies, unsigned topn = PRINT_TOP) {
typedef std::map<Pair, VID> TopMap;
TopMap top;
uint64_t n = dependencies.size();
for (uint64_t src = 0; src < n; src++) {
if (isnan(dependencies[src])) continue;
Pair key(dependencies[src], src);
if (top.size() < topn) {
top.insert(std::make_pair(key, src));
continue;
}
if (top.begin()->first < key) {
top.erase(top.begin());
top.insert(std::make_pair(key, src));
}
}
int rank = 1;
std::cout << "Rank BetweennessCentrality Id\n";
for (auto ii = top.rbegin(), ei = top.rend(); ii != ei; ++ii, ++rank) {
float value = ii->first.value;
VID node = ii->first.id;
printf("%3d: %20.10f %10u\n", rank, value, node);
}
}
int main(int argc, char **argv) {
AgileStart(argc, argv);
Runtime runtime(numComputeThreads, numIoThreads, ioBufferSize * MB);
// Out graph
Graph outGraph;
outGraph.BuildGraph(outIndexFilename, outAdjFilenames);
// In graph
Graph inGraph;
inGraph.BuildGraph(inIndexFilename, inAdjFilenames);
uint64_t n = outGraph.NumberOfNodes();
Array<float> num_paths;
num_paths.allocate(n);
Array<float> dependencies;
dependencies.allocate(n);
Array<float> inverse_num_paths;
inverse_num_paths.allocate(n);
Bitmap visited(n);
visited.reset_parallel();
galois::StatTimer time("Time", "BC_MAIN");
time.start();
galois::do_all(galois::iterate(outGraph),
[&](const VID& node) {
num_paths[node] = 0.0;
});
num_paths[startNode] = 1.0;
visited.set_bit(startNode);
Worklist<VID>* frontier = new Worklist<VID>(n);
frontier->activate(startNode);
std::vector<Worklist<VID>*> levels;
levels.push_back(frontier);
long round = 0;
while (!frontier->empty()) {
round++;
Worklist<VID>* output = edgeMap(outGraph, frontier, BC_F(num_paths, visited), 0);
vertexMap(output, BC_Vertex_F(visited));
levels.push_back(output);
frontier = output;
}
galois::do_all(galois::iterate(outGraph),
[&](const VID& node) {
dependencies[node] = 0.0;
inverse_num_paths[node] = 1 / num_paths[node];
});
delete levels[round];
// reuse visited
visited.reset_parallel();
frontier = levels[round-1];
vertexMap(frontier, BC_Back_Vertex_F(dependencies, inverse_num_paths, visited));
// backward phase
for (long r = round - 2; r >= 0; r--) {
edgeMap(inGraph, frontier, BC_Back_F(dependencies, visited), no_output);
delete frontier;
frontier = levels[r];
vertexMap(frontier, BC_Back_Vertex_F(dependencies, inverse_num_paths, visited));
}
delete frontier;
// update dependencies scores
galois::do_all(galois::iterate(outGraph),
[&](const VID& node) {
dependencies[node] = (dependencies[node] - inverse_num_paths[node]) / inverse_num_paths[node];
});
time.stop();
printTopBC(dependencies);
return 0;
}