-
Notifications
You must be signed in to change notification settings - Fork 0
/
hybrid_bfs_main.c
297 lines (269 loc) · 9.4 KB
/
hybrid_bfs_main.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
#include <string.h>
#include <getopt.h>
#include <limits.h>
#include "load_edge_list.h"
#include "graph_from_edge_list.h"
#include "hybrid_bfs.h"
#define LCG_MUL64 6364136223846793005ULL
#define LCG_ADD64 1
unsigned long lcg_state = 0;
void
lcg_init(unsigned long * x, unsigned long step)
{
unsigned long mul_k, add_k, ran, un;
mul_k = LCG_MUL64;
add_k = LCG_ADD64;
ran = 1;
for (un = step; un; un >>= 1) {
if (un & 1)
ran = mul_k * ran + add_k;
add_k *= (mul_k + 1);
mul_k *= mul_k;
}
*x = ran;
}
unsigned long
lcg_rand(unsigned long * x) {
*x = LCG_MUL64 * *x + LCG_ADD64;
return *x;
}
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},
{"help" , no_argument},
{NULL}
};
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--help Print command line help\n");
}
typedef struct bfs_args {
const char* graph_filename;
bool distributed_load;
long heavy_threshold;
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;
} bfs_args;
struct bfs_args
parse_args(int argc, char *argv[])
{
bfs_args args;
args.graph_filename = NULL;
args.distributed_load = false;
args.heavy_threshold = LONG_MAX;
args.num_trials = 1;
args.source_vertex = -1;
args.algorithm = "remote_writes_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, "heavy_threshold")) {
args.heavy_threshold = atol(optarg);
} 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, "help")) {
print_help(argv[0]);
exit(1);
}
}
if (args.graph_filename == NULL) { LOG( "Missing graph filename\n"); exit(1); }
if (args.heavy_threshold <= 0) { LOG( "heavy_threshold must be > 0\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()
{
long source;
do {
source = lcg_rand(&lcg_state) % G.num_vertices;
} while (G.vertex_out_degree[source] == 0);
return source;
}
int main(int argc, char ** argv)
{
// 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");
}
// Parse command-line argumetns
bfs_args args = parse_args(argc, argv);
hooks_set_attr_i64("heavy_threshold", args.heavy_threshold);
// Load the edge list
if (args.distributed_load) {
load_edge_list_distributed(args.graph_filename);
} else {
load_edge_list(args.graph_filename);
}
if (args.dump_edge_list) {
LOG("Dumping edge list...\n");
dump_edge_list();
}
// Build the graph
LOG("Constructing graph...\n");
construct_graph_from_edge_list(args.heavy_threshold);
if (args.sort_edge_blocks) {
LOG("Sorting edge blocks...\n");
sort_edge_blocks_by_nodelet();
}
print_graph_distribution();
if (args.check_graph) {
LOG("Checking graph...");
if (check_graph()) {
LOG("PASS\n");
} else {
LOG("FAIL\n");
};
}
if (args.dump_graph) {
LOG("Dumping graph...\n");
dump_graph();
}
// 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);
}
// Initialize the algorithm
LOG("Initializing BFS data structures...\n");
hooks_set_attr_str("algorithm", args.algorithm);
hybrid_bfs_alg 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);
}
hybrid_bfs_init();
// Initialize RNG with deterministic seed
lcg_init(&lcg_state, 0);
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();
}
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");
hybrid_bfs_run(alg, source, args.alpha, args.beta);
double time_ms = hooks_region_end();
if (args.check_results) {
LOG("Checking results...\n");
if (hybrid_bfs_check(source)) {
LOG("PASS\n");
} else {
LOG("FAIL\n");
// hybrid_bfs_print_tree();
}
}
// Output results
long num_edges_traversed = hybrid_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)
);
// Reset for next run
if (s+1 < args.num_trials) {
hybrid_bfs_data_clear();
}
}
LOG("Mean performance over all trials: %3.2f MTEPS \n",
(1e-6 * num_edges_traversed_all_trials) / (time_ms_all_trials / 1000)
);
hybrid_bfs_deinit();
return 0;
}