Repository navigation
Expand file tree
/
Copy pathBloomTree.cu
More file actions
781 lines (636 loc) · 20.5 KB
/
Copy pathBloomTree.cu
File metadata and controls
781 lines (636 loc) · 20.5 KB
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
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
#include <stdio.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <math.h>
#include <stdint.h>
#include <inttypes.h>
#define FORCE_INLINE
__device__ int Parent(int node);
__device__ int LeftChild(int node);
__device__ int RightChild(int node);
__device__ int Sibling(int node);
__device__ int calculate_lca(int u, int v);
__device__ void traversal(long int prev, long int lca, long int src, long int dest, int n, uint64_t *hash, bool *bit, int h, int m);
__device__ bool check_traversal_up(int prev, int lca, int src, int dest, int n, bool *mask);
__device__ bool check_traversal_down(int prev, int lca, int src, int dest, int n, bool *mask);
__device__ bool check_traversal_up_no_mask(int prev, int lca, int src, int dest, int n, uint64_t *hash_value, bool *bit, int h, int m);
__device__ bool check_traversal_down_no_mask(int prev, int lca, int src, int dest, int n, uint64_t *hash_value, bool *bit, int h, int m);
__device__ bool CheckBloom(long tid, uint64_t *hash_value, bool *bit, int h, int m);
__device__ void SetBloom(long tid, uint64_t *hash_value, bool *bit, int h, int m);
__device__ static inline FORCE_INLINE uint64_t rotl64 ( uint64_t x, int8_t r )
{
return (x << r) | (x >> (64 - r));
}
#define ROTL64(x,y) rotl64(x,y)
#define BIG_CONSTANT(x) (x##LLU)
#define getblock(p, i) (p[i])
__device__ static inline FORCE_INLINE uint64_t fmix64 ( uint64_t k )
{
k ^= k >> 33;
k *= BIG_CONSTANT(0xff51afd7ed558ccd);
k ^= k >> 33;
k *= BIG_CONSTANT(0xc4ceb9fe1a85ec53);
k ^= k >> 33;
return k;
}
__device__ void MurmurHash3_x64_128 ( int tid, char key[], const int len,
const uint32_t seed, void * out1 , void * out2)
{
uint8_t data[16];
for (int i=0; i<len; i++)
data[i] = (uint8_t) key[i];
const int nblocks = len / 16;
int i;
uint64_t h1 = seed;
uint64_t h2 = seed;
uint64_t c1 = BIG_CONSTANT(0x87c37b91114253d5);
uint64_t c2 = BIG_CONSTANT(0x4cf5ad432745937f);
//----------
// body
uint64_t blocks[16];
for (int i=0; i<len; i++)
blocks[i] = (uint64_t) data[i];
for(i = 0; i < nblocks; i++)
{
uint64_t k1 = blocks[i*2+0];
uint64_t k2 = blocks[i*2+1];
k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1;
h1 = ROTL64(h1,27); h1 += h2; h1 = h1*5+0x52dce729;
k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2;
h2 = ROTL64(h2,31); h2 += h1; h2 = h2*5+0x38495ab5;
}
//----------
// tail
uint8_t tail[16];
for (int i=0; i<len-nblocks*16; i++)
tail[i] = (uint64_t) data[i+nblocks*16];
uint64_t k1 = 0;
uint64_t k2 = 0;
switch(len & 15)
{
case 15: k2 ^= (uint64_t)(tail[14]) << 48;
case 14: k2 ^= (uint64_t)(tail[13]) << 40;
case 13: k2 ^= (uint64_t)(tail[12]) << 32;
case 12: k2 ^= (uint64_t)(tail[11]) << 24;
case 11: k2 ^= (uint64_t)(tail[10]) << 16;
case 10: k2 ^= (uint64_t)(tail[ 9]) << 8;
case 9: k2 ^= (uint64_t)(tail[ 8]) << 0;
k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2;
case 8: k1 ^= (uint64_t)(tail[ 7]) << 56;
case 7: k1 ^= (uint64_t)(tail[ 6]) << 48;
case 6: k1 ^= (uint64_t)(tail[ 5]) << 40;
case 5: k1 ^= (uint64_t)(tail[ 4]) << 32;
case 4: k1 ^= (uint64_t)(tail[ 3]) << 24;
case 3: k1 ^= (uint64_t)(tail[ 2]) << 16;
case 2: k1 ^= (uint64_t)(tail[ 1]) << 8;
case 1: k1 ^= (uint64_t)(tail[ 0]) << 0;
k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1;
};
//----------
// finalization
h1 ^= len; h2 ^= len;
h1 += h2;
h2 += h1;
h1 = fmix64(h1);
h2 = fmix64(h2);
h1 += h2;
h2 += h1;
((uint64_t*)out1)[0] = h1;
((uint64_t*)out2)[0] = h2;
}
__device__ inline uint64_t NthHash(uint8_t n, uint64_t hashA, uint64_t hashB, uint64_t filter_size) {
return ((hashA + n * hashB) % filter_size);
}
__global__ void insert_edge(int *u, int *v, bool *bit, int n, int e, long int ful_vertices,long int valsperloop,int ii,int h, int m, uint64_t *hash)
{
int blockNum = blockIdx.z*(gridDim.x*gridDim.y)+blockIdx.y*gridDim.x+blockIdx.x;
int threadNum = threadIdx.z*(blockDim.x*blockDim.y)+threadIdx.y*blockDim.x+threadIdx.x;
int tid = blockNum*(blockDim.x*blockDim.y*blockDim.z)+threadNum;
long int tid1 = ii*valsperloop + tid;
if (tid1 < e){
u[tid] = u[tid] + n - 1;
v[tid] = v[tid] + n - 1;
int src = u[tid];
int dest = v[tid];
if (!((u[tid] < ful_vertices && v[tid] < ful_vertices) || (u[tid] >= ful_vertices && v[tid] >= ful_vertices))) {
if (u[tid] > v[tid]){
int cur = Parent(u[tid]);
SetBloom((cur*n+u[tid]-n+1) << 1,hash+2*tid*sizeof(uint64_t),bit,h,m);
if (u[tid] == LeftChild(cur)){
SetBloom((cur*n+v[tid]-n+1) << 1,hash+2*tid*sizeof(uint64_t),bit,h,m);
}
else{
SetBloom(((cur*n+v[tid]-n+1) << 1) + 1,hash+2*tid*sizeof(uint64_t),bit,h,m);
}
u[tid] = cur;
}
else{
int cur = Parent(v[tid]);
SetBloom((cur*n+v[tid]-n+1) << 1,hash+2*tid*sizeof(uint64_t),bit,h,m);
if (v[tid] == LeftChild(cur)){
SetBloom((cur*n+u[tid]-n+1) << 1,hash+2*tid*sizeof(uint64_t),bit,h,m);
}
else{
SetBloom(((cur*n+u[tid]-n+1) << 1) + 1,hash+2*tid*sizeof(uint64_t),bit,h,m);
}
v[tid] = cur;
}
}
__syncthreads();
int lca = calculate_lca(u[tid], v[tid]);
traversal(u[tid], lca, src, dest, n, hash+2*tid*sizeof(uint64_t),bit,h,m);
traversal(v[tid], lca, dest, src, n, hash+2*tid*sizeof(uint64_t),bit,h,m);
}
}
__global__ void SetBloom(bool *mask, uint64_t *hash_value, bool *bit, int h, int m, long int valsperloop, int ii)
{
int blockNum = blockIdx.z*(gridDim.x*gridDim.y)+blockIdx.y*gridDim.x+blockIdx.x;
int threadNum = threadIdx.z*(blockDim.x*blockDim.y)+threadIdx.y*blockDim.x+threadIdx.x;
int tid = blockNum*(blockDim.x*blockDim.y*blockDim.z)+threadNum;
if (mask[tid] == 1){
long tid1 = valsperloop*ii + tid;
int i =0;
int count=0;
long num = tid1;
char str[10];
do{
count++;
num /= 10;
}while(num != 0);
num = tid1;
do{
str[count-i-1] = num%10 + '0';
num/=10;
i++;
}while(num !=0);
str[i] = 48+'\0';
uint64_t len1 = (uint64_t) count;
size_t len = (size_t) len1;
MurmurHash3_x64_128(tid1, str, len, 0, (hash_value)+tid*2*sizeof(uint64_t), (hash_value)+(tid*2+1)*sizeof(uint64_t));
for (int i=0; i<h; i++){
bit[NthHash(i,*((hash_value)+tid*2*sizeof(uint64_t)),*((hash_value)+(tid*2+1)*sizeof(uint64_t)),m)] = 1;
}
}
}
__global__ void set_mask(bool *mask, uint64_t *hash_value, bool *bit, int h, int m, int n, long int valsperloop, int ii)
{
int blockNum = blockIdx.z*(gridDim.x*gridDim.y)+blockIdx.y*gridDim.x+blockIdx.x;
int threadNum = threadIdx.z*(blockDim.x*blockDim.y)+threadIdx.y*blockDim.x+threadIdx.x;
int tid = blockNum*(blockDim.x*blockDim.y*blockDim.z)+threadNum;
if ((valsperloop*ii + tid) < n){
long tid1 = valsperloop*ii + tid;
mask[tid] = CheckBloom(tid1, (hash_value)+2*tid*sizeof(uint64_t), bit, h, m);
}
}
__global__ void check_bloom(int *found, bool *bit, bool *mask, uint64_t *hash_value, int m, int h, int n)
{
int val = blockIdx.x;
int hash = threadIdx.x;
uint64_t filter_size = (uint64_t) m;
uint8_t hash_no = (uint8_t) hash;
if (val < n && *found == 0){
if (mask[val] == 1){
if (hash < h){
if (bit[NthHash(hash_no,*(hash_value+2*val*sizeof(uint64_t)),*(hash_value+(2*val+1)*sizeof(uint64_t)), filter_size)] == 0){
atomicAdd(found, 1);
}
}
}
}
}
__global__ void get_neighbours(int u, bool *neighs, bool *mask, int n, long int ful_vertices)
{
int blockNum = blockIdx.z*(gridDim.x*gridDim.y)+blockIdx.y*gridDim.x+blockIdx.x;
int threadNum = threadIdx.z*(blockDim.x*blockDim.y)+threadIdx.y*blockDim.x+threadIdx.x;
int tid = blockNum*(blockDim.x*blockDim.y*blockDim.z)+threadNum;
long v=tid;
*(neighs+(tid)*sizeof(bool)) = 0;
if (tid < n && !(u==tid) ){
u = u + n - 1;
v = v + n - 1;
long src = u;
long dest = v;
if (!((u < ful_vertices && v < ful_vertices) || (u >= ful_vertices && v >= ful_vertices))) {
if (u > v){
long cur = Parent(u);
if ((mask[(cur*n+u-n+1) << 1] == 0)) return;
u = cur;
}
else{
long cur = Parent(v);
if (v == LeftChild(cur))
{
if ((mask[(cur*n+u-n+1) << 1] == 0))
return;
}
else
{
if ((mask[((cur*n+u-n+1) << 1) + 1] == 0))
return;
}
v = cur;
}
}
//__syncthreads();
int lca = calculate_lca(u, v);
if (check_traversal_up(u, lca, src, dest, n, mask))
{
if(check_traversal_down(v, lca, dest, src, n, mask))
{
*(neighs+(dest-n+1)*sizeof(bool)) = 1;
}
}
}
}
__global__ void check_edge(int u, int v, bool *set, uint64_t *hash_value, bool *bit, int h, int m, int n, long int ful_vertices)
{
int blockNum = blockIdx.z*(gridDim.x*gridDim.y)+blockIdx.y*gridDim.x+blockIdx.x;
int threadNum = threadIdx.z*(blockDim.x*blockDim.y)+threadIdx.y*blockDim.x+threadIdx.x;
int tid = blockNum*(blockDim.x*blockDim.y*blockDim.z)+threadNum;
*(set+(tid)*sizeof(bool)) = 0;
if (v < n && !(u==v) ){
u = u + n - 1;
v = v + n - 1;
long src = u;
long dest = v;
if (!((u < ful_vertices && v < ful_vertices) || (u >= ful_vertices && v >= ful_vertices))) {
if (u > v){
long cur = Parent(u);
if (!(CheckBloom((cur*n+u-n+1) << 1, (hash_value)+2*tid*sizeof(uint64_t), bit, h, m))) return;
u = cur;
}
else{
long cur = Parent(v);
if (v == LeftChild(cur))
{
if (!(CheckBloom((cur*n+u-n+1) << 1, (hash_value)+2*tid*sizeof(uint64_t), bit, h, m)))
return;
}
else
{
if (!(CheckBloom(((cur*n+u-n+1) << 1) + 1, (hash_value)+2*tid*sizeof(uint64_t), bit, h, m)))
return;
}
v = cur;
}
}
//__syncthreads();
int lca = calculate_lca(u, v);
if (check_traversal_up_no_mask(u, lca, src, dest, n, hash_value, bit, h, m))
{
if(check_traversal_down_no_mask(v, lca, dest, src, n, hash_value, bit, h, m))
{
*(set+(tid)*sizeof(bool)) = 1;
}
}
}
}
__device__ int Parent(int node)
{
return (((node + 1) >> 1) - 1);
}
__device__ int LeftChild(int node)
{
return (((node + 1) << 1) - 1);
}
__device__ int RightChild(int node)
{
return ((node + 1) << 1);
}
__device__ int Sibling(int node)
{
return (((node + 1) ^ 1) - 1);
}
__device__ int calculate_lca(int u, int v)
{
int val1 = 0;
int val2 = 0;
int i = 1;
do{
float pow_val = 1 << i;
val1 = floor((u+1)/pow_val);
val2 = floor((v+1)/pow_val);
i++;
} while(val1 != val2);
return (val1 - 1);
}
__device__ void traversal(long int prev, long int lca, long int src, long int dest, int n, uint64_t *hash, bool *bit, int h, int m)
{
int cur = Parent(prev);
while (cur != lca){
SetBloom((cur*n+src-n+1) << 1,hash,bit,h,m);
if (prev == LeftChild(cur)){
SetBloom((cur*n+dest-n+1) << 1,hash,bit,h,m);
}
else{
SetBloom(((cur*n+dest-n+1) << 1) + 1,hash,bit,h,m);
}
prev = cur;
cur = Parent(cur);
}
SetBloom(((cur*n+src-n+1) << 1) + 1,hash,bit,h,m);
}
__device__ bool check_traversal_up(int prev, int lca, int src, int dest, int n, bool *mask)
{
int cur = Parent(prev);
while (cur != lca){
if ((mask[(cur*n+src-n+1) << 1]) == 0) return false;
prev = cur;
cur = Parent(cur);
}
if ((mask[((cur*n+src-n+1) << 1) + 1]) == 0) return false;
return true;
}
__device__ bool check_traversal_down(int prev, int lca, int src, int dest, int n, bool *mask)
{
int cur = Parent(prev);
while (cur != lca){
if (prev == LeftChild(cur))
{
if ((mask[(cur*n+dest-n+1) << 1]) == 0)
return false;
}
else
{
if ((mask[((cur*n+dest-n+1) << 1) + 1]) == 0)
return false;
}
prev = cur;
cur = Parent(cur);
}
return true;
}
__device__ bool check_traversal_up_no_mask(int prev, int lca, int src, int dest, int n, uint64_t *hash_value, bool *bit, int h, int m)
{
int cur = Parent(prev);
while (cur != lca){
if (!(CheckBloom((cur*n+src-n+1) << 1, (hash_value)+2*tid*sizeof(uint64_t), bit, h, m))) return false;
prev = cur;
cur = Parent(cur);
}
if (!(CheckBloom(((cur*n+src-n+1) << 1) + 1, (hash_value)+2*tid*sizeof(uint64_t), bit, h, m))) return false;
return true;
}
__device__ bool check_traversal_down_no_mask(int prev, int lca, int src, int dest, int n, uint64_t *hash_value, bool *bit, int h, int m )
{
int cur = Parent(prev);
while (cur != lca){
if (prev == LeftChild(cur))
{
if (!(CheckBloom((cur*n+dest-n+1) << 1, (hash_value)+2*tid*sizeof(uint64_t), bit, h, m)))
return false;
}
else
{
if (!(CheckBloom(((cur*n+dest-n+1) << 1) + 1, (hash_value)+2*tid*sizeof(uint64_t), bit, h, m)))
return false;
}
prev = cur;
cur = Parent(cur);
}
return true;
}
__device__ void SetBloom(long tid, uint64_t *hash_value, bool *bit, int h, int m)
{
int i =0;
int count=0;
long num = tid;
char str[11];
do{
count++;
num /= 10;
}while(num != 0);
num = tid;
do{
str[count-i-1] = num%10 + '0';
num/=10;
i++;
}while(num !=0);
str[i] = 48+'\0';
uint64_t len1 = (uint64_t) count;
size_t len = (size_t) len1;
MurmurHash3_x64_128(tid, str, len, 0, (hash_value), (hash_value)+sizeof(uint64_t));
for (int i=0; i<h; i++){
bit[NthHash(i,*(hash_value),*((hash_value)+sizeof(uint64_t)),m)] = 1;
}
}
__device__ bool CheckBloom(long tid, uint64_t *hash_value, bool *bit, int h, int m)
{
int i =0;
int count=0;
long num = tid;
char str[10];
do{
count++;
num /= 10;
}while(num != 0);
num = tid;
do{
str[count-i-1] = num%10 + '0';
num/=10;
i++;
}while(num !=0);
str[i] = 48+'\0';
uint64_t len1 = (uint64_t) count;
size_t len = (size_t) len1;
MurmurHash3_x64_128(tid, str, len, 0, (hash_value), (hash_value)+sizeof(uint64_t));
for (int i=0; i<h; i++){
if (bit[NthHash(i,*(hash_value),*((hash_value)+sizeof(uint64_t)),m)] == 0){
return false;
}
}
return true;
}
void InsertEdge(int num_vertices, int num_edges, int num_hashes, int num_bits, int *h_u, int *h_v, bool *h_bits)
{
cudaError_t err = cudaSuccess;
cudaEvent_t start1, stop1;
cudaEventCreate(&start1);
cudaEventCreate(&stop1);
size_t size = num_edges * sizeof(int);
int num_ful_levels = floor( log2((double) (2*num_vertices - 1)));
long int ful_vertices = pow((int) 2,(int) num_ful_levels) - 1;
size_t size_bits = num_bits * sizeof(bool);
bool *d_bits = NULL;
err=cudaMalloc((void **)&d_bits, size_bits);
cudaDeviceSynchronize();
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to allocate vector bits (error code %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
cudaMemset(d_bits, 0, size_bits);
int *d_u = NULL, *d_v = NULL;
err=cudaMalloc((void **)&d_u, size);
cudaDeviceSynchronize();
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to allocate vector u (error code %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
err=cudaMalloc((void **)&d_v, size);
cudaDeviceSynchronize();
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to allocate vector v (error code %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
err=cudaMemcpy(d_u, h_u, size, cudaMemcpyHostToDevice);
cudaDeviceSynchronize();
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to copy u (error code %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
err=cudaMemcpy(d_v, h_v, size, cudaMemcpyHostToDevice);
cudaDeviceSynchronize();
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to copy v (error code %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
clock_t ti;
cudaEventRecord(start1);
ti=clock();
dim3 tpb1(32,32,1);
dim3 bpg1(8,2,1);
long int valsperloop = 1 << 14;
int N = ceil(num_edges/valsperloop)+1;
uint64_t *d_hash_value = NULL;
size_t size_hash = 2*num_edges*sizeof(uint64_t);
cudaMalloc((void **)&d_hash_value, size_hash);
for (int i=0; i<N; i++){
insert_edge<<<bpg1,tpb1>>>(d_u+i*valsperloop, d_v+i*valsperloop, d_bits, num_vertices, num_edges, ful_vertices, valsperloop, i, num_hashes, num_bits, d_hash_value);
/*cudaDeviceSynchronize();
err = cudaGetLastError();
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to launch insert_edge kernel %d (error code %s)!\n",i, cudaGetErrorString(err));
exit(EXIT_FAILURE);
}*/
}
cudaMemcpy(h_bits, d_bits, size_bits, cudaMemcpyDeviceToHost);
ti=clock() -ti;
cudaEventRecord(stop1);
cudaEventSynchronize(stop1);
float milliseconds = 0;
cudaEventElapsedTime(&milliseconds, start1, stop1);
printf("%.5f\n", float(ti) / CLOCKS_PER_SEC);
printf("time taken is %.5f\n", milliseconds);
int value=0;
for (long i =0; i<num_bits; i++){
if (h_bits[i] == 1) value++;
}
//printf("value is %d\n",value);
cudaFree(d_u);
cudaFree(d_v);
cudaFree(d_hash_value);
cudaFree(d_bits);
}
int * GetNeighbours(int u, bool *h_bits, int num_vertices)
{
cudaError_t err = cudaSuccess;
uint64_t *d_hash_value = NULL;
size_t size_hash = 2*num_vals*sizeof(uint64_t);
cudaMalloc((void **)&d_hash_value, size_hash);
size_t size_mask = num_vals * sizeof(bool);
bool *h_mask = (bool *)malloc(size_mask);
bool *d_mask = NULL;
cudaMalloc((void **)&d_mask, size_mask);
bool *d_bits = NULL;
cudaMalloc((void **)&d_bits, size_bits);
cudaMemcpy(d_bits, h_bits, size_bits, cudaMemcpyHostToDevice);
bool *d_neighs = NULL;
size_t size_neighs = (num_vertices)*sizeof(bool);
cudaMalloc((void **)&d_neighs, size_neighs);
bool *h_neighs = (bool *)malloc(size_neighs);
int num_ful_levels = floor( log2((double) (2*num_vertices - 1)));
long int ful_vertices = pow((int) 2,(int) num_ful_levels) - 1;
dim3 tpb2(32,32,1);
dim3 bpg2(2,2,1);
dim3 tpb3(32,32,1);
dim3 bpg3(4,4,1);
long int valsperloop = 1 << 14;
int N = ceil(num_vals/valsperloop)+1;
for (int i=0; i<N; i++){
set_mask<<<bpg3,tpb3>>>(d_mask+i*valsperloop,d_hash_value+i*2*valsperloop,d_bits,num_hashes,num_bits,num_vals,valsperloop,i);
cudaDeviceSynchronize();
err = cudaGetLastError();
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to launch set_mask kernel %d (error code %s)!\n",i, cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
}
get_neighbours<<<bpg2,tpb2>>>(u, d_neighs, d_mask, num_vertices, ful_vertices);
cudaMemcpy(h_neighs, d_neighs, size_neighs, cudaMemcpyDeviceToHost);
static int neigh[10000];
int count=0;
for (int i=0; i<num_vertices; i++){
if (h_neigh[i]==1)
neigh[count++]=i;
}
cudaFree(d_neighs);
cudaFree(d_hash_value);
cudaFree(d_bits);
cudaFree(d_mask);
return neigh;
}
bool IsEdge(int u, int v, bool *h_bits, int num_vertices, int num_hashes, int num_bits)
{
int num_ful_levels = floor( log2((double) (2*num_vertices - 1)));
long int ful_vertices = pow((int) 2,(int) num_ful_levels) - 1;
uint64_t *d_hash_value = NULL;
size_t size_hash = 2*sizeof(uint64_t);
cudaMalloc((void **)&d_hash_value, size_hash);
bool *d_set = NULL;
size_t size_set = sizeof(bool);
cudaMalloc((void **)&d_set, size_set);
bool *h_set = (bool *)malloc(size_set);
check_edge<<<1,1>>>(u, v, d_set, d_hash_value, h_bits, num_hashes, num_bits, num_vertices, ful_vertices);
cudaMemcpy(h_set, d_set, size_set, cudaMemcpyDeviceToHost);
if (h_set == 1) return true;
else return false;
}
int main ()
{
cudaError_t err = cudaSuccess;
int num_vertices, num_edges, num_hashes, num_bits;
scanf("%d",&num_vertices);
scanf("%d",&num_edges);
scanf("%d",&num_bits);
scanf("%d",&num_hashes);
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
size_t size = num_edges * sizeof(int);
int num_vals = 2*num_vertices*(num_vertices-1);
int *h_u = (int *)malloc(size);
int *h_v = (int *)malloc(size);
for (int i =0; i<num_edges; i++)
{
scanf("%d",&h_u[i]);
scanf("%d",&h_v[i]);
}
size_t size_bits = num_bits * sizeof(bool);
bool *h_bits = (bool *)malloc(size_bits);
InsertEdge(num_vertices, num_edges, num_hashes, num_bits, h_u, h_v, h_bits);
int *neighbours;
int u;
printf("Get neighbours of?\n");
scanf("%d",&u);
neighbours = GetNeighbours(u, h_bits, num_vertices);
int a,b;
printf("Enter nodes to check if edge is present between them:\n");
scanf("%d",&a);
scanf("%d",&b);
if (IsEdge(a, b, h_bits, num_vertices))
printf("Edge is present.\n");
else
printf("Edge is NOT present.\n");
free(h_u);
free(h_v);
free(h_bits);
cudaDeviceReset();
return 0;
}