-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbindex.cpp
More file actions
2320 lines (2055 loc) · 72 KB
/
bindex.cpp
File metadata and controls
2320 lines (2055 loc) · 72 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 <assert.h>
#include <immintrin.h>
#include <limits.h>
#include <omp.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <algorithm>
#include <iostream>
#include <parallel/algorithm>
#include <random>
#include <sstream>
#include <string>
#include <thread>
#include <iostream>
#include <vector>
#include <regex>
#include <fstream>
#define THREAD_NUM 20
#define MAX_BINDEX_NUM 256
#if !defined(WIDTH_4) && !defined(WIDTH_8) && !defined(WIDTH_12) && !defined(WIDTH_16) && !defined(WIDTH_20) && \
!defined(WIDTH_24) && !defined(WIDTH_28) && !defined(WIDTH_32)
#define WIDTH_32
#endif
#ifndef VAREA_N
#define VAREA_N 128 // 128
#endif
#ifndef DATA_N
#define DATA_N 1e8 // 1e9
#endif
#define ROUNDUP_DIVIDE(x, n) ((x + n - 1) / n)
#define ROUNDUP(x, n) (ROUNDUP_DIVIDE(x, n) * n)
#define PRINT_EXCECUTION_TIME(msg, code) \
do { \
struct timeval t1, t2; \
double elapsed; \
gettimeofday(&t1, NULL); \
do { \
code; \
} while (0); \
gettimeofday(&t2, NULL); \
elapsed = (t2.tv_sec - t1.tv_sec) * 1000.0; \
elapsed += (t2.tv_usec - t1.tv_usec) / 1000.0; \
printf(msg " time: %f ms\n", elapsed); \
} while (0);
#define BATCH_BLOCK_INSERT 0
/*
optional code width
*/
#ifdef WIDTH_4
typedef uint8_t CODE;
#define MAXCODE ((1 << 4) - 1)
#define MINCODE 0
#define CODEWIDTH 4
#define LOADSHIFT 4
#endif
#ifdef WIDTH_8
typedef uint8_t CODE;
#define MAXCODE ((1 << 8) - 1)
#define MINCODE 0
#define CODEWIDTH 8
#define LOADSHIFT 0
#endif
#ifdef WIDTH_12
typedef uint16_t CODE;
#define MAXCODE ((1 << 12) - 1)
#define MINCODE 0
#define CODEWIDTH 12
#define LOADSHIFT 4
#endif
#ifdef WIDTH_16
typedef uint16_t CODE;
#define MAXCODE ((1 << 16) - 1)
#define MINCODE 0
#define CODEWIDTH 16
#define LOADSHIFT 0
#endif
#ifdef WIDTH_20
typedef uint32_t CODE;
#define MAXCODE ((1 << 20) - 1)
#define MINCODE 0
#define CODEWIDTH 20
#define LOADSHIFT 12
#endif
#ifdef WIDTH_24
typedef uint32_t CODE;
#define MAXCODE ((1 << 24) - 1)
#define MINCODE 0
#define CODEWIDTH 24
#define LOADSHIFT 8
#endif
#ifdef WIDTH_28
typedef uint32_t CODE;
#define MAXCODE ((1 << 28) - 1)
#define MINCODE 0
#define CODEWIDTH 28
#define LOADSHIFT 4
#endif
#ifdef WIDTH_32
typedef uint32_t CODE;
#define MAXCODE ((1L << 32) - 1)
#define MINCODE 0
#define CODEWIDTH 32
#define LOADSHIFT 0
#endif
#define DEBUG_TIME_COUNT 1
#include <mutex>
using namespace std;
// time count part
class Timer{
public:
double time[30];
mutex timeMutex[30];
double timebase;
Timer() {
for (int i = 0; i < 30; i++) {
time[i] = 0.0;
}
struct timeval t1;
gettimeofday(&t1, NULL);
timebase = t1.tv_sec * 1000.0 + t1.tv_usec / 1000.0;
}
void clear() {
for (int i = 0; i < 30; i++) {
time[i] = 0.0;
}
}
void showTime() {
cout << endl;
cout << "########### Time ##########" << endl;
cout << "[Time] build bindex: ";
cout << time[0] << " ms" << endl;
// cout << "[Time] append to bindex: ";
// cout << time[1] << endl;
// cout << "[Time] insert to block: ";
// cout << time[2] << endl;
// cout << "[Time] split block: ";
// cout << time[3] << endl;
// cout << "[Time] insert to area: ";
// cout << time[4] << endl;
// cout << "[Time] init new space: ";
// cout << time[5] << endl;
// cout << "[Time] append to filter vector: ";
// cout << time[6] << endl;
cout << "[Time] total time: ";
cout << time[11] << " ms" << endl;
cout << "##############################" << endl;
cout << endl;
}
void commonGetStartTime(int timeId) {
struct timeval t1;
gettimeofday(&t1, NULL);
lock_guard<mutex> lock(timeMutex[timeId]);
time[timeId] -= (t1.tv_sec * 1000.0 + t1.tv_usec / 1000.0) - timebase;
}
void commonGetEndTime(int timeId) {
struct timeval t1;
gettimeofday(&t1, NULL);
lock_guard<mutex> lock(timeMutex[timeId]);
time[timeId] += (t1.tv_sec * 1000.0 + t1.tv_usec / 1000.0) - timebase;
}
};
Timer timer;
typedef int POSTYPE; // Data type for positions
// typedef int CODE; // Codes are stored as int
typedef unsigned int BITS; // 32 0-1 bit results are stored in a BITS
enum OPERATOR {
EQ = 0, // x == a
LT, // x < a
GT, // x > a
LE, // x <= a
GE, // x >= a
BT, // a < x < b
};
const int RUNS = 1;
// const int CODEWIDTH = sizeof(CODE) * 8;
const int BITSWIDTH = sizeof(BITS) * 8;
const int BITSSHIFT = 5; // x / BITSWIDTH == x >> BITSSHIFT
const int SIMD_ALIGEN = 32;
const int SIMD_JOB_UNIT = 8; // 8 * BITSWIDTH == __m256i
const int blockInitSize = 3276; // 2048
const int blockMaxSize = 4096; // blockInitSize * 2;
const int K = VAREA_N; // Number of virtual areas
const int N = (int)DATA_N;
// const int MAXCODE = INT_MAX;
// const int MINCODE = INT_MIN;
const int blockNumMax = (N / (K * blockInitSize)) * 4;
int prefetch_stride = 6;
std::vector<CODE> target_numbers_l; // left target numbers
std::vector<CODE> target_numbers_r; // right target numbers
BITS *result1;
BITS *result2;
CODE *current_raw_data;
char scan_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;
}
void display_bitmap(BITS *bitmap, int bitmap_len);
template <typename T>
POSTYPE *argsort(const T *v, POSTYPE n) {
POSTYPE *idx = (POSTYPE *)malloc(n * sizeof(POSTYPE));
for (POSTYPE i = 0; i < n; i++) {
idx[i] = i;
}
__gnu_parallel::sort(idx, idx + n, [&v](size_t i1, size_t i2) { return v[i1] < v[i2]; });
return idx;
}
inline int bits_num_needed(int n) {
// calculate the number of bits for storing n 0-1 bit results
return ((n - 1) / BITSWIDTH) + 1;
}
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;
}
typedef struct {
// Struct for a position block
POSTYPE *pos; // Position array in a block
CODE *val; // Sorted codes in a block TODO: needed to be
// removed in future implementation for saving
// space (emmmm... actually we don't need
// to remove it for experimental evaluations).
int length;
} pos_block;
typedef struct {
pos_block *blocks[blockNumMax];
int blockNum;
int length;
} Area;
typedef struct {
Area *areas[K];
BITS *filterVectors[K - 1];
POSTYPE area_counts[K]; // Counts of values contained in the first i areas
POSTYPE length;
} BinDex;
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;
}
int insert_to_block_without_val(pos_block *pb, CODE *val_f, POSTYPE *pos_f, int n, CODE *raw_data) {
// 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;
}
if (BATCH_BLOCK_INSERT) {
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] >= raw_data[pb->pos[i]]) {
pb->pos[k--] = pos_f[j--];
} else {
pb->pos[k--] = pb->pos[i--];
}
}
while (j >= 0) {
pb->pos[k--] = pos_f[j--];
}
}
else {
// Separate insert to block
int insert_num = (flagNum > 0)?flagNum:n;
for (int j = 0; j < insert_num; j++){
int noflag = 0;
for (int i = 0; i < pb->length; i++) {
if (raw_data[pb->pos[i]] > val_f[j]) {
noflag = 1;
for (int k = pb->length; k > i; k--) {
pb->pos[k] = pb->pos[k-1];
}
pb->pos[i] = pos_f[j];
pb->length = pb->length + 1;
break;
}
}
if (!noflag) {
pb->pos[pb->length] = pos_f[j];
pb->length = pb->length + 1;
}
}
}
pb->length = length_new;
if (DEBUG_TIME_COUNT) timer.commonGetEndTime(2);
return flagNum;
}
void init_area(Area *area, CODE *val, POSTYPE *pos, int n) {
// TODO: An area may explode for extremely skewed data.
// 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 area_split_block(Area *area, int block_idx) {
if (DEBUG_TIME_COUNT) timer.commonGetStartTime(3);
assert(area->blockNum < blockNumMax);
pos_block *pb_old = area->blocks[block_idx];
pb_old->length = blockInitSize; // Split pb_old into two blocks, only keep
// half of the original values in pb_old
// Fill values into new block
pos_block *pb_new = (pos_block *)malloc(sizeof(pos_block));
init_pos_block(pb_new, pb_old->val + blockInitSize, pb_old->pos + blockInitSize, blockMaxSize - blockInitSize);
// Update blocks in area
for (int i = area->blockNum; i > (block_idx + 1); i--) {
area->blocks[i] = area->blocks[i - 1];
}
area->blocks[block_idx + 1] = pb_new;
area->blockNum++;
if (DEBUG_TIME_COUNT) timer.commonGetEndTime(3);
}
void insert_to_area(Area *area, CODE *val, POSTYPE *pos, int n, CODE *raw_data) {
// TODO: Inserting too many elements into an area will explode (the blocks
// array will be filled up), automatically rebuilding the whole BinDex
// structure (or enlarging current area) is needed in future implementation
int i, j;
for (i = 0, j = 0; i < n && j < area->blockNum - 1;) {
int num_insert_to_block = 0;
int start = i;
while (val[i] < block_start_value(area->blocks[j + 1]) && i < n) {
i++;
num_insert_to_block++;
}
if (num_insert_to_block) {
int flagNum = insert_to_block_without_val(area->blocks[j], val + start, pos + start, num_insert_to_block, raw_data);
while (flagNum) {
area_split_block(area, j++);
num_insert_to_block -= flagNum;
start += flagNum;
flagNum = insert_to_block_without_val(area->blocks[j], val + start, pos + start, num_insert_to_block, raw_data);
}
}
j++;
}
if (i < n) { // Insert val[i] to the end of last block if val[i] is no less
// than the maximum value of current area
int num_insert_to_block = n - i;
int start = i;
int flagNum = insert_to_block_without_val(area->blocks[j], val + start, pos + start, num_insert_to_block, raw_data);
while (flagNum) {
area_split_block(area, j++);
num_insert_to_block -= flagNum;
start += flagNum;
flagNum = insert_to_block_without_val(area->blocks[j], val + start, pos + start, num_insert_to_block, raw_data);
}
}
area->length += n;
}
void show_volume(Area *area)
{
cout << "[+] Area volume:" << endl;
for (int i = 0; i < area->blockNum; i++)
{
cout << "Block " << i << ": " << "start value: " << area->blocks[i]->val[0] << " Size: " << area->blocks[i]->length << endl;
}
}
void display_block(pos_block *pb) {
printf("Virtual space values:\t");
for (int i = 0; i < pb->length; i++) {
printf("%d,", pb->val[i]);
}
printf("\nPositions:\t\t");
for (int i = 0; i < pb->length; i++) {
printf("%d,", pb->pos[i]);
}
}
void display_area(Area *area) {
printf("Virtual space values:\t\n");
for (int i = 0; i < area->blockNum; i++) {
printf("block%d--", i);
for (int j = 0; j < area->blocks[i]->length; j++) {
printf("%d,", area->blocks[i]->val[j]);
}
printf("\t");
printf("\n");
}
printf("\nPositions:\t\t\n");
for (int i = 0; i < area->blockNum; i++) {
printf("block%d--", i);
for (int j = 0; j < area->blocks[i]->length; j++) {
printf("%d,", area->blocks[i]->pos[j]);
}
printf("\t");
printf("\n");
}
}
void display_bindex(BinDex *bindex, CODE *raw_data) {
for (int i = 0; i < K; i++) {
printf("Area%d:\n", i);
display_area(bindex->areas[i]);
printf("\t\n");
}
printf("Length:%d\t raw_data:", bindex->length);
for (int i = 0; i < bindex->length; i++) {
printf("%d,", raw_data[i]);
if ((i + 1) % 4 == 0) printf("|");
if ((i + 1) % 32 == 0) printf("--------");
}
printf("\t\n==\n");
for (int i = 0; i < K - 1; i++) {
printf("filterVector%d:\n", i);
display_bitmap(bindex->filterVectors[i], bits_num_needed(bindex->length));
printf("\t\n");
}
}
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);
}
int padding_fv_val_less(BITS *bitmap, POSTYPE length_old, const CODE *val_new, CODE compare, POSTYPE n) {
// Padding new bit results to old bitmap to fill up a BITS variable, return
// the number of bits needed for fill up a BITS variable
if (length_old % BITSWIDTH == 0) return 0; // No padding is needed for fill up a BITS
int result_bits_count;
int bitmap_insert_pos = length_old / BITSWIDTH;
int padding = (bitmap_insert_pos + 1) * BITSWIDTH - length_old;
result_bits_count = padding;
if (padding > (int)n) {
result_bits_count = n; // Too little new data, still not filled up,
// return the number of actually appended bits
}
for (int i = 0; i < result_bits_count; i++) {
if (val_new[i] < compare) {
bitmap[bitmap_insert_pos] += (1 << (padding - i - 1));
}
}
return result_bits_count;
}
void append_fv_val_less(BITS *bitmap, POSTYPE length_old, const CODE *val, CODE compare, POSTYPE n) {
int padding = padding_fv_val_less(bitmap, length_old, val, compare, n);
int bitmap_insert_pos = (length_old - 1) / BITSWIDTH + 1;
set_fv_val_less(bitmap + bitmap_insert_pos, val + padding, compare, n - padding);
}
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];
}
}
CODE *data_sorted;
void init_bindex(BinDex *bindex, CODE *data, POSTYPE n) {
bindex->length = n;
POSTYPE avgAreaSize = n / K;
CODE areaStartValues[K];
POSTYPE areaStartIdx[K];
data_sorted = (CODE *)malloc(n * sizeof(CODE)); // Sorted codes
POSTYPE *pos = argsort(data, n);
for (int i = 0; i < n; i++) {
data_sorted[i] = data[pos[i]];
}
areaStartValues[0] = data_sorted[0];
areaStartIdx[0] = 0;
for (int i = 1; i < K; i++) {
areaStartValues[i] = data_sorted[i * avgAreaSize];
int j = i * avgAreaSize;
if (areaStartValues[i] == areaStartValues[i - 1]) {
areaStartIdx[i] = j;
} else {
// To find the first element which is less than startValue
// TODO: in current implementation, an area must contain at least two
// different values, area containing unique code should be considered in
// future implementation
while (data_sorted[j] == areaStartValues[i]) {
j--;
}
areaStartIdx[i] = j + 1;
}
}
// Build the areas
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);
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();
}
}
// 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);
// Build the filterVectors
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, 2 * bits_num_needed(n) * sizeof(BITS));
threads[j] = std::thread(set_fv_val_less, bindex->filterVectors[k * THREAD_NUM + j], data,
area_start_value(bindex->areas[k * THREAD_NUM + j + 1]), n);
}
for (int j = 0; j < THREAD_NUM && (k * THREAD_NUM + j) < (K - 1); j++) {
threads[j].join();
}
}
free(pos);
free(data_sorted);
}
void append_to_bindex(BinDex *bindex, CODE *new_data, POSTYPE n, CODE *raw_data) {
if (DEBUG_TIME_COUNT) timer.commonGetStartTime(1);
POSTYPE *idx = argsort(new_data, n);
CODE *data_sorted = (CODE *)malloc(n * sizeof(CODE));
POSTYPE areaStartIdx[K];
POSTYPE *new_pos = (POSTYPE *)malloc(n * sizeof(POSTYPE));
areaStartIdx[0] = 0;
int k = 1;
for (POSTYPE i = 0; i < n; i++) {
// TODO: multithread appending should(?) be done in future implementation
data_sorted[i] = new_data[idx[i]];
new_pos[i] = idx[i] + bindex->length;
raw_data[bindex->length + i] = new_data[i];
while (k < K && data_sorted[i] >= area_start_value(bindex->areas[k])) {
// TODO: Here a naive linear search is used for calculating the
// areaStartIdx, optimizations may(?may not) be done in future implementation
areaStartIdx[k++] = i;
}
}
while (k < K) {
areaStartIdx[k++] = n;
}
std::thread threads[THREAD_NUM];
int i, j;
// Update the areas
int accum_add_count = 0;
for (k = 0; k * THREAD_NUM < K; k++) {
if (DEBUG_TIME_COUNT) timer.commonGetStartTime(4);
for (j = 0; j < THREAD_NUM && (k * THREAD_NUM + j) < K; j++) {
i = k * THREAD_NUM + j;
int num_added = num_insert_to_area(areaStartIdx, i, n);
accum_add_count += num_added;
threads[j] = std::thread(insert_to_area, bindex->areas[i], data_sorted + areaStartIdx[i],
new_pos + areaStartIdx[i], num_added, raw_data);
bindex->area_counts[i] += accum_add_count;
}
for (j = 0; j < THREAD_NUM && (k * THREAD_NUM + j) < K; j++) {
threads[j].join();
}
if (DEBUG_TIME_COUNT) timer.commonGetEndTime(4);
}
// Append to the filter vectors
for (k = 0; k * THREAD_NUM < (K - 1); k++) {
if (DEBUG_TIME_COUNT) timer.commonGetStartTime(6);
for (j = 0; j < THREAD_NUM && (k * THREAD_NUM + j) < (K - 1); j++) {
i = k * THREAD_NUM + j;
threads[j] = std::thread(append_fv_val_less, bindex->filterVectors[i], bindex->length, new_data,
area_start_value(bindex->areas[i + 1]), n);
}
for (j = 0; j < THREAD_NUM && (k * THREAD_NUM + j) < (K - 1); j++) {
threads[j].join();
}
if (DEBUG_TIME_COUNT) timer.commonGetEndTime(6);
}
bindex->length += n;
free(idx);
if (DEBUG_TIME_COUNT) timer.commonGetEndTime(1);
}
char *bin_repr(BITS x) {
// Generate binary representation of a BITS variable
int len = BITSWIDTH + BITSWIDTH / 4 + 1;
char *result = (char *)malloc(sizeof(char) * len);
BITS ref;
int j = 0;
for (int i = 0; i < BITSWIDTH; i++) {
ref = 1 << (BITSWIDTH - i - 1);
result[i + j] = (ref & x) ? '1' : '0';
if ((i + 1) % 4 == 0) {
j++;
result[i + j] = '-';
}
}
result[len - 1] = '\0';
return result;
}
void display_bitmap(BITS *bitmap, int bitmap_len) {
for (int i = 0; i < bitmap_len; i++) {
printf("%s;", bin_repr(*(bitmap + i)));
}
}
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");
}
// memcpy(result, ref, n * sizeof(BITS));
// TODO: bitwise operation on large memory block?
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");
}
// memcpy(result, ref, n * sizeof(BITS));
// TODO: bitwise operation on large memory block?
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_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();
// }