-
Notifications
You must be signed in to change notification settings - Fork 2
/
hybrid_bfs_main.cc
309 lines (285 loc) · 10.5 KB
/
hybrid_bfs_main.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
#include <cstring>
#include <getopt.h>
#include "graph.h"
#include "dist_edge_list.h"
#include "hybrid_bfs.h"
#include "lcg.h"
#include "git_sha1.h"
const struct option long_options[] = {
{"graph_filename" , required_argument},
{"distributed_load" , no_argument},
{"heavy_threshold" , required_argument},
{"num_trials" , required_argument},
{"source_vertex" , required_argument},
{"algorithm" , required_argument},
{"alpha" , required_argument},
{"beta" , required_argument},
{"sort_edge_blocks" , no_argument},
{"dump_edge_list" , no_argument},
{"check_graph" , no_argument},
{"dump_graph" , no_argument},
{"check_results" , no_argument},
{"version" , no_argument},
{"help" , no_argument},
{nullptr}
};
void
print_help(const char* argv0)
{
LOG( "Usage: %s [OPTIONS]\n", argv0);
LOG("\t--graph_filename Path to graph file to load\n");
LOG("\t--distributed_load Load the graph from all nodes at once (File must exist on all nodes, use absolute path).\n");
LOG("\t--heavy_threshold Vertices with this many neighbors will be spread across nodelets\n");
LOG("\t--num_trials Run BFS this many times.\n");
LOG("\t--source_vertex Use this as the source vertex. If unspecified, pick random vertices.\n");
LOG("\t--algorithm Select BFS implementation to run\n");
LOG("\t--alpha Alpha parameter for direction-optimizing BFS\n");
LOG("\t--beta Beta parameter for direction-optimizing BFS\n");
LOG("\t--sort_edge_blocks Sort edge blocks to group neighbors by home nodelet.\n");
LOG("\t--dump_edge_list Print the edge list to stdout after loading (slow)\n");
LOG("\t--check_graph Validate the constructed graph against the edge list (slow)\n");
LOG("\t--dump_graph Print the graph to stdout after construction (slow)\n");
LOG("\t--check_results Validate the BFS results (slow)\n");
LOG("\t--version Print git version info\n");
LOG("\t--help Print command line help\n");
}
struct bfs_args
{
const char* graph_filename;
bool distributed_load;
long num_trials;
long source_vertex;
const char* algorithm;
long alpha;
long beta;
bool sort_edge_blocks;
bool dump_edge_list;
bool check_graph;
bool dump_graph;
bool check_results;
static bfs_args
parse(int argc, char *argv[])
{
bfs_args args = {};
args.graph_filename = NULL;
args.distributed_load = false;
args.num_trials = 1;
args.source_vertex = -1;
args.algorithm = "beamer_hybrid";
args.alpha = 15;
args.beta = 18;
args.sort_edge_blocks = false;
args.dump_edge_list = false;
args.check_graph = false;
args.dump_graph = false;
args.check_results = false;
int option_index;
while (true) {
int c = getopt_long(argc, argv, "", long_options, &option_index);
// Done parsing
if (c == -1) { break; }
// Parse error
if (c == '?') {
LOG( "Invalid arguments\n");
print_help(argv[0]);
exit(1);
}
const char* option_name = long_options[option_index].name;
if (!strcmp(option_name, "graph_filename")) {
args.graph_filename = optarg;
} else if (!strcmp(option_name, "distributed_load")) {
args.distributed_load = true;
} else if (!strcmp(option_name, "num_trials")) {
args.num_trials = atol(optarg);
} else if (!strcmp(option_name, "source_vertex")) {
args.source_vertex = atol(optarg);
} else if (!strcmp(option_name, "algorithm")) {
args.algorithm = optarg;
} else if (!strcmp(option_name, "alpha")) {
args.alpha = atol(optarg);
} else if (!strcmp(option_name, "beta")) {
args.beta = atol(optarg);
} else if (!strcmp(option_name, "sort_edge_blocks")) {
args.sort_edge_blocks = true;
} else if (!strcmp(option_name, "dump_edge_list")) {
args.dump_edge_list = true;
} else if (!strcmp(option_name, "check_graph")) {
args.check_graph = true;
} else if (!strcmp(option_name, "dump_graph")) {
args.dump_graph = true;
} else if (!strcmp(option_name, "check_results")) {
args.check_results = true;
} else if (!strcmp(option_name, "version")) {
LOG("%s\n", g_GIT_TAG);
exit(0);
} else if (!strcmp(option_name, "help")) {
print_help(argv[0]);
exit(1);
}
}
if (args.graph_filename == NULL) { LOG( "Missing graph filename\n"); exit(1); }
if (args.num_trials <= 0) { LOG( "num_trials must be > 0\n"); exit(1); }
if (args.alpha <= 0) { LOG( "alpha must be > 0\n"); exit(1); }
if (args.beta <= 0) { LOG( "beta must be > 0\n"); exit(1); }
return args;
}
};
long
pick_random_vertex(graph& g, lcg& rng)
{
long source;
do {
source = rng() % g.num_vertices();
} while (g.out_degree(source) == 0);
return source;
}
int main(int argc, char ** argv)
{
bool success = true;
// Set active region for hooks
const char* active_region = getenv("HOOKS_ACTIVE_REGION");
if (active_region != NULL) {
hooks_set_active_region(active_region);
} else {
hooks_set_active_region("bfs");
}
hooks_set_attr_str("git_tag", g_GIT_TAG);
// Initialize RNG with deterministic seed
lcg rng(0);
// Parse command-line arguments
bfs_args args = bfs_args::parse(argc, argv);
// Load edge list from file
dist_edge_list::handle dist_el;
hooks_region_begin("load_edge_list");
if (args.distributed_load) {
dist_el = dist_edge_list::load_distributed(args.graph_filename);
} else {
dist_el = dist_edge_list::load_binary(args.graph_filename);
}
hooks_set_attr_i64("num_edges", dist_el->num_edges());
hooks_set_attr_i64("num_vertices", dist_el->num_vertices());
auto load_time_ms = hooks_region_end();
LOG("Loaded %li edges in %3.2f ms, %3.2f MB/s\n",
dist_el->num_edges(),
load_time_ms,
(1e-6 * dist_el->num_edges() * sizeof(edge)) / (1e-3 * load_time_ms));
if (args.dump_edge_list) {
LOG("Dumping edge list...\n");
dist_el->dump();
}
// Build the graph
LOG("Constructing graph...\n");
auto g = create_graph_from_edge_list<graph>(*dist_el);
if (args.sort_edge_blocks) {
LOG("Sorting edge lists by nodelet...\n");
g->sort_edge_lists([](long lhs, long rhs) {
unsigned long nlet_mask = NODELETS() - 1;
unsigned long lhs_nlet = lhs & nlet_mask;
unsigned long rhs_nlet = rhs & nlet_mask;
return lhs_nlet < rhs_nlet;
});
}
// Print graph statistics
g->print_distribution();
if (args.check_graph) {
LOG("Checking graph...");
if (g->check(*dist_el)) {
LOG("PASS\n");
} else {
LOG("FAIL\n");
success = false;
};
}
if (args.dump_graph) {
LOG("Dumping graph...\n");
g->dump();
}
// Check for valid source vertex
if (args.source_vertex >= g->num_vertices()) {
LOG("Source vertex %li out of range.\n", args.source_vertex);
exit(1);
}
// Quit early if algorithm == none
if (!strcmp(args.algorithm, "none")) {
return !success;
}
// Initialize the algorithm
LOG("Initializing BFS data structures...\n");
hooks_set_attr_str("algorithm", args.algorithm);
enum algorithm {
REMOTE_WRITES,
MIGRATING_THREADS,
REMOTE_WRITES_HYBRID,
BEAMER_HYBRID,
} alg;
if (!strcmp(args.algorithm, "remote_writes")) {
alg = REMOTE_WRITES;
} else if (!strcmp(args.algorithm, "migrating_threads")) {
alg = MIGRATING_THREADS;
} else if (!strcmp(args.algorithm, "remote_writes_hybrid")) {
alg = REMOTE_WRITES_HYBRID;
} else if (!strcmp(args.algorithm, "beamer_hybrid")) {
alg = BEAMER_HYBRID;
} else {
LOG("Algorithm '%s' not implemented!\n", args.algorithm);
exit(1);
}
auto bfs = emu::make_repl_shallow<hybrid_bfs>(*g);
// Run trials
long num_edges_traversed_all_trials = 0;
double time_ms_all_trials = 0;
long source;
for (long s = 0; s < args.num_trials; ++s) {
// Randomly pick a source vertex with positive degree
if (args.source_vertex >= 0) {
source = args.source_vertex;
} else {
source = pick_random_vertex(*g, rng);
}
// Clear out the bfs data structures
bfs->clear();
LOG("Doing breadth-first search from vertex %li (sample %li of %li)\n",
source, s + 1, args.num_trials);
// Run the BFS
hooks_set_attr_i64("source_vertex", source);
hooks_region_begin("bfs");
switch(alg) {
case (REMOTE_WRITES):
bfs->run_with_remote_writes(source);
break;
case (MIGRATING_THREADS):
bfs->run_with_migrating_threads(source);
break;
case (REMOTE_WRITES_HYBRID):
bfs->run_with_remote_writes_hybrid(source, args.alpha, args.beta);
break;
case (BEAMER_HYBRID):
bfs->run_beamer(source, args.alpha, args.beta);
break;
}
double time_ms = hooks_region_end();
if (args.check_results) {
LOG("Checking results...\n");
if (bfs->check(source)) {
LOG("PASS\n");
} else {
LOG("FAIL\n");
success = false;
// hybrid_bfs_print_tree();
}
}
// Output results
long num_edges_traversed = bfs->count_num_traversed_edges();
num_edges_traversed_all_trials += num_edges_traversed;
time_ms_all_trials += time_ms;
LOG("Traversed %li edges in %3.2f ms, %3.2f MTEPS \n",
num_edges_traversed,
time_ms,
(1e-6 * num_edges_traversed) / (time_ms / 1000)
);
}
LOG("Mean performance over all trials: %3.2f MTEPS \n",
(1e-6 * num_edges_traversed_all_trials) / (time_ms_all_trials / 1000)
);
return !success;
}