-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.h
344 lines (251 loc) · 10.1 KB
/
helper.h
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
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <time.h>
#include <stdlib.h>
#include <x86intrin.h>
#define WARMUP_QUERIES 100000000
#define VERBOSE 0
#define MARK_SIZE 16
#define SUPERBLOCK_SIZE 63488
#define HALF_SUPERBLOCK_SIZE 31744
#define MARK_MASK 0xffff000000000000lu
#define BIT_MASK ~MARK_MASK
typedef uint64_t (*FUNC_PTR)(uint64_t);
typedef struct {
uint64_t num_elements;
uint64_t num_ones;
uint64_t nbits;
uint64_t l0_size;
double float_ratio;
} bit_meta;
uint64_t* modified_bit_array;
uint64_t*l0_a;
uint16_t* ll_select_array;
uint64_t* hl_select_array;
uint64_t log_hl_select_width;
uint64_t ones_per_slot;
uint64_t log_ones_per_slot;
int free_metadata() {
free(modified_bit_array);
free(hl_select_array);
free(ll_select_array);
free(l0_a);
return 0;
}
int help() {
printf("Usage: ./spider <bitfile> <numbits> <r/s>\n");
exit(-1);
return 0;
}
static inline uint64_t pdep_select64(uint64_t x, unsigned n) {
return _lzcnt_u64(_pdep_u64(1lu << (__builtin_popcountll(x) - n), x));
}
//SECTION - build rank
bit_meta read_and_build_rank(char* filename, uint64_t file_size, int log_sma_bits) {
uint64_t size = 0;
FILE* byte_file;
byte_file = fopen (filename, "rb");
if (byte_file == NULL) {
perror("Couldn't open file");
}
if ((int64_t)file_size == -1) {
fseek(byte_file, 0L, SEEK_END);
size = ftell(byte_file) * 8; // bytes to bits
rewind(byte_file);
} else {
size = file_size;
}
uint64_t array_length = ((size -1) / 62 + 1);
modified_bit_array = (uint64_t*)aligned_alloc(512, (array_length + 8) * sizeof(uint64_t));
uint64_t num_elements = 0;
uint64_t block;
unsigned char byte;
uint64_t total_count = 0;
uint64_t relative_count = 0;
uint64_t l0_i = 0;
uint64_t num_bits_read = 0;
l0_a = (uint64_t*)aligned_alloc(512, sizeof(uint64_t) * (1 + 1 + ((size - 1) / SUPERBLOCK_SIZE)));
for (uint64_t j = 0; j < (((size - 1) / 62) + 1); j++) { // 62 = ((x / 64) * 512) / 496
block = 0;
int max = 8;
int adjust = 0;
if (j % 1024 == 0) {
l0_a[l0_i] = total_count;
l0_i++;
relative_count = 0;
}
// each byte makes up a 8 bit section of the block
if (j % 8 == 0) { // 8 = 512 / 64
block |= relative_count;
max = 6;
adjust = __builtin_popcountll(block);
}
for (int i = 0; i < max; i++) {
block = block << 8;
byte = fgetc(byte_file);
num_bits_read += 8;
if (num_bits_read <= size) {
block |= byte;
}
}
total_count += __builtin_popcountll(block) - adjust;
relative_count += __builtin_popcountll(block) - adjust;
modified_bit_array[num_elements] = block;
num_elements++;
}
// needs to be 8 because we look 8 in the future
for (int i = num_elements; i < num_elements + 8; i++) {
modified_bit_array[i] = -1;
}
num_elements += 8;
bit_meta metadata_to_send = {};
uint64_t num_bits = size;
metadata_to_send.float_ratio = ((double)(num_bits) / (double)total_count) / (double)SUPERBLOCK_SIZE;
log_hl_select_width = (64 - __builtin_clzll((uint64_t)(1 / metadata_to_send.float_ratio)));
int base_log_ones_per_slot = 8 + log_sma_bits;
uint64_t base_ones_per_slot = 1 << base_log_ones_per_slot;
double sparcity = ((double)total_count / (double)(num_bits)) * 0.99;
uint64_t rough_ones_per_slot = (uint64_t)(base_ones_per_slot * sparcity);
log_ones_per_slot = (64 - __builtin_clzll(rough_ones_per_slot));
ones_per_slot = 1lu << log_ones_per_slot;
l0_a[l0_i] = -1;
metadata_to_send.l0_size = l0_i;
metadata_to_send.num_ones = total_count;
// this is number of 64 bit uints blocks
metadata_to_send.num_elements = num_elements;
metadata_to_send.nbits = size;
return metadata_to_send;
}
//!SECTION
//SECTION - BUILD SELECT
int build_select_from_modified(bit_meta data) {
assert(data.num_ones > 0);
uint64_t hl_length = (((data.num_ones - 1) >> log_hl_select_width) + 1) + 1; // plus one is for the dummy
uint64_t ll_length = (((data.num_ones - 1) / ones_per_slot) + 1) + 1;
hl_select_array = (uint64_t*)aligned_alloc(512, hl_length * sizeof(uint64_t));
ll_select_array = (uint16_t *)aligned_alloc(512, ll_length * sizeof(uint16_t));
uint64_t hl_position = 0;
uint64_t ll_position = 0;
uint64_t hl_sma_index = 0;
uint64_t ll_sma_index = 0;
uint64_t ll_ones_in_sma_block = 0;
uint64_t hl_ones_in_sma_block = 0;
uint64_t first_one_found = 0;
uint64_t next = 0;
for (uint64_t i = 0; i < data.num_elements; i++) {
uint64_t curr_uint = modified_bit_array[i];
uint64_t update = 64;
if ((i % 8) == 0) {
// this is a modified block
curr_uint <<= 16;
update = 48;
}
if (i % ((1024)) == 0) {
// allocate space for the next sma and zero out the position indicator
ll_position = 0;
}
if (!first_one_found) {
if (!curr_uint) { // all 0s
hl_position += update;
ll_position += update;
continue;
} else {
// find the position of the first one in the sma array and save its position
uint16_t block_select = pdep_select64(curr_uint, 1);
hl_select_array[hl_sma_index] = (hl_position + block_select + HALF_SUPERBLOCK_SIZE) / SUPERBLOCK_SIZE;
ll_select_array[ll_sma_index] = ll_position + block_select;
hl_sma_index++;
ll_sma_index++;
// counting the rest of ones in the block, and updating the
// position counter to reflect that block i has been counted
// and setting the first_one_found flag to true
ll_ones_in_sma_block += __builtin_popcountll(curr_uint) - 1;
hl_ones_in_sma_block += __builtin_popcountll(curr_uint) - 1;
hl_position += update;
ll_position += update;
first_one_found = 1;
continue;
}
}
next = __builtin_popcountll(curr_uint);
if (next + hl_ones_in_sma_block < (1lu << log_hl_select_width)) {
hl_ones_in_sma_block += next;
} else {
uint64_t hl_ones_left = (1lu << log_hl_select_width) - hl_ones_in_sma_block;
uint64_t hl_block_select = pdep_select64(curr_uint, hl_ones_left);
hl_select_array[hl_sma_index] = (hl_block_select + hl_position + HALF_SUPERBLOCK_SIZE) / SUPERBLOCK_SIZE;
hl_sma_index++;
hl_ones_in_sma_block = next - hl_ones_left;
}
if (next + ll_ones_in_sma_block < ones_per_slot) {
ll_ones_in_sma_block += next;
} else {
uint16_t ones_left = ones_per_slot - ll_ones_in_sma_block;
uint16_t block_select = pdep_select64(curr_uint, ones_left);
ll_select_array[ll_sma_index] = block_select + ll_position;
ll_sma_index++;
ll_ones_in_sma_block = next - ones_left;
}
hl_position += update;
ll_position += update;
}
hl_select_array[hl_sma_index] = data.l0_size - 1;
uint16_t ll_dummy = ll_select_array[ll_sma_index - 1];
ll_select_array[ll_sma_index] = data.nbits % SUPERBLOCK_SIZE;
return (hl_sma_index * 64) + (ll_sma_index * 16);
}
//!SECTION
//SECTION - BUILD 1L Select
uint64_t* select_array_1L;
int build_1L_select_from_modified(bit_meta data) {
assert(data.num_ones > 0);
uint64_t length = (((data.num_ones - 1) / ones_per_slot) + 1) + 1; // plus one is for the dummy
select_array_1L = (uint64_t*)aligned_alloc(512, length * sizeof(uint64_t));
uint64_t position = 0;
uint64_t sma_index = 0;
uint64_t ones_in_sma_block = 0;
uint64_t first_one_found = 0;
uint64_t next = 0;
for (uint64_t i = 0; i < data.num_elements; i++) {
uint64_t curr_uint = modified_bit_array[i];
uint64_t update = 64;
if ((i % 8) == 0) {
// this is a modified thing
curr_uint <<= 16;
update = 48;
}
if (!first_one_found) {
if (!curr_uint) { // all 0s
position += update;
continue;
} else {
// find the position of the first one in the sma array and save its position
uint16_t block_select = pdep_select64(curr_uint, 1);
select_array_1L[sma_index] = position + block_select;
sma_index++;
// counting the rest of ones in the block, and updating the
// position counter to reflect that block i has been counted
// and setting the first_one_found flag to true
ones_in_sma_block += __builtin_popcountll(curr_uint) - 1;
position += update;
first_one_found = 1;
continue;
}
}
next = __builtin_popcountll(curr_uint);
if (next + ones_in_sma_block < ones_per_slot) {
ones_in_sma_block += next;
} else {
uint16_t ones_left = ones_per_slot - ones_in_sma_block;
uint16_t block_select = pdep_select64(curr_uint, ones_left);
select_array_1L[sma_index] = block_select + position;
sma_index++;
ones_in_sma_block = next - ones_left;
}
position += update;
}
select_array_1L[sma_index] = data.nbits;
return ((sma_index + 1) * 64);
}
//!SECTION