-
Notifications
You must be signed in to change notification settings - Fork 11
/
bfs_simple.c
300 lines (237 loc) · 7.2 KB
/
bfs_simple.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
298
299
300
/*** BFS PUSH, PULL, PUSH/PULL and BFS EDGE CENTRIC **/
#include "random.h"
#include <cilk/reducer_opadd.h>
#include "parallel_ligra.h"
#define BFS_ROOT 0
struct node* node_list;
uint32_t* dist;
static void print_stats(void);
static struct thread_stats {
uint64_t tasks, updates;
} thread_stats[64];
static inline void bfs_algo();
uint32_t degree_in_frontier = 0;
static short* active;
int switched = 0;
static struct thread_buffer thread_buffers[ALGO_NB_THREADS];
static int iterations = 0;
inline int writeMin(long* curr, long newV) {
long c; int r =0;
do c = *curr;
while (c > newV && !(r = __sync_bool_compare_and_swap(curr,c,newV)) );
return r;
}
void bfs_construct(void) {
uint64_t start,stop;
rdtscll(start);
dist = (uint32_t*) malloc(NB_NODES *sizeof(uint32_t));
parallel_for(uint32_t i = 0; i < NB_NODES; i++) {
dist[i] = 0;
in_frontier_next[i] = 0;
in_frontier[i] = 0;
}
rdtscll(stop);
printf ("#Init time for state array %f\n", (float)(stop - start)/(float)get_cpu_freq());
memset(thread_stats, 0, 64*sizeof(struct thread_stats));
}
void bfs_destruct(void) {
uint64_t nodes_discovered=0;
for(uint64_t i = 0; i < NB_NODES; i++)
if(dist[i] != 0) nodes_discovered++;
printf("Total nodes discovered:: %lu\n", nodes_discovered);
}
void bfs_edgecentric(struct node* nodes){
items_in_frontier = 1;
uint64_t start_iter, stop_iter;
while(items_in_frontier != 0) {
rdtscll(start_iter);
start_iteration();
parallel_for(uint32_t i = 0; i < nb_edges; i++) {
struct edge_t* e = &memblock[i];
uint32_t src = e->src;
uint32_t dst = e->dst;
if(in_frontier[src] && __sync_bool_compare_and_swap(&dist[dst] ,0 , dist[src])) {
dist[dst] = dist[src] + 1;
in_frontier_next[dst] = 1;
}
}
rdtscll(stop_iter);
#if CILK
cilk::reducer_opadd<unsigned long> temp(0);
parallel_for(uint32_t i = 0; i < NB_NODES; i++) {
if(in_frontier_next[i] == 1)
*temp += 1;
in_frontier[i] = 0;
}
items_in_frontier = temp.get_value();
#else
items_in_frontier = 0;
parallel_for(uint32_t i = 0; i < NB_NODES; i++) {
if(in_frontier_next[i] == 1)
__sync_fetch_and_add(&items_in_frontier,1);
in_frontier[i] = 0;
}
#endif
stop_iteration();
printf("#iter %d items %u time %f\n", iterations, items_in_frontier, (float)(stop_iter - start_iter)/(float)get_cpu_freq());
iterations++;
}
}
static void bfs_pull(uint32_t n_id , struct thread_buffer* b) {
struct node*n = &nodes[n_id];
struct node *dst;
for(uint32_t idx = 0; idx < n->nb_in_edges; idx++) {
uint32_t dst_id = edge_array_in[n->incoming_edges + idx].dst;
if(in_frontier[dst_id]) {
dist[n_id] += 1;
in_frontier_next[n_id] = 1;
b->current_buffer_index++;
b->nb_cleaning+=n->nb_out_edges;
break;
}
}
}
static void bfs_push(uint32_t n_id, struct thread_buffer* b){
struct node* n = &nodes[n_id];
struct node* dst;
for(uint32_t idx = 0; idx < n->nb_out_edges; idx++) {
uint32_t dst_id = edge_array_out[n->outgoing_edges + idx].dst;
if(dist[dst_id] == 0) {
dist[dst_id] += 1;
if(__sync_bool_compare_and_swap(&in_frontier_next[dst_id], 0, 1)) {
thread_add_task(b, &nodes[dst_id]);
}
}
}
}
static used void iterator(struct node *nodes) {
float compute_time, switch_time = 0;
uint32_t total_items = 1;
uint64_t iter_start, iter_stop;
uint64_t prev_nodes=0;
uint64_t prev_mode = mode;
while ( items_in_frontier != 0) {
rdtscll(iter_start);
start_iteration();
uint32_t active_partitions = 0;
uint32_t total_edges_to_stream = 0;
uint32_t degree = 0;
items_in_frontier = 0;
rdtscll(iter_stop);
uint32_t items_in_wq = has_work_to_do();
degree_in_frontier = 0;
if(mode == PUSH) {
parallel_for(uint32_t i = 0 ; i < items_in_wq; i++) {
int id_ = get_threadid();
uint32_t n_id = current_ids[i];
bfs_push(n_id, &thread_buffers[id_]);
}
parallel_for(uint32_t i = 0; i < ALGO_NB_THREADS; i++) {
thread_flush(&thread_buffers[i]);
}
}
else {
parallel_for(uint32_t i = 0; i < NB_NODES; i++) {
int id_ = get_threadid();
if(dist[i] == 0)
bfs_pull(i, &thread_buffers[id_]);
}
}
if(mode == PULL)
for(uint32_t i = 0; i < ALGO_NB_THREADS; i++) {
items_in_frontier += thread_buffers[i].current_buffer_index;
degree_in_frontier +=thread_buffers[i]. nb_cleaning;
}
stop_iteration_only();
if(mode == PUSH) {
items_in_frontier = has_work_to_do();
degree_in_frontier = wq_old_nb_cleaning();
}
if((switch_mode && mode == PUSH && items_in_frontier + degree_in_frontier > nb_edges/20.) || (mode == PULL && items_in_frontier + degree_in_frontier > nb_edges/20.)) {
mode = PULL;
switched = 1;
}
else {
mode = PUSH;
}
if(mode == PUSH && switched) {
for(int i =0;i < ALGO_NB_THREADS; i++)
init_thread_buffer(&thread_buffers[i]);
parallel_for(uint32_t i = 0; i < NB_NODES; i++ ){
if(in_frontier_next[i]) {
int id_ = get_threadid();
thread_add_task(&thread_buffers[id_],&nodes[i]);
}
}
parallel_for (int i = 0; i < ALGO_NB_THREADS; i++) {
thread_flush(&thread_buffers[i]);
}
switched = 0;
stop_iteration_only();
}
parallel_for (uint32_t i = 0; i < NB_NODES; i++)
in_frontier[i] = 0;
std::swap(in_frontier, in_frontier_next);
for(int i =0;i < ALGO_NB_THREADS; i++)
init_thread_buffer(&thread_buffers[i]);
total_items += items_in_frontier ;
rdtscll(iter_stop);
printf("#Iter %d, items %d , time %f\n", iterations, items_in_frontier, (float)(iter_stop - iter_start)/(float)get_cpu_freq());
iterations++;
}
printf("Done in %d iterations and found %d nodes\n", iterations, total_items);
}
void bfs_reset(struct node *nodes) {
}
/*
* Default function that launches a bfs from node 0
*/
void bfs(struct node *nodes) {
uint64_t construct_start, construct_stop;
rdtscll(construct_start);
node_list = nodes;
dist[BFS_ROOT] = 1;
in_frontier[BFS_ROOT] = 1;
items_in_frontier = 1;
if(!get_task_list())
init_task_list(NB_NODES);
reset_task_lists();
add_task(&nodes[BFS_ROOT]);
rdtscll(construct_stop);
printf ("#Task list time %lu, ( %.3f sec) \n", construct_stop - construct_start, ((float)(construct_stop - construct_start) / (float)(get_cpu_freq())) );
rdtscll(construct_start);
if(load_mode < 2 || load_mode == 6) {
printf("Use the BFS grid file, don't know how to run BFS here with this layout\n");
exit(1);
}
if(load_mode > 6)
bfs_edgecentric(nodes);
else
iterator(nodes);
rdtscll(construct_stop);
printf ("#Algo time %lu, ( %.3f sec) \n", construct_stop - construct_start, ((float)(construct_stop - construct_start) / (float)(get_cpu_freq())) );
parallel_for(uint32_t i = 0; i < ALGO_NB_THREADS; i++) {
int id = get_threadid();
if(id != 0) {
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(id, &mask);
sched_setaffinity(gettid(), sizeof(mask), &mask);
}
init_thread_buffer(&thread_buffers[i]);
}
}
static void print_stats(void) {
/* Stats */
uint64_t tasks = 0, updates = 0;
for(size_t i = 0; i < ALGO_NB_THREADS; i++) {
tasks += thread_stats[i].tasks;
updates += thread_stats[i].updates;
}
printf("\t[bfs - TOTAL] %lu tasks done %lu updates pushed\n", tasks, updates);
}
void *bfs_parallels(void *data){
}
struct algo_func current_algo = {
.reset = bfs_reset, .main = bfs, .construct = bfs_construct, .destruct = bfs_destruct,
};