-
Notifications
You must be signed in to change notification settings - Fork 2
/
hybrid_bfs.cc
443 lines (399 loc) · 13.9 KB
/
hybrid_bfs.cc
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
#include "hybrid_bfs.h"
#include "ack_control.h"
#include <emu_cxx_utils/execution_policy.h>
#include <emu_cxx_utils/for_each.h>
#include <emu_cxx_utils/find.h>
using namespace emu;
hybrid_bfs::hybrid_bfs(graph & g)
: g_(&g)
, parent_(g.num_vertices())
, new_parent_(g.num_vertices())
, queue_(g.num_vertices())
, scout_count_(0L)
, awake_count_(0L)
, worklist_(g.num_vertices())
{
// Force ack controller singleton to initialize itself
ack_control_init();
}
// Shallow copy constructor
hybrid_bfs::hybrid_bfs(const hybrid_bfs& other, emu::shallow_copy shallow)
: g_(other.g_)
, parent_(other.parent_, shallow)
, new_parent_(other.new_parent_, shallow)
, queue_(other.queue_, shallow)
, scout_count_(other.scout_count_)
, awake_count_(other.awake_count_)
, worklist_(other.worklist_, shallow)
{}
/**
* Top-down BFS step ("migrating threads" variant)
* For each edge in the frontier, migrate to the dst vertex's nodelet
* If the dst vertex doesn't have a parent, set src as parent
* Then append to local queue for next frontier
* Return the sum of the degrees of the vertices in the new frontier
*/
long
hybrid_bfs::top_down_step_with_remote_writes()
{
ack_control_disable_acks();
// For each vertex in the queue...
queue_.forall_items([&](long src) {
// for each neighbor of that vertex...
g_->for_each_out_edge(src, [&](long dst) {
// write the vertex ID to the neighbor's new_parent entry.
new_parent_[dst] = src; // Remote write
});
});
ack_control_reenable_acks();
// Add to the queue all vertices that didn't have a parent before
scout_count_ = 0;
g_->for_each_vertex([&](long v) {
if (parent_[v] < 0 && new_parent_[v] >= 0) {
// Update count with degree of new vertex
REMOTE_ADD(&scout_count_, -parent_[v]);
// Set parent
parent_[v] = new_parent_[v];
// Add to the queue for the next frontier
queue_.push_back(v);
}
});
// Combine per-nodelet values of scout_count
return emu::repl_reduce(scout_count_, std::plus<>());
}
long
hybrid_bfs::top_down_step_with_migrating_threads()
{
// Spawn a thread on each nodelet to process the local queue
// For each neighbor without a parent, add self as parent and append to queue
scout_count_ = 0;
worklist_.clear_all();
queue_.forall_items([this](long src) {
worklist_.append(src, g_->out_edges_begin(src), g_->out_edges_end(src));
});
worklist_.process_all_edges(dynamic_unroll_policy<64>(),
[this](long src, long dst) {
// Look up the parent of the vertex we are visiting
long * parent = &parent_[dst];
long curr_val = *parent;
// If we are the first to visit this vertex
if (curr_val < 0) {
// Set self as parent of this vertex
if (atomic_cas(parent, curr_val, src) == curr_val) {
// Add it to the queue
queue_.push_back(dst);
remote_add(&scout_count_, -curr_val);
}
}
}
);
// Combine per-nodelet values of scout_count
return repl_reduce(scout_count_, std::plus<>());
}
/**
* Bottom-up BFS step
* For each vertex that is not yet a part of the BFS tree,
* check all in-neighbors to see if they are in the current frontier
* using a replicated bitmap.
* If a parent is found, put the child in the bitmap for the next frontier
* Returns the number of vertices that found a parent (size of next frontier)
*/
long
hybrid_bfs::bottom_up_step()
{
awake_count_ = 0;
// For all vertices without a parent...
g_->for_each_vertex(fixed, [this](long child) {
if (parent_[child] >= 0) { return; }
// Look for neighbors who are in the frontier
g_->find_out_edge_if(unroll, child, [this, child](long parent) {
// If the neighbor is in the frontier...
if (parent_[parent] >= 0) {
// Claim as a parent
new_parent_[child] = parent;
// No need to keep searching
return true;
} else return false;
});
});
// Add to the queue all vertices that didn't have a parent before
g_->for_each_vertex(fixed, [this](long v) {
if (parent_[v] < 0 && new_parent_[v] >= 0) {
// Set parent
parent_[v] = new_parent_[v];
// Add to the queue for the next frontier
queue_.push_back(v);
// Track number of vertices woken up in this step
remote_add(&awake_count_, 1);
}
});
return repl_reduce(awake_count_, std::plus<>());
}
/**
* Run BFS using Scott Beamer's direction-optimizing algorithm
* 1. Do top-down steps with migrating threads until condition is met
* 2. Do bottom-up steps until condition is met
* 3. Do top-down steps with migrating threads until done
*/
void
hybrid_bfs::run_beamer (long source, long alpha, long beta)
{
assert(source < g_->num_vertices());
// Start with the source vertex in the first frontier, at level 0, and mark it as visited
queue_.push_back(source);
queue_.slide_all_windows();
parent_[source] = source;
long edges_to_check = g_->num_edges() * 2;
long scout_count = g_->out_degree(source);
// While there are vertices in the queue...
while (!queue_.all_empty()) {
if (scout_count > edges_to_check / alpha) {
long awake_count, old_awake_count;
awake_count = queue_.combined_size();
// Do bottom-up steps for a while
do {
old_awake_count = awake_count;
// hooks_set_attr_i64("awake_count", awake_count);
// hooks_region_begin("bottom_up_step");
awake_count = bottom_up_step();
queue_.slide_all_windows();
// hooks_region_end();
} while (awake_count >= old_awake_count ||
(awake_count > g_->num_vertices() / beta));
scout_count = 1;
} else {
edges_to_check -= scout_count;
// Do a top-down step
// hooks_region_begin("top_down_step");
scout_count = top_down_step_with_migrating_threads();
// Slide all queues to explore the next frontier
queue_.slide_all_windows();
// hooks_region_end();
}
}
}
/**
* Run BFS using top-down steps with migrating threads
*/
void
hybrid_bfs::run_with_migrating_threads(long source)
{
assert(source < g_->num_vertices());
// Start with the source vertex in the first frontier, at level 0, and mark it as visited
queue_.get_nth(0).push_back(source);
queue_.slide_all_windows();
parent_[source] = source;
// While there are vertices in the queue...
while (!queue_.all_empty()) {
// Explore the frontier
top_down_step_with_migrating_threads();
// Slide all queues to explore the next frontier
queue_.slide_all_windows();
}
}
/**
* Run BFS using top-down steps with remote writes
*/
void
hybrid_bfs::run_with_remote_writes(long source)
{
assert(source < g_->num_vertices());
// Start with the source vertex in the first frontier, at level 0, and mark it as visited
queue_.get_nth(0).push_back(source);
queue_.slide_all_windows();
parent_[source] = source;
// While there are vertices in the queue...
while (!queue_.all_empty()) {
// Explore the frontier
top_down_step_with_remote_writes();
// Slide all queues to explore the next frontier
queue_.slide_all_windows();
}
}
/**
* Run BFS using a hybrid algorithm
* 1. Do top-down steps with migrating threads until condition is met
* 2. Do top-down steps with remote writes until condition is met
* 3. Do top-down steps with migrating threads until done
*/
void
hybrid_bfs::run_with_remote_writes_hybrid(long source, long alpha, long beta)
{
assert(source < g_->num_vertices());
// Start with the source vertex in the first frontier, at level 0, and mark it as visited
queue_.get_nth(0).push_back(source);
queue_.slide_all_windows();
parent_[source] = source;
long edges_to_check = g_->num_edges() * 2;
long scout_count = g_->out_degree(source);
// While there are vertices in the queue...
while (!queue_.all_empty()) {
if (scout_count > edges_to_check / alpha) {
long awake_count, old_awake_count;
awake_count = queue_.combined_size();
// Do remote-write steps for a while
do {
old_awake_count = awake_count;
top_down_step_with_remote_writes();
queue_.slide_all_windows();
awake_count = queue_.combined_size();
} while (awake_count >= old_awake_count ||
(awake_count > g_->num_vertices() / beta));
scout_count = 1;
} else {
edges_to_check -= scout_count;
scout_count = top_down_step_with_migrating_threads();
// Slide all queues to explore the next frontier
queue_.slide_all_windows();
}
}
}
bool
hybrid_bfs::check(long source)
{
// Local array to store the depth of each vertex in the tree
std::vector<long> depth(g_->num_vertices(), -1);
// Do a serial BFS
std::queue<long> q;
// Push source into the queue
q.push(source);
depth[source] = 0;
// For each vertex in the queue...
while (!q.empty()) {
long u = q.front(); q.pop();
// For each out-neighbor of this vertex...
auto edges_begin = g_->out_neighbors(u);
auto edges_end = edges_begin + g_->out_degree(u);
for (auto e = edges_begin; e < edges_end; ++e) {
long v = *e;
// Add unexplored neighbors to the queue
if (depth[v] == -1) {
depth[v] = depth[u] + 1;
q.push(v);
}
}
}
// // Dump the tree to stdout
// // For each level in the tree...
// bool is_level_empty = false;
// for (long current_depth = 0; !is_level_empty; ++current_depth) {
// is_level_empty = true;
// printf("Level %li:", current_depth);
// for (long v = 0; v < g_->num_vertices(); ++v) {
// // Print every vertex at this depth in the tree
// if (depth[v] == current_depth) {
// printf(" %li", v);
// // Keep going as long as we find a vertex at each level
// is_level_empty = false;
// }
// }
// printf("\n");
// }
// fflush(stdout);
//
// print_tree();
// Check each vertex
// We are comparing the parent array produced by the parallel BFS
// with the depth array produced by the serial BFS
bool correct = true;
for (long u = 0; u < g_->num_vertices(); ++u) {
// Is the vertex a part of both BFS trees?
if (depth[u] >= 0 && parent_[u] >= 0) {
// Special case for source vertex
if (u == source) {
if (!((parent_[u] == u) && (depth[u] == 0))) {
LOG("Source wrong\n");
correct = false;
break;
}
continue;
}
// Verify that this vertex is connected to its parent
bool parent_found = false;
// For all in-edges...
auto edges_begin = g_->out_neighbors(u);
auto edges_end = edges_begin + g_->out_degree(u);
for (auto iter = edges_begin; iter < edges_end; ++iter) {
long v = *iter;
// If v is the parent of u, their depths should differ by 1
if (v == parent_[u]) {
if (depth[v] != depth[u] - 1) {
LOG("Wrong depths for %li and %li\n", u, v);
break;
}
parent_found = true;
break;
}
}
if (!parent_found) {
LOG("Couldn't find edge from %li to %li\n", parent_[u], u);
correct = false;
break;
}
// Do both trees agree about whether this vertex is in the tree?
} else if ((depth[u] < 0) != (parent_[u] < 0)) {
LOG("Reachability mismatch: depth[%li] = %li, parent[%li] = %li\n",
u, depth[u], u, parent_[u]);
correct = false;
break;
}
}
return correct;
}
void
hybrid_bfs::print_tree()
{
for (long v = 0; v < g_->num_vertices(); ++v) {
long parent = parent_[v];
if (parent < 0) { continue; }
printf("%4li", v);
// Climb the tree back to the root
while(true) {
LOG(" <- %4li", parent);
if (parent == -1) { break; }
if (parent == parent_[parent]) { break; }
parent = parent_[parent];
}
printf("\n");
}
fflush(stdout);
}
long
hybrid_bfs::count_num_traversed_edges()
{
auto repl_sum = emu::make_repl<long>(0);
long * sum = &*repl_sum;
g_->for_each_vertex(fixed, [this, sum] (long v) {
if (parent_[v] >= 0) {
emu::remote_add(sum, g_->out_degree(v));
}
});
// Divide by two, since each undirected edge is counted twice
return emu::repl_reduce(*repl_sum, std::plus<>()) / 2;
}
void
hybrid_bfs::dump_queue_stats()
{
printf("Queue contents: ");
queue_.dump_all();
printf("\n");
fflush(stdout);
printf("Frontier size per nodelet: ");
for (long n = 0; n < NODELETS(); ++n) {
sliding_queue & local_queue = queue_.get_nth(n);
printf("%li ", local_queue.size());
}
printf("\n");
fflush(stdout);
}
void
hybrid_bfs::clear()
{
g_->for_each_vertex([&](long v) {
long out_degree = g_->out_degree(v);
parent_[v] = out_degree != 0 ? -out_degree : -1;
new_parent_[v] = -1;
});
// Reset the queue
queue_.reset_all();
}