-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmonotonicity.cpp
281 lines (257 loc) · 9.71 KB
/
monotonicity.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
/*
* Copyright (c) 2023 Amos Brocco.
* Adapted from cpp-anchorhash Copyright (c) 2020 anchorhash (MIT License)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <boost/unordered/unordered_flat_map.hpp>
#include <boost/unordered_map.hpp>
#include <cxxopts.hpp>
#ifdef USE_PCG32
#include "pcg_random.hpp"
#include <random>
#endif
#include "anchor/anchorengine.h"
#include "jump/jumpengine.h"
#include "memento/mashtable.h"
#include "memento/mementoengine.h"
#include "power/powerengine.h"
#include <fmt/core.h>
#include <fstream>
#include <gtl/phmap.hpp>
#include <unordered_map>
/*
* Benchmark routine
*/
template <typename Algorithm>
int bench(const std::string_view name, const std::string &filename,
uint32_t anchor_set, uint32_t working_set, uint32_t num_removals,
uint32_t num_keys) {
Algorithm engine(anchor_set, working_set);
// random removals
uint32_t *bucket_status = new uint32_t[anchor_set]();
// all nodes are working
for (uint32_t i = 0; i < working_set; i++) {
bucket_status[i] = 1;
}
// simulate num_removals removals
uint32_t i = 0;
while (i < num_removals) {
#ifdef USE_PCG32
uint32_t removed = rng() % working_set;
#else
uint32_t removed = rand() % working_set;
#endif
if (bucket_status[removed] == 1) {
auto rnode = engine.removeBucket(removed);
bucket_status[rnode] = 0; // Remove the actually removed node
i++;
}
}
boost::unordered_flat_map<std::pair<uint32_t, uint32_t>, uint32_t> bucket;
std::ofstream results_file;
results_file.open(filename, std::ofstream::out | std::ofstream::app);
// Determine the current key bucket assigment
for (uint32_t i = 0; i < num_keys;) {
#ifdef USE_PCG32
auto a{rng()};
auto b{rng()};
#else
auto a{rand()};
auto b{rand()};
#endif
if (bucket.contains({a, b}))
continue;
auto target = engine.getBucketCRC32c(a, b);
bucket[{a, b}] = target;
// Verify that we got a working bucket
if (!bucket_status[target]) {
throw "Crazy bug";
}
++i;
}
fmt::println("Done determining initial assignment of {} unique keys",
num_keys);
// Remove a random working node
uint32_t removed{0};
uint32_t rnode{0};
for (;;) {
#ifdef USE_PCG32
removed = rng() % working_set;
#else
removed = rand() % working_set;
#endif
if (bucket_status[removed] == 1) {
rnode = engine.removeBucket(removed);
fmt::println("Removed node {}", rnode);
if (!bucket_status[rnode]) {
throw "Crazy bug";
}
bucket_status[rnode] = 0; // Remove the actually removed node
break;
}
}
uint32_t misplaced{0};
for (const auto &i : bucket) {
auto oldbucket = i.second;
auto a{i.first.first};
auto b{i.first.second};
auto newbucket = engine.getBucketCRC32c(a, b);
if (oldbucket != newbucket && (oldbucket != rnode)) {
fmt::println("(After Removal) Misplaced key {},{}: before in bucket {}, "
"now in bucket {} (status? old bucket {}, new bucket {})",
a, b, oldbucket, newbucket, bucket_status[oldbucket],
bucket_status[newbucket]);
++misplaced;
}
}
double m = (double)misplaced / (num_keys);
#ifdef USE_PCG32
fmt::println(
"{}: after removal % misplaced keys are {}% ({} keys out of {})\n", name,
m * 100, misplaced, num_keys);
results_file << name << ": "
<< "MisplacedRem: " << misplaced << "\t" << num_keys << "\t" << m
<< "\t" << m << "\tPCG32\n";
#else
fmt::println("{}: after removal misplaced keys are {}% ({} keys out of {})",
name, m * 100, misplaced, num_keys);
results_file << name << ": "
<< "MisplacedRem: " << misplaced << "\t" << num_keys << "\t" << m
<< "\t" << m << "\trand()\n";
#endif
misplaced = 0;
// Add back a node
auto anode = engine.addBucket();
bucket_status[anode] = 1;
fmt::println("Added node {}", anode);
for (const auto &i : bucket) {
auto oldbucket = i.second;
auto a{i.first.first};
auto b{i.first.second};
auto newbucket = engine.getBucketCRC32c(a, b);
if (oldbucket != newbucket) {
fmt::println("(After Add) Misplaced key {},{}: before in bucket {}, now "
"in bucket {} (status? old bucket {}, new bucket {})",
a, b, oldbucket, newbucket, bucket_status[oldbucket],
bucket_status[newbucket]);
++misplaced;
}
}
m = (double)misplaced / (num_keys);
#ifdef USE_PCG32
fmt::println(
"{}: after adding back % misplaced keys are {}% ({} keys out of {})\n",
name, m * 100, misplaced, num_keys);
results_file << name << ": "
<< "MisplacedAdd: " << misplaced << "\t" << num_keys << "\t" << m
<< "\t" << m << "\tPCG32\n";
#else
fmt::println(
"{}: after adding back misplaced keys are {}% ({} keys out of {})", name,
m * 100, misplaced, num_keys);
results_file << name << ": "
<< "MisplacedAdd: " << misplaced << "\t" << num_keys << "\t" << m
<< "\t" << m << "\trand()\n";
#endif
////////////////////////////////////////////////////////////////////
results_file.close();
delete[] bucket_status;
return 0;
}
int main(int argc, char *argv[]) {
cxxopts::Options options("speed_test", "MementoHash vs AnchorHash benchmark");
options.add_options()("Algorithm",
"Algorithm "
"(null|baseline|anchor|memento|mementoboost|"
"mementomash|mementostd|mementogtl|jump|power)",
cxxopts::value<std::string>())(
"AnchorSet", "Size of the AnchorSet (ignored by Memento)",
cxxopts::value<int>())("WorkingSet", "Size of the WorkingSet",
cxxopts::value<int>())(
"NumRemovals", "Number of random removals", cxxopts::value<int>())(
"NumKeys", "Number of keys to lookup for",
cxxopts::value<int>())("ResFileName", "Number of keys to lookup for",
cxxopts::value<std::string>());
options.positional_help(
"Algorithm AnchorSet WorkingSet NumRemovals Numkeys ResFilename");
options.parse_positional({"Algorithm", "AnchorSet", "WorkingSet",
"NumRemovals", "NumKeys", "ResFileName"});
auto result = options.parse(argc, argv);
if (argc != 7) {
fmt::println("{}", options.help());
exit(1);
}
auto algorithm = result["Algorithm"].as<std::string>();
auto anchor_set = static_cast<uint32_t>(result["AnchorSet"].as<int>());
auto working_set = static_cast<uint32_t>(result["WorkingSet"].as<int>());
auto num_removals = static_cast<uint32_t>(result["NumRemovals"].as<int>());
auto num_keys = static_cast<uint32_t>(result["NumKeys"].as<int>());
auto filename = result["ResFileName"].as<std::string>();
fmt::println("Algorithm: {}, AnchorSet: {}, WorkingSet: {}, NumRemovals: {}, "
"NumKeys: {}, ResFileName: {}",
algorithm, anchor_set, working_set, num_removals, num_keys,
filename);
srand(time(NULL));
if (algorithm == "null") {
// do nothing
} else if (algorithm == "baseline") {
fmt::println("Allocating {} buckets of size {} bytes...", anchor_set,
sizeof(uint32_t));
uint32_t *bucket_status = new uint32_t[anchor_set]();
for (uint32_t i = 0; i < working_set; i++) {
bucket_status[i] = 1;
}
uint32_t i = 0;
while (i < num_removals) {
uint32_t removed = rand() % working_set;
if (bucket_status[removed] == 1) {
bucket_status[removed] = 0;
i++;
}
}
delete[] bucket_status;
} else if (algorithm == "anchor") {
return bench<AnchorEngine>("Anchor", filename, anchor_set, working_set,
num_removals, num_keys);
} else if (algorithm == "memento") {
return bench<MementoEngine<boost::unordered_flat_map>>(
"Memento<boost::unordered_flat_map>", filename, anchor_set, working_set,
num_removals, num_keys);
} else if (algorithm == "mementoboost") {
return bench<MementoEngine<boost::unordered_map>>(
"Memento<boost::unordered_map>", filename, anchor_set, working_set,
num_removals, num_keys);
} else if (algorithm == "mementostd") {
return bench<MementoEngine<std::unordered_map>>(
"Memento<std::unordered_map>", filename, anchor_set, working_set,
num_removals, num_keys);
} else if (algorithm == "mementogtl") {
return bench<MementoEngine<gtl::flat_hash_map>>(
"Memento<std::gtl::flat_hash_map>", filename, anchor_set, working_set,
num_removals, num_keys);
} else if (algorithm == "mementomash") {
return bench<MementoEngine<MashTable>>("Memento<MashTable>", filename,
anchor_set, working_set,
num_removals, num_keys);
} else if (algorithm == "jump") {
return bench<JumpEngine>("JumpEngine", filename, anchor_set, working_set,
num_removals, num_keys);
} else if (algorithm == "power") {
return bench<PowerEngine>("PowerEngine", filename, anchor_set, working_set,
num_removals, num_keys);
} else {
fmt::println("Unknown algorithm {}", algorithm);
return 2;
}
}