-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathbench.cc
313 lines (259 loc) · 9.88 KB
/
bench.cc
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
#include <chrono>
#include <fstream>
#include <iostream>
#include <map>
#include "include/rs/multi_map.h"
using namespace std;
namespace rs_manual_tuning {
// Returns <num_radix_bits, max_error>
pair<uint64_t, uint64_t> GetTuning(const string& data_filename,
uint32_t size_scale) {
assert(size_scale >= 1 && size_scale <= 10);
string dataset = data_filename;
// Cut the prefix of the filename
size_t pos = dataset.find_last_of('/');
if (pos != string::npos) {
dataset.erase(dataset.begin(), dataset.begin() + pos + 1);
}
using Configs = const vector<pair<size_t, size_t>>;
if (dataset == "normal_200M_uint32") {
Configs configs = {{10, 6}, {15, 1}, {16, 1}, {18, 1}, {20, 1},
{21, 1}, {24, 1}, {25, 1}, {26, 1}, {26, 1}};
return configs[10 - size_scale];
}
if (dataset == "normal_200M_uint64") {
Configs configs = {{14, 2}, {16, 1}, {16, 1}, {20, 1}, {22, 1},
{24, 1}, {26, 1}, {26, 1}, {28, 1}, {28, 1}};
return configs[10 - size_scale];
}
if (dataset == "lognormal_200M_uint32") {
Configs configs = {{12, 20}, {16, 3}, {16, 2}, {18, 1}, {20, 1},
{22, 1}, {24, 1}, {24, 1}, {26, 1}, {28, 1}};
return configs[10 - size_scale];
}
if (dataset == "lognormal_200M_uint64") {
Configs configs = {{12, 3}, {18, 1}, {18, 1}, {20, 1}, {22, 1},
{24, 1}, {26, 1}, {26, 1}, {28, 1}, {28, 1}};
return configs[10 - size_scale];
}
if (dataset == "uniform_dense_200M_uint32") {
Configs configs = {{4, 2}, {16, 2}, {18, 1}, {20, 1}, {20, 1},
{22, 2}, {24, 1}, {26, 3}, {26, 3}, {28, 2}};
return configs[10 - size_scale];
}
if (dataset == "uniform_dense_200M_uint64") {
Configs configs = {{4, 2}, {16, 1}, {16, 1}, {20, 1}, {22, 1},
{24, 1}, {24, 1}, {26, 1}, {28, 1}, {28, 1}};
return configs[10 - size_scale];
}
if (dataset == "uniform_dense_200M_uint64") {
Configs configs = {{4, 2}, {16, 1}, {16, 1}, {20, 1}, {22, 1},
{24, 1}, {24, 1}, {26, 1}, {28, 1}, {28, 1}};
return configs[10 - size_scale];
}
if (dataset == "uniform_sparse_200M_uint32") {
Configs configs = {{12, 220}, {14, 100}, {14, 80}, {16, 30}, {18, 20},
{20, 10}, {20, 8}, {20, 5}, {24, 3}, {26, 1}};
return configs[10 - size_scale];
}
if (dataset == "uniform_sparse_200M_uint64") {
Configs configs = {{12, 150}, {14, 70}, {16, 50}, {18, 20}, {20, 20},
{20, 9}, {20, 5}, {24, 3}, {26, 2}, {28, 1}};
return configs[10 - size_scale];
}
// Books (or amazon in the paper)
if (dataset == "books_200M_uint32") {
Configs configs = {{14, 250}, {14, 250}, {16, 190}, {18, 80}, {18, 50},
{22, 20}, {22, 9}, {22, 8}, {24, 3}, {28, 2}};
return configs[10 - size_scale];
}
if (dataset == "books_200M_uint64") {
Configs configs = {{12, 380}, {16, 170}, {16, 110}, {20, 50}, {20, 30},
{22, 20}, {22, 10}, {24, 3}, {26, 3}, {28, 2}};
return configs[10 - size_scale];
}
if (dataset == "books_400M_uint64") {
Configs configs = {{16, 220}, {16, 220}, {18, 160}, {20, 60}, {20, 40},
{22, 20}, {22, 7}, {26, 3}, {28, 2}, {28, 1}};
return configs[10 - size_scale];
}
if (dataset == "books_600M_uint64") {
Configs configs = {{18, 330}, {18, 330}, {18, 190}, {20, 70}, {22, 50},
{22, 20}, {24, 7}, {26, 3}, {28, 2}, {28, 1}};
return configs[10 - size_scale];
}
if (dataset == "books_800M_uint64") {
Configs configs = {{18, 320}, {18, 320}, {18, 200}, {22, 80}, {22, 60},
{22, 20}, {24, 9}, {26, 3}, {28, 3}, {28, 3}};
return configs[10 - size_scale];
}
// Facebook
if (dataset == "fb_200M_uint64") {
Configs configs = {{8, 140}, {8, 140}, {8, 140}, {8, 140}, {10, 90},
{22, 90}, {24, 70}, {26, 80}, {26, 7}, {28, 80}};
return configs[10 - size_scale];
}
// OSM
if (dataset == "osm_cellids_200M_uint64") {
Configs configs = {{20, 160}, {20, 160}, {20, 160}, {20, 160}, {20, 80},
{24, 40}, {24, 20}, {26, 8}, {26, 3}, {28, 2}};
return configs[10 - size_scale];
}
if (dataset == "osm_cellids_400M_uint64") {
Configs configs = {{20, 190}, {20, 190}, {20, 190}, {20, 190}, {22, 80},
{24, 20}, {26, 20}, {26, 10}, {28, 6}, {28, 2}};
return configs[10 - size_scale];
}
if (dataset == "osm_cellids_600M_uint64") {
Configs configs = {{20, 190}, {20, 190}, {20, 190}, {22, 180}, {22, 100},
{24, 20}, {26, 20}, {28, 7}, {28, 5}, {28, 2}};
return configs[10 - size_scale];
}
if (dataset == "osm_cellids_800M_uint64") {
Configs configs = {{22, 190}, {22, 190}, {22, 190}, {22, 190}, {24, 190},
{26, 30}, {26, 20}, {28, 7}, {28, 5}, {28, 1}};
return configs[10 - size_scale];
}
// Wiki
if (dataset == "wiki_ts_200M_uint64") {
Configs configs = {{14, 100}, {14, 100}, {16, 60}, {18, 20}, {20, 20},
{20, 9}, {20, 5}, {22, 3}, {26, 2}, {26, 1}};
return configs[10 - size_scale];
}
cerr << "No tuning config for this dataset" << endl;
throw;
}
} // namespace rs_manual_tuning
namespace util {
// Loads values from binary file into vector.
template <typename T>
static vector<T> load_data(const string& filename, bool print = true) {
vector<T> data;
ifstream in(filename, ios::binary);
if (!in.is_open()) {
cerr << "unable to open " << filename << endl;
exit(EXIT_FAILURE);
}
// Read size.
uint64_t size;
in.read(reinterpret_cast<char*>(&size), sizeof(uint64_t));
data.resize(size);
// Read values.
in.read(reinterpret_cast<char*>(data.data()), size * sizeof(T));
return data;
}
// Generates deterministic values for keys.
template <class KeyType>
static vector<pair<KeyType, uint64_t>> add_values(const vector<KeyType>& keys) {
vector<pair<KeyType, uint64_t>> result;
result.reserve(keys.size());
for (uint64_t i = 0; i < keys.size(); ++i) {
pair<KeyType, uint64_t> row;
row.first = keys[i];
row.second = i;
result.push_back(row);
}
return result;
}
} // namespace util
namespace {
template <class KeyType, class ValueType>
class NonOwningMultiMap {
public:
using element_type = pair<KeyType, ValueType>;
NonOwningMultiMap(const vector<element_type>& elements,
size_t num_radix_bits = 18, size_t max_error = 32)
: data_(elements) {
assert(elements.size() > 0);
// Create spline builder.
const auto min_key = data_.front().first;
const auto max_key = data_.back().first;
rs::Builder<KeyType> rsb(min_key, max_key, num_radix_bits, max_error);
// Build the radix spline.
for (const auto& iter : data_) {
rsb.AddKey(iter.first);
}
rs_ = rsb.Finalize();
}
typename vector<element_type>::const_iterator lower_bound(KeyType key) const {
rs::SearchBound bound = rs_.GetSearchBound(key);
return ::lower_bound(data_.begin() + bound.begin, data_.begin() + bound.end,
key, [](const element_type& lhs, const KeyType& rhs) {
return lhs.first < rhs;
});
}
uint64_t sum_up(KeyType key) const {
uint64_t result = 0;
auto iter = lower_bound(key);
while (iter != data_.end() && iter->first == key) {
result += iter->second;
++iter;
}
return result;
}
size_t GetSizeInByte() const { return rs_.GetSize(); }
private:
const vector<element_type>& data_;
rs::RadixSpline<KeyType> rs_;
};
template <class KeyType>
struct Lookup {
KeyType key;
uint64_t value;
};
template <class KeyType>
void Run(const string& data_file, const string lookup_file) {
// Load data
vector<KeyType> keys = util::load_data<KeyType>(data_file);
vector<pair<KeyType, uint64_t>> elements = util::add_values(keys);
vector<Lookup<KeyType>> lookups =
util::load_data<Lookup<KeyType>>(lookup_file);
for (uint32_t size_config = 1; size_config <= 10; ++size_config) {
// Get the config for tuning
auto tuning = rs_manual_tuning::GetTuning(data_file, size_config);
// Build RS
auto build_begin = chrono::high_resolution_clock::now();
NonOwningMultiMap<KeyType, uint64_t> map(elements, tuning.first,
tuning.second);
auto build_end = chrono::high_resolution_clock::now();
uint64_t build_ns =
chrono::duration_cast<chrono::nanoseconds>(build_end - build_begin)
.count();
// Run queries
auto lookup_begin = chrono::high_resolution_clock::now();
for (const Lookup<KeyType>& lookup_iter : lookups) {
uint64_t sum = map.sum_up(lookup_iter.key);
if (sum != lookup_iter.value) {
cerr << "wrong result!" << endl;
throw "error";
}
}
auto lookup_end = chrono::high_resolution_clock::now();
uint64_t lookup_ns =
chrono::duration_cast<chrono::nanoseconds>(lookup_end - lookup_begin)
.count();
cout << "RESULT:"
<< " data_file: " << data_file << " lookup_file: " << lookup_file
<< " radix_bit_count: " << tuning.first
<< " spline_error: " << tuning.second
<< " size_config: " << size_config
<< " used_memory[MB]: " << (map.GetSizeInByte() / 1000) / 1000.0
<< " build_time[s]: " << (build_ns / 1000 / 1000) / 1000.0
<< " ns/lookup: " << lookup_ns / lookups.size() << endl;
}
}
} // namespace
int main(int argc, char** argv) {
if (argc != 3) {
cerr << "usage: " << argv[0] << " <data_file> <lookup_file>" << endl;
throw;
}
const string data_file = argv[1];
const string lookup_file = argv[2];
if (data_file.find("32") != string::npos) {
Run<uint32_t>(data_file, lookup_file);
} else {
Run<uint64_t>(data_file, lookup_file);
}
return 0;
}