forked from learnedsystems/SOSD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.h
448 lines (388 loc) · 14.2 KB
/
benchmark.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
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
#pragma once
#include <immintrin.h>
#include <math.h>
#include <algorithm>
#include <dtl/thread.hpp>
#include <fstream>
#include <iostream>
#include <sstream>
#include "config.h"
#include "searches/branching_binary_search.h"
#include "util.h"
#include "utils/perf_event.h"
#ifdef __linux__
#define checkLinux(x) (x)
#else
#define checkLinux(x) \
{ util::fail("Only supported on Linux."); }
#endif
// Get the CPU affinity for the process.
static const auto cpu_mask = dtl::this_thread::get_cpu_affinity();
// Batch size in number of lookups.
static constexpr std::size_t batch_size = 1u << 16;
namespace sosd {
// KeyType: Controls the type of the key (the value will always be uint64_t)
// Use uint64_t for 64 bit types and uint32_t for 32 bit types
// KeyType must implement operator<
template <typename KeyType = uint64_t,
template <typename> typename SearchClass = BranchingBinarySearch>
class Benchmark {
public:
Benchmark(const std::string& data_filename,
const std::string& lookups_filename, const size_t num_repeats,
const bool perf, const bool build, const bool fence,
const bool cold_cache, const bool track_errors, const bool csv,
const size_t num_threads, const SearchClass<KeyType> searcher)
: data_filename_(data_filename),
lookups_filename_(lookups_filename),
num_repeats_(num_repeats),
first_run_(true),
perf_(perf),
build_(build),
fence_(fence),
cold_cache_(cold_cache),
track_errors_(track_errors),
csv_(csv),
num_threads_(num_threads),
searcher_(searcher) {
if ((int)cold_cache + (int)perf + (int)fence > 1) {
util::fail(
"Can only specify one of cold cache, perf counters, or fence.");
}
static constexpr const char* prefix = "data/";
dataset_name_ = data_filename.data();
dataset_name_.erase(
dataset_name_.begin(),
dataset_name_.begin() + dataset_name_.find(prefix) + strlen(prefix));
// Load data.
std::vector<KeyType> keys = util::load_data<KeyType>(data_filename_);
log_sum_search_bound_ = 0.0;
l1_sum_search_bound_ = 0.0;
l2_sum_search_bound_ = 0.0;
if (!is_sorted(keys.begin(), keys.end()))
util::fail("keys have to be sorted");
// Check whether keys are unique.
unique_keys_ = util::is_unique(keys);
if (unique_keys_)
std::cout << "data is unique" << std::endl;
else
std::cout << "data contains duplicates" << std::endl;
// Add artificial values to keys.
data_ = util::add_values(keys);
// Load lookups.
lookups_ = util::load_data<EqualityLookup<KeyType>>(lookups_filename_);
// Create the data for the index (key -> position).
for (uint64_t pos = 0; pos < data_.size(); pos++) {
index_data_.push_back((KeyValue<KeyType>){data_[pos].key, pos});
}
if (cold_cache) {
memory_.resize(26e6 / 8); // NOTE: L3 size of the machine
util::FastRandom ranny(8128);
for (uint64_t& iter : memory_) {
iter = ranny.RandUint32();
}
}
}
template <class Index>
void Run() {
// Build index.
Index index;
if (!index.applicable(unique_keys_, data_filename_)) {
std::cout << "index " << index.name() << " is not applicable"
<< std::endl;
return;
}
build_ns_ = index.Build(index_data_);
// Do equality lookups.
if constexpr (!sosd_config::fast_mode) {
if (track_errors_) {
return DoLookupsWithErrorTracking(index);
}
if (perf_) {
checkLinux(({
BenchmarkParameters params;
params.setParam("index", index.name());
params.setParam("variant", index.variant());
PerfEventBlock e(lookups_.size(), params, /*printHeader=*/first_run_);
DoEqualityLookups<Index, false, false, false>(index);
}));
} else if (cold_cache_) {
if (num_threads_ > 1)
util::fail("cold cache not supported with multiple threads");
DoEqualityLookups<Index, true, false, true>(index);
PrintResult(index);
} else if (fence_) {
DoEqualityLookups<Index, false, true, false>(index);
PrintResult(index);
} else {
DoEqualityLookups<Index, false, false, false>(index);
PrintResult(index);
}
} else {
if (perf_ || cold_cache_ || fence_) {
util::fail(
"Perf, cold cache, and fence mode require full builds. Disable "
"fast mode.");
}
DoEqualityLookups<Index, false, false, false>(index);
PrintResult(index);
}
first_run_ = false;
}
bool uses_binary_search() const {
return (searcher_.name() == "BinarySearch") ||
(searcher_.name() == "BranchlessBinarySearch");
}
bool uses_lienar_search() const { return searcher_.name() == "LinearSearch"; }
private:
bool CheckResults(uint64_t actual, uint64_t expected, KeyType lookup_key,
SearchBound bound) {
if (actual != expected) {
const auto pos = std::find_if(
data_.begin(), data_.end(),
[lookup_key](const auto& kv) { return kv.key == lookup_key; });
const auto idx = std::distance(data_.begin(), pos);
std::cerr << "equality lookup returned wrong result:" << std::endl;
std::cerr << "lookup key: " << lookup_key << std::endl;
std::cerr << "actual: " << actual << ", expected: " << expected
<< std::endl
<< "correct index is: " << idx << std::endl
<< "index start: " << bound.start << " stop: " << bound.stop
<< std::endl;
return false;
}
return true;
}
template <class Index, bool time_each, bool fence, bool clear_cache>
void DoEqualityLookups(Index& index) {
if (build_) return;
// Atomic counter used to assign work to threads.
std::atomic<std::size_t> cntr(0);
bool run_failed = false;
if (clear_cache) std::cout << "rsum was: " << random_sum_ << std::endl;
runs_.resize(num_repeats_);
for (unsigned int i = 0; i < num_repeats_; ++i) {
random_sum_ = 0;
individual_ns_sum_ = 0;
uint64_t ms;
if (num_threads_ == 1) {
ms = util::timing([&] {
DoEqualityLookupsCoreLoop<Index, time_each, fence, clear_cache>(
index, 0, lookups_.size(), run_failed);
});
} else {
// Reset atomic counter.
cntr.store(0);
ms = util::timing([&] {
while (true) {
const size_t begin = cntr.fetch_add(batch_size);
if (begin >= lookups_.size()) break;
unsigned int limit = std::min(begin + batch_size, lookups_.size());
DoEqualityLookupsCoreLoop<Index, time_each, fence, clear_cache>(
index, begin, limit, run_failed);
}
});
}
runs_[i] = ms;
if (run_failed) {
runs_ = std::vector<uint64_t>(num_repeats_, 0);
return;
}
}
}
template <class Index, bool time_each, bool fence, bool clear_cache>
void DoEqualityLookupsCoreLoop(Index& index, unsigned int start,
unsigned int limit, bool& run_failed) {
SearchBound bound = {};
size_t qualifying;
uint64_t result;
typename std::vector<Row<KeyType>>::iterator iter;
for (unsigned int idx = start; idx < limit; ++idx) {
// Compute the actual index for debugging.
const volatile uint64_t lookup_key = lookups_[idx].key;
const volatile uint64_t expected = lookups_[idx].result;
if constexpr (clear_cache) {
// Make sure that all cache lines from large buffer are loaded
for (uint64_t& iter : memory_) {
random_sum_ += iter;
}
_mm_mfence();
const auto start = std::chrono::high_resolution_clock::now();
bound = index.EqualityLookup(lookup_key);
uint64_t actual = searcher_.search(data_, lookup_key, &qualifying,
bound.start, bound.stop);
if (!CheckResults(actual, expected, lookup_key, bound)) {
run_failed = true;
return;
}
const auto end = std::chrono::high_resolution_clock::now();
const auto timing =
std::chrono::duration_cast<std::chrono::nanoseconds>(end - start)
.count();
individual_ns_sum_ += timing;
} else {
// not tracking errors, measure the lookup time.
bound = index.EqualityLookup(lookup_key);
iter = std::lower_bound(
data_.begin() + bound.start, data_.begin() + bound.stop, lookup_key,
[](const Row<KeyType>& lhs, const KeyType lookup_key) {
return lhs.key < lookup_key;
});
result = 0;
while (iter != data_.end() && iter->key == lookup_key) {
result += iter->data[0];
++iter;
}
if (result != expected) {
run_failed = true;
return;
}
}
if constexpr (fence) __sync_synchronize();
}
}
template <class Index>
void DoLookupsWithErrorTracking(Index& index) {
assert(track_errors_);
if (num_threads_ > 1 || perf_ || cold_cache_ || fence_) {
util::fail(
"error tracking can not be used in combination with: num_threads_ > "
"1 || perf || cold_cache || fence");
}
SearchBound bound = {};
for (unsigned int idx = 0; idx < lookups_.size(); ++idx) {
const volatile uint64_t lookup_key = lookups_[idx].key;
bound = index.EqualityLookup(lookup_key);
if (bound.start != bound.stop) {
log_sum_search_bound_ += log2((double)(bound.stop - bound.start));
l1_sum_search_bound_ += abs((double)(bound.stop - bound.start));
l2_sum_search_bound_ += pow((double)(bound.stop - bound.start), 2);
}
}
log_sum_search_bound_ /= static_cast<double>(lookups_.size());
l1_sum_search_bound_ /= static_cast<double>(lookups_.size());
l2_sum_search_bound_ /= static_cast<double>(lookups_.size());
}
template <class Index>
void PrintResult(const Index& index) {
if (track_errors_) {
std::cout << "RESULT: " << index.name() << "," << index.variant() << ","
<< log_sum_search_bound_ << "," << l1_sum_search_bound_ << ","
<< l2_sum_search_bound_ << std::endl;
return;
}
if (build_) {
std::cout << "RESULT: " << index.name() << "," << index.variant() << ","
<< build_ns_ << "," << index.size() << std::endl;
return;
}
if (cold_cache_) {
const double ns_per = (static_cast<double>(individual_ns_sum_)) /
(static_cast<double>(lookups_.size()));
std::cout << "RESULT: " << index.name() << "," << index.variant() << ","
<< ns_per << "," << index.size() << "," << build_ns_ << ","
<< searcher_.name() << std::endl;
return;
}
// print main results
std::ostringstream all_times;
for (unsigned int i = 0; i < runs_.size(); ++i) {
const double ns_per_lookup =
static_cast<double>(runs_[i]) / lookups_.size();
all_times << "," << ns_per_lookup;
}
// don't print a line if (the first) run failed
if (runs_[0] != 0) {
std::cout << "RESULT: " << index.name() << "," << index.variant()
<< all_times.str() // has a leading comma
<< "," << index.size() << "," << build_ns_ << ","
<< searcher_.name() << std::endl;
}
if (csv_) {
PrintResultCSV(index);
}
}
template <class Index>
void PrintResultCSV(const Index& index) {
const std::string filename =
"./results/" + dataset_name_ + "_results_table.csv";
std::ofstream fout(filename, std::ofstream::out | std::ofstream::app);
if (!fout.is_open()) {
std::cerr << "Failure to print CSV on " << filename << std::endl;
return;
}
if (track_errors_) {
fout << index.name() << "," << index.variant() << ","
<< log_sum_search_bound_ << "," << l1_sum_search_bound_ << ","
<< l2_sum_search_bound_ << std::endl;
return;
}
if (build_) {
fout << index.name() << "," << index.variant() << "," << build_ns_ << ","
<< index.size() << std::endl;
return;
}
if (cold_cache_) {
const double ns_per = (static_cast<double>(individual_ns_sum_)) /
(static_cast<double>(lookups_.size()));
fout << index.name() << "," << index.variant() << "," << ns_per << ","
<< index.size() << "," << build_ns_ << "," << searcher_.name()
<< std::endl;
return;
}
// compute median time
std::vector<double> times;
double median_time;
for (unsigned int i = 0; i < runs_.size(); ++i) {
const double ns_per_lookup =
static_cast<double>(runs_[i]) / lookups_.size();
times.push_back(ns_per_lookup);
}
std::sort(times.begin(), times.end());
if (times.size() % 2 == 0) {
median_time =
0.5 * (times[times.size() / 2 - 1] + times[times.size() / 2]);
} else {
median_time = times[times.size() / 2];
}
// don't print a line if (the first) run failed
if (runs_[0] != 0) {
fout << index.name() << "," << index.variant() << "," << median_time
<< "," << index.size() << "," << build_ns_ << "," << searcher_.name()
<< "," << dataset_name_ << std::endl;
}
fout.close();
return;
}
uint64_t random_sum_ = 0;
uint64_t individual_ns_sum_ = 0;
const std::string data_filename_;
const std::string lookups_filename_;
std::string dataset_name_;
std::vector<Row<KeyType>> data_;
std::vector<KeyValue<KeyType>> index_data_;
bool unique_keys_;
std::vector<EqualityLookup<KeyType>> lookups_;
uint64_t build_ns_;
double log_sum_search_bound_;
double l1_sum_search_bound_;
double l2_sum_search_bound_;
// Run times.
std::vector<uint64_t> runs_;
// Number of times we repeat the lookup code.
size_t num_repeats_;
// Used to only print profiling header information for first run.
bool first_run_;
bool perf_;
bool build_;
bool fence_;
bool measure_each_;
bool cold_cache_;
bool track_errors_;
bool csv_;
// Number of lookup threads.
const size_t num_threads_;
std::vector<uint64_t> memory_; // Some memory we can read to flush the cache
SearchClass<KeyType> searcher_;
};
} // namespace sosd