-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathexpander.cu
515 lines (460 loc) · 11.2 KB
/
expander.cu
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*
* Copyright 2016 The George Washington University
* Written by Hang Liu
* Directed by Prof. Howie Huang
*
* https://www.seas.gwu.edu/~howie/
* Contact: [email protected]
*
*
* Please cite the following paper:
*
* Hang Liu, H. Howie Huang and Yang Hu. 2016. iBFS: Concurrent Breadth-First Search on GPUs. Proceedings of the 2016 International Conference on Management of Data. ACM.
*
* This file is part of iBFS.
*
* iBFS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* iBFS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with iBFS. If not, see <http://www.gnu.org/licenses/>.
*/
//Hang Dec/10/2013
//Hang Mar/05/2015
#include "gpu_ibfs.cuh"
#include "util.h"
__device__ void __sync_warp(int predicate){
while((!__all(predicate))){
;
}
}
//This kernel is executed by one thread
__global__ void init_expand_sort
(
vertex_t *src_list,
index_t *beg_pos_d,
index_t src_grp_off,
index_t joint_count,
index_t bit_count,
index_t concurr_count,
comp_t *depth_comp_last,
comp_t *depth_comp_curr,
index_t *ex_lrg_sz_d,
vertex_t *ex_lrg_q_d
)
{
index_t tid=threadIdx.x+blockIdx.x*blockDim.x;
const index_t GRNTY=blockDim.x*gridDim.x;
while(tid<concurr_count){
index_t inst_off=tid/bit_count;
vertex_t vert_id=src_list[tid+src_grp_off];
index_t card=beg_pos_d[vert_id+1]-beg_pos_d[vert_id];
if(!card){return;}
//put all src into big queue
//-since we have enough threads
ex_lrg_q_d[tid]=vert_id;
comp_t status=(make_uint4(1))<<(tid%bit_count);
depth_comp_last[vert_id*joint_count+inst_off]=status;
depth_comp_curr[vert_id*joint_count+inst_off]=status;
tid+=GRNTY;
}
return ;
}
void
gpu_ibfs::
init_bfs(index_t src_grp_off){
init_expand_sort
<<<BLKS_NUM,THDS_NUM>>>
(
src_list_d,
beg_pos_d,
src_grp_off,
joint_count,
bit_count,
concurr_count,
depth_comp_last,
depth_comp_curr,
ex_lrg_sz_d,
ex_lrg_q_d
);
}
//+----------------------
//|for ex_sml_q_d expansion
//+---------------------------
__global__ void td_expand_thd
(
comp_t *depth_comp_last,
comp_t *depth_comp_curr,
depth_t curr_level,
index_t ex_sml_sz,
const vertex_t* __restrict__ ex_q_d,
const index_t* __restrict__ beg_pos_d,
const vertex_t* __restrict__ adj_list_d
)
{
const index_t q_sz = ex_sml_sz;
const index_t GRNLTY = blockDim.x * gridDim.x;
index_t tid = threadIdx.x+blockIdx.x*blockDim.x;
//used for prefetching
vertex_t ex_ver_curr;
index_t end_curr;
index_t beg_curr;
vertex_t adj_vert;
comp_t vert_depth, adj_depth;
while(tid<q_sz)
{
ex_ver_curr = ex_q_d[tid];
beg_curr = beg_pos_d[ex_ver_curr];
end_curr = beg_pos_d[ex_ver_curr+1];
vert_depth = depth_comp_last[ex_ver_curr];
while(beg_curr<end_curr)
{
adj_vert=adj_list_d[beg_curr];
adj_depth=depth_comp_curr[adj_vert];
if((~(adj_depth.x))&vert_depth.x)
atomicOr(&(depth_comp_curr[adj_vert].x),vert_depth.x);
if((~(adj_depth.y))&vert_depth.y)
atomicOr(&(depth_comp_curr[adj_vert].y),vert_depth.y);
if((~(adj_depth.z))&vert_depth.z)
atomicOr(&(depth_comp_curr[adj_vert].z),vert_depth.z);
if((~(adj_depth.w))&vert_depth.w)
atomicOr(&(depth_comp_curr[adj_vert].w),vert_depth.w);
beg_curr++;
}
tid += GRNLTY;
}
}
//+-----------------------
//|ex_mid_q_d expansion
//+------------------------------
__global__ void td_expand_warp
(
comp_t *depth_comp_last,
comp_t *depth_comp_curr,
depth_t curr_level,
index_t ex_mid_sz,
const vertex_t* __restrict__ ex_q_d,
const index_t* __restrict__ beg_pos_d,
const vertex_t* __restrict__ adj_list_d
)
{
const index_t q_sz = ex_mid_sz;
const index_t vec_sz = 32;
const index_t TID = threadIdx.x+blockIdx.x*blockDim.x;
const index_t lane_s = TID & (vec_sz-1);
const index_t GRNLTY = (blockDim.x*gridDim.x)/vec_sz;
index_t vec_id = TID/vec_sz;
vertex_t ex_ver_curr,adj_vert;
index_t beg_curr, end_curr;
comp_t vert_depth, adj_depth;
while(vec_id<q_sz)
{
ex_ver_curr = ex_q_d[vec_id];
beg_curr = beg_pos_d[ex_ver_curr];
end_curr = beg_pos_d[ex_ver_curr+1];
vert_depth = depth_comp_last[ex_ver_curr];
index_t lane= beg_curr+lane_s;
while(lane<end_curr)
{
adj_vert=adj_list_d[lane];
adj_depth=depth_comp_curr[adj_vert];
if((~(adj_depth.x))&vert_depth.x)
atomicOr(&(depth_comp_curr[adj_vert].x),vert_depth.x);
if((~(adj_depth.y))&vert_depth.y)
atomicOr(&(depth_comp_curr[adj_vert].y),vert_depth.y);
if((~(adj_depth.z))&vert_depth.z)
atomicOr(&(depth_comp_curr[adj_vert].z),vert_depth.z);
if((~(adj_depth.w))&vert_depth.w)
atomicOr(&(depth_comp_curr[adj_vert].w),vert_depth.w);
lane+=vec_sz;
}
vec_id += GRNLTY;
}
}
//+-----------------------
//|ex_lrg_q_d expansion
//+------------------------------
__global__ void td_expand_cta
(
comp_t *depth_comp_last,
comp_t *depth_comp_curr,
depth_t curr_level,
index_t ex_lrg_sz,
const vertex_t* __restrict__ ex_q_d,
const index_t* __restrict__ beg_pos_d,
const vertex_t* __restrict__ adj_list_d
)
{
const index_t q_sz = ex_lrg_sz;
index_t vec_id = blockIdx.x;
vertex_t ex_ver_curr,adj_vert;
index_t end_curr;
index_t beg_curr;
comp_t vert_depth, adj_depth;
while(vec_id<q_sz)
{
ex_ver_curr = ex_q_d[vec_id];
beg_curr = beg_pos_d[ex_ver_curr];
end_curr = beg_pos_d[ex_ver_curr+1];
vert_depth = depth_comp_last[ex_ver_curr];
index_t lane= beg_curr+threadIdx.x;
while(lane<end_curr)
{
adj_vert=adj_list_d[lane];
adj_depth=depth_comp_curr[adj_vert];
if((~(adj_depth.x))&vert_depth.x)
atomicOr(&(depth_comp_curr[adj_vert].x),vert_depth.x);
if((~(adj_depth.y))&vert_depth.y)
atomicOr(&(depth_comp_curr[adj_vert].y),vert_depth.y);
if((~(adj_depth.z))&vert_depth.z)
atomicOr(&(depth_comp_curr[adj_vert].z),vert_depth.z);
if((~(adj_depth.w))&vert_depth.w)
atomicOr(&(depth_comp_curr[adj_vert].w),vert_depth.w);
lane+=blockDim.x;
}
vec_id += gridDim.x;
}
}
//+------------------------
//|ex_mid_q_d expansion
//+------------------------------
__global__ void sw_expand_warp
(
comp_t *depth_comp_last,
comp_t *depth_comp_curr,
depth_t curr_level,
index_t ex_sz,
const vertex_t* __restrict__ ex_q_d,
const index_t* __restrict__ beg_pos_d,
const vertex_t* __restrict__ adj_list_d
)
{
const index_t q_sz = ex_sz;
const index_t vec_sz = 32;
const index_t TID = threadIdx.x+blockIdx.x*blockDim.x;
const index_t lane_s = TID & (vec_sz-1);
const index_t GRNLTY = (blockDim.x*gridDim.x)/vec_sz;
index_t vec_id = TID/vec_sz;
vertex_t ex_ver_curr;
index_t end_curr, end_curr_revised;
index_t beg_curr;
unsigned int vert_depth_up, vert_depth_low;
comp_t vert_depth;
while(vec_id<q_sz)
{
ex_ver_curr = ex_q_d[vec_id];
beg_curr = beg_pos_d[ex_ver_curr];
end_curr = beg_pos_d[ex_ver_curr+1];
vert_depth = depth_comp_curr[ex_ver_curr];
index_t lane= beg_curr+lane_s;
//make sure all 32 threads can get into the voting loop
if((end_curr-beg_curr)&(vec_sz-1))
end_curr_revised = beg_curr+((((end_curr-beg_curr)>>5)+1)<<5);
else
end_curr_revised = end_curr;
while(lane<end_curr_revised)
{
if(__any((~vert_depth)==0)) break;
if(lane<end_curr)
vert_depth|=depth_comp_last[adj_list_d[lane]];
for(int i=16;i>=1;i>>=1){
vert_depth.x|=__shfl_xor((int)vert_depth.x,i,32);
vert_depth.y|=__shfl_xor((int)vert_depth.y,i,32);
vert_depth.z|=__shfl_xor((int)vert_depth.z,i,32);
vert_depth.w|=__shfl_xor((int)vert_depth.w,i,32);
}
lane+=vec_sz;
}
if(lane_s==0) depth_comp_curr[ex_ver_curr]=vert_depth;
vec_id += GRNLTY;
}
}
//+------------------------
//|ex_mid_q_d expansion
//+------------------------------
__global__ void sw_expand_thd
(
comp_t *depth_comp_last,
comp_t *depth_comp_curr,
depth_t curr_level,
index_t ex_sz,
const vertex_t* __restrict__ ex_q_d,
const index_t* __restrict__ beg_pos_d,
const vertex_t* __restrict__ adj_list_d
)
{
const index_t q_sz = ex_sz;
index_t tid = threadIdx.x+blockIdx.x*blockDim.x;
const index_t GRNLTY = blockDim.x*gridDim.x;
vertex_t ex_ver_curr;
index_t beg_curr, end_curr;
comp_t adj_depth_curr, vert_depth;
while(tid<q_sz)
{
ex_ver_curr = ex_q_d[tid];
end_curr = beg_pos_d[ex_ver_curr+1];
beg_curr = beg_pos_d[ex_ver_curr];
index_t lane= beg_curr;
vert_depth=depth_comp_curr[ex_ver_curr];
while(lane<end_curr)
{
if((~vert_depth)==0) break;
adj_depth_curr=depth_comp_last[adj_list_d[lane]];
if(((~vert_depth)&adj_depth_curr) != 0)
vert_depth|=adj_depth_curr;
lane++;
}
if((vert_depth^depth_comp_curr[ex_ver_curr]) != 0)
depth_comp_curr[ex_ver_curr]=vert_depth;
tid += GRNLTY;
}
}
//+-----------------
//|CLFY_EXPAND_SORT
//+----------------------
void
gpu_ibfs::
td_expand(depth_t curr_level)
{
td_expand_thd
<<<BLKS_NUM, THDS_NUM, 0, stream[0]>>>
(
depth_comp_last,
depth_comp_curr,
curr_level,
ex_sml_sz[0],
ex_sml_q_d,
beg_pos_d,
adj_list_d
);
td_expand_warp
<<<BLKS_NUM, THDS_NUM, 0, stream[1]>>>
(
depth_comp_last,
depth_comp_curr,
curr_level,
ex_mid_sz[0],
ex_mid_q_d,
beg_pos_d,
adj_list_d
);
td_expand_cta
<<<BLKS_NUM, THDS_NUM, 0, stream[2]>>>
(
depth_comp_last,
depth_comp_curr,
curr_level,
ex_lrg_sz[0],
ex_lrg_q_d,
beg_pos_d,
adj_list_d
);
for(index_t i=0;i<Q_CARD; i++)
cudaStreamSynchronize(stream[i]);
}
//+----------------------
//|CLFY_EXPAND_SORT
//+----------------------
void
gpu_ibfs::
bu_expand(depth_t curr_level)
{
sw_expand_thd
<<<BLKS_NUM, THDS_NUM, 0, stream[0]>>>
(
depth_comp_last,
depth_comp_curr,
curr_level,
ex_sml_sz[0],
ex_sml_q_d,
beg_pos_d,
adj_list_d
);
sw_expand_thd
<<<BLKS_NUM, THDS_NUM, 0, stream[1]>>>
(
depth_comp_last,
depth_comp_curr,
curr_level,
ex_mid_sz[0],
ex_mid_q_d,
beg_pos_d,
adj_list_d
);
sw_expand_thd
<<<BLKS_NUM, THDS_NUM, 0, stream[2]>>>
(
depth_comp_last,
depth_comp_curr,
curr_level,
ex_lrg_sz[0],
ex_lrg_q_d,
beg_pos_d,
adj_list_d
);
for(index_t i=0;i<Q_CARD; i++)
cudaStreamSynchronize(stream[i]);
}
//+----------------------
//|CLFY_EXPAND_SORT
//+----------------------
void
gpu_ibfs::
sw_expand(depth_t curr_level)
{
sw_expand_thd
<<<BLKS_NUM<<1, THDS_NUM, 0, stream[0]>>>
(
depth_comp_last,
depth_comp_curr,
curr_level,
ex_sml_sz[0],
ex_sml_q_d,
beg_pos_d,
adj_list_d
);
sw_expand_warp
<<<BLKS_NUM<<1, THDS_NUM, 0, stream[1]>>>
(
depth_comp_last,
depth_comp_curr,
curr_level,
ex_mid_sz[0],
ex_mid_q_d,
beg_pos_d,
adj_list_d
);
sw_expand_warp
<<<BLKS_NUM<<1, THDS_NUM, 0, stream[2]>>>
(
depth_comp_last,
depth_comp_curr,
curr_level,
ex_lrg_sz[0],
ex_lrg_q_d,
beg_pos_d,
adj_list_d
);
for(index_t i=0;i<Q_CARD; i++)
cudaStreamSynchronize(stream[i]);
}
void
gpu_ibfs::
expander(depth_t level)
{
if(level<=sw_level)
td_expand(level);
else if((level==sw_level+1))
sw_expand(level);
else{//also use merged fq
//differ sw_expand of no imbalance issue
bu_expand(level);
}
}