forked from fast-pack/FastPFor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit.cpp
193 lines (183 loc) · 7.01 KB
/
unit.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
/**
* This code is released under the
* Apache License Version 2.0 http://www.apache.org/licenses/.
*
* (c) Daniel Lemire, http://lemire.me/en/
*/
#include <memory>
#include <iostream>
#include <iomanip>
#include <time.h>
#include "codecfactory.h"
#include "util.h"
#include "ztimer.h"
#include "bitpacking.h"
#include "synthetic.h"
#include "cpubenchmark.h"
using namespace std;
using namespace FastPForLib;
int main() {
CODECFactory factory;
vector<shared_ptr<IntegerCODEC>> myalgos = factory.allSchemes();
for (uint32_t b = 0; b <= 28; ++b) {
cout << "testing... b = " << b << endl;
for (size_t length = 128; length < (1U << 12); length += 128) {
vector<uint32_t, cacheallocator> data(length);
data.reserve(length + 1024);
for (size_t i = 0; i < data.size(); ++i) {
data[i] = (static_cast<uint32_t>(i) +
(24 - static_cast<uint32_t>(i)) *
(12 - static_cast<uint32_t>(i))) %
(1U << b);
}
for (vector<shared_ptr<IntegerCODEC>>::iterator i = myalgos.begin();
i != myalgos.end(); ++i) {
IntegerCODEC &c = **i;
vector<uint32_t, cacheallocator> out(2 * data.size() + 1024);
if (c.name() !=
"VSEncoding") // VSEncoding is fragile, output must be zero
for (uint32_t k = 0; k < out.size(); ++k)
out[k] = rand();
size_t nvalue = out.size();
// next magic is to satisfy cachepacking aligment requirements; should
// not hurt others
uint32_t *aligned_out = &out[0];
assert(!needPaddingTo64bytes(aligned_out));
c.encodeArray(&data[0], data.size(), aligned_out, nvalue);
out.resize(nvalue);
vector<uint32_t, cacheallocator> recover(data.size() + 1024);
for (uint32_t k = 0; k < recover.size(); ++k)
recover[k] = rand();
size_t recoveredsize = recover.size();
c.decodeArray(aligned_out, out.size(), &recover[0], recoveredsize);
recover.resize(recoveredsize);
if (recover != data) {
if (recoveredsize != data.size()) {
cerr << "lengths don't match!" << endl;
cerr << recoveredsize << endl;
cerr << data.size() << endl;
}
uint32_t howmany = 0;
for (size_t k = 0; k < data.size(); ++k)
if (recover[k] != data[k]) {
cout << "recovered[" << k << "] = " << recover[k] << endl;
cout << "data[" << k << "] = " << data[k] << endl;
howmany++;
if (howmany > 15)
break;
}
cerr << c.name() << endl;
cerr << length << endl;
throw logic_error("we have a bug");
}
}
}
for (size_t length = 0; length < (1U << 8); ++length) {
vector<uint32_t, cacheallocator> data(length);
data.reserve(length + 1024);
for (size_t i = 0; i < data.size(); ++i) {
data[i] =
(33231 - static_cast<uint32_t>(i) + static_cast<uint32_t>(i * i)) %
(1U << b);
}
for (vector<shared_ptr<IntegerCODEC>>::iterator i = myalgos.begin();
i != myalgos.end(); ++i) {
IntegerCODEC &c = **i;
vector<uint32_t, cacheallocator> out(2 * data.size() + 1024);
if (c.name() !=
"VSEncoding") // VSEncoding is fragile, output must be zero
for (uint32_t k = 0; k < out.size(); ++k)
out[k] = rand();
size_t nvalue = out.size();
uint32_t *aligned_out = &out[0];
assert(!needPaddingTo64bytes(aligned_out));
c.encodeArray(data.data(), data.size(), aligned_out, nvalue);
out.resize(nvalue);
vector<uint32_t, cacheallocator> recover(data.size() + 1024);
for (uint32_t k = 0; k < recover.size(); ++k)
recover[k] = rand();
size_t recoveredsize = recover.size();
c.decodeArray(aligned_out, out.size(), &recover[0], recoveredsize);
recover.resize(recoveredsize);
if (recoveredsize != data.size()) {
cerr << "lengths don't match!" << endl;
cerr << recoveredsize << endl;
cerr << data.size() << endl;
throw logic_error("we have a bug");
}
if (!equal(recover.begin(), recover.end(), data.begin())) {
uint32_t howmany = 0;
for (size_t k = 0; k < data.size(); ++k)
if (recover[k] != data[k]) {
cout << "recovered[" << k << "] = " << recover[k] << endl;
cout << "data[" << k << "] = " << data[k] << endl;
howmany++;
if (howmany > 15)
break;
}
cerr << c.name() << endl;
cerr << length << endl;
throw logic_error("we have a bug");
}
}
}
}
cout << " testing large zipfian arrays..." << endl;
for (size_t lengthb = 5; lengthb <= 25; lengthb += 5) {
uint32_t length = 1U << lengthb;
cout << endl << "length = " << length << endl;
vector<uint32_t, cacheallocator> data =
generateZipfianArray32(length, 2, (1U << 9) - 1);
data.reserve(data.size() + 1024);
for (vector<shared_ptr<IntegerCODEC>>::iterator i = myalgos.begin();
i != myalgos.end(); ++i) {
IntegerCODEC &c = **i;
cout << c.name();
cout.flush();
vector<uint32_t, cacheallocator> out(2 * data.size() + 1024);
if (c.name() !=
"VSEncoding") // VSEncoding is fragile, output must be zero
for (uint32_t k = 0; k < out.size(); ++k)
out[k] = rand();
size_t nvalue = out.size();
cout << " encoding ...";
cout.flush();
uint32_t *aligned_out = &out[0];
assert(!needPaddingTo64bytes(aligned_out));
c.encodeArray(&data[0], data.size(), aligned_out, nvalue);
out.resize(nvalue);
assert(out.size() == nvalue); // paranoid
vector<uint32_t, cacheallocator> recover(data.size() + 1024);
assert(recover.size() == data.size() + 1024); // being paranoid
for (uint32_t k = 0; k < recover.size(); ++k)
recover[k] = rand();
size_t recoveredsize = recover.size();
cout << " decoding ...";
cout.flush();
c.decodeArray(aligned_out, out.size(), &recover[0], recoveredsize);
recover.resize(recoveredsize);
if (recoveredsize != data.size()) {
cerr << "lengths don't match!" << endl;
cerr << recoveredsize << endl;
cerr << data.size() << endl;
throw logic_error("we have a bug");
}
if (!equal(recover.begin(), recover.end(), data.begin())) {
uint32_t howmany = 0;
for (size_t k = 0; k < data.size(); ++k)
if (recover[k] != data[k]) {
cout << "recovered[" << k << "] = " << recover[k] << endl;
cout << "data[" << k << "] = " << data[k] << endl;
howmany++;
if (howmany > 15)
break;
}
cerr << c.name() << endl;
cerr << length << endl;
throw logic_error("we have a bug");
}
cout << " ok!" << endl;
}
}
cout << endl << endl << "testing...ok. Your code is good." << endl;
}