-
Notifications
You must be signed in to change notification settings - Fork 1
/
rpma1.cuh
1717 lines (1421 loc) · 69.1 KB
/
rpma1.cuh
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
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
#include <thrust/remove.h>
#include <thrust/sort.h>
#include <cub/cub.cuh>
#include <cassert>
#include <stdio.h>
#include <algorithm>
#define cErr(errcode) { gpuAssert((errcode), __FILE__, __LINE__); }
__inline__ __host__ __device__
void gpuAssert(cudaError_t code, const char *file, int line) {
if (code != cudaSuccess) {
printf("GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
}
}
typedef unsigned long long KEY_TYPE;
typedef unsigned char VALUE_TYPE;
typedef unsigned int SIZE_TYPE;
typedef thrust::device_vector<KEY_TYPE> DEV_VEC_KEY;
typedef thrust::device_vector<VALUE_TYPE> DEV_VEC_VALUE;
typedef thrust::device_vector<SIZE_TYPE> DEV_VEC_SIZE;
typedef KEY_TYPE* KEY_PTR;
typedef VALUE_TYPE* VALUE_PTR;
typedef thrust::device_vector<KEY_PTR> DEV_VEC_LEVEL_KEY_PTR;
typedef thrust::device_vector<VALUE_PTR> DEV_VEC_LEVEL_VALUE_PTR;
#define RAW_PTR(x) thrust::raw_pointer_cast((x).data())
const KEY_TYPE KEY_NONE = -1;
const KEY_TYPE KEY_MAX = -2;
const SIZE_TYPE SIZE_NONE = -1;
const VALUE_TYPE VALUE_NONE = -1;
const KEY_TYPE SRC_MASK = 0xFFFFFC0000000000;
const KEY_TYPE SRC_SHIFT = 42;
const KEY_TYPE DST_MASK = 0x000003FFFFF00000;
const KEY_TYPE DST_SHIFT = 20;
const KEY_TYPE TIME_MASK = 0x00000000000FFFFF;
const KEY_TYPE DST_AND_TIME_MASK = 0x000003FFFFFFFFFF;
const KEY_TYPE DST_AND_TIME_END = 0x000003FFFFEFFFFF;
const SIZE_TYPE NBR_END = 0x003FFFFE;
const SIZE_TYPE WINDOW_LENGTH = 100000;
const SIZE_TYPE MAX_BLOCKS_NUM = 96 * 8;
#define CALC_BLOCKS_NUM(ITEMS_PER_BLOCK, CALC_SIZE) min(MAX_BLOCKS_NUM, (CALC_SIZE - 1) / ITEMS_PER_BLOCK + 1)
class RPMA {
public:
/*
DEV_VEC_KEY keys;
DEV_VEC_VALUE values;
*/
DEV_VEC_LEVEL_KEY_PTR levels_key_ptr_array;
DEV_VEC_LEVEL_VALUE_PTR levels_value_ptr_array;
SIZE_TYPE segment_length;
SIZE_TYPE tree_height;
double density_lower_thres_leaf = 0.08;
//double density_lower_thres_root = 0.42;
//double density_upper_thres_root = 0.84;
double density_lower_thres_root = 0.15;
double density_upper_thres_root = 1;
double density_upper_thres_leaf = 1;
thrust::host_vector<SIZE_TYPE> lower_element;
thrust::host_vector<SIZE_TYPE> upper_element;
SIZE_TYPE row_num;
DEV_VEC_SIZE csr_idx;
DEV_VEC_SIZE csr_lane;
};
__forceinline__ __host__ __device__
SIZE_TYPE fls(SIZE_TYPE x) {
SIZE_TYPE r = 32;
if (!x)
return 0;
if (!(x & 0xffff0000u))
x <<= 16, r -= 16;
if (!(x & 0xff000000u))
x <<= 8, r -= 8;
if (!(x & 0xf0000000u))
x <<= 4, r -= 4;
if (!(x & 0xc0000000u))
x <<= 2, r -= 2;
if (!(x & 0x80000000u))
x <<= 1, r -= 1;
return r;
}
__forceinline__ __host__ __device__
SIZE_TYPE lls(SIZE_TYPE x) {
SIZE_TYPE r = 1;
if (!(x & 0x0000ffffu))
x >>= 16, r += 16;
if (!(x & 0x000000ffu))
x >>= 8, r += 8;
if (!(x & 0x0000000fu))
x >>= 4, r += 4;
if (!(x & 0x00000003u))
x >>= 2, r += 2;
if (!(x & 0x00000001u))
x >>= 1, r += 1;
return r;
}
#define GET_IDX(LINEAR_IDX, HEIGHT) (HEIGHT + 1 - lls((LINEAR_IDX >> 5) + 1))
#define GET_LANE(LINEAR_IDX, IDX, HEIGHT) ((((((LINEAR_IDX >> 5) + 1) >> (HEIGHT - IDX)) - 1) >> 1) << 5) + (LINEAR_IDX % 32)
#define GET_LINEAR_IDX(IDX, LANE, HEIGHT) ((32 << (HEIGHT- IDX)) - 32 + ((LANE >> 5) << (HEIGHT - IDX + 1) << 5))
__host__
void recalculate_density(RPMA &rpma) {
rpma.lower_element.resize(26);
rpma.upper_element.resize(26);
cErr(cudaDeviceSynchronize());
SIZE_TYPE level_length = rpma.segment_length;
for (SIZE_TYPE i = 0; i <= 25; i++) {
/*
double density_lower = rpma.density_lower_thres_root
+ (rpma.density_lower_thres_leaf - rpma.density_lower_thres_root) * (rpma.tree_height - i)
/ rpma.tree_height;
double density_upper = rpma.density_upper_thres_root
+ (rpma.density_upper_thres_leaf - rpma.density_upper_thres_root) * (rpma.tree_height - i)
/ rpma.tree_height;
double density_lower = 0;
double density_upper = 1;
rpma.lower_element[i] = (SIZE_TYPE) ceil(density_lower * level_length);
rpma.upper_element[i] = (SIZE_TYPE) floor(density_upper * level_length);
// special trim for wrong threshold introduced by float-integer conversion
if (0 < i) {
rpma.lower_element[i] = max(rpma.lower_element[i], 2 * rpma.lower_element[i - 1]);
rpma.upper_element[i] = min(rpma.upper_element[i], 2 * rpma.upper_element[i - 1]);
}
level_length <<= 1;
*/
rpma.lower_element[i] = rpma.density_lower_thres_root * level_length;
rpma.upper_element[i] = level_length;
level_length <<= 1;
}
}
template<typename T>
__global__
void memcpy_kernel(T *dest, const T *src, SIZE_TYPE size) {
SIZE_TYPE global_thread_id = blockDim.x * blockIdx.x + threadIdx.x;
SIZE_TYPE block_offset = gridDim.x * blockDim.x;
for (SIZE_TYPE i = global_thread_id; i < size; i += block_offset) {
dest[i] = src[i];
}
}
template<typename T>
__global__
void memset_kernel(T *data, T value, SIZE_TYPE size) {
SIZE_TYPE global_thread_id = blockDim.x * blockIdx.x + threadIdx.x;
SIZE_TYPE block_offset = gridDim.x * blockDim.x;
for (SIZE_TYPE i = global_thread_id; i < size; i += block_offset) {
data[i] = value;
}
}
template<typename T>
__global__
void level_memset_kernel(T* data[], T value, SIZE_TYPE size, SIZE_TYPE data_offset, SIZE_TYPE tree_height) {
SIZE_TYPE global_thread_id = blockDim.x * blockIdx.x + threadIdx.x;
SIZE_TYPE block_offset = gridDim.x * blockDim.x;
for (SIZE_TYPE i = global_thread_id + data_offset; i < size + data_offset; i += block_offset) {
SIZE_TYPE idx = GET_IDX(i, tree_height);
SIZE_TYPE lane = GET_LANE(i, idx, tree_height);
data[idx][lane] = value;
}
}
__device__
void cub_sort_key_value(KEY_TYPE *keys, VALUE_TYPE *values, SIZE_TYPE size, KEY_PTR tmp_keys,
VALUE_PTR tmp_values, SIZE_TYPE update_node) {
void *d_temp_storage = NULL;
size_t temp_storage_bytes = 0;
cErr(cub::DeviceRadixSort::SortPairs(d_temp_storage, temp_storage_bytes, keys, tmp_keys, values, tmp_values, size));
cErr(cudaDeviceSynchronize());
//printf("sizeis: %llu\n", temp_storage_bytes);
cErr(cudaMalloc(&d_temp_storage, temp_storage_bytes));
cErr(cudaDeviceSynchronize());
cErr(cub::DeviceRadixSort::SortPairs(d_temp_storage, temp_storage_bytes, keys, tmp_keys, values, tmp_values, size));
cErr(cudaDeviceSynchronize());
cErr(cudaFree(d_temp_storage));
}
__global__
void global_cub_sort_key_value(KEY_TYPE *keys, VALUE_TYPE *values, SIZE_TYPE size, KEY_PTR tmp_keys,
VALUE_PTR tmp_values, SIZE_TYPE update_node) {
void *d_temp_storage = NULL;
size_t temp_storage_bytes = 0;
cErr(cub::DeviceRadixSort::SortPairs(d_temp_storage, temp_storage_bytes, keys, tmp_keys, values, tmp_values, size));
cErr(cudaDeviceSynchronize());
cErr(cudaMalloc(&d_temp_storage, temp_storage_bytes));
cErr(cudaDeviceSynchronize());
cErr(cub::DeviceRadixSort::SortPairs(d_temp_storage, temp_storage_bytes, keys, tmp_keys, values, tmp_values, size));
cErr(cudaDeviceSynchronize());
cErr(cudaFree(d_temp_storage));
}
__device__
SIZE_TYPE handle_del_mod(KEY_PTR keys[], VALUE_PTR values[], SIZE_TYPE seg_length,
KEY_TYPE key, VALUE_TYPE value, SIZE_TYPE leaf, SIZE_TYPE idx, SIZE_TYPE lane) {
if (VALUE_NONE == value)
leaf = SIZE_NONE;
for (SIZE_TYPE i = 0; i < seg_length; i++) {
if (keys[idx][lane + i] == key) {
values[idx][lane + i] = value;
leaf = SIZE_NONE;
break;
}
}
return leaf;
}
__global__
void locate_leaf_kernel(KEY_PTR key_ptrs[], VALUE_PTR value_ptrs[], SIZE_TYPE seg_length,
SIZE_TYPE tree_height, KEY_TYPE *update_keys, VALUE_TYPE *update_values, SIZE_TYPE update_size,
SIZE_TYPE *leaf) {
SIZE_TYPE global_thread_id = blockDim.x * blockIdx.x + threadIdx.x;
SIZE_TYPE block_offset = gridDim.x * blockDim.x;
__shared__ KEY_PTR block_key_ptrs[26];
__shared__ VALUE_PTR block_value_ptrs[26];
__shared__ KEY_TYPE levels_key[32 * ((1<<5)-1)];
__shared__ VALUE_TYPE levels_value[32 * ((1<<5)-1)];
if (threadIdx.x == 0) {
int thres = (tree_height > 4 ? 4 : tree_height);
for (int i = 0; i <= thres; ++i) {
int offset = (32 << i) - 32;
memcpy(levels_key + offset, key_ptrs[i], sizeof(KEY_TYPE) * (32 << i));
memcpy(levels_value + offset, value_ptrs[i], sizeof(VALUE_TYPE) * (32 << i));
block_key_ptrs[i] = levels_key + offset;
block_value_ptrs[i] = levels_value + offset;
}
memcpy(block_key_ptrs + thres + 1, key_ptrs + thres + 1, sizeof(KEY_PTR) * (25 - thres));
memcpy(block_value_ptrs + thres + 1, value_ptrs + thres + 1, sizeof(VALUE_PTR) * (25 - thres));
}
__syncthreads();
for (SIZE_TYPE i = global_thread_id; i < update_size; i += block_offset) {
KEY_TYPE key = update_keys[i];
VALUE_TYPE value = update_values[i];
SIZE_TYPE idx = 0, lane = 0;
while(idx < tree_height && KEY_NONE != block_key_ptrs[idx][lane]) {
if (block_key_ptrs[idx][lane] > key) {
++idx;
lane <<= 1;
continue;
}
SIZE_TYPE l = 0, r = seg_length - 1;
while(l < r) {
SIZE_TYPE mid = (l + r + 1) >> 1;
if (block_key_ptrs[idx][lane + mid] == KEY_NONE)
r = mid - 1;
else
l = mid;
}
if (key > block_key_ptrs[idx][lane + l]) {
++idx;
lane = (lane << 1) + seg_length;
}
else
break;
}
//SIZE_TYPE prefix = (seg_length << (tree_height - idx)) - seg_length + (lane << (tree_height - idx + 1));
SIZE_TYPE prefix = GET_LINEAR_IDX(idx, lane, tree_height);
//prefix = handle_del_mod(block_key_ptrs, block_value_ptrs, seg_length, key, value, prefix, idx, lane);
leaf[i] = prefix;
}
}
__host__
void locate_leaf_batch(KEY_PTR keys[], VALUE_PTR values[], SIZE_TYPE seg_length,
SIZE_TYPE tree_height, KEY_TYPE *update_keys, VALUE_TYPE *update_values, SIZE_TYPE update_size,
SIZE_TYPE *leaf) {
SIZE_TYPE THREADS_NUM = 256;
SIZE_TYPE BLOCKS_NUM = CALC_BLOCKS_NUM(THREADS_NUM, update_size);
locate_leaf_kernel<<<BLOCKS_NUM, THREADS_NUM>>>(keys, values, seg_length, tree_height, update_keys,
update_values, update_size, leaf);
cErr(cudaDeviceSynchronize());
}
struct three_tuple_first_none {
typedef thrust::tuple<SIZE_TYPE, KEY_TYPE, VALUE_TYPE> Tuple;
__host__ __device__
bool operator()(const Tuple &a) {
return SIZE_NONE == thrust::get<0>(a);
}
};
__host__
void compact_insertions(DEV_VEC_SIZE &update_nodes, DEV_VEC_KEY &update_keys, DEV_VEC_VALUE &update_values,
SIZE_TYPE &update_size) {
auto zip_begin = thrust::make_zip_iterator(
thrust::make_tuple(update_nodes.begin(), update_keys.begin(), update_values.begin()));
auto zip_end = thrust::remove_if(zip_begin, zip_begin + update_size, three_tuple_first_none());
cErr(cudaDeviceSynchronize());
update_size = zip_end - zip_begin;
}
__host__ SIZE_TYPE group_insertion_by_node(SIZE_TYPE *update_nodes, SIZE_TYPE update_size,
SIZE_TYPE *unique_update_nodes, SIZE_TYPE *update_offset) {
// step1: encode
void *d_temp_storage = NULL;
size_t temp_storage_bytes = 0;
SIZE_TYPE *tmp_offset;
cErr(cudaMalloc(&tmp_offset, sizeof(SIZE_TYPE) * update_size));
SIZE_TYPE *num_runs_out;
cErr(cudaMalloc(&num_runs_out, sizeof(SIZE_TYPE)));
cErr(cudaDeviceSynchronize());
cErr(cub::DeviceRunLengthEncode::Encode(d_temp_storage, temp_storage_bytes, update_nodes,
unique_update_nodes, tmp_offset, num_runs_out, update_size));
cErr(cudaDeviceSynchronize());
cErr(cudaMalloc(&d_temp_storage, temp_storage_bytes));
cErr(cudaDeviceSynchronize());
cErr(cub::DeviceRunLengthEncode::Encode(d_temp_storage, temp_storage_bytes, update_nodes,
unique_update_nodes, tmp_offset, num_runs_out, update_size));
cErr(cudaDeviceSynchronize());
SIZE_TYPE unique_node_size[1];
cErr(cudaMemcpy(unique_node_size, num_runs_out, sizeof(SIZE_TYPE), cudaMemcpyDeviceToHost));
cErr(cudaDeviceSynchronize());
cErr(cudaFree(num_runs_out));
cErr(cudaFree(d_temp_storage));
// step2: exclusive scan
d_temp_storage = NULL;
temp_storage_bytes = 0;
cErr(cub::DeviceScan::ExclusiveSum(d_temp_storage, temp_storage_bytes, tmp_offset,
update_offset, unique_node_size[0]));
cErr(cudaDeviceSynchronize());
cErr(cudaMalloc(&d_temp_storage, temp_storage_bytes));
cErr(cudaDeviceSynchronize());
cErr(cub::DeviceScan::ExclusiveSum(d_temp_storage, temp_storage_bytes, tmp_offset,
update_offset, unique_node_size[0]));
cErr(cudaDeviceSynchronize());
cErr(cudaFree(d_temp_storage));
cErr(cudaMemcpy(update_offset + unique_node_size[0], &update_size, sizeof(SIZE_TYPE), cudaMemcpyHostToDevice));
cErr(cudaDeviceSynchronize());
cErr(cudaFree(tmp_offset));
return unique_node_size[0];
}
__host__
void compress_insertions_by_node(DEV_VEC_SIZE &update_nodes, SIZE_TYPE update_size,
DEV_VEC_SIZE &unique_update_nodes, DEV_VEC_SIZE &update_offset, SIZE_TYPE &unique_node_size) {
unique_node_size = group_insertion_by_node(RAW_PTR(update_nodes), update_size, RAW_PTR(unique_update_nodes),
RAW_PTR(update_offset));
cErr(cudaDeviceSynchronize());
}
struct kv_tuple_none {
typedef thrust::tuple<KEY_TYPE, VALUE_TYPE> Tuple;
__host__ __device__
bool operator()(const Tuple &a) {
return KEY_NONE == thrust::get<0>(a) || VALUE_NONE == thrust::get<1>(a);
}
};
template<SIZE_TYPE THREAD_PER_BLOCK, SIZE_TYPE ITEM_PER_THREAD>
__device__
void block_compact_kernel(KEY_TYPE *keys, VALUE_TYPE *values, SIZE_TYPE &compacted_size,
KEY_TYPE *tmp_keys, VALUE_TYPE *tmp_values, SIZE_TYPE thres) {
typedef cub::BlockScan<SIZE_TYPE, THREAD_PER_BLOCK> BlockScan;
SIZE_TYPE thread_id = threadIdx.x;
KEY_TYPE *block_keys = keys;
VALUE_TYPE *block_values = values;
KEY_TYPE thread_keys[ITEM_PER_THREAD];
VALUE_TYPE thread_values[ITEM_PER_THREAD];
SIZE_TYPE thread_offset = thread_id * ITEM_PER_THREAD;
for (SIZE_TYPE i = 0; i < ITEM_PER_THREAD; i++) {
thread_keys[i] = block_keys[i];
thread_values[i] = block_values[i];
//block_keys[i] = KEY_NONE;
}
__shared__ typename BlockScan::TempStorage temp_storage;
SIZE_TYPE thread_data[ITEM_PER_THREAD];
for (SIZE_TYPE i = 0; i < ITEM_PER_THREAD; i++) {
thread_data[i] = (thread_keys[i] == KEY_NONE || (thread_keys[i] & TIME_MASK) < (KEY_TYPE)thres) ? 0 : 1 ;
}
__syncthreads();
BlockScan(temp_storage).ExclusiveSum(thread_data, thread_data);
__syncthreads();
__shared__ SIZE_TYPE exscan[THREAD_PER_BLOCK * ITEM_PER_THREAD];
for (SIZE_TYPE i = 0; i < ITEM_PER_THREAD; i++) {
exscan[i + thread_offset] = thread_data[i];
}
__syncthreads();
for (SIZE_TYPE i = 0; i < ITEM_PER_THREAD; i++) {
if (thread_id == THREAD_PER_BLOCK - 1 && i == ITEM_PER_THREAD - 1)
continue;
if (exscan[thread_offset + i] != exscan[thread_offset + i + 1]) {
SIZE_TYPE loc = exscan[thread_offset + i];
tmp_keys[loc] = thread_keys[i];
tmp_values[loc] = thread_values[i];
}
}
// special logic for the last element
if (thread_id == THREAD_PER_BLOCK - 1) {
SIZE_TYPE loc = exscan[THREAD_PER_BLOCK * ITEM_PER_THREAD - 1];
if (thread_keys[ITEM_PER_THREAD - 1] == KEY_NONE || (thread_keys[ITEM_PER_THREAD - 1] & TIME_MASK) < (KEY_TYPE)thres) {
compacted_size = loc;
} else {
compacted_size = loc + 1;
tmp_keys[loc] = thread_keys[ITEM_PER_THREAD - 1];
tmp_values[loc] = thread_values[ITEM_PER_THREAD - 1];
}
}
__syncthreads();
for (SIZE_TYPE i = compacted_size + thread_id; i < THREAD_PER_BLOCK * ITEM_PER_THREAD; i += THREAD_PER_BLOCK) {
tmp_keys[i] = KEY_NONE;
}
}
template<typename FIRST_TYPE, typename SECOND_TYPE>
__device__
void block_pair_copy_kernel(FIRST_TYPE *dest_first, SECOND_TYPE *dest_second, FIRST_TYPE *src_first,
SECOND_TYPE *src_second, SIZE_TYPE size) {
for (SIZE_TYPE i = threadIdx.x; i < size; i += blockDim.x) {
dest_first[i] = src_first[i];
dest_second[i] = src_second[i];
}
}
template<SIZE_TYPE THREAD_PER_BLOCK, SIZE_TYPE ITEM_PER_THREAD>
__device__
void block_redispatch_kernel(KEY_PTR keys[], VALUE_PTR values[], SIZE_TYPE rebalance_width, SIZE_TYPE seg_length,
SIZE_TYPE merge_size, SIZE_TYPE update_node, KEY_PTR block_keys, VALUE_PTR block_values,
SIZE_TYPE block_offset, SIZE_TYPE tree_height, SIZE_TYPE *csr_idx, SIZE_TYPE *csr_lane) {
// step2: sort by key with value on shared memory
typedef cub::BlockLoad<KEY_TYPE, THREAD_PER_BLOCK, ITEM_PER_THREAD, cub::BLOCK_LOAD_TRANSPOSE> BlockKeyLoadT;
typedef cub::BlockLoad<VALUE_TYPE, THREAD_PER_BLOCK, ITEM_PER_THREAD, cub::BLOCK_LOAD_TRANSPOSE> BlockValueLoadT;
typedef cub::BlockStore<KEY_TYPE, THREAD_PER_BLOCK, ITEM_PER_THREAD, cub::BLOCK_STORE_TRANSPOSE> BlockKeyStoreT;
typedef cub::BlockStore<VALUE_TYPE, THREAD_PER_BLOCK, ITEM_PER_THREAD, cub::BLOCK_STORE_TRANSPOSE> BlockValueStoreT;
typedef cub::BlockRadixSort<KEY_TYPE, THREAD_PER_BLOCK, ITEM_PER_THREAD, VALUE_TYPE> BlockRadixSortT;
__shared__ union {
typename BlockKeyLoadT::TempStorage key_load;
typename BlockValueLoadT::TempStorage value_load;
typename BlockKeyStoreT::TempStorage key_store;
typename BlockValueStoreT::TempStorage value_store;
typename BlockRadixSortT::TempStorage sort;
} temp_storage;
KEY_TYPE thread_keys[ITEM_PER_THREAD];
VALUE_TYPE thread_values[ITEM_PER_THREAD];
BlockKeyLoadT(temp_storage.key_load).Load(block_keys, thread_keys);
BlockValueLoadT(temp_storage.value_load).Load(block_values, thread_values);
__syncthreads();
BlockRadixSortT(temp_storage.sort).Sort(thread_keys, thread_values);
__syncthreads();
BlockKeyStoreT(temp_storage.key_store).Store(block_keys, thread_keys);
BlockValueStoreT(temp_storage.value_store).Store(block_values, thread_values);
__syncthreads();
// step3: evenly re-dispatch KVs to leaf segments
KEY_TYPE frac = rebalance_width / seg_length;
KEY_TYPE deno = merge_size;
for (SIZE_TYPE i = threadIdx.x + block_offset; i < rebalance_width + block_offset; i += blockDim.x) {
SIZE_TYPE idx = GET_IDX(i, tree_height);
SIZE_TYPE lane = GET_LANE(i, idx, tree_height);
keys[idx][lane] = KEY_NONE;
}
__syncthreads();
for (SIZE_TYPE i = threadIdx.x; i < merge_size; i += blockDim.x) {
SIZE_TYPE seg_idx = (SIZE_TYPE) (frac * i / deno);
SIZE_TYPE seg_lane = (SIZE_TYPE) (frac * i % deno / frac);
SIZE_TYPE proj_location = seg_idx * seg_length + seg_lane;
KEY_TYPE cur_key = block_keys[i];
VALUE_TYPE cur_value = block_values[i];
SIZE_TYPE linear_idx = proj_location + block_offset;
SIZE_TYPE idx = GET_IDX(linear_idx, tree_height);
SIZE_TYPE lane = GET_LANE(linear_idx, idx, tree_height);
keys[idx][lane] = cur_key;
values[idx][lane] = cur_value;
//addition for csr
if ((cur_key & DST_AND_TIME_MASK) == DST_AND_TIME_END) {
SIZE_TYPE cur_row = (SIZE_TYPE) (cur_key >> SRC_SHIFT);
csr_idx[cur_row + 1] = idx;
csr_lane[cur_row + 1] = lane;
}
}
}
template<SIZE_TYPE THREAD_PER_BLOCK, SIZE_TYPE ITEM_PER_THREAD>
__global__
void block_rebalancing_kernel(SIZE_TYPE seg_length, SIZE_TYPE level, KEY_PTR keys[], VALUE_PTR values[],
SIZE_TYPE *update_nodes, KEY_TYPE *update_keys, VALUE_TYPE *update_values, SIZE_TYPE *unique_update_nodes,
SIZE_TYPE *update_offset, SIZE_TYPE lower_bound, SIZE_TYPE upper_bound, SIZE_TYPE tree_height, \
SIZE_TYPE *csr_idx, SIZE_TYPE *csr_lane, SIZE_TYPE thres, SIZE_TYPE *stat) {
SIZE_TYPE update_id = blockIdx.x;
SIZE_TYPE update_node = unique_update_nodes[update_id];
SIZE_TYPE rebalance_width = seg_length << level;
SIZE_TYPE linear_idx = -1, idx = -1, lane = -1;
KEY_PTR key;
VALUE_PTR value;
__shared__ KEY_TYPE none_keys[ITEM_PER_THREAD];
__shared__ VALUE_TYPE none_values[ITEM_PER_THREAD];
if (threadIdx.x == 0) {
memset(none_keys, -1, sizeof(KEY_TYPE) * ITEM_PER_THREAD);
memset(none_values, -1, sizeof(VALUE_TYPE) * ITEM_PER_THREAD);
}
__syncthreads();
if (update_node + rebalance_width >= (seg_length << (tree_height + 1))) {
rebalance_width -= seg_length;
upper_bound -= seg_length;
if (rebalance_width == 0)
return;
}
if (threadIdx.x * ITEM_PER_THREAD >= rebalance_width) {
key = none_keys;
value = none_values;
}
else {
linear_idx = update_node + threadIdx.x * ITEM_PER_THREAD;
idx = GET_IDX(linear_idx, tree_height);
lane = GET_LANE(linear_idx, idx, tree_height);
key = keys[idx] + lane;
value = values[idx] + lane;
}
// compact
__shared__ SIZE_TYPE compacted_size;
__shared__ KEY_TYPE tmp_keys[THREAD_PER_BLOCK * ITEM_PER_THREAD];
__shared__ VALUE_TYPE tmp_values[THREAD_PER_BLOCK * ITEM_PER_THREAD];
block_compact_kernel<THREAD_PER_BLOCK, ITEM_PER_THREAD>(key, value, compacted_size, tmp_keys, tmp_values, thres);
__syncthreads();
// judge whether fit the density threshold
SIZE_TYPE interval_a = update_offset[update_id];
SIZE_TYPE interval_b = update_offset[update_id + 1];
SIZE_TYPE interval_size = interval_b - interval_a;
SIZE_TYPE merge_size = compacted_size + interval_size;
__syncthreads();
if (lower_bound <= merge_size && merge_size <= upper_bound) {
atomicAdd(&stat[fls(rebalance_width) - 1], 1);
// move
block_pair_copy_kernel<KEY_TYPE, VALUE_TYPE>(tmp_keys + compacted_size, tmp_values + compacted_size,
update_keys + interval_a, update_values + interval_a, interval_size);
__syncthreads();
// set SIZE_NONE for executed update
for (SIZE_TYPE i = interval_a + threadIdx.x; i < interval_b; i += blockDim.x) {
update_nodes[i] = SIZE_NONE;
}
// re-dispatch
block_redispatch_kernel<THREAD_PER_BLOCK, ITEM_PER_THREAD>(keys, values, rebalance_width, seg_length,
merge_size, update_node, tmp_keys, tmp_values, update_node, tree_height, csr_idx, csr_lane);
}
}
__global__
void copy_compacted_kv(SIZE_TYPE *exscan, KEY_PTR keys[], VALUE_PTR values[], SIZE_TYPE size, KEY_TYPE *tmp_keys,
VALUE_TYPE *tmp_values, SIZE_TYPE *compacted_size, SIZE_TYPE update_node, SIZE_TYPE tree_height, SIZE_TYPE thres) {
SIZE_TYPE global_thread_id = blockDim.x * blockIdx.x + threadIdx.x;
SIZE_TYPE block_offset = gridDim.x * blockDim.x;
for (SIZE_TYPE i = global_thread_id; i < size; i += block_offset) {
if (i == size - 1)
continue;
if (exscan[i] != exscan[i + 1]) {
SIZE_TYPE loc = exscan[i];
SIZE_TYPE linear_idx = update_node + i;
SIZE_TYPE idx = GET_IDX(linear_idx, tree_height);
SIZE_TYPE lane = GET_LANE(linear_idx, idx, tree_height);
tmp_keys[loc] = keys[idx][lane];
tmp_values[loc] = values[idx][lane];
}
}
if (0 == global_thread_id) {
SIZE_TYPE loc = exscan[size - 1];
SIZE_TYPE linear_idx = update_node + size - 1;
SIZE_TYPE idx = GET_IDX(linear_idx, tree_height);
SIZE_TYPE lane = GET_LANE(linear_idx, idx, tree_height);
if (keys[idx][lane] == KEY_NONE || (keys[idx][lane] & TIME_MASK) < (KEY_TYPE)thres) {
*compacted_size = loc;
} else {
*compacted_size = loc + 1;
tmp_keys[loc] = keys[idx][lane];
tmp_values[loc] = values[idx][lane];
}
}
}
__global__
void label_key_whether_none_kernel(SIZE_TYPE *label, KEY_PTR keys[], VALUE_PTR values[], SIZE_TYPE size, SIZE_TYPE update_node,
SIZE_TYPE tree_height, SIZE_TYPE thres, SIZE_TYPE fuck=0) {
SIZE_TYPE global_thread_id = blockDim.x * blockIdx.x + threadIdx.x;
SIZE_TYPE block_offset = gridDim.x * blockDim.x;
for (SIZE_TYPE i = global_thread_id + update_node; i < size + update_node; i += block_offset) {
SIZE_TYPE idx = GET_IDX(i, tree_height);
SIZE_TYPE lane = GET_LANE(i, idx, tree_height);
label[i - update_node] = (keys[idx][lane] == KEY_NONE || (keys[idx][lane] & TIME_MASK) < (KEY_TYPE)thres) ? 0 : 1;
//if(fuck && (keys[idx][lane]>>SRC_SHIFT)==1224622)
//printf("%u,%u,%u,%llu\n",i,idx,lane,(keys[idx][lane]&DST_MASK)>>DST_SHIFT);
}
}
__device__
void compact_kernel(SIZE_TYPE size, KEY_PTR keys[], VALUE_PTR values[], SIZE_TYPE *compacted_size,
KEY_TYPE *tmp_keys, VALUE_TYPE *tmp_values, SIZE_TYPE *exscan, SIZE_TYPE *label, SIZE_TYPE update_node,
SIZE_TYPE tree_height, SIZE_TYPE thres) {
SIZE_TYPE THREADS_NUM = 32;
SIZE_TYPE BLOCKS_NUM = CALC_BLOCKS_NUM(THREADS_NUM, size);
label_key_whether_none_kernel<<<BLOCKS_NUM, THREADS_NUM>>>(label, keys, values, size, update_node, tree_height, thres);
cErr(cudaDeviceSynchronize());
// exscan
void *d_temp_storage = NULL;
size_t temp_storage_bytes = 0;
cErr(cub::DeviceScan::ExclusiveSum(d_temp_storage, temp_storage_bytes, label, exscan, size));
cErr(cudaDeviceSynchronize());
cErr(cudaMalloc(&d_temp_storage, temp_storage_bytes));
cErr(cudaDeviceSynchronize());
cErr(cub::DeviceScan::ExclusiveSum(d_temp_storage, temp_storage_bytes, label, exscan, size));
cErr(cudaDeviceSynchronize());
cErr(cudaFree(d_temp_storage));
// copy compacted kv to tmp, and set the original to none
copy_compacted_kv<<<BLOCKS_NUM, THREADS_NUM>>>(exscan, keys, values, size, tmp_keys, tmp_values, compacted_size, update_node, tree_height, thres);
cErr(cudaDeviceSynchronize());
}
__global__
void get_data_kernel(SIZE_TYPE size, KEY_PTR keys[], VALUE_PTR values[], SIZE_TYPE *compacted_size,
KEY_TYPE *tmp_keys, VALUE_TYPE *tmp_values, SIZE_TYPE *exscan, SIZE_TYPE *label, SIZE_TYPE update_node,
SIZE_TYPE tree_height, SIZE_TYPE thres) {
SIZE_TYPE THREADS_NUM = 32;
SIZE_TYPE BLOCKS_NUM = CALC_BLOCKS_NUM(THREADS_NUM, size);
label_key_whether_none_kernel<<<BLOCKS_NUM, THREADS_NUM>>>(label, keys, values, size, update_node, tree_height, thres);
cErr(cudaDeviceSynchronize());
// exscan
void *d_temp_storage = NULL;
size_t temp_storage_bytes = 0;
cErr(cub::DeviceScan::ExclusiveSum(d_temp_storage, temp_storage_bytes, label, exscan, size));
cErr(cudaDeviceSynchronize());
cErr(cudaMalloc(&d_temp_storage, temp_storage_bytes));
cErr(cudaDeviceSynchronize());
cErr(cub::DeviceScan::ExclusiveSum(d_temp_storage, temp_storage_bytes, label, exscan, size));
cErr(cudaDeviceSynchronize());
cErr(cudaFree(d_temp_storage));
// copy compacted kv to tmp, and set the original to none
copy_compacted_kv<<<BLOCKS_NUM, THREADS_NUM>>>(exscan, keys, values, size, tmp_keys, tmp_values, compacted_size, update_node, tree_height, thres);
cErr(cudaDeviceSynchronize());
}
__global__
void redispatch_kernel(KEY_TYPE *tmp_keys, VALUE_TYPE *tmp_values, KEY_PTR keys[], VALUE_PTR values[],
SIZE_TYPE update_width, SIZE_TYPE seg_length, SIZE_TYPE merge_size, SIZE_TYPE update_node, SIZE_TYPE tree_height, \
SIZE_TYPE *csr_idx, SIZE_TYPE *csr_lane) {
SIZE_TYPE global_thread_id = blockDim.x * blockIdx.x + threadIdx.x;
SIZE_TYPE block_offset = gridDim.x * blockDim.x;
KEY_TYPE frac = update_width / seg_length;
KEY_TYPE deno = merge_size;
for (SIZE_TYPE i = global_thread_id; i < merge_size; i += block_offset) {
SIZE_TYPE seg_idx = (SIZE_TYPE) ((unsigned long long)frac * (unsigned long long)i / deno);
SIZE_TYPE seg_lane = (SIZE_TYPE) ((unsigned long long)frac * (unsigned long long)i % deno / frac);
SIZE_TYPE proj_location = seg_idx * seg_length + seg_lane;
KEY_TYPE cur_key = tmp_keys[i];
VALUE_TYPE cur_value = tmp_values[i];
SIZE_TYPE linear_idx = proj_location + update_node;
SIZE_TYPE idx = GET_IDX(linear_idx, tree_height);
SIZE_TYPE lane = GET_LANE(linear_idx, idx, tree_height);
keys[idx][lane] = cur_key;
values[idx][lane] = cur_value;
//addition for csr
if ((cur_key & DST_AND_TIME_MASK) == DST_AND_TIME_END) {
SIZE_TYPE cur_row = (SIZE_TYPE) (cur_key >> SRC_SHIFT);
csr_idx[cur_row + 1] = idx;
csr_lane[cur_row + 1] = lane;
}
}
}
__global__
void rebalancing_kernel(SIZE_TYPE unique_update_size, SIZE_TYPE seg_length, SIZE_TYPE level, KEY_PTR keys[], VALUE_PTR values[],
SIZE_TYPE *update_nodes, KEY_TYPE *update_keys, VALUE_TYPE *update_values,
SIZE_TYPE *unique_update_nodes, SIZE_TYPE *update_offset, SIZE_TYPE lower_bound, SIZE_TYPE upper_bound,
SIZE_TYPE tree_height, SIZE_TYPE *csr_idx, SIZE_TYPE *csr_lane, SIZE_TYPE thres, SIZE_TYPE *stat) {
SIZE_TYPE global_thread_id = blockDim.x * blockIdx.x + threadIdx.x;
SIZE_TYPE block_offset = gridDim.x * blockDim.x;
SIZE_TYPE update_width = seg_length << level;
SIZE_TYPE *compacted_size;
cErr(cudaMalloc(&compacted_size, sizeof(SIZE_TYPE)));
cErr(cudaDeviceSynchronize());
KEY_TYPE *tmp_keys;
VALUE_TYPE *tmp_values;
SIZE_TYPE *tmp_exscan;
SIZE_TYPE *tmp_label;
KEY_TYPE *tmp_keys_sorted;
VALUE_TYPE *tmp_values_sorted;
cErr(cudaMalloc(&tmp_keys, update_width * sizeof(KEY_TYPE)));
cErr(cudaMalloc(&tmp_values, update_width * sizeof(VALUE_TYPE)));
cErr(cudaMalloc(&tmp_exscan, update_width * sizeof(SIZE_TYPE)));
cErr(cudaMalloc(&tmp_label, update_width * sizeof(SIZE_TYPE)));
cErr(cudaMalloc(&tmp_keys_sorted, update_width * sizeof(KEY_TYPE)));
cErr(cudaMalloc(&tmp_values_sorted, update_width * sizeof(VALUE_TYPE)));
cErr(cudaDeviceSynchronize());
for (SIZE_TYPE i = global_thread_id; i < unique_update_size; i += block_offset) {
SIZE_TYPE update_node = unique_update_nodes[i];
if (update_node + update_width >= (seg_length << (tree_height + 1))) {
update_width -= seg_length;
upper_bound -= seg_length;
if (update_width == 0)
continue;
}
// compact
compact_kernel(update_width, keys, values, compacted_size, tmp_keys, tmp_values, tmp_exscan, tmp_label, update_node, tree_height, thres);
cErr(cudaDeviceSynchronize());
// judge whether fit the density threshold
SIZE_TYPE interval_a = update_offset[i];
SIZE_TYPE interval_b = update_offset[i + 1];
SIZE_TYPE interval_size = interval_b - interval_a;
SIZE_TYPE merge_size = (*compacted_size) + interval_size;
if (lower_bound <= merge_size && merge_size <= upper_bound) {
atomicAdd(&stat[fls(update_width) - 1], 1);
SIZE_TYPE THREADS_NUM = 32;
SIZE_TYPE BLOCKS_NUM;
// move
BLOCKS_NUM = CALC_BLOCKS_NUM(THREADS_NUM, interval_size);
memcpy_kernel<KEY_TYPE> <<<BLOCKS_NUM, THREADS_NUM>>>(tmp_keys + (*compacted_size),
update_keys + interval_a, interval_size);
memcpy_kernel<VALUE_TYPE> <<<BLOCKS_NUM, THREADS_NUM>>>(tmp_values + (*compacted_size),
update_values + interval_a, interval_size);
cErr(cudaDeviceSynchronize());
// set SIZE_NONE for executed updates
memset_kernel<SIZE_TYPE> <<<BLOCKS_NUM, THREADS_NUM>>>(update_nodes + interval_a, SIZE_NONE, interval_size);
cErr(cudaDeviceSynchronize());
cub_sort_key_value(tmp_keys, tmp_values, merge_size, tmp_keys_sorted, tmp_values_sorted, update_node);
// re-dispatch
BLOCKS_NUM = CALC_BLOCKS_NUM(THREADS_NUM, update_width);
level_memset_kernel<KEY_TYPE> <<<BLOCKS_NUM, THREADS_NUM>>>(keys, KEY_NONE, update_width, update_node, tree_height);
cErr(cudaDeviceSynchronize());
BLOCKS_NUM = CALC_BLOCKS_NUM(THREADS_NUM, merge_size);
redispatch_kernel<<<BLOCKS_NUM, THREADS_NUM>>>(tmp_keys_sorted, tmp_values_sorted, keys, values, update_width, seg_length,
merge_size, update_node, tree_height, csr_idx, csr_lane);
cErr(cudaDeviceSynchronize());
}
}
cErr(cudaFree(compacted_size));
cErr(cudaFree(tmp_keys));
cErr(cudaFree(tmp_values));
cErr(cudaFree(tmp_exscan));
cErr(cudaFree(tmp_label));
cErr(cudaFree(tmp_keys_sorted));
cErr(cudaFree(tmp_values_sorted));
}
__host__
void rebalance_batch(SIZE_TYPE level, SIZE_TYPE seg_length, KEY_PTR keys[], VALUE_PTR values[],
SIZE_TYPE *update_nodes, KEY_TYPE *update_keys, VALUE_TYPE *update_values, SIZE_TYPE update_size,
SIZE_TYPE *unique_update_nodes, SIZE_TYPE *update_offset, SIZE_TYPE unique_update_size,
SIZE_TYPE lower_bound, SIZE_TYPE upper_bound, SIZE_TYPE tree_height, SIZE_TYPE *csr_idx, SIZE_TYPE *csr_lane, SIZE_TYPE thres, DEV_VEC_SIZE &stat) {
SIZE_TYPE update_width = seg_length << level;
if (update_width <= 1024) {
// func pointer for each template
void (*func_arr[10])(SIZE_TYPE, SIZE_TYPE, KEY_PTR[], VALUE_PTR[], SIZE_TYPE*, KEY_TYPE*, VALUE_TYPE*,
SIZE_TYPE*, SIZE_TYPE*, SIZE_TYPE, SIZE_TYPE, SIZE_TYPE, SIZE_TYPE*, SIZE_TYPE*, SIZE_TYPE, SIZE_TYPE*);
func_arr[0] = block_rebalancing_kernel<2, 1>;
func_arr[1] = block_rebalancing_kernel<4, 1>;
func_arr[2] = block_rebalancing_kernel<8, 1>;
func_arr[3] = block_rebalancing_kernel<16, 1>;
func_arr[4] = block_rebalancing_kernel<32, 1>;
func_arr[5] = block_rebalancing_kernel<32, 2>;
func_arr[6] = block_rebalancing_kernel<32, 4>;
func_arr[7] = block_rebalancing_kernel<32, 8>;
func_arr[8] = block_rebalancing_kernel<32, 16>;
func_arr[9] = block_rebalancing_kernel<32, 32>;
// operate each tree node by cuda-block
SIZE_TYPE THREADS_NUM = update_width > 32 ? 32 : update_width;
SIZE_TYPE BLOCKS_NUM = unique_update_size;
func_arr[fls(update_width) - 2]<<<BLOCKS_NUM, THREADS_NUM>>>(seg_length, level, keys, values, update_nodes,
update_keys, update_values, unique_update_nodes, update_offset, lower_bound, upper_bound, tree_height, csr_idx, csr_lane, thres, RAW_PTR(stat));
} else {
// operate each tree node by cub-kernel (dynamic parallelsim)
SIZE_TYPE BLOCKS_NUM = min(256, unique_update_size);
rebalancing_kernel<<<BLOCKS_NUM, 1>>>(unique_update_size, seg_length, level, keys, values, update_nodes,
update_keys, update_values, unique_update_nodes, update_offset, lower_bound, upper_bound, tree_height, csr_idx, csr_lane, thres, RAW_PTR(stat));
}
cErr(cudaDeviceSynchronize());
}
__global__
void item_counting_kernel(KEY_PTR keys[], VALUE_PTR values[], SIZE_TYPE *c_sum, SIZE_TYPE size, SIZE_TYPE tree_height, SIZE_TYPE thres) {
SIZE_TYPE global_idx = blockDim.x * blockIdx.x + threadIdx.x;
SIZE_TYPE sum = 0;
for (int i = global_idx; i < size; i += gridDim.x * blockDim.x) {
SIZE_TYPE idx = GET_IDX(i, tree_height);
SIZE_TYPE lane = GET_LANE(i, idx, tree_height);
if (KEY_NONE != keys[idx][lane] && (keys[idx][lane] & TIME_MASK) >= (KEY_TYPE)thres) {
++sum;
}
}
sum += __shfl_down(sum,16);
sum += __shfl_down(sum,8);
sum += __shfl_down(sum,4);
sum += __shfl_down(sum,2);
sum += __shfl_down(sum,1);
if (threadIdx.x % 32 == 0) {
c_sum[global_idx >> 5] = sum;
}
return ;
}
__global__
void level_counting_kernel(KEY_PTR keys, SIZE_TYPE *d_empty_segs, SIZE_TYPE size) {
SIZE_TYPE global_idx = blockDim.x * blockIdx.x + threadIdx.x;
for (int i = global_idx; i < size; i += gridDim.x * blockDim.x) {
SIZE_TYPE sum = (KEY_NONE != keys[i]);
sum += __shfl_down(sum,16);
sum += __shfl_down(sum,8);
sum += __shfl_down(sum,4);
sum += __shfl_down(sum,2);
sum += __shfl_down(sum,1);
if (threadIdx.x % 32 == 0) {
if (sum == 0)
*d_empty_segs = 1;
}
}
return ;
}
__host__
int resize_rpma(RPMA &rpma, DEV_VEC_KEY &update_keys, DEV_VEC_VALUE &update_values, SIZE_TYPE update_size, SIZE_TYPE thres) {
SIZE_TYPE item_num_include_none = (rpma.segment_length << (rpma.tree_height + 1)) - rpma.segment_length;
SIZE_TYPE THREADS_NUM = 32;
SIZE_TYPE BLOCKS_NUM = CALC_BLOCKS_NUM(THREADS_NUM, item_num_include_none);
SIZE_TYPE SUM_SIZE = THREADS_NUM * BLOCKS_NUM / 32;
SIZE_TYPE *d_sum, *d_compacted_size, compacted_size;
cErr(cudaMalloc(&d_sum, sizeof(SIZE_TYPE) * SUM_SIZE));
cErr(cudaMalloc(&d_compacted_size, sizeof(SIZE_TYPE)));
cErr(cudaMemset(d_sum, 0, sizeof(SIZE_TYPE) * SUM_SIZE));
item_counting_kernel<<<BLOCKS_NUM, THREADS_NUM>>>(RAW_PTR(rpma.levels_key_ptr_array), RAW_PTR(rpma.levels_value_ptr_array), d_sum, item_num_include_none, rpma.tree_height, thres);
cErr(cudaDeviceSynchronize());
void *d_temp_storage = NULL;
size_t temp_storage_bytes = 0;
cErr(cub::DeviceReduce::Sum(d_temp_storage, temp_storage_bytes, d_sum, d_compacted_size, SUM_SIZE));
cErr(cudaDeviceSynchronize());
cErr(cudaMalloc(&d_temp_storage, temp_storage_bytes));
cErr(cudaDeviceSynchronize());
cErr(cub::DeviceReduce::Sum(d_temp_storage, temp_storage_bytes, d_sum, d_compacted_size, SUM_SIZE));
cErr(cudaDeviceSynchronize());
cErr(cudaMemcpy(&compacted_size, d_compacted_size, sizeof(SIZE_TYPE), cudaMemcpyDeviceToHost));
cErr(cudaFree(d_temp_storage));
cErr(cudaFree(d_sum));
cErr(cudaFree(d_compacted_size));
cErr(cudaDeviceSynchronize());
SIZE_TYPE merge_size = compacted_size + update_size;
SIZE_TYPE original_tree_height = rpma.tree_height;
SIZE_TYPE updated_tree_height = 0;
SIZE_TYPE tree_size = rpma.segment_length * ((2 << updated_tree_height) - 1);
while (floor(rpma.density_upper_thres_root * tree_size) < merge_size) {
updated_tree_height += 1;
tree_size = rpma.segment_length * ((2 << updated_tree_height) - 1);
}
assert(original_tree_height != updated_tree_height);
return updated_tree_height;
}
__global__
void up_level_kernel(SIZE_TYPE *update_nodes, SIZE_TYPE update_size, SIZE_TYPE update_width) {
SIZE_TYPE global_thread_id = blockDim.x * blockIdx.x + threadIdx.x;
SIZE_TYPE block_offset = gridDim.x * blockDim.x;
for (SIZE_TYPE i = global_thread_id; i < update_size; i += block_offset) {
SIZE_TYPE node = update_nodes[i];
update_nodes[i] = node & ~update_width;
}
}
__host__
void up_level_batch(SIZE_TYPE *update_nodes, SIZE_TYPE update_size, SIZE_TYPE update_width) {
SIZE_TYPE THREADS_NUM = 32;
SIZE_TYPE BLOCKS_NUM = CALC_BLOCKS_NUM(THREADS_NUM, update_size);
up_level_kernel<<<BLOCKS_NUM, THREADS_NUM>>>(update_nodes, update_size, update_width);
cErr(cudaDeviceSynchronize());
}
void show_rpma(RPMA &rpma) {
for (int i = 0; i <= rpma.tree_height; ++i) {
SIZE_TYPE size = rpma.segment_length << i;
KEY_TYPE *h_keys = new KEY_TYPE[size];
VALUE_TYPE *h_values = new VALUE_TYPE[size];
cErr(cudaMemcpy(h_keys, rpma.levels_key_ptr_array[i], sizeof(KEY_TYPE) * size, cudaMemcpyDeviceToHost));
cErr(cudaMemcpy(h_values, rpma.levels_value_ptr_array[i], sizeof(VALUE_TYPE) * size, cudaMemcpyDeviceToHost));
std::cout << "level " << i << std::endl;
std::cout << "\tnum:\tkey:\tvalue:" << std::endl;