-
Notifications
You must be signed in to change notification settings - Fork 12
/
inspector.cu
671 lines (617 loc) · 15.9 KB
/
inspector.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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
/*
* 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/>.
*/
//Dec/15/2013
//Feb/16/2015
//Mar/4/2015
#include "util.h"
#include "gpu_ibfs.cuh"
#include "scan.cuh"
__global__ void extract_depth(
comp_t *depth_comp_last,
comp_t *depth_comp_curr,
bool *is_done_d,
depth_t curr_level,
index_t vert_count
){
//+-
//|Diff of comp_last and comp_curr = just visited verts
//+--------------------------------------
index_t tid=threadIdx.x+blockIdx.x*blockDim.x;
index_t wid=tid>>5;
// const index_t vec_sz=32;
// const index_t LANE=tid%32;
const index_t GRNTY=blockDim.x*gridDim.x;
const index_t WGRNTY=GRNTY>>5;
// const depth_t LEVEL=curr_level;
const index_t COMP_COUNT=vert_count;
comp_t comp_last, comp_curr;
// unsigned int recv_up, recv_low;
bool is_done=true;
while(tid<COMP_COUNT){
comp_last=depth_comp_last[tid];
comp_curr=depth_comp_curr[tid];
if(comp_last != comp_curr){
depth_comp_last[tid]=comp_curr;
if(is_done) is_done=false;
}
// for(int i=0;i<vec_sz;i++){
// recv_up=change>>32;
// recv_low=change&0xffffffff;
// recv_up=__shfl((int)recv_up,i);
// recv_low=__shfl((int)recv_low,i);
// if(recv_up&(1<<LANE))
// depth_merge[wid*vec_sz*num_agg_bfs+i*num_agg_bfs+vec_sz+LANE]=LEVEL;
// if(recv_low&(1<<LANE))
// depth_merge[wid*vec_sz*num_agg_bfs+i*num_agg_bfs+LANE]=LEVEL;
// }
// __syncthreads();
tid += GRNTY;
wid += WGRNTY;
}
//is_done_d[0]=false;
if(is_done==false) is_done_d[0]=false;
}
__global__ void td_frontier_count(
index_t *beg_pos_d,
index_t *cat_sml_sz_d,
index_t *cat_mid_sz_d,
index_t *cat_lrg_sz_d,
comp_t *depth_comp_last,
comp_t *depth_comp_curr,
depth_t curr_level,
index_t vert_count,
const index_t sml_shed,
const index_t lrg_shed
)
{
//+-
//|Diff of comp_last and comp_curr = just visited verts
//+--------------------------------------
index_t TID_ST=threadIdx.x+blockIdx.x*blockDim.x;
index_t tid=TID_ST;
index_t wid=TID_ST>>5;
// const index_t vec_sz=32;
// const index_t LANE=tid%32;
const index_t GRNTY=blockDim.x*gridDim.x;
const index_t WGRNTY=GRNTY>>5;
// const depth_t LEVEL=curr_level;
const index_t COMP_COUNT=vert_count;
comp_t comp_last, comp_curr;
index_t card_curr;
// unsigned int recv_up, recv_low;
index_t ex_sml_ct= 0;
index_t ex_mid_ct= 0;
index_t ex_lrg_ct= 0;
while(tid<COMP_COUNT){
comp_last=depth_comp_last[tid];
comp_curr=depth_comp_curr[tid];
if((comp_last^comp_curr) != 0){
//depth_comp_last[tid]=comp_curr;
//CANNOT CHANGE NOW!!!
//NEED diff FOR GATHERING
card_curr=beg_pos_d[tid+1]-beg_pos_d[tid];
if(card_curr>0)
if(card_curr<sml_shed) ex_sml_ct++;
else if(card_curr>lrg_shed) ex_lrg_ct++;
else ex_mid_ct++;
}
// for(int i=0;i<vec_sz;i++){
// recv_up=change>>32;
// recv_low=change&0xffffffff;
// recv_up=__shfl((int)recv_up,i);
// recv_low=__shfl((int)recv_low,i);
// if(recv_up&(1<<LANE))
// depth_merge[wid*vec_sz*num_agg_bfs+i*num_agg_bfs+vec_sz+LANE]=LEVEL;
// if(recv_low&(1<<LANE))
// depth_merge[wid*vec_sz*num_agg_bfs+i*num_agg_bfs+LANE]=LEVEL;
// }
__syncthreads();
tid += GRNTY;
wid += WGRNTY;
}
cat_sml_sz_d[TID_ST]= ex_sml_ct;
cat_mid_sz_d[TID_ST]= ex_mid_ct;
cat_lrg_sz_d[TID_ST]= ex_lrg_ct;
}
__global__ void sw_frontier_count(
index_t *beg_pos_d,
index_t *cat_sml_sz_d,
index_t *cat_mid_sz_d,
index_t *cat_lrg_sz_d,
comp_t *depth_comp_curr,
depth_t curr_level,
index_t vert_count,
const index_t sml_shed,
const index_t lrg_shed
)
{
const index_t TID_ST = threadIdx.x +blockIdx.x*blockDim.x;
const index_t NUM_VER = vert_count;
const index_t GRNTY = blockDim.x*gridDim.x;
index_t card_curr,card_next;
comp_t depth_curr,depth_next;
index_t ex_sml_ct = 0;
index_t ex_mid_ct = 0;
index_t ex_lrg_ct = 0;
index_t tid = TID_ST;
//Figure out its own start and end pos
//We want each thread to inspect a continuous block
index_t step_sz = NUM_VER/GRNTY;
if(step_sz<16){
//small problem size
const index_t REMAINDER = NUM_VER-step_sz*GRNTY;
if(TID_ST<REMAINDER) step_sz ++;
const index_t beg_pos=step_sz*TID_ST+
(TID_ST >= REMAINDER ? REMAINDER:0);
const index_t end_pos=beg_pos+step_sz;
tid=beg_pos;
if(step_sz){
card_curr = beg_pos_d[beg_pos+1]-beg_pos_d[beg_pos];
depth_curr = depth_comp_curr[beg_pos];
// depth_comp_last[beg_pos]=depth_curr;
}
while(tid<end_pos){
tid++;
if(tid<end_pos){
card_next = beg_pos_d[tid+1]-beg_pos_d[tid];
depth_next = depth_comp_curr[tid];
// depth_comp_last[tid]=depth_next;
}
if(((~depth_curr)!=0) && (card_curr>0)){
if(card_curr<sml_shed) ex_sml_ct++;
else if(card_curr>lrg_shed) ex_lrg_ct++;
else ex_mid_ct++;
}
depth_curr=depth_next;
card_curr = card_next;
}
}else{
//big problem size
//we want each thread to get 16x indices to check.
if(step_sz&0xf) step_sz=((step_sz>>4)+1)<<4;
if(NUM_VER-step_sz*GRNTY>0) step_sz<<=1;
__shared__ index_t beg_pos_s[THDS_NUM];
__shared__ depth_t mark_s[THDS_NUM<<4];
index_t beg_pos_own = step_sz*TID_ST;
beg_pos_s[threadIdx.x] = beg_pos_own;
const index_t TRIES = step_sz>>4;
const index_t lane_id = threadIdx.x%32;
const index_t warp_id = threadIdx.x>>5;
const index_t thread_off= threadIdx.x<<4;
index_t tries = 0;
index_t load_ptr;
index_t proc_vert;
//+--------------------
//in shared memory data representation
//---------------------------------------
// 0000|1111
// ||||
// |||+-------- INFTY ?
// ||+--------- SML ?
// |+---------- MID ?
// +----------- LRG ?
//FURTHER OPTIMIZATION CAN BE CARRIED OUT
//BY PACKING MORE HERE.
//-----------------------------------------
while(tries<TRIES){
//warp stride loading
for(int i=0; i<32; i+=2){
proc_vert=(warp_id<<5)+i+(lane_id>>4);
depth_curr.x=0;depth_curr.y=0;depth_curr.y=0;depth_curr.w=0;
depth_curr=~depth_curr;
if(proc_vert<NUM_VER)
{
load_ptr=(tries<<4)+beg_pos_s[proc_vert]+(lane_id%16);
if(load_ptr<NUM_VER)
{
depth_curr=depth_comp_curr[load_ptr];
card_curr=beg_pos_d[load_ptr+1]-beg_pos_d[load_ptr];
// depth_comp_last[load_ptr]=depth_curr;
}else card_curr=0;//out-of-boundary, set it orphan
}else card_curr=0;//out-of-boundary, set it orphan
mark_s[(proc_vert<<4)+(lane_id%16)]=0x00;
//construct hub-cache
// if(depth_curr == LEVEL){
// hub_ptr = load_ptr & (HUB_BU_SZ-1);
// if(card_curr > hub_card[hub_ptr]){
// hub_vert[hub_ptr] = load_ptr;
// hub_card[hub_ptr] = card_curr;
// }
// }else
if(((~depth_curr)!=0) && (card_curr>0))
if(card_curr<sml_shed)
mark_s[(proc_vert<<4)+(lane_id%16)]=0x03;//0011
else if(card_curr>lrg_shed)
mark_s[(proc_vert<<4)+(lane_id%16)]=0x09;//1001
else mark_s[(proc_vert<<4)+(lane_id%16)]=0x05;//0101
}
__syncthreads();
//thread stride checking
for(int i=0; i<16; i++)
if(mark_s[thread_off+i]&0x02){
ex_sml_ct++;
}else if(mark_s[thread_off+i]&0x08){
ex_lrg_ct++;
}else if(mark_s[thread_off+i]&0x04){
ex_mid_ct++;
}
++tries;
__syncthreads();
}
}
cat_sml_sz_d[TID_ST]= ex_sml_ct;
cat_mid_sz_d[TID_ST]= ex_mid_ct;
cat_lrg_sz_d[TID_ST]= ex_lrg_ct;
}
__global__ void td_frontier_gather(
index_t *beg_pos_d,
index_t *ex_sml_q_d,
index_t *ex_mid_q_d,
index_t *ex_lrg_q_d,
index_t *cat_sml_off_d,
index_t *cat_mid_off_d,
index_t *cat_lrg_off_d,
comp_t *depth_comp_last,
comp_t *depth_comp_curr,
depth_t curr_level,
index_t vert_count,
const index_t sml_shed,
const index_t lrg_shed
)
{
index_t TID_ST=threadIdx.x+blockIdx.x*blockDim.x;
index_t tid=TID_ST;
const index_t GRNLTY=blockDim.x*gridDim.x;
const index_t COMP_COUNT=vert_count;
comp_t comp_last, comp_curr;
index_t card_curr;
index_t ex_sml_ptr = cat_sml_off_d[TID_ST];
index_t ex_mid_ptr = cat_mid_off_d[TID_ST];
index_t ex_lrg_ptr = cat_lrg_off_d[TID_ST];
while(tid<COMP_COUNT){
comp_last=depth_comp_last[tid];
comp_curr=depth_comp_curr[tid];
comp_t change=comp_curr-comp_last;
if(change!=0){
depth_comp_last[tid]=comp_curr;
card_curr=beg_pos_d[tid+1]-beg_pos_d[tid];
if(card_curr>0)
if(card_curr<sml_shed)
{
ex_sml_q_d[ex_sml_ptr]=tid;
ex_sml_ptr++;
}
else if(card_curr>lrg_shed)
{
ex_lrg_q_d[ex_lrg_ptr]=tid;
ex_lrg_ptr++;
}
else
{
ex_mid_q_d[ex_mid_ptr]=tid;
ex_mid_ptr++;
}
}
tid+= GRNLTY;
}
}
__global__ void sw_frontier_gather(
index_t *beg_pos_d,
index_t *ex_sml_q_d,
index_t *ex_mid_q_d,
index_t *ex_lrg_q_d,
index_t *cat_sml_off_d,
index_t *cat_mid_off_d,
index_t *cat_lrg_off_d,
comp_t *depth_comp_curr,
depth_t curr_level,
index_t vert_count,
const index_t sml_shed,
const index_t lrg_shed
)
{
const index_t TID_ST = threadIdx.x +blockIdx.x*blockDim.x;
const index_t NUM_VER = vert_count;
const index_t GRNTY = blockDim.x*gridDim.x;
index_t ex_sml_ptr = cat_sml_off_d[TID_ST];
index_t ex_mid_ptr = cat_mid_off_d[TID_ST];
index_t ex_lrg_ptr = cat_lrg_off_d[TID_ST];
index_t card_curr, card_next;
comp_t depth_curr,depth_next;
index_t tid,tid_next;
//Figure out its own start and end pos
//We want each thread to inspect a continuous block
index_t step_sz = NUM_VER/GRNTY;
if(step_sz<16){
//small problem size
const index_t REMAINDER = NUM_VER-step_sz*GRNTY;
if(TID_ST<REMAINDER) step_sz ++;
const index_t beg_pos=step_sz*TID_ST+
(TID_ST >= REMAINDER ? REMAINDER:0);
const index_t end_pos=beg_pos+step_sz;
tid=beg_pos;tid_next=beg_pos;
if(step_sz){
card_curr = beg_pos_d[beg_pos+1]-beg_pos_d[beg_pos];
depth_curr = depth_comp_curr[beg_pos];
tid_next++;
}
while(tid<end_pos){
if(tid_next < end_pos){
card_next = beg_pos_d[tid_next+1]-beg_pos_d[tid_next];
depth_next = depth_comp_curr[tid_next];
}
if((card_curr>0)&&((~depth_curr)!=0)){
if(card_curr<sml_shed)
{
ex_sml_q_d[ex_sml_ptr]=tid;
ex_sml_ptr++;
}
else if(card_curr>lrg_shed)
{
ex_lrg_q_d[ex_lrg_ptr]=tid;
ex_lrg_ptr++;
}
else
{
ex_mid_q_d[ex_mid_ptr]=tid;
ex_mid_ptr++;
}
}
tid=tid_next;
tid_next++;
card_curr = card_next;
depth_curr=depth_next;
}
}else{
//big problem size
//We want each thread to get 16x indices to check.
if(step_sz&0xf) step_sz=((step_sz>>4)+1)<<4;
if(NUM_VER-step_sz*GRNTY>0) step_sz<<=1;
__shared__ index_t beg_pos_s[THDS_NUM];
__shared__ depth_t mark_s[THDS_NUM<<4];
index_t beg_pos_own = step_sz*TID_ST;
beg_pos_s[threadIdx.x] = beg_pos_own;
const index_t TRIES = step_sz>>4;
const index_t lane_id = threadIdx.x%32;
const index_t warp_id = threadIdx.x>>5;
const index_t thread_off= threadIdx.x<<4;
index_t tries = 0;
index_t load_ptr;
index_t proc_vert;
//+--------------------
//in shared memory data representation
//---------------------------------------
// 0000|1111
// ||||
// |||+-------- INFTY ?
// ||+--------- SML ?
// |+---------- MID ?
// +----------- LRG ?
//FURTHER OPTIMIZATION CAN BE CARRIED OUT
//BY PACKING MORE HERE.
//-----------------------------------------
while(tries<TRIES){
//warp stride loading
for(int i=0; i<32; i+=2){
proc_vert=(warp_id<<5)+i+(lane_id>>4);
depth_curr.x=0;depth_curr.y=0;depth_curr.y=0;depth_curr.w=0;
depth_curr=~depth_curr;
if(proc_vert<NUM_VER)
{
load_ptr=(tries<<4)+beg_pos_s[proc_vert]+(lane_id%16);
if(load_ptr<NUM_VER)
{
depth_curr=depth_comp_curr[load_ptr];
card_curr=beg_pos_d[load_ptr+1]-beg_pos_d[load_ptr];
}else card_curr=0;//out-of-boundary, set it orphan
}else card_curr=0;//out-of-boundary, set it orphan
mark_s[(proc_vert<<4)+(lane_id%16)]=0x00;
if(((~depth_curr)!=0)&&(card_curr>0))
if(card_curr<sml_shed)
mark_s[(proc_vert<<4)+(lane_id%16)]=0x03;//0011
else if(card_curr>lrg_shed)
mark_s[(proc_vert<<4)+(lane_id%16)]=0x09;//1001
else mark_s[(proc_vert<<4)+(lane_id%16)]=0x05;//0101
}
__syncthreads();
//thread stride checking
for(int i=0; i<16; i++)
if(mark_s[thread_off+i]&0x02){
ex_sml_q_d[ex_sml_ptr]=beg_pos_own+(tries<<4)+i;
ex_sml_ptr++;
}else if(mark_s[thread_off+i]&0x08){
ex_lrg_q_d[ex_lrg_ptr]=beg_pos_own+(tries<<4)+i;
ex_lrg_ptr++;
}else if(mark_s[thread_off+i]&0x04){
ex_mid_q_d[ex_mid_ptr]=beg_pos_own+(tries<<4)+i;
ex_mid_ptr++;
}
++tries;
__syncthreads();
}
}
}
//+--------------------------------------------
//|CLASSIFIED BASED STORAGE FOR EX_QUEUE
//+---------------------------------------------------
void
gpu_ibfs::
td_inspect(depth_t curr_level)
{
td_frontier_count
<<<BLKS_NUM,THDS_NUM>>>(
beg_pos_d,
cat_sml_sz_d,
cat_mid_sz_d,
cat_lrg_sz_d,
depth_comp_last,
depth_comp_curr,
curr_level,
g->vert_count,
sml_shed,
lrg_shed
);
cudaDeviceSynchronize();
insp_scan
(
cat_sml_sz_d,
cat_sml_off_d,
THDS_NUM*BLKS_NUM,
BLKS_NUM>>1,
THDS_NUM>>1,
ex_sml_sz_d,
stream[0]
);
insp_scan
(
cat_mid_sz_d,
cat_mid_off_d,
THDS_NUM*BLKS_NUM,
BLKS_NUM>>1,
THDS_NUM>>1,
ex_mid_sz_d,
stream[1]
);
insp_scan
(
cat_lrg_sz_d,
cat_lrg_off_d,
THDS_NUM*BLKS_NUM,
BLKS_NUM>>1,
THDS_NUM>>1,
ex_lrg_sz_d,
stream[2]
);
cudaStreamSynchronize(stream[0]);
cudaStreamSynchronize(stream[1]);
cudaStreamSynchronize(stream[2]);
cudaDeviceSynchronize();
td_frontier_gather
<<<BLKS_NUM,THDS_NUM>>>(
beg_pos_d,
ex_sml_q_d,
ex_mid_q_d,
ex_lrg_q_d,
cat_sml_off_d,
cat_mid_off_d,
cat_lrg_off_d,
depth_comp_last,
depth_comp_curr,
curr_level,
g->vert_count,
sml_shed,
lrg_shed
);
}
void
gpu_ibfs::
sw_inspect(depth_t curr_level)
{
is_done[0]=true;
extract_depth
<<<BLKS_NUM<<2,THDS_NUM<<2,0,gstream>>>
(
depth_comp_last,
depth_comp_curr,
is_done_d,
curr_level,
g->vert_count
);
sw_frontier_count
<<<BLKS_NUM,THDS_NUM>>>(
beg_pos_d,
cat_sml_sz_d,
cat_mid_sz_d,
cat_lrg_sz_d,
depth_comp_curr,
curr_level,
g->vert_count,
sml_shed,
lrg_shed
);
cudaDeviceSynchronize();
insp_scan
(
cat_sml_sz_d,
cat_sml_off_d,
THDS_NUM*BLKS_NUM,
BLKS_NUM>>1,
THDS_NUM>>1,
ex_sml_sz_d,
stream[0]
);
insp_scan
(
cat_mid_sz_d,
cat_mid_off_d,
THDS_NUM*BLKS_NUM,
BLKS_NUM>>1,
THDS_NUM>>1,
ex_mid_sz_d,
stream[1]
);
insp_scan
(
cat_lrg_sz_d,
cat_lrg_off_d,
THDS_NUM*BLKS_NUM,
BLKS_NUM>>1,
THDS_NUM>>1,
ex_lrg_sz_d,
stream[2]
);
cudaStreamSynchronize(stream[0]);
cudaStreamSynchronize(stream[1]);
cudaStreamSynchronize(stream[2]);
cudaDeviceSynchronize();
sw_frontier_gather
<<<BLKS_NUM, THDS_NUM>>>(
beg_pos_d,
ex_sml_q_d,
ex_mid_q_d,
ex_lrg_q_d,
cat_sml_off_d,
cat_mid_off_d,
cat_lrg_off_d,
depth_comp_curr,
curr_level,
g->vert_count,
sml_shed,
lrg_shed
);
cudaStreamSynchronize(gstream);
}
void gpu_ibfs::
inspector(depth_t level)
{
if(level<sw_level)
td_inspect(level);
else
sw_inspect(level);
}