-
Notifications
You must be signed in to change notification settings - Fork 26
/
range_search.cpp
387 lines (324 loc) · 11.1 KB
/
range_search.cpp
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
//
// Range search baselines
//
// Linear and binary search code derived from:
// https://dirtyhandscoding.wordpress.com/2017/08/25/performance-comparison-linear-search-vs-binary-search/
// https://www.pvk.ca/Blog/2012/07/03/binary-search-star-eliminates-star-branch-mispredictions/
// https://github.com/stgatilov/linear-vs-binary-search/blob/master/search.cpp
//
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <ctime>
#include <iostream>
#include <numeric>
#include <random>
#include <utility>
#include <immintrin.h>
#include <stx/btree_map.h>
#define BUF_SIZE 2048
#define QUERIES_PER_TRIAL (50 * 1000 * 1000)
#define SHUF(i0, i1, i2, i3) (i0 + i1*4 + i2*16 + i3*64)
#define FORCEINLINE __attribute__((always_inline)) inline
// power of 2 at most x, undefined for x == 0
FORCEINLINE uint32_t bsr(uint32_t x) {
return 31 - __builtin_clz(x);
}
static int binary_search_std (const int *arr, int n, int key) {
return (int) (std::lower_bound(arr, arr + n, key) - arr);
}
static int binary_search_simple(const int *arr, int n, int key) {
intptr_t left = -1;
intptr_t right = n;
while (right - left > 1) {
intptr_t middle = (left + right) >> 1;
if (arr[middle] < key)
left = middle;
else
right = middle;
}
return (int) right;
}
static int binary_search_branchless(const int *arr, int n, int key) {
intptr_t pos = -1;
intptr_t logstep = bsr(n - 1);
intptr_t step = intptr_t(1) << logstep;
pos = (arr[pos + n - step] < key ? pos + n - step : pos);
step >>= 1;
while (step > 0) {
pos = (arr[pos + step] < key ? pos + step : pos);
step >>= 1;
}
pos += 1;
return (int) (arr[pos] >= key ? pos : n);
}
static uint32_t interpolation_search( const int32_t* data, uint32_t n, int32_t key ) {
uint32_t low = 0;
uint32_t high = n-1;
uint32_t mid;
if ( key <= data[low] ) return low; /* in the first page */
uint32_t iters = 0;
while ( data[high] > data[low] and
data[high] > key and
data[low] < key ) {
iters += 1;
if ( iters > 1 ) return binary_search_branchless( data + low, high-low, key );
mid = low + (((long)key - (long)data[low]) * (double)(high - low) / ((long)data[high] - (long)data[low]));
if ( data[mid] < key ) {
low = mid + 1;
} else {
high = mid - 1;
}
}
if ( key <= data[low] ) return low;
if ( key <= data[high] ) return high;
return high + 1;
}
static int linear_search(const int *arr, int n, int key) {
intptr_t i = 0;
while (i < n) {
if (arr[i] >= key)
break;
++i;
}
return i;
}
static int linear_search_avx (const int *arr, int n, int key) {
__m256i vkey = _mm256_set1_epi32(key);
__m256i cnt = _mm256_setzero_si256();
for (int i = 0; i < n; i += 16) {
__m256i mask0 = _mm256_cmpgt_epi32(vkey, _mm256_load_si256((__m256i *)&arr[i+0]));
__m256i mask1 = _mm256_cmpgt_epi32(vkey, _mm256_load_si256((__m256i *)&arr[i+8]));
__m256i sum = _mm256_add_epi32(mask0, mask1);
cnt = _mm256_sub_epi32(cnt, sum);
}
__m128i xcnt = _mm_add_epi32(_mm256_extracti128_si256(cnt, 1), _mm256_castsi256_si128(cnt));
xcnt = _mm_add_epi32(xcnt, _mm_shuffle_epi32(xcnt, SHUF(2, 3, 0, 1)));
xcnt = _mm_add_epi32(xcnt, _mm_shuffle_epi32(xcnt, SHUF(1, 0, 3, 2)));
return _mm_cvtsi128_si32(xcnt);
}
class TwoLevelIndex {
const int* data_;
int k1_, k2_, k2stride_, page_size_;
int* tables_;
public:
explicit TwoLevelIndex(std::vector<int>& data, int k2, int page_size)
: k2_(k2), page_size_(page_size) {
int hang = data.size() % page_size;
if (hang > 0) {
for (int i = 0; i < page_size - hang; i++) {
data.push_back(INT_MAX);
}
}
data_ = &data[0];
k1_ = (int) std::ceil(data.size() / double(k2_ * page_size_));
std::cerr << "2-level index top level size = " << k1_ << std::endl;
tables_ = (int*) calloc((size_t) k1_ * (k2 + 1), sizeof(int));
k2stride_ = k2 * page_size_;
// populate second level index
for (int i = 0; i < k1_ * k2_; i++) {
tables_[k1_ + i] = ((i + 1) * page_size_ > data.size()) ? INT_MAX : data[(i + 1) * page_size_ - 1];
}
// populate top level index
for (int i = 0; i < k1_; i++) {
tables_[i] = tables_[k1_ + (i + 1) * k2 - 1];
}
}
~TwoLevelIndex() {
free(tables_);
}
// assumes that key is <= max(data)
int find(int key) {
int i = binary_search_branchless(tables_, k1_, key);
//int i = interpolation_search(tables_, k1_, key);
//int i = linear_search_avx(tables_, k1_, key);
//int j = binary_search_branchless(tables_ + k1_ + i * k2_, k2_, key);
int j = linear_search_avx(tables_ + k1_ + i * k2_, k2_, key);
int pos = i * k2stride_ + j * page_size_;
int offset = linear_search_avx(data_ + pos, page_size_, key);
return pos + offset;
}
};
class ThreeLevelIndex {
const int* data_;
int k1_, k2_, k3_, stride1_, stride2_, page_size_;
int* tables_;
int* table2_;
int* table3_;
public:
explicit ThreeLevelIndex(std::vector<int>& data, int k2, int k3, int page_size)
: k2_(k2), k3_(k3), page_size_(page_size) {
int hang = data.size() % page_size;
if (hang > 0) {
for (int i = 0; i < page_size - hang; i++) {
data.push_back(INT_MAX);
}
}
data_ = &data[0];
k1_ = (int) std::ceil(data.size() / double(k2_ * k3_ * page_size_));
std::cerr << "3-level index top level size = " << k1_ << std::endl;
tables_ = (int*) calloc((size_t) k1_ * (1 + k2_ * (1 + k3_)), sizeof(int));
table2_ = tables_ + k1_;
table3_ = table2_ + k1_ * k2_;
stride1_ = k2_ * k3_ * page_size_;
stride2_ = k3_ * page_size_;
// populate third level index
for (int i = 0; i < k1_ * k2_ * k3_; i++) {
table3_[i] = ((i + 1) * page_size_ > data.size()) ? INT_MAX : data[(i + 1) * page_size_ - 1];
}
// populate second level index
for (int i = 0; i < k1_ * k2_; i++) {
table2_[i] = table3_[(i + 1) * k3_ - 1];
}
// populate top level index
for (int i = 0; i < k1_; i++) {
tables_[i] = table2_[(i + 1) * k2_ - 1];
}
}
~ThreeLevelIndex() {
free(tables_);
}
// assumes that key is <= max(data)
int find(int key) {
int i = binary_search_branchless(tables_, k1_, key);
//int i = interpolation_search(tables_, k1_, key);
//int i = linear_search_avx(tables_, k1_, key);
int j = linear_search_avx(table2_ + i * k2_, k2_, key);
int k = linear_search_avx(table3_ + i * k2_ * k3_ + j * k3_, k3_, key);
int pos = i * stride1_ + j * stride2_ + k * page_size_;
int offset = linear_search_avx(data_ + pos, page_size_, key);
return pos + offset;
}
};
std::vector<int> read_data(const char *path) {
std::vector<int> vec;
FILE *fin = fopen(path, "rb");
int buf[BUF_SIZE];
while (true) {
size_t num_read = fread(buf, sizeof(int), BUF_SIZE, fin);
for (int i = 0; i < num_read; i++) {
vec.push_back(buf[i]);
}
if (num_read < BUF_SIZE) break;
}
fclose(fin);
return vec;
}
int main(int argc, char** argv) {
if (argc < 6) {
std::cerr << "Usage: " << argv[0] << " DATA_PATH TRIALS K1 K2 K3" << std::endl;
exit(1);
}
int num_trials = std::atoi(argv[2]);
int k1 = std::atoi(argv[3]); // first level size (keys per page)
int k2 = std::atoi(argv[4]); // second level size
int k3 = std::atoi(argv[5]); // third level size
printf("index page sizes\tk1: %d, k2: %d, k3: %d\n", k1, k2, k3);
std::vector<int> keys = read_data(argv[1]);
keys.push_back(INT_MAX);
int n = (int) keys.size();
printf("num elements: %d\n", n);
// Clone vec so we don't bring pages from it into cache when selecting random keys
std::vector<int> keys_clone(keys.begin(), keys.end());
// Create vector of values
std::vector<int> values;
for (int i = 0; i < n; i++) {
values.push_back(i);
}
// Construct B-Tree
std::vector<std::pair<int, int>> pairs;
for (int i = 0; i < n; i++) {
pairs.emplace_back(keys[i], values[i]);
}
stx::btree_map<int, int> btree(pairs.begin(), pairs.end());
// Construct indexes
TwoLevelIndex index2(keys, k1, k2);
ThreeLevelIndex index3(keys, k1, k2, k3);
uint32_t seed = std::random_device()();
std::mt19937 rng;
std::uniform_int_distribution<> dist(0, n - 1);
std::vector<int> queries(QUERIES_PER_TRIAL);
std::vector<double> times_bs; // binary search
std::vector<double> times_bt; // b-tree
std::vector<double> times_h2; // 2-level index
std::vector<double> times_h3; // 3-level index
// binary search baseline
printf("Running binary search\n");
rng.seed(seed);
long check_bs = 0;
for (int t = 0; t < num_trials; t++) {
for (int &query : queries) {
query = keys_clone[dist(rng)];
}
auto start = clock();
for (const int& key : queries) {
int pos = binary_search_branchless(keys.data(), n, key);
check_bs += values[pos];
}
double elapsed = double(clock() - start) / CLOCKS_PER_SEC;
times_bs.push_back(elapsed);
}
printf("binary search checksum = %ld\n", check_bs);
// stx::btree baseline
printf("Running stx::btree\n");
rng.seed(seed);
long check_bt = 0;
for (int t = 0; t < num_trials; t++) {
for (int &query : queries) {
query = keys_clone[dist(rng)];
}
auto start = clock();
for (const int& key : queries) {
check_bt += btree[key];
}
double elapsed = double(clock() - start) / CLOCKS_PER_SEC;
times_bt.push_back(elapsed);
}
printf("stx::btree checksum = %ld\n", check_bt);
// benchmark 2-level index
printf("Running 2-level index\n");
rng.seed(seed);
long check_h2 = 0;
for (int t = 0; t < num_trials; t++) {
for (int &query : queries) {
query = keys_clone[dist(rng)];
}
auto start = clock();
for (const int& key : queries) {
int pos = index2.find(key);
check_h2 += values[pos];
}
double elapsed = double(clock() - start) / CLOCKS_PER_SEC;
times_h2.push_back(elapsed);
}
printf("2-level index checksum = %ld\n", check_h2);
// benchmark 3-level index
printf("Running 3-level index\n");
rng.seed(seed);
long check_h3 = 0;
for (int t = 0; t < num_trials; t++) {
for (int &query : queries) {
query = keys_clone[dist(rng)];
}
auto start = clock();
for (const int& key : queries) {
int pos = index3.find(key);
check_h3 += values[pos];
}
double elapsed = double(clock() - start) / CLOCKS_PER_SEC;
times_h3.push_back(elapsed);
}
printf("3-level index checksum = %ld\n", check_h3);
double time_bs = 1e+9 * std::accumulate(times_bs.begin(), times_bs.end(), 0.) / (num_trials * QUERIES_PER_TRIAL);
double time_bt = 1e+9 * std::accumulate(times_bt.begin(), times_bt.end(), 0.) / (num_trials * QUERIES_PER_TRIAL);
double time_h2 = 1e+9 * std::accumulate(times_h2.begin(), times_h2.end(), 0.) / (num_trials * QUERIES_PER_TRIAL);
double time_h3 = 1e+9 * std::accumulate(times_h3.begin(), times_h3.end(), 0.) / (num_trials * QUERIES_PER_TRIAL);
printf("Mean time per query\n");
printf("%8.1lf ns : %.40s\n", time_bs, "binary search");
printf("%8.1lf ns : %.40s\n", time_bt, "stx::btree");
printf("%8.1lf ns : %.40s\n", time_h2, "2-level index");
printf("%8.1lf ns : %.40s\n", time_h3, "3-level index");
return 0;
}