-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcomp_comp.cpp
More file actions
260 lines (234 loc) · 8.07 KB
/
comp_comp.cpp
File metadata and controls
260 lines (234 loc) · 8.07 KB
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
#include <sys/resource.h>
#include <cassert>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <random>
#include <cmath>
#include "bit_vector/bv.hpp"
#include "bit_vector/internal/deb.hpp"
template <class bva, class bvb>
void check(bva* a, bvb* b, uint64_t s_offset = 0) {
uint64_t size = a->size();
assert(size == b->size());
for (uint64_t i = 0; i < size; i++) {
if (a->at(i) != b->at(i)) {
std::cerr << "a->at(" << i << ") = " << a->at(i) << ", b->at(" << i
<< ") = " << b->at(i) << std::endl;
assert(a->at(i) == b->at(i));
}
assert(a->rank(i) == b->rank(i));
}
uint64_t ones = a->rank(size);
assert(b->rank(size) == ones);
for (uint64_t i = 0; i < ones; i++) {
uint64_t ex = b->select(i + s_offset);
uint64_t ac = a->select(i + 1);
if (ex != ac) {
std::cerr << "select(" << i + 1 << ") should be " << ex
<< ", was " << ac << std::endl;
}
assert(ac == ex);
}
}
bool get_val(uint64_t i, uint64_t lim, uint64_t val) {
double x = ((20.0 * i) / lim) - 10.0;
return val < atan(x) / M_PI_2;
}
template <class bit_vector>
void test(uint64_t size, uint64_t steps, uint64_t seed) {
uint64_t threshold = 69183097;
bit_vector bv;
bv::bv control;
std::mt19937 mt(seed);
std::uniform_int_distribution<unsigned long long> gen(
std::numeric_limits<std::uint64_t>::min(),
std::numeric_limits<std::uint64_t>::max());
std::uniform_real_distribution dist(-1.0, 1.0);
using std::chrono::duration_cast;
using std::chrono::high_resolution_clock;
using std::chrono::microseconds;
uint64_t start_size = 1000000;
double startexp = log2(double(start_size));
double delta = (log2(double(size)) - log2(double(start_size))) / steps;
uint64_t ops = 100000;
std::cerr << "startexp: " << startexp << ". delta: " << delta << std::endl;
std::vector<uint64_t> loc, val;
for (uint64_t i = 0; i < 900000; i++) {
uint64_t aloc = gen(mt) % (i + 1);
bool aval = get_val(aloc, i + 1, dist(mt));
bv.insert(aloc, aval);
control.insert(aloc, aval);
}
for (uint64_t step = 1; step <= steps; step++) {
uint64_t start = bv.size();
uint64_t target = uint64_t(pow(2.0, startexp + delta * step));
std::cerr << "sizing to " << target;
for (size_t i = start; i < target; i++) {
uint64_t aloc = gen(mt) % (i + 1);
bool aval = get_val(aloc, i + 1, dist(mt));
if (i == 69133053) {
bv::do_debug = true;
//bv.print();
std::cout << "\nInserting " << aval << " to position " << aloc << std::endl;
}
bv.insert(aloc, aval);
if (bv::do_debug) {
std::cout << "===============================================================" << std::endl;
}
control.insert(aloc, aval);
if (i == 69133053) {
//bv.print();
bv::do_debug = false;
std::cout << "Validating" << std::endl;
bv.validate();
check(&bv, &control, 1);
}
//if (i > 69133050) {
// std::cerr << "i = " << i << std::endl;
// check(&bv, &control, 1);
//}
}
std::cerr << " ... ";
if (target >= threshold) {
check(&bv, &control, 1);
}
std::cerr << "OK" << std::endl;
loc.clear();
for (uint64_t i = target; i > target - ops; i--) {
loc.push_back(gen(mt) % i);
}
std::cerr << "remove ";
for (size_t i = 0; i < ops; i++) {
//if (target == 11220184 && i == 4876) {
// std::cout << "Remove " << i << " targetting " << loc[i] << std::endl;
// bv.print();
//}
bv.remove(loc[i]);
control.remove(loc[i]);
//if (target == 11220184 && i == 4876) {
// bv.print();
// bv.validate();
//}
}
std::cerr << " ... ";
if (target >= threshold) {
check(&bv, &control, 1);
}
std::cerr << "OK" << std::endl;
loc.clear();
val.clear();
for (uint64_t i = bv.size(); i < target; i++) {
uint64_t aloc = gen(mt) % (i + 1);
loc.push_back(aloc);
val.push_back(get_val(aloc, i, dist(mt)));
}
std::cerr << "insert ";
for (size_t i = 0; i < loc.size(); i++) {
bv.insert(loc[i], val[i]);
control.insert(loc[i], val[i]);
}
std::cerr << " ... ";
if (target >= threshold) {
check(&bv, &control, 1);
}
std::cerr << "OK" << std::endl;
loc.clear();
val.clear();
for (uint64_t i = 0; i < ops; i++) {
uint64_t aloc = gen(mt) % target;
loc.push_back(aloc);
val.push_back(get_val(aloc, target, dist(mt)));
}
std::cerr << "set ";
for (size_t i = 0; i < loc.size(); i++) {
bv.set(loc[i], val[i]);
control.set(loc[i], val[i]);
}
std::cerr << " ... ";
if (target >= threshold) {
check(&bv, &control, 1);
}
std::cerr << "OK" << std::endl;
uint64_t checksum = 0;
std::cerr << "at: ";
loc.clear();
for (size_t i = 0; i < ops; i++) {
loc.push_back(gen(mt) % target);
}
for (size_t i = 0; i < ops; i++) {
checksum += bv.at(loc[i]);
checksum -= bv.at(loc[i]);
}
std::cerr << checksum << std::endl;
std::cerr << "rank: ";
loc.clear();
for (size_t i = 0; i < ops; i++) {
loc.push_back(gen(mt) % target);
}
for (size_t i = 0; i < ops; i++) {
checksum += bv.rank(loc[i]);
checksum -= bv.rank(loc[i]);
}
std::cerr << checksum << std::endl;
std::cerr << "select: ";
uint64_t limit = bv.rank(target - 1);
loc.clear();
for (size_t i = 0; i < ops; i++) {
uint64_t val = gen(mt) % limit;
loc.push_back(val);
}
for (size_t i = 0; i < ops; i++) {
checksum += bv.select(loc[i] + 1);
checksum -= control.select(loc[i] + 1);
}
std::cerr << checksum << std::endl;
}
}
void help() {
std::cout << "Benchmark bit vectors with non-uniform random input.\n"
<< "Type and seed is required.\n"
<< "Size should be at least 10^7 and defaults to 10^7\n"
<< "Steps defaults to 100.\n\n";
std::cout << "Usage: bench <type> <seed> <size> <steps>\n";
std::cout << " <type> 0 for uncompressed buffered bv\n"
<< " 1 for hybrid-rle bv\n"
<< " 2 for small hybrid-rle bv\n";
std::cout << " <seed> seed to use for running the test\n";
std::cout << " <size> number of bits in the bitvector\n";
std::cout << " <steps> How many data points to generate in the "
"[10^6..size] range\n\n";
std::cout << "Example: bench 0 1337 10000000 100" << std::endl;
exit(0);
}
int main(int argc, char const* argv[]) {
if (argc < 3) {
help();
return 0;
}
uint64_t type;
uint64_t seed;
uint64_t size = 10000000;
uint64_t steps = 100;
std::sscanf(argv[1], "%lu", &type);
std::sscanf(argv[2], "%lu", &seed);
if (argc > 3) {
std::sscanf(argv[3], "%lu", &size);
if (size < 10000000) {
std::cerr << "Invalid size argument" << std::endl;
help();
return 1;
}
}
if (argc > 4) {
std::sscanf(argv[4], "%lu", &steps);
}
if (type == 0) {
test<bv::bv>(size, steps, seed);
} else if (type == 1){
test<bv::simple_bv<16, 16384, 64, true, true, true>>(size, steps, seed);
} else {
test<bv::simple_bv<4, 2048, 64, true, true, true>>(size, steps, seed);
}
return 0;
}