-
Notifications
You must be signed in to change notification settings - Fork 4
/
thresh_compress.hpp
381 lines (327 loc) · 12.8 KB
/
thresh_compress.hpp
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
// Copyright (c) Lorenz Hübschle-Schneider
// All Rights Reserved. This source code is licensed under the Apache 2.0
// License (found in the LICENSE file in the root directory).
#pragma once
#include "config.hpp"
#include "permute.hpp"
#include "serialization.hpp"
#include <include/cuckoo_simple.h>
#include <memory>
#include <istream>
#include <ostream>
namespace ribbon {
template <typename Config>
class NormalThreshold : public Permuter<Config> {
public:
using Permuter_ = Permuter<Config>;
using Permuter_::Permuter;
IMPORT_RIBBON_CONFIG(Config);
void Prepare(Index, double) {
sLOGC(Config::log) << "Using uncompressed thresholds";
}
static constexpr Index NoBumpThresh() {
if constexpr (kSparseCoeffs) {
return (kBucketSize >> Permuter_::shift_) - 1;
} else {
return kBucketSize - 1;
}
}
inline Index Compress(Index intra_bucket) const {
if constexpr (kSparseCoeffs) {
// all intra_bucket values passed to this function are a multiple of
// Permuter_::step_ plus some offset
intra_bucket = (intra_bucket >> Permuter_::shift_);
}
return intra_bucket == 0 ? 0 : intra_bucket - 1;
}
void SerializeIntern(std::ostream &os) const {
(void)os;
}
void DeserializeIntern(std::istream &is, bool switchendian, Index num_buckets) {
(void)is;
(void)switchendian;
(void)num_buckets;
}
};
template <typename Config>
class TwoBitThreshold : public Permuter<Config> {
public:
using Permuter_ = Permuter<Config>;
using Permuter_::Permuter;
IMPORT_RIBBON_CONFIG(Config);
static constexpr Index NoBumpThresh() {
return 3;
}
void Prepare(Index num_slots, double slots_per_item) {
if (num_slots == 0) {
return;
}
const double eps = slots_per_item - 1.0;
// TODO so many magic values
if (kCoeffBits == 16) {
lower_ = static_cast<Index>((0.6 + eps / 2) * kBucketSize);
upper_ = static_cast<Index>((0.82 + eps / 2) * kBucketSize);
} else if (kCoeffBits == 32) {
lower_ = static_cast<Index>((0.7 + eps / 2) * kBucketSize);
upper_ = static_cast<Index>((0.87 + eps / 2) * kBucketSize);
} else {
lower_ = static_cast<Index>((0.78 + 1.30 * eps) * kBucketSize);
upper_ = static_cast<Index>((0.91 + 0.75 * eps) * kBucketSize);
}
sLOGC(Config::log) << "Using" << lower_ << "and" << upper_
<< "as thresholds";
}
inline Index Compress(Index intrabucket) const {
if (intrabucket >= kBucketSize) // no bumping
return 3;
else if (intrabucket > upper_) // some bumping
return 2;
else if (intrabucket > lower_) // lots of bumping
return 1;
else // everything bumped
return 0;
}
void SerializeIntern(std::ostream &os) const {
os.write(reinterpret_cast<const char *>(&lower_), sizeof(Index));
os.write(reinterpret_cast<const char *>(&upper_), sizeof(Index));
}
void DeserializeIntern(std::istream &is, bool switchendian, Index num_buckets) {
(void)num_buckets;
is.read(reinterpret_cast<char *>(&lower_), sizeof(Index));
if (switchendian && !bswap_generic(lower_))
throw parse_error("error converting endianness");
is.read(reinterpret_cast<char *>(&upper_), sizeof(Index));
if (switchendian && !bswap_generic(upper_))
throw parse_error("error converting endianness");
}
/*
inline Index GetCompressedIntraBucket(Index sortpos, Index gap) const {
return Compress(GetIntraBucket(sortpos, gap));
}
*/
protected:
Index lower_, upper_;
};
namespace {
// xxhash3 for cuckoo table
struct dysect_xxh3 {
static constexpr std::string_view name = "xxhash3";
static constexpr size_t significant_digits = 64;
dysect_xxh3() = default;
dysect_xxh3(size_t) {}
inline uint64_t operator()(const uint64_t k) const {
auto local = k;
return XXH3_64bits(&local, sizeof(local));
}
};
template <typename Index, bool log>
class OnePlusBase {
public:
using table_t =
dysect::cuckoo_standard<Index, Index, dysect_xxh3, dysect::cuckoo_config<4, 2>>;
// 32 results in approximately 20% set bits in filter
// 64 -> 35%
// 128 -> 60%
// 16 -> 10%
static constexpr Index granularity = 32, bin_thresh = 32;
// threshold before switching to the cuckoo table
// minimum table size is 1024, 5% slack -> 975 as threshold
static constexpr Index ht_thresh = 975;
static constexpr Index NoBumpThresh() {
return 1;
}
inline void Set(Index bucket, Index val) {
assert(val < thresh_);
buffer_.emplace_back(bucket, val);
// plus_.insert(bucket + 1, val);
// assert(plus_.find(bucket + 1) != plus_.end());
}
void Finalise(Index num_buckets) {
if (buffer_.size() > ht_thresh) {
plus_ = std::make_unique<table_t>(buffer_.size(), 1.03);
for (const auto [bucket, val] : buffer_) {
// Key 0 not allowed -> offset by 1
plus_->insert(bucket + 1, val);
}
} else {
std::sort(
buffer_.begin(), buffer_.end(),
[](const auto &x, const auto &y) { return x.first < y.first; });
buffer_.shrink_to_fit();
}
// Construct filter to probe before hitting hash table or binary search
// TODO threshold tuning?
if (buffer_.size() > bin_thresh) {
const Index filter_size = (num_buckets + granularity - 1) / granularity;
filter_.resize(filter_size);
for (const auto [bucket, _] : buffer_) {
filter_[bucket / granularity] = true;
}
size_t c = 0;
for (const bool set : filter_) {
c += set;
}
sLOGC(log) << "1+ Bit Threshold:" << c << "of" << filter_size
<< "bits set =" << c * 100.0 / filter_size << "% vs"
<< buffer_.size() << "of" << num_buckets
<< "buckets with thresholds ="
<< buffer_.size() * 100.0 / num_buckets
<< "%, buckets per filter bit:" << granularity;
}
// If we're using a hash table, deallocate the vector
if (plus_ != nullptr) {
buffer_ = std::vector<std::pair<Index, Index>>();
}
// hits = 0;
// count = 0;
}
size_t NumEntries() const {
return (plus_ == nullptr) ? buffer_.size() : plus_->size();
}
size_t Size() const {
size_t capacity = (plus_ == nullptr) ? buffer_.size() : plus_->capacity;
return capacity * sizeof(Index) * 2 + (filter_.size() + 7) / 8;
}
// unfortunately, this can't be const because there appears to be a bug in the
// implementation of const iterators in DySECT
void SerializeIntern(std::ostream &os) {
os.write(reinterpret_cast<const char *>(&thresh_), sizeof(Index));
std::size_t buffer_sz = NumEntries();
os.write(reinterpret_cast<const char *>(&buffer_sz), sizeof(std::size_t));
if (plus_ == nullptr) {
for (const auto& e : buffer_) {
os.write(reinterpret_cast<const char *>(&e.first), sizeof(Index));
os.write(reinterpret_cast<const char *>(&e.second), sizeof(Index));
}
} else {
for (auto it = plus_->begin(); it != plus_->end(); it++) {
// the keys are offset by one in Finalise, so one needs to be
// subtracted again here
Index key = it->first - 1;
os.write(reinterpret_cast<const char *>(&key), sizeof(Index));
os.write(reinterpret_cast<const char *>(&it->second), sizeof(Index));
}
}
}
void DeserializeIntern(std::istream &is, bool switchendian, Index num_buckets) {
is.read(reinterpret_cast<char *>(&thresh_), sizeof(Index));
if (switchendian && !bswap_generic(thresh_))
throw parse_error("error converting endianness");
std::size_t buffer_sz = 0;
is.read(reinterpret_cast<char *>(&buffer_sz), sizeof(std::size_t));
if (switchendian && !bswap_generic(buffer_sz))
throw parse_error("error converting endianness");
buffer_.reserve(buffer_sz);
Index key, value;
/* NOTE: This could technically be improved a bit by storing whether the hash table
or buffer was saved - if it was the original buffer, it doesn't need to be sorted
again */
for (std::size_t i = 0; i < buffer_sz; ++i) {
is.read(reinterpret_cast<char *>(&key), sizeof(Index));
// a call to bswap_type_supported is unnecessary because previous
// calls to bswap_generic already check that Index is supported
if (switchendian)
bswap_generic(key);
is.read(reinterpret_cast<char *>(&value), sizeof(Index));
if (switchendian)
bswap_generic(value);
buffer_.emplace_back(key, value);
}
Finalise(num_buckets);
}
protected:
inline Index Get(Index bucket, const Index kBucketSize) const {
// count++;
if (!filter_.empty() && !filter_[bucket / granularity]) {
// hits++;
return kBucketSize;
}
if (plus_ != nullptr) {
auto it = plus_->find(bucket + 1);
if (it != plus_->end())
return it->second;
} else if (buffer_.size() > bin_thresh) {
// binary search on buffer
auto it = std::lower_bound(
buffer_.begin(), buffer_.end(), bucket,
[](const auto &x, const auto &b) { return x.first < b; });
if (it != buffer_.end() && it->first == bucket) {
return it->second;
}
} else {
// linear search
auto it = buffer_.begin();
while (it != buffer_.end() && it->first < bucket)
it++;
if (it != buffer_.end() && it->first == bucket)
return it->second;
}
return kBucketSize;
}
void Prepare(Index kBucketSize, Index /* kCoeffBits */, double slots_per_item) {
// t = (1 + 2eps) B - c * stdev(bucket load)
// = (1 + 2eps) B - c' * sqrt(B/(1+eps))
// cap epsilon at 0 to prevent useless thresholds in base ribbon
const double eps = std::min(0.0, slots_per_item - 1.0);
slots_per_item = std::min(1.0, slots_per_item);
thresh_ = static_cast<Index>((1 + 2 * eps) * kBucketSize -
0.5 * sqrt(kBucketSize / slots_per_item));
sLOGC(log) << "1+ Bit Threshold: thresh =" << thresh_ << "="
<< 1 + 2 * eps << "*" << kBucketSize << "- 0.5 * sqrt("
<< kBucketSize / slots_per_item << "); B =" << kBucketSize;
}
Index thresh_;
std::vector<bool> filter_;
std::vector<std::pair<Index, Index>> buffer_;
std::unique_ptr<table_t> plus_ = nullptr;
};
} // namespace
template <typename Config>
class OnePlusBitThreshold
: public Permuter<Config>,
public OnePlusBase<typename Config::Index, Config::log> {
public:
using Permuter_ = Permuter<Config>;
using Base = OnePlusBase<typename Config::Index, Config::log>;
using Permuter_::Permuter;
IMPORT_RIBBON_CONFIG(Config);
void Prepare(Index /* num_slots */, double slots_per_item) {
Base::Prepare(kBucketSize, kCoeffBits, slots_per_item);
}
inline Index Compress(Index intrabucket) const {
if (intrabucket >= kBucketSize)
// no bumping
return 1;
else if (intrabucket >= Base::thresh_)
// some bumping
return 0;
else
// the "plus" case, store threshold externally
return 2;
}
inline Index Get(Index bucket) const {
return Base::Get(bucket, kBucketSize);
}
};
namespace {
template <ThreshMode mode, typename Config>
struct ThreshChooser;
template <typename Config>
struct ThreshChooser<ThreshMode::normal, Config> {
static_assert(Config::kThreshMode == ThreshMode::normal, "wtf");
using type = NormalThreshold<Config>;
};
template <typename Config>
struct ThreshChooser<ThreshMode::onebit, Config> {
static_assert(Config::kThreshMode == ThreshMode::onebit, "wtf");
using type = OnePlusBitThreshold<Config>;
};
template <typename Config>
struct ThreshChooser<ThreshMode::twobit, Config> {
static_assert(Config::kThreshMode == ThreshMode::twobit, "wtf");
using type = TwoBitThreshold<Config>;
};
} // namespace
template <typename Config>
using ChooseThreshold = typename ThreshChooser<Config::kThreshMode, Config>::type;
} // namespace ribbon