-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrtscan.cpp
More file actions
1226 lines (1105 loc) · 43.1 KB
/
rtscan.cpp
File metadata and controls
1226 lines (1105 loc) · 43.1 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 "rt.h"
#include "helper.h"
Timer timer;
mutex scan_refine_mutex;
int scan_refine_in_position;
CODE scan_selected_compares[MAX_BINDEX_NUM][2];
bool scan_skip_refine;
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];
int density_width = 1200, density_height = 1200;
int default_ray_segment_num = 64;
int default_ray_length = 48000000;
int default_ray_mode = 1; // 0 for continuous ray, 1 for ray with space, 2 for ray as point
CODE min_val = UINT32_MAX;
CODE max_val = 0;
int NUM_QUERIES;
int RANGE_QUERY = 1;
int REDUCED_SCANNING = 0;
uint32_t data_range_list[3] = {UINT32_MAX, UINT32_MAX, UINT32_MAX};
int cube_width = 0;
bool READ_QUERIES_FROM_FILE = true;
int face_direction = 1; // 0: launch rays from wide face, 1: narrow face
bool with_refine = true;
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 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);
}
void init_bindex_in_GPU(BinDex *bindex, CODE *data, POSTYPE n, int bindex_id, POSTYPE *pos, CODE *data_sorted) {
bindex->length = n;
POSTYPE avgAreaSize = n / K;
#if ENCODE == 0
data_sorted = (CODE *)malloc(n * sizeof(CODE)); // Sorted codes
#endif
timeval start;
double elapsed_time = 0;
#if ENCODE == 0
start_timer(&start);
pos = argsort(data, n);
stop_timer(&start, &elapsed_time);
cerr << "Sort[" << bindex_id << "]: " << elapsed_time << endl;
cout << "Sort[" << bindex_id << "]: " << elapsed_time << endl;
elapsed_time = 0.0;
start_timer(&start);
for (int i = 0; i < n; i++) {
data_sorted[i] = data[pos[i]];
}
stop_timer(&start, &elapsed_time);
cerr << "Assign[" << bindex_id << "]: " << elapsed_time << endl;
cout << "Assign[" << bindex_id << "]: " << elapsed_time << endl;
#endif
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];
elapsed_time = 0.0;
start_timer(&start);
for (int i = 1; i < K; i++) { // TODO: 为了设置bindex->areaStartValues
bindex->areaStartValues[i] = data_sorted[i * avgAreaSize];
int j = i * avgAreaSize;
// if (bindex->areaStartValues[i] == bindex->areaStartValues[i - 1]) {
if (!ifEncodeEqual(bindex->areaStartValues[i], bindex->areaStartValues[i - 1], bindex_id)) { // 两个area的开头不一样时,才重新设置
// To find the first element which is less than startValue
// while (data_sorted[j] == bindex->areaStartValues[i]) {
while (ifEncodeEqual(data_sorted[j], bindex->areaStartValues[i], bindex_id)) {
j--;
}
bindex->areaStartValues[i] = data_sorted[j + 1];
}
// if(DEBUG_INFO) printf("area[%u] = %u\n", i, bindex->areaStartValues[i]);
}
stop_timer(&start, &elapsed_time);
cerr << "Set areaStartValues[" << bindex_id << "]: " << elapsed_time << endl;
cout << "Set areaStartValues[" << bindex_id << "]: " << elapsed_time << endl;
thread threads[THREAD_NUM];
elapsed_time = 0.0;
start_timer(&start);
// 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++) {
// Malloc 2 times of space, prepared for future appending
bindex->filterVectors[k * THREAD_NUM + j] =
(BITS *)aligned_alloc(SIMD_ALIGEN, bits_num_needed(n) * sizeof(BITS));
threads[j] = 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();
}
}
stop_timer(&start, &elapsed_time);
cerr << "Build FV[" << bindex_id << "]: " << elapsed_time << endl;
cout << "Build FV[" << bindex_id << "]: " << elapsed_time << endl;
for (int i = 0; i < K - 1; i++) {
cudaMalloc((void**)&(bindex->filterVectorsInGPU[i]), bits_num_needed(n) * sizeof(BITS));
timer.commonGetStartTime(24);
cudaMemcpy(bindex->filterVectorsInGPU[i], bindex->filterVectors[i], bits_num_needed(n) * sizeof(BITS), cudaMemcpyHostToDevice);
timer.commonGetEndTime(24);
free(bindex->filterVectors[i]);
}
free(pos);
free(data_sorted);
}
void copy_filter_vector_in_GPU(BinDex *bindex, BITS *dev_bitmap, int k, bool negation = false) {
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);
}
inline void refine(BITS *bitmap, POSTYPE pos) { bitmap[pos >> BITSSHIFT] ^= (1U << (BITSWIDTH - 1 - pos % BITSWIDTH)); }
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]);
}
}
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
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;
if (area_idx < 0) {
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;
}
printf("bindex->areaStartValues[%d]=%u, comapre[%d]: %u %u\n",
area_idx, bindex->areaStartValues[area_idx], 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) copy_filter_vector_in_GPU(bindex, dev_bitmap, area_idx);
else 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) {
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]
// ==0: may not cause problem here?
// K-1: set all bits to 0. just scan one face: CS[this bindex] x CM[other bindexs].
// K: set all bits to 0. should not call rt. interrupt other procedures now. should cancel merge as well?
if (area_idx > K - 1) {
// 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 == K - 1) {
// '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] = compare;
scan_selected_compares[bindex_id][1] = bindex->data_max;
scan_refine_in_position += 1;
}
scan_refine_mutex.unlock();
return;
}
if (area_idx < 0) {
// 'compare' less than all raw_data, return all 1 result
scan_refine_mutex.lock();
if(!scan_skip_refine) {
scan_skip_this_face[bindex_id] = true;
scan_selected_compares[bindex_id][0] = compare;
scan_selected_compares[bindex_id][1] = bindex->data_max;
scan_refine_in_position += 1;
}
scan_refine_mutex.unlock();
return;
}
// set refine compares here
scan_refine_mutex.lock();
scan_selected_compares[bindex_id][0] = compare;
if (area_idx + 1 < K) {
scan_selected_compares[bindex_id][1] = bindex->areaStartValues[area_idx + 1];
printf("bindex->areaStartValues[%d]=%u, comapre[%d]: %u %u\n",
area_idx + 1, bindex->areaStartValues[area_idx + 1], bindex_id, scan_selected_compares[bindex_id][0], scan_selected_compares[bindex_id][1]);
} else {
scan_selected_compares[bindex_id][1] = bindex->data_max;
printf("bindex->areaStartValues[%d]=%u, comapre[%d]: %u %u\n",
area_idx, bindex->areaStartValues[area_idx], 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 larger than compare here, so rt must return result to append (maybe with and) retrun range -> | RT Scan | Bindex filter vector |
PRINT_EXCECUTION_TIME("copy",
copy_filter_vector_in_GPU(bindex, dev_bitmap, area_idx, true))
}
void free_bindex(BinDex *bindex) {
free(bindex);
}
// remember to free data ptr after using
void get_data_from_file(char *DATA_PATH, CODE **initial_data, int bindex_num) {
FILE* fp;
if (!(fp = fopen(DATA_PATH, "rb"))) {
printf("init_data_from_file: fopen(%s) faild\n", DATA_PATH);
exit(-1);
}
printf("initing data from %s\n", DATA_PATH);
for (int bindex_id = 0; bindex_id < bindex_num; bindex_id++) {
initial_data[bindex_id] = (CODE*)malloc(N * sizeof(CODE));
CODE* data = initial_data[bindex_id];
if (fread(data, sizeof(CODE), N, fp) == 0) {
printf("init_data_from_file: fread faild.\n");
exit(-1);
}
for (int i = 0; i < N; i++)
data[i] = data[i] & data_range_list[bindex_id];
printf("[CHECK] col %d first num: %u last num: %u\n", bindex_id, initial_data[bindex_id][0], initial_data[bindex_id][N - 1]);
}
}
void compare_bitmap(BITS *bitmap_a, BITS *bitmap_b, int len, CODE **raw_data, int bindex_num) {
int total_hit = 0;
int true_hit = 0;
for (int i = 0; i < len; i++) {
int data_a = (bitmap_a[i >> BITSSHIFT] & (1U << (BITSWIDTH - 1 - i % BITSWIDTH)));
int data_b = (bitmap_b[i >> BITSSHIFT] & (1U << (BITSWIDTH - 1 - i % BITSWIDTH)));
if (data_a) {
total_hit += 1;
if (data_b) true_hit += 1;
}
if (data_a != data_b) {
printf("[ERROR] check error in raw_data[%d]=", i);
printf(" %u / %u / %u \n", raw_data[0][i], raw_data[1][i], raw_data[2][i]);
printf("the correct is %u, but we have %u\n", data_a, data_b);
for (int j = 0; j < bindex_num; j++) {
printf("SC[%d] = [%u,%u], MC[%d] = [%u,%u]\n",j,scan_selected_compares[j][0],scan_selected_compares[j][1],
j,scan_max_compares[j][0],scan_max_compares[j][1]);
}
break;
}
}
printf("[CHECK]hit %d/%d\n", true_hit, total_hit);
}
void raw_scan(BITS *bitmap, CODE target1, OPERATOR OP, CODE *raw_data, BITS* compare_bitmap = NULL) {
for(int i = 0; i < N; i++) {
bool hit = false;
switch (OP)
{
case LT:
if (raw_data[i] < target1) hit = true;
break;
case LE:
if (raw_data[i] <= target1) hit = true;
break;
case GT:
if (raw_data[i] > target1) hit = true;
break;
case GE:
if (raw_data[i] >= target1) hit = true;
break;
case EQ:
if (raw_data[i] == target1) hit = true;
break;
default:
break;
}
if (hit) {
// bitmap[i >> BITSSHIFT] |= (1U << (BITSWIDTH - 1 - i % BITSWIDTH));
if (compare_bitmap != NULL) {
int compare_bit = (compare_bitmap[i >> BITSSHIFT] & (1U << (BITSWIDTH - 1 - i % BITSWIDTH)));
if(compare_bitmap == 0) {
printf("[ERROR] check error in raw_data[%d]=", i);
printf(" %u\n", raw_data[i]);
break;
}
} else {
refine(bitmap, i);
}
}
}
}
void raw_scan_entry(CODE target1, string search_cmd, BITS* bitmap, BITS* mergeBitmap, CODE* raw_data) {
if (search_cmd == "lt") {
raw_scan(bitmap, target1, LT, raw_data);
} else if (search_cmd == "le") {
raw_scan(bitmap, target1, LE, raw_data);
} else if (search_cmd == "gt") {
raw_scan(bitmap, target1, GT, raw_data);
} else if (search_cmd == "ge") {
raw_scan(bitmap, target1, GE, raw_data);
} else if (search_cmd == "eq") {
raw_scan(bitmap, target1, EQ, raw_data);
} else {
printf("Error: Invalid operator %s\n", search_cmd.c_str());
exit(-1);
}
int max_idx = (N + CODEWIDTH - 1) / CODEWIDTH;
int stride = (max_idx + THREAD_NUM - 1) / THREAD_NUM;
if (mergeBitmap != bitmap) {
thread threads[THREAD_NUM];
int start_idx = 0;
int end_idx = 0;
size_t t_id = 0;
while (end_idx < max_idx && t_id < THREAD_NUM) {
end_idx = start_idx + stride;
if (end_idx > max_idx) {
end_idx = max_idx;
}
threads[t_id] = thread(
refine_result_bitmap,
mergeBitmap, bitmap,
start_idx, end_idx, t_id
);
start_idx += stride;
t_id += 1;
}
for (int i = 0; i < THREAD_NUM; i++)
threads[i].join();
}
}
void scan_multithread_withGPU(CODE target1, string search_cmd, BinDex *bindex, BITS *bitmap, int bindex_id) {
if (search_cmd == "lt" || search_cmd == "le") {
bindex_scan_lt_in_GPU(bindex, bitmap, target1, bindex_id);
} else if (search_cmd == "gt" || search_cmd == "ge") {
bindex_scan_gt_in_GPU(bindex, bitmap, target1, bindex_id);
} else {
printf("Error: Invalid operator %s\n", search_cmd.c_str());
exit(-1);
}
}
int calculate_ray_segment_num(int direction, double *predicate, BinDex **bindexs, int best_ray_num) {
int width = density_width;
int height = density_height;
int launch_width;
int launch_height;
if (direction == 2) {
launch_width = static_cast<int>((predicate[1] - predicate[0]) * width / (bindexs[0]->data_max - bindexs[0]->data_min)) + 1;
launch_height = static_cast<int>((predicate[3] - predicate[2]) * height / (bindexs[1]->data_max - bindexs[1]->data_min)) + 1;
} else if (direction == 1) {
launch_width = static_cast<int>((predicate[1] - predicate[0]) * width / (bindexs[0]->data_max - bindexs[0]->data_min)) + 1;
launch_height = static_cast<int>((predicate[5] - predicate[4]) * height / (bindexs[2]->data_max - bindexs[2]->data_min)) + 1;
} else {
launch_width = static_cast<int>((predicate[3] - predicate[2]) * width / (bindexs[1]->data_max - bindexs[1]->data_min)) + 1;
launch_height = static_cast<int>((predicate[5] - predicate[4]) * height / (bindexs[2]->data_max - bindexs[2]->data_min)) + 1;
}
printf("[LOG] launch width: %d launch height: %d\n",launch_width, launch_height);
int ray_segment_num = best_ray_num / launch_width / launch_height;
printf("[LOG] ray_segment_num: %d\n",ray_segment_num);
if (ray_segment_num <= 0) {
return 1;
} else {
return ray_segment_num;
}
}
void special_eq_scan(CODE *target_l, BinDex **bindexs, BITS *dev_bitmap, int bindex_num, string *search_cmd) {
if(DEBUG_TIME_COUNT) timer.commonGetStartTime(13);
if (DEBUG_INFO) {
printf("[INFO] use special eq scan\n");
}
double **compares = (double **)malloc(bindex_num * sizeof(double *));
double *dev_predicate = (double *)malloc(bindex_num * 2 * sizeof(double));
for (int i = 0; i < bindex_num; i++) {
compares[i] = &(dev_predicate[i * 2]);
}
// prepare MC and SC
int direction = 0;
for (int bindex_id = 0; bindex_id < bindex_num; bindex_id++) {
if (search_cmd[bindex_id] == "eq") {
compares[bindex_id][0] = double(target_l[bindex_id]) - 1.0;
if (compares[bindex_id][0] < 0) compares[bindex_id][0] = 0;
compares[bindex_id][1] = double(target_l[bindex_id]) + 2.0;
direction = bindex_id;
} else if (search_cmd[bindex_id] == "lt") {
compares[bindex_id][0] = bindexs[bindex_id]->data_min - 1.0;
compares[bindex_id][1] = double(target_l[bindex_id]);
if (compares[bindex_id][0] > compares[bindex_id][1]) {
swap(compares[bindex_id][0],compares[bindex_id][1]);
}
} else if (search_cmd[bindex_id] == "gt") {
compares[bindex_id][0] = double(target_l[bindex_id]);
compares[bindex_id][1] = bindexs[bindex_id]->data_max + 1.0;
} else {
printf("Error: Invalid operator %s\n", search_cmd[bindex_id].c_str());
exit(-1);
}
}
if(DEBUG_INFO) {
for (int i = 0; i < 6; i++) {
printf("%f ", dev_predicate[i]);
}
printf("\n");
printf("direction = %d\n", direction);
printf("ray segment num = %d\n", default_ray_segment_num);
printf("[INFO] compares prepared.\n");
}
if (with_refine) {
refineWithOptix(dev_bitmap, dev_predicate, bindex_num, default_ray_length, default_ray_segment_num, false, direction, default_ray_mode);
}
if(DEBUG_TIME_COUNT) timer.commonGetEndTime(13);
}
int get_refine_space_side(double **compares, int bindex_num, int face_direction) {
double dis = compares[0][1] - compares[0][0];
int d = 0;
for (int bindex_id = 1; bindex_id < bindex_num; bindex_id++) {
if (face_direction == 0) { // wide face, short side
if (compares[bindex_id][1] - compares[bindex_id][0] < dis) {
dis = compares[bindex_id][1] - compares[bindex_id][0];
d = bindex_id;
}
} else { // narrow face, long side
if (compares[bindex_id][1] - compares[bindex_id][0] > dis) {
dis = compares[bindex_id][1] - compares[bindex_id][0];
d = bindex_id;
}
}
}
return d;
}
void refine_with_GPU(BinDex **bindexs, BITS *dev_bitmap, const int bindex_num) {
timer.commonGetStartTime(23);
bool adjust_ray_num = false; // switch for fixed ray number: `true` for `fixed`
int default_ray_total_num = 20000;
int ray_length = default_ray_length; // set to -1 so rt will use default segmentnum set
if(DEBUG_TIME_COUNT) timer.commonGetStartTime(18);
double **compares = (double **)malloc(bindex_num * sizeof(double *));
double *dev_predicate = (double *)malloc(bindex_num * 2 * sizeof(double));
for (int i = 0; i < bindex_num; i++) {
compares[i] = &(dev_predicate[i * 2]);
}
if(DEBUG_TIME_COUNT) timer.commonGetEndTime(18);
if(DEBUG_TIME_COUNT) timer.commonGetStartTime(13);
// if there is a compare totally out of boundary, refine procedure can be skipped
if (scan_skip_refine) {
if(DEBUG_INFO) printf("[INFO] Search out of boundary, skip all refine.\n");
if(DEBUG_TIME_COUNT) timer.commonGetEndTime(13);
timer.commonGetEndTime(23);
return;
}
// check if we can scan only one face
for (int bindex_id = 0; bindex_id < bindex_num; bindex_id++) {
if(scan_skip_other_face[bindex_id]) {
if(DEBUG_INFO) printf("[INFO] %d face scan, other skipped.\n",bindex_id);
// no matter use sc or mc
compares[bindex_id][0] = scan_selected_compares[bindex_id][0];
compares[bindex_id][1] = scan_selected_compares[bindex_id][1];
for (int other_bindex_id = 0; other_bindex_id < bindex_num; other_bindex_id++) {
if (other_bindex_id == bindex_id) continue;
compares[other_bindex_id][0] = scan_max_compares[other_bindex_id][0];
compares[other_bindex_id][1] = scan_max_compares[other_bindex_id][1];
}
for (int i = 0; i < bindex_num; i++) {
if (compares[i][0] == compares[i][1]) {
if(DEBUG_INFO) printf("[INFO] %d face scan skipped for the same compares[0] and compares[1].\n",bindex_id);
if(DEBUG_TIME_COUNT) timer.commonGetEndTime(13);
timer.commonGetEndTime(23);
return;
}
}
int direction = get_refine_space_side(compares, bindex_num, face_direction);
// calculate ray_segment_num
if (adjust_ray_num) default_ray_segment_num = calculate_ray_segment_num(direction, dev_predicate, bindexs, default_ray_total_num);
// Solve bound problem
for (int i = 0; i < bindex_num; i++) {
compares[i][0] -= 1;
}
// add refine here
// send compares, dev_bitmap, the result is in dev_bitmap
if(DEBUG_INFO) {
printf("[Prepared predicate]");
for (int i = 0; i < 6; i++) {
printf("%f ", dev_predicate[i]);
}
printf("\n");
printf("direction = %d\n", direction);
printf("[INFO] compares prepared.\n");
}
timer.commonGetEndTime(23);
refineWithOptix(dev_bitmap, dev_predicate, bindex_num, ray_length, default_ray_segment_num, false, direction, default_ray_mode);
if(DEBUG_TIME_COUNT) timer.commonGetEndTime(13);
return;
}
}
double selectivity = 0.0;
// rt scan every face
/// split inversed face and non-inversed face first
vector<int> inversed_face;
vector<int> normal_face;
for (int bindex_id = 0; bindex_id < bindex_num; bindex_id++) {
if (scan_inverse_this_face[bindex_id]) {
inversed_face.push_back(bindex_id);
}
else {
normal_face.push_back(bindex_id);
}
}
/// start refine
int max_MS_face_count = 0;
for (int i = 0; i < bindex_num; i++) {
double face_selectivity = 1.0;
bool inverse = false;
int bindex_id;
if (normal_face.size() != 0) {
bindex_id = normal_face[0];
normal_face.erase(normal_face.begin());
}
else if (inversed_face.size() != 0) {
bindex_id = inversed_face[0];
inversed_face.erase(inversed_face.begin());
inverse = true;
}
int current_MS_face_count = 0;
if(scan_skip_this_face[bindex_id]) {
if(DEBUG_INFO) printf("[INFO] %d face scan skipped.\n",bindex_id);
continue;
}
// select SC face
compares[bindex_id][0] = scan_selected_compares[bindex_id][0];
compares[bindex_id][1] = scan_selected_compares[bindex_id][1];
// revise S and C here to avoid a < x < b scan
// compares[bindex_id][0] -= 1.0;
for (int other_bindex_id = 0; other_bindex_id < bindex_num; other_bindex_id++) {
if (other_bindex_id == bindex_id) continue;
if (current_MS_face_count < max_MS_face_count) {
CODE S;
if (inverse)
S = scan_selected_compares[other_bindex_id][1];
else
S = scan_selected_compares[other_bindex_id][0];
if (scan_max_compares[other_bindex_id][0] < S) {
compares[other_bindex_id][0] = scan_max_compares[other_bindex_id][0];
compares[other_bindex_id][1] = S;
}
else {
compares[other_bindex_id][0] = S;
compares[other_bindex_id][1] = scan_max_compares[other_bindex_id][0];
}
current_MS_face_count += 1;
} else {
compares[other_bindex_id][0] = scan_max_compares[other_bindex_id][0];
compares[other_bindex_id][1] = scan_max_compares[other_bindex_id][1];
}
}
bool mid_skip_flag = false;
for (int j = 0; j < bindex_num; j++) {
if (compares[j][0] == compares[j][1]) {
if(DEBUG_INFO) printf("[INFO] %d face scan skipped for the same compares[0] and compares[1].\n",bindex_id);
mid_skip_flag = true;
break;
}
}
if (mid_skip_flag) continue;
if(DEBUG_INFO) {
for (int i = 0; i < bindex_num; i++) {
face_selectivity *= double(compares[i][1] - compares[i][0]) / double(bindexs[i]->data_max - bindexs[i]->data_min);
}
selectivity += face_selectivity;
}
int direction = get_refine_space_side(compares, bindex_num, face_direction);
// calculate ray_segment_num
if (adjust_ray_num) default_ray_segment_num = calculate_ray_segment_num(direction, dev_predicate, bindexs, default_ray_total_num);
// Solve bound problem
for (int i = 0; i < bindex_num; i++) {
compares[i][0] -= 1;
}
// add refine here
// send compares, dev_bitmap, the result is in dev_bitmap
if(DEBUG_INFO) {
printf("[Prepared predicate] MS = %d, predicate: ", max_MS_face_count);
for (int i = 0; i < 6; i++) {
printf("%f ", dev_predicate[i]);
}
printf("\n");
printf("direction = %d\n", direction);
printf("[INFO] compares prepared.\n");
}
timer.commonGetEndTime(23);
refineWithOptix(dev_bitmap, dev_predicate, bindex_num, ray_length, default_ray_segment_num, inverse, direction, default_ray_mode);
timer.commonGetStartTime(23);
max_MS_face_count += 1;
}
timer.commonGetEndTime(23);
if(DEBUG_INFO) printf("total selectivity: %f\n", selectivity);
if(DEBUG_TIME_COUNT) timer.commonGetEndTime(13);
}
void merge_with_GPU(BITS *merge_bitmap, BITS **dev_bitmaps, const int bindex_num, const int bindex_len) {
timer.commonGetStartTime(15);
int bitmap_len = bits_num_needed(bindex_len);
if (scan_skip_refine) {
if(DEBUG_INFO) printf("[INFO] Search out of boundary, skip all merge.\n");
cudaMemset(merge_bitmap, 0, bitmap_len * sizeof(BITS));
timer.commonGetEndTime(15);
return;
}
for (int bindex_id = 0; bindex_id < bindex_num; bindex_id++) {
if(scan_skip_other_face[bindex_id]) {
if(DEBUG_INFO) printf("[INFO] %d face merge, other skipped.\n",bindex_id);
cudaMemset(merge_bitmap, 0, bitmap_len * sizeof(BITS));
timer.commonGetEndTime(15);
return;
}
}
for (int bindex_id = 0; bindex_id < bindex_num; bindex_id++) {
if (merge_bitmap == dev_bitmaps[bindex_id]) {
if (scan_skip_this_face[bindex_id]) {
if(DEBUG_INFO) printf("[INFO] merge face %d skipped, set it to 0xFF\n", bindex_id);
cudaMemset(merge_bitmap, 0xFF, bitmap_len * sizeof(BITS));
}
break;
}
}
for (int bindex_id = 0; bindex_id < bindex_num; bindex_id++) {
if (!scan_skip_this_face[bindex_id]) {
if (merge_bitmap != dev_bitmaps[bindex_id]) {
GPUbitAndWithCuda(merge_bitmap, dev_bitmaps[bindex_id], bindex_len);
}
}
else {
if(DEBUG_INFO) printf("[INFO] %d face merge skipped.\n",bindex_id);
}
}
timer.commonGetEndTime(15);
return;
}
// Only support `lt` now.
void generate_range_queries(vector<CODE> &all_targets,
vector<string> &search_cmd,
int column_num,
CODE *range) {
#ifndef RANGE_SELECTIVITY_11
#if DISTRITION == 0
CODE span = UINT32_MAX / (NUM_QUERIES + 1);
#else
CODE span = (max_val - min_val + NUM_QUERIES - 1) / NUM_QUERIES;
#endif
all_targets.resize(NUM_QUERIES * column_num);
search_cmd.resize(NUM_QUERIES * column_num);
for (int i = 0; i < NUM_QUERIES; i++) {
for (int column_id = 0; column_id < column_num; column_id++) {
all_targets[i * column_num + column_id] = (i + 1) * span;
search_cmd[i * column_num + column_id] = "lt";
}
}
#else
double span[3] = {1.0 * data_range_list[0] / (NUM_QUERIES - 1),
1.0 * data_range_list[1] / (NUM_QUERIES - 1),
1.0 * data_range_list[2] / (NUM_QUERIES - 1)};
all_targets.resize(NUM_QUERIES * column_num);
search_cmd.resize(NUM_QUERIES * column_num);
for (int i = 0; i < NUM_QUERIES; i++) {
for (int column_id = 0; column_id < column_num; column_id++) {
all_targets[i * column_num + column_id] = CODE(i * span[column_id]);
if (i == NUM_QUERIES - 1) {
if (range[2 * column_id + 1] != UINT32_MAX) {
all_targets[i * column_num + column_id] = range[2 * column_id + 1] + 1;
} else {
all_targets[i * column_num + column_id] = UINT32_MAX;
}
}
search_cmd[i * column_num + column_id] = "lt";
}
}
#endif
for (int i = 0; i < NUM_QUERIES; i++) {
for (int column_id = 0; column_id < column_num; column_id++) {
printf("%s %u\n", search_cmd[i * column_num + column_id].c_str(), all_targets[i * column_num + column_id]);
}
}
}
void generate_point_queries(vector<CODE> &all_targets,
vector<string> &search_cmd,
int column_num) {
#if DISTRITION == 0
CODE span = UINT32_MAX / (NUM_QUERIES + 1);
#else
CODE span = (max_val - min_val + NUM_QUERIES - 1) / NUM_QUERIES;
#endif
all_targets.resize(NUM_QUERIES * column_num);
search_cmd.resize(NUM_QUERIES * column_num);
for (int i = 0; i < NUM_QUERIES; i++) {
for (int column_id = 0; column_id < column_num; column_id++) {
all_targets[i * column_num + column_id] = (i + 1) * span;
search_cmd[i * column_num + column_id] = "eq";
}
}
}
void check(CODE **original_data, CODE *queries, string *search_cmd, int bindex_num, BITS *rt_result) {
int bitmap_len = bits_num_needed(N);
BITS *check_bitmap[bindex_num];
for (int bindex_id = 0; bindex_id < bindex_num; bindex_id++) {
check_bitmap[bindex_id] = (BITS *)aligned_alloc(SIMD_ALIGEN, bitmap_len * sizeof(BITS));
memset_mt(check_bitmap[bindex_id], 0, bitmap_len, THREAD_NUM, SIMD_ALIGEN);
}
for (int bindex_id = 0; bindex_id < bindex_num; bindex_id++) {
raw_scan_entry(
queries[bindex_id],
search_cmd[bindex_id],
check_bitmap[bindex_id],
check_bitmap[0],
original_data[bindex_id]
);
}
compare_bitmap(check_bitmap[0], rt_result, N, original_data, bindex_num);
printf("[CHECK] check final result done.\n\n");
for (int bindex_id = 0; bindex_id < bindex_num; bindex_id++) {
free(check_bitmap[bindex_id]);
}
}
void parse_args(int argc, char *argv[], char DATA_PATH[], char SCAN_FILE[], int &bindex_num) {
char opt;
while ((opt = getopt(argc, argv, "ha:b:c:d:e:f:g:w:m:o:p:q:s:u:v:y:z:")) != -1) {
switch (opt) {
case 'h':
printf(
"Usage: %s \n"
"[-l <left target list>] [-r <right target list>]\n"
"[-a <ray-length>]\n" // -1: flexible length controlled by ray_segment_num, -2: fixed length controlled by ray_segment_num
"[-b <column-num>]\n"
"[-c <reduced-scanning>]\n"
"[-d <data-range>]\n"
"[-e <ray-mode>]\n"
"[-f <input-file>]\n"
"[-g <range-query>]\n"
"[-w <ray-range-width>] [-m <ray-range-height>]\n"
"[-p <scan-predicate-file>]\n"
"[-q <query-num>]\n"
"[-s <ray-segment-num>]\n"
"[-u <with-refine>]\n"
"[-v <cube-width>]\n"
"[-y <switch-for-reading-queries-from-file>]"
"[-z <face-direction-0=wide-1=narrow>]\n",
argv[0]);
exit(0);
case 'f':
strcpy(DATA_PATH, optarg);
break;
case 'b':
bindex_num = atoi(optarg);
break;
case 's':
default_ray_segment_num = atoi(optarg);
break;
case 'w':
density_width = atoi(optarg);
break;
case 'm':
density_height = atoi(optarg);
break;
case 'p':
strcpy(SCAN_FILE, optarg);
break;
case 'a':
default_ray_length = atoi(optarg);
break;
case 'e':
default_ray_mode = atoi(optarg);
break;
case 'q':
NUM_QUERIES = atoi(optarg);
break;
case 'g':
RANGE_QUERY = atoi(optarg);
break;
case 'c':
REDUCED_SCANNING = atoi(optarg);
break;
case 'd':
sscanf(optarg, "%u,%u,%u", &data_range_list[0], &data_range_list[1], &data_range_list[2]);
break;
case 'u':
with_refine = atoi(optarg);
break;
case 'v':
cube_width = atoi(optarg);
break;
case 'y':
READ_QUERIES_FROM_FILE = atoi(optarg);
break;
case 'z':
face_direction = atoi(optarg);
break;
default:
printf("Error: unknown option %c\n", (char)opt);
exit(-1);
}
}
assert(bindex_num >= 1);
assert(THREAD_NUM >= bindex_num);
}
void get_data(CODE *initial_data[], CODE *original_data[], char DATA_PATH[], int bindex_num) {
if (!strlen(DATA_PATH)) {
printf("initing data by random\n");
for (int bindex_id = 0; bindex_id < bindex_num; bindex_id++) {
initial_data[bindex_id] = (CODE *)malloc(N * sizeof(CODE));
CODE *data = initial_data[bindex_id];
for (int i = 0; i < N; i++) {
data[i] = i % (long(data_range_list[bindex_id]) + 1);
}
random_shuffle(data, data + N);
}
} else {
get_data_from_file(DATA_PATH, initial_data, bindex_num);
}
for (int bindex_id = 0; bindex_id < bindex_num; bindex_id++) {
original_data[bindex_id] = (CODE *)malloc(N * sizeof(CODE));
memcpy(original_data[bindex_id], initial_data[bindex_id], N * sizeof(CODE));
}
}
void get_queries(vector<CODE> &targets, vector<string> &search_cmd,
CODE *old_queries, char *SCAN_FILE, int bindex_num, CODE *range) {
if (!READ_QUERIES_FROM_FILE) { // generate queries
if (RANGE_QUERY) {
generate_range_queries(targets, search_cmd, bindex_num, range);
} else {
generate_point_queries(targets, search_cmd, bindex_num);
}
} else { // read queries from file