-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbindex_cuda.cpp
More file actions
1995 lines (1757 loc) · 65.2 KB
/
bindex_cuda.cpp
File metadata and controls
1995 lines (1757 loc) · 65.2 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
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
#include "bindex.h"
Timer timer;
int prefetch_stride = 6;
std::mutex scan_refine_mutex;
int scan_refine_in_position;
CODE scan_selected_compares[MAX_BINDEX_NUM][2];
bool scan_skip_refine;
// bool scan_use_special_compare;
bool scan_skip_other_face[MAX_BINDEX_NUM];
bool scan_skip_this_face[MAX_BINDEX_NUM];
CODE scan_max_compares[MAX_BINDEX_NUM][2];
bool scan_inverse_this_face[MAX_BINDEX_NUM];
char scan_cmd_file[256];
std::vector<std::string> stringSplit(const std::string& str, char delim) {
std::string s;
s.append(1, delim);
std::regex reg(s);
std::vector<std::string> elems(std::sregex_token_iterator(str.begin(), str.end(), reg, -1),
std::sregex_token_iterator());
return elems;
}
BITS gen_less_bits(const CODE *val, CODE compare, int n) {
// n must <= BITSWIDTH (32)
BITS result = 0;
for (int i = 0; i < n; i++) {
if (val[i] < compare) {
result += (1 << (BITSWIDTH - 1 - i));
}
}
return result;
}
void init_pos_block(pos_block *pb, CODE *val_f, POSTYPE *pos_f, int n) {
assert(n <= blockInitSize);
pb->length = n;
pb->pos = (POSTYPE *)malloc(blockMaxSize * sizeof(POSTYPE));
pb->val = (CODE *)malloc(blockMaxSize * sizeof(CODE));
for (int i = 0; i < n; i++) {
pb->pos[i] = pos_f[i];
pb->val[i] = val_f[i];
}
}
CODE block_start_value(pos_block *pb) { return pb->val[0]; }
int insert_to_block(pos_block *pb, CODE *val_f, POSTYPE *pos_f, int n) {
// Insert max(n, #vacancy) elements to a block, return 0 if the block is still
// not filled up.
if (DEBUG_TIME_COUNT) timer.commonGetStartTime(2);
int flagNum, length_new;
if ((n + pb->length) >= blockMaxSize) {
// Block filled up! This block will be splitted in insert_to_area(..)
flagNum = blockMaxSize - pb->length; // The number of successfully inserted
// elements, flagNum will be return
length_new = blockMaxSize;
} else {
flagNum = 0;
length_new = pb->length + n;
}
int k, i, j;
// Merge two sorted array
for (k = length_new - 1, i = pb->length - 1, j = length_new - pb->length - 1; i >= 0 && j >= 0;) {
if (val_f[j] >= pb->val[i]) {
pb->val[k] = val_f[j];
pb->pos[k--] = pos_f[j--];
} else {
pb->val[k] = pb->val[i];
pb->pos[k--] = pb->pos[i--];
}
}
while (j >= 0) {
pb->val[k] = val_f[j];
pb->pos[k--] = pos_f[j--];
}
pb->length = length_new;
if (DEBUG_TIME_COUNT) timer.commonGetEndTime(2);
return flagNum;
}
void init_area(Area *area, CODE *val, POSTYPE *pos, int n) {
// Area containing only unique code should be considered in
// future implementation
int i = 0;
area->blockNum = 0;
area->length = n;
while (i + blockInitSize < n) {
area->blocks[area->blockNum] = (pos_block *)malloc(sizeof(pos_block));
init_pos_block(area->blocks[area->blockNum], val + i, pos + i, blockInitSize);
(area->blockNum)++;
i += blockInitSize;
}
area->blocks[area->blockNum] = (pos_block *)malloc(sizeof(pos_block));
init_pos_block(area->blocks[area->blockNum], val + i, pos + i, n - i);
area->blockNum++;
assert(area->blockNum <= blockNumMax);
}
CODE area_start_value(Area *area) { return area->blocks[0]->val[0]; }
void set_fv_val_less(BITS *bitmap, const CODE *val, CODE compare, POSTYPE n) {
// Set values for filter vectors
int i;
for (i = 0; i + BITSWIDTH < (int)n; i += BITSWIDTH) {
bitmap[i / BITSWIDTH] = gen_less_bits(val + i, compare, BITSWIDTH);
}
bitmap[i / BITSWIDTH] = gen_less_bits(val + i, compare, n - i);
}
inline POSTYPE num_insert_to_area(POSTYPE *areaStartIdx, int k, int n) {
if (k < K - 1) {
return areaStartIdx[k + 1] - areaStartIdx[k];
} else {
return n - areaStartIdx[k];
}
}
void copy_block_to_GPU(pos_block *block_in_CPU, pos_block *block_in_GPU) {
cudaError_t cudaStatus;
block_in_GPU->length = block_in_CPU->length;
cudaStatus = cudaMalloc((void**)&(block_in_GPU->pos), blockMaxSize * sizeof(POSTYPE));
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMalloc pos block failed!");
exit(-1);
}
cudaStatus = cudaMemcpy(block_in_GPU->pos, block_in_CPU->pos, blockMaxSize * sizeof(POSTYPE), cudaMemcpyHostToDevice);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaCopy pos block failed!");
exit(-1);
}
}
void copy_area_to_GPU(Area *area_in_CPU, Area *area_in_GPU) {
cudaError_t cudaStatus;
area_in_GPU->blockNum = area_in_CPU->blockNum;
area_in_GPU->length = area_in_CPU->length;
for (int i = 0; i < area_in_CPU->blockNum; i++) {
area_in_GPU->blocks[i] = (pos_block *)malloc(sizeof(pos_block));
copy_block_to_GPU(area_in_CPU->blocks[i], area_in_GPU->blocks[i]);
}
}
void start_timer(struct timeval* t) {
gettimeofday(t, NULL);
}
void stop_timer(struct timeval* t, double* elapsed_time) {
struct timeval end;
gettimeofday(&end, NULL);
*elapsed_time += (end.tv_sec - t->tv_sec) * 1000.0 + (end.tv_usec - t->tv_usec) / 1000.0;
}
void init_bindex_in_GPU(BinDex *bindex, CODE *data, POSTYPE n, size_t &base_data) {
bindex->length = n;
POSTYPE avgAreaSize = n / K;
cudaError_t cudaStatus;
// CODE areaStartValues[K];
POSTYPE areaStartIdx[K];
CODE *data_sorted = (CODE *)malloc(n * sizeof(CODE)); // Sorted codes
double elapse_time = 0.0;
timeval start;
start_timer(&start);
POSTYPE *pos = argsort(data, n);
stop_timer(&start, &elapse_time);
cout << "Sort time: " << elapse_time << endl;
cerr << "Sort time: " << elapse_time << endl;
elapse_time = 0.0;
start_timer(&start);
for (int i = 0; i < n; i++) {
data_sorted[i] = data[pos[i]];
}
stop_timer(&start, &elapse_time);
cout << "Assign time: " << elapse_time << endl;
cerr << "Assign time: " << elapse_time << endl;
size_t total, t1, t2;
cudaMemGetInfo( &t1, &total );
// copy raw_data to GPU
cudaMalloc((void**)&(bindex->rawDataInGPU), n * sizeof(CODE));
cudaMemcpy(bindex->rawDataInGPU, data, n * sizeof(CODE), cudaMemcpyHostToDevice);
cudaMemGetInfo( &t2, &total );
base_data += t1 - t2;
bindex->data_min = data_sorted[0];
bindex->data_max = data_sorted[bindex->length - 1];
printf("Bindex data min: %u max: %u\n", bindex->data_min, bindex->data_max);
bindex->areaStartValues[0] = data_sorted[0];
areaStartIdx[0] = 0;
for (int i = 1; i < K; i++) {
bindex->areaStartValues[i] = data_sorted[i * avgAreaSize];
int j = i * avgAreaSize;
if (bindex->areaStartValues[i] == bindex->areaStartValues[i - 1]) {
areaStartIdx[i] = j;
} else {
// To find the first element which is less than startValue
while (data_sorted[j] == bindex->areaStartValues[i]) {
j--;
}
areaStartIdx[i] = j + 1;
}
}
std::thread threads[THREAD_NUM];
for (int k = 0; k * THREAD_NUM < K; k++) {
for (int j = 0; j < THREAD_NUM && (k * THREAD_NUM + j) < K; j++) {
int area_idx = k * THREAD_NUM + j;
bindex->areas[area_idx] = (Area *)malloc(sizeof(Area));
POSTYPE area_size = num_insert_to_area(areaStartIdx, area_idx, n);
// printf("[area_size] %d\n", area_size);
bindex->area_counts[area_idx] = area_size;
threads[j] = std::thread(init_area, bindex->areas[area_idx], data_sorted + areaStartIdx[area_idx],
pos + areaStartIdx[area_idx], area_size);
}
for (int j = 0; j < THREAD_NUM && (k * THREAD_NUM + j) < K; j++) {
threads[j].join();
}
}
printf("[INFO] area initialized.\n");
// Accumulative adding
for (int i = 1; i < K; i++) {
bindex->area_counts[i] += bindex->area_counts[i - 1];
}
assert(bindex->area_counts[K - 1] == bindex->length);
// copy CPU areas to GPU areas
for (int i = 0; i < K; i++) {
bindex->areasInGPU[i] = (Area *)malloc(sizeof(Area));
copy_area_to_GPU(bindex->areas[i], bindex->areasInGPU[i]);
}
printf("[INFO] copy areas to GPU done.\n");
// Build the filterVectors
// Now we build them in CPU memory and then copy them to GPU memory
for (int k = 0; k * THREAD_NUM < K - 1; k++) {
for (int j = 0; j < THREAD_NUM && (k * THREAD_NUM + j) < (K - 1); j++) {
bindex->filterVectors[k * THREAD_NUM + j] =
(BITS *)aligned_alloc(SIMD_ALIGEN, bits_num_needed(n) * sizeof(BITS));
threads[j] = std::thread(set_fv_val_less, bindex->filterVectors[k * THREAD_NUM + j], data,
bindex->areaStartValues[k * THREAD_NUM + j + 1], n);
}
for (int j = 0; j < THREAD_NUM && (k * THREAD_NUM + j) < (K - 1); j++) {
threads[j].join();
}
}
printf("[INFO] build filterVectors done.\n");
for (int i = 0; i < K - 1; i++) {
cudaStatus = cudaMalloc((void**)&(bindex->filterVectorsInGPU[i]), bits_num_needed(n) * sizeof(BITS));
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMalloc failed!");
exit(-1);
}
cudaStatus = cudaMemcpy(bindex->filterVectorsInGPU[i], bindex->filterVectors[i], bits_num_needed(n) * sizeof(BITS), cudaMemcpyHostToDevice);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMemcpy failed!");
exit(-1);
}
}
free(pos);
free(data_sorted);
}
void copy_bitmap(BITS *result, BITS *ref, int n, int t_id) {
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(t_id * 2, &mask);
if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) {
fprintf(stderr, "set thread affinity failed\n");
}
memcpy(result, ref, n * sizeof(BITS));
// for (int i = 0; i < n; i++) {
// result[i] = ref[i];
// }
}
void copy_bitmap_not(BITS *result, BITS *ref, int start_n, int end_n, int t_id) {
int jobs = ROUNDUP_DIVIDE(end_n - start_n, THREAD_NUM);
int start = start_n + t_id * jobs;
int end = start_n + (t_id + 1) * jobs;
if (end > end_n) end = end_n;
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(t_id * 2, &mask);
if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) {
fprintf(stderr, "set thread affinity failed\n");
}
for (int i = start; i < end; i++) {
result[i] = ~(ref[i]);
}
}
void copy_bitmap_bt(BITS *result, BITS *ref_l, BITS *ref_r, int start_n, int end_n, int t_id) {
int jobs = ROUNDUP_DIVIDE(end_n - start_n, THREAD_NUM);
int start = start_n + t_id * jobs;
int end = start_n + (t_id + 1) * jobs;
if (end > end_n) end = end_n;
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(t_id * 2, &mask);
if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) {
fprintf(stderr, "set thread affinity failed\n");
}
for (int i = start; i < end; i++) {
result[i] = ref_r[i] & (~(ref_l[i]));
}
}
void copy_bitmap_bt_simd(BITS *to, BITS *from_l, BITS *from_r, int bitmap_len, int t_id) {
int jobs = ((bitmap_len / SIMD_JOB_UNIT - 1) / THREAD_NUM + 1) * SIMD_JOB_UNIT;
assert(jobs % SIMD_JOB_UNIT == 0);
assert(bitmap_len % SIMD_JOB_UNIT == 0);
assert(jobs * THREAD_NUM >= bitmap_len);
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(t_id * 2, &mask);
if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) {
fprintf(stderr, "set thread affinity failed\n");
}
int cur = t_id * jobs;
int end = (t_id + 1) * jobs;
if (end > bitmap_len) end = bitmap_len;
while (cur < end) {
__m256i buf_l = _mm256_load_si256((__m256i *)(from_l + cur));
__m256i buf_r = _mm256_load_si256((__m256i *)(from_r + cur));
__m256i buf = _mm256_andnot_si256(buf_l, buf_r);
_mm256_store_si256((__m256i *)(to + cur), buf);
cur += SIMD_JOB_UNIT;
}
}
void copy_bitmap_simd(BITS *to, BITS *from, int bitmap_len, int t_id) {
int jobs = ((bitmap_len / SIMD_JOB_UNIT - 1) / THREAD_NUM + 1) * SIMD_JOB_UNIT;
assert(jobs % SIMD_JOB_UNIT == 0);
assert(bitmap_len % SIMD_JOB_UNIT == 0);
assert(jobs * THREAD_NUM >= bitmap_len);
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(t_id * 2, &mask);
if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) {
fprintf(stderr, "set thread affinity failed\n");
}
int cur = t_id * jobs;
int end = (t_id + 1) * jobs;
if (end > bitmap_len) end = bitmap_len;
while (cur < end) {
__m256i buf = _mm256_load_si256((__m256i *)(from + cur));
_mm256_store_si256((__m256i *)(to + cur), buf);
cur += SIMD_JOB_UNIT;
}
}
void copy_bitmap_not_simd(BITS *to, BITS *from, int bitmap_len, int t_id) {
int jobs = ((bitmap_len / SIMD_JOB_UNIT - 1) / THREAD_NUM + 1) * SIMD_JOB_UNIT;
assert(jobs % SIMD_JOB_UNIT == 0);
assert(bitmap_len % SIMD_JOB_UNIT == 0);
assert(jobs * THREAD_NUM >= bitmap_len);
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(t_id * 2, &mask);
if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) {
fprintf(stderr, "set thread affinity failed\n");
}
int cur = t_id * jobs;
int end = (t_id + 1) * jobs;
if (end > bitmap_len) end = bitmap_len;
while (cur < end) {
__m256i buf = _mm256_load_si256((__m256i *)(from + cur));
buf = ~buf;
_mm256_store_si256((__m256i *)(to + cur), buf);
cur += SIMD_JOB_UNIT;
}
}
void memset_numa0(BITS *p, int val, int n, int t_id) {
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(t_id * 2, &mask);
if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) {
fprintf(stderr, "set thread affinity failed\n");
}
int avg_workload = (n / (THREAD_NUM * SIMD_ALIGEN)) * SIMD_ALIGEN;
int start = t_id * avg_workload;
// int end = start + avg_workload;
int end = t_id == (THREAD_NUM - 1) ? n : start + avg_workload;
memset(p + start, val, (end - start) * sizeof(BITS));
// if (t_id == THREAD_NUM) {
// BITS val_bits = 0U;
// for (int i = 0; i < sizeof(BITS); i++) {
// val_bits |= ((unsigned int)val) << (8 * i);
// }
// for (; end < n; end++) {
// p[end] = val_bits;
// }
// }
}
void memset_mt(BITS *p, int val, int n) {
std::thread threads[THREAD_NUM];
for (int t_id = 0; t_id < THREAD_NUM; t_id++) {
threads[t_id] = std::thread(memset_numa0, p, val, n, t_id);
}
for (int t_id = 0; t_id < THREAD_NUM; t_id++) {
threads[t_id].join();
}
}
void copy_filter_vector(BinDex *bindex, BITS *result, int k) {
std::thread threads[THREAD_NUM];
int bitmap_len = bits_num_needed(bindex->length);
// BITS* result = (BITS*)aligned_alloc(SIMD_ALIGEN, bitmap_len *
// sizeof(BITS));
if (k < 0) {
memset_mt(result, 0, bitmap_len);
return;
}
if (k >= (K - 1)) {
memset_mt(result, 0xFF, bitmap_len); // Slower(?) than a loop
return;
}
// simd copy
// int mt_bitmap_n = (bitmap_len / SIMD_JOB_UNIT) * SIMD_JOB_UNIT; // must
// be SIMD_JOB_UNIT aligened for (int i = 0; i < THREAD_NUM; i++)
// threads[i] = std::thread(copy_bitmap_simd, result,
// bindex->filterVectors[k], mt_bitmap_n, i);
// for (int i = 0; i < THREAD_NUM; i++)
// threads[i].join();
// memcpy(result + mt_bitmap_n, bindex->filterVectors[k] + mt_bitmap_n,
// bitmap_len - mt_bitmap_n);
// naive copy
int avg_workload = bitmap_len / THREAD_NUM;
int i;
for (i = 0; i < THREAD_NUM - 1; i++) {
threads[i] = std::thread(copy_bitmap, result + (i * avg_workload), bindex->filterVectors[k] + (i * avg_workload),
avg_workload, i);
}
threads[i] = std::thread(copy_bitmap, result + (i * avg_workload), bindex->filterVectors[k] + (i * avg_workload),
bitmap_len - (i * avg_workload), i);
for (i = 0; i < THREAD_NUM; i++) {
threads[i].join();
}
}
void copy_filter_vector_in_GPU(BinDex *bindex, BITS *dev_bitmap, int k, bool negation) {
std::thread threads[THREAD_NUM];
int bitmap_len = bits_num_needed(bindex->length);
if (k < 0) {
cudaMemset(dev_bitmap, 0, bitmap_len * sizeof(BITS));
return;
}
if (k >= (K - 1)) {
cudaMemset(dev_bitmap, 0xFF, bitmap_len * sizeof(BITS));
return;
}
if (!negation)
GPUbitCopyWithCuda(dev_bitmap, bindex->filterVectorsInGPU[k], bitmap_len);
else
GPUbitCopyNegationWithCuda(dev_bitmap, bindex->filterVectorsInGPU[k], bitmap_len);
}
void copy_filter_vector_not(BinDex *bindex, BITS *result, int k) {
std::thread threads[THREAD_NUM];
int bitmap_len = bits_num_needed(bindex->length);
// BITS* result = (BITS*)aligned_alloc(SIMD_ALIGEN, bitmap_len *
// sizeof(BITS));
if (k < 0) {
memset_mt(result, 0xFF, bitmap_len);
return;
}
if (k >= (K - 1)) {
memset_mt(result, 0, bitmap_len); // Slower(?) than a loop
return;
}
// simd copy not
int mt_bitmap_n = (bitmap_len / SIMD_JOB_UNIT) * SIMD_JOB_UNIT; // must be SIMD_JOB_UNIT aligened
for (int i = 0; i < THREAD_NUM; i++)
threads[i] = std::thread(copy_bitmap_not_simd, result, bindex->filterVectors[k], mt_bitmap_n, i);
for (int i = 0; i < THREAD_NUM; i++) threads[i].join();
for (int i = 0; i < bitmap_len - mt_bitmap_n; i++) {
(result + mt_bitmap_n)[i] = ~((bindex->filterVectors[k] + mt_bitmap_n)[i]);
}
// naive copy
// for (int i = 0; i < THREAD_NUM; i++) {
// threads[i] = std::thread(copy_bitmap_not, result,
// bindex->filterVectors[k],
// 0, bitmap_len, i);
// }
// for (int i = 0; i < THREAD_NUM; i++) {
// threads[i].join();
// }
return;
}
void copy_filter_vector_bt(BinDex *bindex, BITS *result, int kl, int kr) {
std::thread threads[THREAD_NUM];
int bitmap_len = bits_num_needed(bindex->length);
if (kr < 0) {
// assert(0);
// printf("1\n");
memset_mt(result, 0, bitmap_len);
return;
} else if (kr >= (K - 1)) {
// assert(0);
// printf("2\n");
copy_filter_vector_not(bindex, result, kl);
return;
}
if (kl < 0) {
// assert(0);
// printf("3\n");
copy_filter_vector(bindex, result, kr);
return;
} else if (kl >= (K - 1)) {
// assert(0);
// printf("4\n");
memset_mt(result, 0, bitmap_len); // Slower(?) than a loop
return;
}
// simd copy_bt
int mt_bitmap_n = (bitmap_len / SIMD_JOB_UNIT) * SIMD_JOB_UNIT; // must be SIMD_JOB_UNIT aligened
for (int i = 0; i < THREAD_NUM; i++)
threads[i] =
std::thread(copy_bitmap_bt_simd, result, bindex->filterVectors[kl], bindex->filterVectors[kr], mt_bitmap_n, i);
for (int i = 0; i < THREAD_NUM; i++) threads[i].join();
for (int i = 0; i < bitmap_len - mt_bitmap_n; i++) {
(result + mt_bitmap_n)[i] =
(~((bindex->filterVectors[kl] + mt_bitmap_n)[i])) & ((bindex->filterVectors[kr] + mt_bitmap_n)[i]);
}
// naive copy
// for (int i = 0; i < THREAD_NUM; i++) {
// threads[i] = std::thread(copy_bitmap_bt, result,
// bindex->filterVectors[kl],
// bindex->filterVectors[kr], 0, bitmap_len, i);
// }
// for (int i = 0; i < THREAD_NUM; i++) {
// threads[i].join();
// }
}
void copy_filter_vector_bt_in_GPU(BinDex *bindex, BITS *result, int kl, int kr) {
int bitmap_len = bits_num_needed(bindex->length);
if (kr < 0) {
// assert(0);
// printf("1\n");
// memset_mt(result, 0, bitmap_len);
cudaMemset(result, 0, bitmap_len * sizeof(BITS));
return;
} else if (kr >= (K - 1)) {
// assert(0);
// printf("2\n");
// copy_filter_vector_not(bindex, result, kl);
copy_filter_vector_in_GPU(bindex, result, kl, true);
return;
}
if (kl < 0) {
// assert(0);
// printf("3\n");
// copy_filter_vector(bindex, result, kr);
copy_filter_vector_in_GPU(bindex, result, kr);
return;
} else if (kl >= (K - 1)) {
// assert(0);
// printf("4\n");
// memset_mt(result, 0, bitmap_len); // Slower(?) than a loop
cudaMemset(result, 0, bitmap_len * sizeof(BITS));
return;
}
GPUbitCopySIMDWithCuda(result,
bindex->filterVectorsInGPU[kl],
bindex->filterVectorsInGPU[kr],
bitmap_len);
// simd copy_bt
// int mt_bitmap_n = (bitmap_len / SIMD_JOB_UNIT) * SIMD_JOB_UNIT; // must be SIMD_JOB_UNIT aligened
// for (int i = 0; i < THREAD_NUM; i++)
// threads[i] =
// std::thread(copy_bitmap_bt_simd, result, bindex->filterVectors[kl], bindex->filterVectors[kr], mt_bitmap_n, i);
// for (int i = 0; i < THREAD_NUM; i++) threads[i].join();
// for (int i = 0; i < bitmap_len - mt_bitmap_n; i++) {
// (result + mt_bitmap_n)[i] =
// (~((bindex->filterVectors[kl] + mt_bitmap_n)[i])) & ((bindex->filterVectors[kr] + mt_bitmap_n)[i]);
// }
}
void copy_bitmap_xor_simd(BITS *to, BITS *bitmap1, BITS *bitmap2,
int bitmap_len, int t_id) {
int jobs =
((bitmap_len / SIMD_JOB_UNIT - 1) / THREAD_NUM + 1) * SIMD_JOB_UNIT;
assert(jobs % SIMD_JOB_UNIT == 0);
assert(bitmap_len % SIMD_JOB_UNIT == 0);
assert(jobs * THREAD_NUM >= bitmap_len);
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(t_id * 2, &mask);
if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) {
fprintf(stderr, "set thread affinity failed\n");
}
int cur = t_id * jobs;
int end = (t_id + 1) * jobs;
if (end > bitmap_len) end = bitmap_len;
while (cur < end) {
__m256i buf1 = _mm256_load_si256((__m256i *)(bitmap1 + cur));
__m256i buf2 = _mm256_load_si256((__m256i *)(bitmap2 + cur));
__m256i buf = _mm256_andnot_si256(buf1, buf2);
_mm256_store_si256((__m256i *)(to + cur), buf);
cur += SIMD_JOB_UNIT;
}
}
void copy_filter_vector_xor(BinDex *bindex, BITS *result, int kl, int kr) {
std::thread threads[THREAD_NUM];
int bitmap_len = bits_num_needed(bindex->length);
if (kr < 0) {
assert(0);
// printf("1\n");
memset_mt(result, 0, bitmap_len);
return;
} else if (kr >= (K - 1)) {
assert(0);
// printf("2\n");
copy_filter_vector_not(bindex, result, kl);
return;
}
if (kl < 0) {
// assert(0);
// printf("3\n");
copy_filter_vector(bindex, result, kr);
return;
} else if (kl >= (K - 1)) {
assert(0);
// printf("4\n");
memset_mt(result, 0, bitmap_len); // Slower(?) than a loop
return;
}
// simd copy_xor
int mt_bitmap_n = (bitmap_len / SIMD_JOB_UNIT) *
SIMD_JOB_UNIT; // must be SIMD_JOB_UNIT aligened
for (int i = 0; i < THREAD_NUM; i++)
threads[i] =
std::thread(copy_bitmap_xor_simd, result, bindex->filterVectors[kl],
bindex->filterVectors[kr], mt_bitmap_n, i);
for (int i = 0; i < THREAD_NUM; i++) threads[i].join();
for (int i = 0; i < bitmap_len - mt_bitmap_n; i++) {
(result + mt_bitmap_n)[i] = ((bindex->filterVectors[kl] + mt_bitmap_n)[i]) ^
((bindex->filterVectors[kr] + mt_bitmap_n)[i]);
}
// naive copy
// for (int i = 0; i < THREAD_NUM; i++) {
// threads[i] = std::thread(copy_bitmap_bt, result,
// hydex->filterVectors[kl],
// hydex->filterVectors[kr], 0, bitmap_len, i);
// }
// for (int i = 0; i < THREAD_NUM; i++) {
// threads[i].join();
// }
}
int in_which_area(BinDex *bindex, CODE compare) {
if (compare < area_start_value(bindex->areas[0])) return -1;
for (int i = 0; i < K; i++) {
CODE area_sv = area_start_value(bindex->areas[i]);
if (area_sv == compare) return i;
if (area_sv > compare) return i - 1;
}
return K - 1;
}
int in_which_block(Area *area, CODE compare) {
assert(compare >= area_start_value(area));
int res = area->blockNum - 1;
for (int i = 0; i < area->blockNum; i++) {
CODE area_sv = block_start_value(area->blocks[i]);
if (area_sv == compare) {
res = i;
break;
}
if (area_sv > compare) {
res = i - 1;
break;
}
}
if (res) {
pos_block *pre_blk = area->blocks[res - 1];
if (pre_blk->val[pre_blk->length - 1] == compare) {
res--;
}
}
return res;
}
int on_which_pos(pos_block *pb, CODE compare) {
// Find the first value which is no less than 'compare', return pb->length if
// all data in the block are less than compare
assert(compare >= pb->val[0]);
int low = 0, high = pb->length, mid = (low + high) / 2;
while (low < high) {
if (pb->val[mid] >= compare) {
high = mid;
} else {
low = mid + 1;
}
mid = (low + high) / 2;
}
return mid;
}
inline void refine(BITS *bitmap, POSTYPE pos) { bitmap[pos >> BITSSHIFT] ^= (1U << (BITSWIDTH - 1 - pos % BITSWIDTH)); }
void refine_positions(BITS *bitmap, POSTYPE *pos, POSTYPE n) {
for (int i = 0; i < n; i++) {
refine(bitmap, *(pos + i));
}
}
void refine_positions_mt(BITS *bitmap, Area *area, int start_blk_idx, int end_blk_idx, int t_id) {
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(t_id * 2, &mask);
if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) {
fprintf(stderr, "set thread affinity failed\n");
}
int jobs = ROUNDUP_DIVIDE(end_blk_idx - start_blk_idx, THREAD_NUM);
int cur = start_blk_idx + t_id * jobs;
int end = start_blk_idx + (t_id + 1) * jobs;
if (end > end_blk_idx) end = end_blk_idx;
// int prefetch_stride = 6;
while (cur < end) {
POSTYPE *pos_list = area->blocks[cur]->pos;
POSTYPE n = area->blocks[cur]->length;
int i;
for (i = 0; i + prefetch_stride < n; i++) {
if(prefetch_stride) {
__builtin_prefetch(&bitmap[*(pos_list + i + prefetch_stride) >> BITSSHIFT], 1, 1);
}
POSTYPE pos = *(pos_list + i);
__sync_fetch_and_xor(&bitmap[pos >> BITSSHIFT], (1U << (BITSWIDTH - 1 - pos % BITSWIDTH)));
}
while (i < n) {
POSTYPE pos = *(pos_list + i);
__sync_fetch_and_xor(&bitmap[pos >> BITSSHIFT], (1U << (BITSWIDTH - 1 - pos % BITSWIDTH)));
i++;
}
cur++;
}
}
void refine_result_bitmap(BITS *bitmap_a, BITS *bitmap_b, int start_idx, int end_idx, int t_id) {
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(t_id * 2, &mask);
if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) {
fprintf(stderr, "set thread affinity failed\n");
}
int i;
for (i = start_idx; i < end_idx; i++) {
__sync_fetch_and_and(&bitmap_a[i], bitmap_b[i]);
}
}
void set_eq_bitmap_mt(BITS *bitmap, Area *area, CODE compare, int start_blk_idx, int end_blk_idx, int t_id) {
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(t_id * 2, &mask);
if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) {
fprintf(stderr, "set thread affinity failed\n");
}
int jobs = ROUNDUP_DIVIDE(end_blk_idx - start_blk_idx, THREAD_NUM);
int start = start_blk_idx + t_id * jobs;
int end = start_blk_idx + (t_id + 1) * jobs;
if (end > end_blk_idx) end = end_blk_idx;
for (int i = start; i < end && block_start_value(area->blocks[i]) <= compare; i++) {
pos_block *blk = area->blocks[i];
POSTYPE *pos_list = blk->pos;
POSTYPE n = blk->length;
for (int j = 0; j < n && blk->val[j] <= compare; j++) {
if (blk->val[j] == compare) {
POSTYPE pos = *(pos_list + j);
__sync_fetch_and_xor(&bitmap[pos >> BITSSHIFT], (1U << (BITSWIDTH - 1 - pos % BITSWIDTH)));
}
}
}
}
void refine_positions_in_blks_mt(BITS *bitmap, Area *area, int start_blk_idx,
int end_blk_idx, int t_id) {
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(t_id * 2, &mask);
if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) {
fprintf(stderr, "set thread affinity failed\n");
}
int jobs = ROUNDUP_DIVIDE(end_blk_idx - start_blk_idx, THREAD_NUM);
int cur = start_blk_idx + t_id * jobs;
int end = start_blk_idx + (t_id + 1) * jobs;
if (end > end_blk_idx) end = end_blk_idx;
int prefetch_stride = 6;
while (cur < end) {
POSTYPE *pos_list = area->blocks[cur]->pos;
POSTYPE n = area->blocks[cur]->length;
int i;
for (i = 0; i + prefetch_stride < n; i++) {
__builtin_prefetch(
&bitmap[*(pos_list + i + prefetch_stride) >> BITSSHIFT], 1, 1);
POSTYPE pos = *(pos_list + i);
__sync_fetch_and_xor(&bitmap[pos >> BITSSHIFT],
(1U << (BITSWIDTH - 1 - pos % BITSWIDTH)));
}
while (i < n) {
POSTYPE pos = *(pos_list + i);
__sync_fetch_and_xor(&bitmap[pos >> BITSSHIFT],
(1U << (BITSWIDTH - 1 - pos % BITSWIDTH)));
i++;
}
cur++;
}
}
int find_appropriate_fv(BinDex *bindex, CODE compare) {
if (compare < bindex->areaStartValues[0]) return -1;
for (int i = 0; i < K; i++) {
CODE area_sv = bindex->areaStartValues[i];
if (area_sv == compare) return i;
if (area_sv > compare) return i - 1;
}
// check if actually out of boundary here.
// if so, return K
// need extra check before use the return value avoid of error.
if (compare <= bindex->data_max)
return K - 1;
else
return K;
}
void bindex_scan_lt_in_GPU(BinDex *bindex, BITS *dev_bitmap, CODE compare, int bindex_id) {
int bitmap_len = bits_num_needed(bindex->length);
int area_idx = find_appropriate_fv(bindex, compare);
// set common compare here for use in refine
scan_max_compares[bindex_id][0] = bindex->data_min;
scan_max_compares[bindex_id][1] = compare;
// handle some boundary problems: <0, ==0, K-1, >K-1
// <0: set all bits to 0. should not call rt. interrupt other procedures now. should cancel merge as well?
// ==0: set all bits to 0. just scan one face: SC[this bindex] x MC[other bindexs].
// K-1: area_idx = K - 1 may not cause problem now?
// K: set all bits to 1. skip this face: SC[this bindex] x MC[other bindexs]
if (area_idx < 0) {
// 'compare' less than all raw_data, return all zero result
// set skip to true so refine thread will be informed to skip
scan_refine_mutex.lock();
scan_skip_refine = true;
scan_refine_mutex.unlock();
return;
}
if (area_idx == 0) {
// 'compare' less than all raw_data, return all zero result
scan_refine_mutex.lock();
if(!scan_skip_refine) {
scan_skip_other_face[bindex_id] = true;
scan_selected_compares[bindex_id][0] = bindex->data_min;
scan_selected_compares[bindex_id][1] = compare;
// printf("comapre[%d]: %u %u\n", bindex_id, scan_selected_compares[bindex_id][0], scan_selected_compares[bindex_id][1]);
scan_refine_in_position += 1;
}
scan_refine_mutex.unlock();
return;
}
if (area_idx > K - 1) {
// set skip this bindex so rt will skip the scan for this face
scan_refine_mutex.lock();
if(!scan_skip_refine) {
scan_skip_this_face[bindex_id] = true;
scan_selected_compares[bindex_id][0] = bindex->data_min;
scan_selected_compares[bindex_id][1] = compare;
scan_refine_in_position += 1;
}
scan_refine_mutex.unlock();
return;
}
// choose use inverse or normal
bool inverse = false;
if (area_idx < K - 1) {
if (compare - bindex->areaStartValues[area_idx] > bindex->areaStartValues[area_idx + 1] - compare) {
inverse = true;
scan_inverse_this_face[bindex_id] = true;
}
}
// set refine compares here
scan_refine_mutex.lock();
if (inverse) {
scan_selected_compares[bindex_id][0] = compare;
scan_selected_compares[bindex_id][1] = bindex->areaStartValues[area_idx + 1];
} else {
scan_selected_compares[bindex_id][0] = bindex->areaStartValues[area_idx];
scan_selected_compares[bindex_id][1] = compare;
}
if(DEBUG_INFO) printf("comapre[%d]: %u %u\n", bindex_id, scan_selected_compares[bindex_id][0], scan_selected_compares[bindex_id][1]);
scan_refine_in_position += 1;
scan_refine_mutex.unlock();
// we use the one small than compare here, so rt must return result to append (maybe with and)
if(!scan_skip_refine) {
if (inverse) {
PRINT_EXCECUTION_TIME("copy",
copy_filter_vector_in_GPU(bindex, dev_bitmap, area_idx))
}
else {
PRINT_EXCECUTION_TIME("copy",
copy_filter_vector_in_GPU(bindex, dev_bitmap, area_idx - 1))
}
}
}
void bindex_scan_gt_in_GPU(BinDex *bindex, BITS *dev_bitmap, CODE compare, int bindex_id) {
compare = compare + 1;
int bitmap_len = bits_num_needed(bindex->length);
int area_idx = find_appropriate_fv(bindex, compare);
// set common compare here for use in refine
scan_max_compares[bindex_id][0] = compare;
scan_max_compares[bindex_id][1] = bindex->data_max;
// handle some boundary problems: <0, ==0, K-1, >K-1 (just like lt)
// <0: set all bits to 1. skip this face: CS[this bindex] x CM[other bindexs]