-
Notifications
You must be signed in to change notification settings - Fork 114
/
alex_base.h
397 lines (338 loc) · 10.8 KB
/
alex_base.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
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
/* This file contains the classes for linear models and model builders, helpers
* for the bitmap,
* cost model weights, statistic accumulators for collecting cost model
* statistics,
* and other miscellaneous functions
*/
#pragma once
#include <algorithm>
#include <array>
#include <chrono>
#include <cstring>
#include <iostream>
#include <limits>
#include <memory>
#include <random>
#include <set>
#include <string>
#include <utility>
#include <vector>
#ifdef _MSC_VER
#include <intrin.h>
#else
#include <x86intrin.h>
#endif
#include <bitset>
#include <cassert>
#ifdef _WIN32
#include <intrin.h>
#include <limits.h>
typedef unsigned __int32 uint32_t;
#else
#include <stdint.h>
#endif
#ifdef _MSC_VER
#define forceinline __forceinline
#elif defined(__GNUC__)
#define forceinline inline __attribute__((__always_inline__))
#elif defined(__CLANG__)
#if __has_attribute(__always_inline__)
#define forceinline inline __attribute__((__always_inline__))
#else
#define forceinline inline
#endif
#else
#define forceinline inline
#endif
namespace alex {
/*** Linear model and model builder ***/
// Forward declaration
template <class T>
class LinearModelBuilder;
// Linear regression model
template <class T>
class LinearModel {
public:
double a_ = 0; // slope
double b_ = 0; // intercept
LinearModel() = default;
LinearModel(double a, double b) : a_(a), b_(b) {}
explicit LinearModel(const LinearModel& other) : a_(other.a_), b_(other.b_) {}
void expand(double expansion_factor) {
a_ *= expansion_factor;
b_ *= expansion_factor;
}
inline int predict(T key) const {
return static_cast<int>(a_ * static_cast<double>(key) + b_);
}
inline double predict_double(T key) const {
return a_ * static_cast<double>(key) + b_;
}
};
template <class T>
class LinearModelBuilder {
public:
LinearModel<T>* model_;
explicit LinearModelBuilder<T>(LinearModel<T>* model) : model_(model) {}
inline void add(T x, int y) {
count_++;
x_sum_ += static_cast<long double>(x);
y_sum_ += static_cast<long double>(y);
xx_sum_ += static_cast<long double>(x) * x;
xy_sum_ += static_cast<long double>(x) * y;
x_min_ = std::min<T>(x, x_min_);
x_max_ = std::max<T>(x, x_max_);
y_min_ = std::min<double>(y, y_min_);
y_max_ = std::max<double>(y, y_max_);
}
void build() {
if (count_ <= 1) {
model_->a_ = 0;
model_->b_ = static_cast<double>(y_sum_);
return;
}
if (static_cast<long double>(count_) * xx_sum_ - x_sum_ * x_sum_ == 0) {
// all values in a bucket have the same key.
model_->a_ = 0;
model_->b_ = static_cast<double>(y_sum_) / count_;
return;
}
auto slope = static_cast<double>(
(static_cast<long double>(count_) * xy_sum_ - x_sum_ * y_sum_) /
(static_cast<long double>(count_) * xx_sum_ - x_sum_ * x_sum_));
auto intercept = static_cast<double>(
(y_sum_ - static_cast<long double>(slope) * x_sum_) / count_);
model_->a_ = slope;
model_->b_ = intercept;
// If floating point precision errors, fit spline
if (model_->a_ <= 0) {
if (x_max_ - x_min_ == 0){
model_->a_ = 0;
model_->b_ = static_cast<double>(y_sum_) / count_;
}
else{
model_->a_ = (y_max_ - y_min_) / (x_max_ - x_min_);
model_->b_ = -static_cast<double>(x_min_) * model_->a_;
}
}
}
private:
int count_ = 0;
long double x_sum_ = 0;
long double y_sum_ = 0;
long double xx_sum_ = 0;
long double xy_sum_ = 0;
T x_min_ = std::numeric_limits<T>::max();
T x_max_ = std::numeric_limits<T>::lowest();
double y_min_ = std::numeric_limits<double>::max();
double y_max_ = std::numeric_limits<double>::lowest();
};
/*** Comparison ***/
struct AlexCompare {
template <class T1, class T2>
bool operator()(const T1& x, const T2& y) const {
static_assert(
std::is_arithmetic<T1>::value && std::is_arithmetic<T2>::value,
"Comparison types must be numeric.");
return x < y;
}
};
/*** Helper methods for bitmap ***/
// Extract the rightmost 1 in the binary representation.
// e.g. extract_rightmost_one(010100100) = 000000100
inline uint64_t extract_rightmost_one(uint64_t value) {
return value & -static_cast<int64_t>(value);
}
// Remove the rightmost 1 in the binary representation.
// e.g. remove_rightmost_one(010100100) = 010100000
inline uint64_t remove_rightmost_one(uint64_t value) {
return value & (value - 1);
}
// Count the number of 1s in the binary representation.
// e.g. count_ones(010100100) = 3
inline int count_ones(uint64_t value) {
return static_cast<int>(_mm_popcnt_u64(value));
}
// Get the offset of a bit in a bitmap.
// word_id is the word id of the bit in a bitmap
// bit is the word that contains the bit
inline int get_offset(int word_id, uint64_t bit) {
return (word_id << 6) + count_ones(bit - 1);
}
/*** Cost model weights ***/
// Intra-node cost weights
constexpr double kExpSearchIterationsWeight = 20;
constexpr double kShiftsWeight = 0.5;
// TraverseToLeaf cost weights
constexpr double kNodeLookupsWeight = 20;
constexpr double kModelSizeWeight = 5e-7;
/*** Stat Accumulators ***/
struct DataNodeStats {
double num_search_iterations = 0;
double num_shifts = 0;
};
// Used when stats are computed using a sample
struct SampleDataNodeStats {
double log2_sample_size = 0;
double num_search_iterations = 0;
double log2_num_shifts = 0;
};
// Accumulates stats that are used in the cost model, based on the actual vs
// predicted position of a key
class StatAccumulator {
public:
virtual ~StatAccumulator() = default;
virtual void accumulate(int actual_position, int predicted_position) = 0;
virtual double get_stat() = 0;
virtual void reset() = 0;
};
// Mean log error represents the expected number of exponential search
// iterations when doing a lookup
class ExpectedSearchIterationsAccumulator : public StatAccumulator {
public:
void accumulate(int actual_position, int predicted_position) override {
cumulative_log_error_ +=
std::log2(std::abs(predicted_position - actual_position) + 1);
count_++;
}
double get_stat() override {
if (count_ == 0) return 0;
return cumulative_log_error_ / count_;
}
void reset() override {
cumulative_log_error_ = 0;
count_ = 0;
}
public:
double cumulative_log_error_ = 0;
int count_ = 0;
};
// Mean shifts represents the expected number of shifts when doing an insert
class ExpectedShiftsAccumulator : public StatAccumulator {
public:
explicit ExpectedShiftsAccumulator(int data_capacity)
: data_capacity_(data_capacity) {}
// A dense region of n keys will contribute a total number of expected shifts
// of approximately
// ((n-1)/2)((n-1)/2 + 1) = n^2/4 - 1/4
// This is exact for odd n and off by 0.25 for even n.
// Therefore, we track n^2/4.
void accumulate(int actual_position, int) override {
if (actual_position > last_position_ + 1) {
long long dense_region_length = last_position_ - dense_region_start_idx_ + 1;
num_expected_shifts_ += (dense_region_length * dense_region_length) / 4;
dense_region_start_idx_ = actual_position;
}
last_position_ = actual_position;
count_++;
}
double get_stat() override {
if (count_ == 0) return 0;
// first need to accumulate statistics for current packed region
long long dense_region_length = last_position_ - dense_region_start_idx_ + 1;
long long cur_num_expected_shifts =
num_expected_shifts_ + (dense_region_length * dense_region_length) / 4;
return cur_num_expected_shifts / static_cast<double>(count_);
}
void reset() override {
last_position_ = -1;
dense_region_start_idx_ = 0;
num_expected_shifts_ = 0;
count_ = 0;
}
public:
int last_position_ = -1;
int dense_region_start_idx_ = 0;
long long num_expected_shifts_ = 0;
int count_ = 0;
int data_capacity_ = -1; // capacity of node
};
// Combines ExpectedSearchIterationsAccumulator and ExpectedShiftsAccumulator
class ExpectedIterationsAndShiftsAccumulator : public StatAccumulator {
public:
ExpectedIterationsAndShiftsAccumulator() = default;
explicit ExpectedIterationsAndShiftsAccumulator(int data_capacity)
: data_capacity_(data_capacity) {}
void accumulate(int actual_position, int predicted_position) override {
cumulative_log_error_ +=
std::log2(std::abs(predicted_position - actual_position) + 1);
if (actual_position > last_position_ + 1) {
long long dense_region_length = last_position_ - dense_region_start_idx_ + 1;
num_expected_shifts_ += (dense_region_length * dense_region_length) / 4;
dense_region_start_idx_ = actual_position;
}
last_position_ = actual_position;
count_++;
}
double get_stat() override {
assert(false); // this should not be used
return 0;
}
double get_expected_num_search_iterations() {
if (count_ == 0) return 0;
return cumulative_log_error_ / count_;
}
double get_expected_num_shifts() {
if (count_ == 0) return 0;
long long dense_region_length = last_position_ - dense_region_start_idx_ + 1;
long long cur_num_expected_shifts =
num_expected_shifts_ + (dense_region_length * dense_region_length) / 4;
return cur_num_expected_shifts / static_cast<double>(count_);
}
void reset() override {
cumulative_log_error_ = 0;
last_position_ = -1;
dense_region_start_idx_ = 0;
num_expected_shifts_ = 0;
count_ = 0;
}
public:
double cumulative_log_error_ = 0;
int last_position_ = -1;
int dense_region_start_idx_ = 0;
long long num_expected_shifts_ = 0;
int count_ = 0;
int data_capacity_ = -1; // capacity of node
};
/*** Miscellaneous helpers ***/
// https://stackoverflow.com/questions/364985/algorithm-for-finding-the-smallest-power-of-two-thats-greater-or-equal-to-a-giv
inline int pow_2_round_up(int x) {
--x;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x + 1;
}
// https://stackoverflow.com/questions/994593/how-to-do-an-integer-log2-in-c
inline int log_2_round_down(int x) {
int res = 0;
while (x >>= 1) ++res;
return res;
}
// https://stackoverflow.com/questions/1666093/cpuid-implementations-in-c
class CPUID {
uint32_t regs[4];
public:
explicit CPUID(unsigned i, unsigned j) {
#ifdef _WIN32
__cpuidex((int*)regs, (int)i, (int)j);
#else
asm volatile("cpuid"
: "=a"(regs[0]), "=b"(regs[1]), "=c"(regs[2]), "=d"(regs[3])
: "a"(i), "c"(j));
#endif
}
const uint32_t& EAX() const { return regs[0]; }
const uint32_t& EBX() const { return regs[1]; }
const uint32_t& ECX() const { return regs[2]; }
const uint32_t& EDX() const { return regs[3]; }
};
// https://en.wikipedia.org/wiki/CPUID#EAX=7,_ECX=0:_Extended_Features
inline bool cpu_supports_bmi() {
return static_cast<bool>(CPUID(7, 0).EBX() & (1 << 3));
}
}