-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
393 lines (326 loc) · 12.5 KB
/
test.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
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
#define VRANDGEN_TESTING 1
#include "TestUtils.h"
#include "../SFMT-src-1.5.1/SFMT.h"
#include <utility>
const uint32_t seedlength = 4;
const uint32_t seedinit[seedlength] = { 0x123, 0x234, 0x345, 0x456 };
const uint64_t nRandomTest = 50ul * 624 * 16;
extern "C" unsigned long genrand_int32();
extern "C" void init_by_array(unsigned long init_key[], int key_length);
template <typename T>
struct GenTraits;
template <size_t VecLen, size_t ImplLen, VRandGenQueryMode QryMode>
struct GenTraits<VMT19937<VecLen, QryMode, ImplLen>>
{
static const char* name() { return "VMT19937"; }
};
template <size_t VecLen, size_t ImplLen, VRandGenQueryMode QryMode>
struct GenTraits<VSFMT19937<VecLen, QryMode, ImplLen>>
{
static const char* name() { return "VSFMT19937"; }
};
std::vector<uint32_t> benchmark(nRandomTest + 10000);
void printSome(const std::vector<uint32_t>& v)
{
std::cout << "\n";
for (size_t i = 0; i < 16; ++i)
std::cout << std::setw(10) << v[i] << ((i + 1) % 8 == 0 ? "\n" : " ");
std::cout << "...\n";
for (size_t i = 240; i < 240 + 16; ++i)
std::cout << std::setw(10) << v[i] << ((i + 1) % 8 == 0 ? "\n" : " ");
std::cout << "...\n";
for (size_t i = 624 - 16; i < 624; ++i)
std::cout << std::setw(10) << v[i] << ((i + 1) % 8 == 0 ? "\n" : " ");
std::cout << "\n";
}
enum EncodeMode {Base64, Hex};
template <size_t nRows, size_t nCols>
void testEncoder(const BinaryMatrix<nRows, nCols>& m, EncodeMode enc)
{
const char* modename = enc == Base64 ? "base64" : "hex";
BinaryMatrix<nRows, nCols> m2;
std::cout << "saving matrix to " << modename << " stream\n";
std::ostringstream os;
if (enc == Base64)
m.toBase64(os);
else
m.toHex(os);
std::cout << "first 32 characters of the stream\n";
std::string s = os.str();
for (size_t i = 0; i < 32; ++i)
std::cout << s[i];
std::cout << "\n";
std::cout << "reading back the matrix from " << modename << " stream\n";
std::istringstream is(os.str());
if (enc == Base64)
m2.fromBase64(is);
else
m2.fromHex(is);
std::cout << "compare with original matrix\n";
MYASSERT((m == m2), "error in roundtrip");
std::cout << "completed\n";
}
template <size_t NBITS>
void testSquare(const BinarySquareMatrix<NBITS>& m)
{
BinarySquareMatrix<NBITS> m2, m3;
// slow bit by bit multiplication
//std::cout << "compute matrix multiplication the classical way\n";
for (size_t r = 0; r < NBITS; ++r) {
//std::cout << r << "\n";
for (size_t c = 0; c < NBITS; ++c) {
size_t s = 0;
for (size_t k = 0; k < NBITS; ++k) {
s ^= m.getBit(r, k) && m.getBit(k, c);
}
if (s)
m2.setBit(r, c);
}
}
const size_t nThreads = 4;
//std::cout << "compute matrix multiplication vectorially\n";
std::vector<typename BinarySquareMatrix<NBITS>::buffer_t> buffers(nThreads);
m3.square(m, buffers);
MYASSERT((m2 == m3), "error in square");
//std::cout << "SUCCESS\n";
}
template <size_t nRows, size_t nCols>
void encodingTests()
{
BinaryMatrix<nRows, nCols> m;
m.initRand();
std::cout << "\ngenerated random matrix with size (" << m.s_nBitRows << "x" << m.s_nBitCols << ") with " << m.nnz() << " non zero elements\n";
m.printBits(0, 0, 10, 32);
testEncoder(m, Base64);
testEncoder(m, Hex);
}
template <size_t NBits>
void squareTest()
{
std::cout << "testing multiplication with matrices of size: " << NBits << "\n";
BinarySquareMatrix<NBits> m;
for (size_t i = 0; i < 10; ++i) {
m.resetZero();
m.initRand();
testSquare(m);
}
}
template <size_t...NBits>
void squareTests(std::index_sequence<NBits...>&&)
{
(squareTest<NBits>(), ...);
}
void generateBenchmark_MT19937()
{
unsigned long init[seedlength];
for (size_t i = 0; i < seedlength; ++i)
init[i] = seedinit[i];
std::cout << "Generate MT19937 random numbers with the original C source code ... ";
init_by_array(init, seedlength);
for (size_t i = 0, n = benchmark.size(); i < n; ++i)
benchmark[i] = (uint32_t)genrand_int32();
std::cout << "done!\n";
printSome(benchmark);
}
void generateBenchmark_SFMT19937()
{
std::cout << "Generate SFMT19937 random numbers with the original C source code ... ";
sfmt_t sfmtgen;
sfmt_init_by_array(&sfmtgen, const_cast<uint32_t *>(seedinit), seedlength);
for (size_t i = 0, n = benchmark.size(); i < n; ++i)
benchmark[i] = sfmt_genrand_uint32(&sfmtgen);
std::cout << "done!\n";
printSome(benchmark);
}
void startTest(const char* name)
{
std::cout << "\n"
<< std::setw(40) << std::setfill('*') << "" << "\n"
<< "Test " << name << "\n"
<< std::setw(40) << std::setfill('*') << "" << "\n\n"
<< std::setfill(' ');
}
void testEncoding()
{
startTest("encoding");
encodingTests<19937, 19937>();
encodingTests<19937, 1007>();
encodingTests<1007, 19937>();
encodingTests<1007, 1007>();
}
void testSquareMatrix()
{
startTest("square matrix calculation");
squareTests(std::index_sequence<1, 5, 8, 13, 16, 20, 28, 32, 36, 60, 64, 68, 85, 126, 128, 150>{});
}
template <typename M>
struct JumpMatrix
{
JumpMatrix() : jumpSize(0) {}
JumpMatrix(const M* m, size_t jumpSize) : p(m), jumpSize(jumpSize) {}
std::unique_ptr<const M> p;
size_t jumpSize; // jump size (i.e. number of elements skipped)
};
template <typename Gen, typename M>
void testEquivalence(size_t nCommonJumpRepeat, const JumpMatrix<M>& commonJump, const JumpMatrix<M>& seqJump)
{
const size_t commonJumpSize = commonJump.p ? commonJump.jumpSize : 0;
const size_t sequenceJumpSize = seqJump.p ? seqJump.jumpSize : 0;
MYASSERT((!commonJump.p && nCommonJumpRepeat == 0) || (commonJump.p && nCommonJumpRepeat > 0), "(nCommonJumpRepeat>0) <=> commonJumpSize>0 ");
const size_t VecLen = Gen::s_regLenBits;
const VRandGenQueryMode QryMode = Gen::s_queryMode;
size_t blkSize;
switch (QryMode) {
case QM_Any: blkSize = 0; break;
case QM_Scalar: blkSize = 1; break;
case QM_Block16: blkSize = 16; break;
case QM_StateSize: blkSize = Gen::s_n32InFullState; break;
default: THROW("how did we get here?");
}
const size_t s_nStates = Gen::s_nStates;
const size_t s_n32InOneWord = Gen::s_n32InOneWord;
std::cout << GenTraits<Gen>::name() << "< " << std::setw(3) << VecLen << ", "
<< std::setw(7) << queryModeName(QryMode) << ", " << std::setw(3) << Gen::s_regLenImplBits << ">"
<< ", common jump of " << std::setw(4) << commonJumpSize << " repeated " << nCommonJumpRepeat << " times, sequence jump of " << std::setw(4) << sequenceJumpSize
<< ", block size " << std::setw(4);
if (blkSize > 0)
std::cout << blkSize;
else
std::cout << "rand";
std::cout << " ... ";
std::vector<uint32_t> aligneddst(nRandomTest);
std::unique_ptr<Gen> mt( new Gen(seedinit, seedlength, nCommonJumpRepeat, commonJump.p.get(), seqJump.p.get()));
uint32_t* dst = aligneddst.data();
if constexpr (QryMode != QM_Any) {
for (size_t i = 0; i < nRandomTest / blkSize; ++i)
if constexpr (QryMode == QM_Scalar)
*dst++ = mt->genrand_uint32();
else if constexpr (QryMode == QM_Block16) {
mt->genrand_uint32_blk16(dst);
dst += blkSize;
}
else if constexpr (QryMode == QM_StateSize) {
mt->genrand_uint32_stateBlk(dst);
dst += blkSize;
}
else
NOT_IMPLEMENTED;
}
else { // QryMode == QM_Any
size_t n = nRandomTest;
uint32_t* dst = aligneddst.data();
const size_t sz[] = { 1, 2, 3, 4, 7, 8, 9, 15, 16, 17, 31, 32, 33, 127, 128, 129, 623, 624, 625, 800
, 624 * 2 - 1, 624 * 2, 624 * 2 + 1
, 624 * 4 - 1, 624 * 4, 624 * 4 + 1
, 624 * 8 - 1, 624 * 8, 624 * 8 + 1
};
while (n) {
size_t szi = std::rand() % (sizeof(sz) / sizeof(*sz));
size_t m = std::min<size_t>(n, sz[szi]);
mt->genrand_uint32_anySize(dst, m);
dst += m;
n -= m;
}
}
for (size_t i = 0; i < nRandomTest; ++i) {
uint32_t r2 = aligneddst[i];
size_t genIndex = (i % (s_n32InOneWord * s_nStates)) / s_n32InOneWord;
size_t seqIndex = (i % s_n32InOneWord) + (i / (s_n32InOneWord * s_nStates)) * s_n32InOneWord;
size_t benchmarkindex = seqIndex + commonJumpSize * nCommonJumpRepeat + sequenceJumpSize * genIndex;
MYASSERT(benchmark[benchmarkindex] == r2, "FAILED!\n"
<< "Difference found: out[" << i << "] = " << r2
<< ", benchmark[" << benchmarkindex << "] = " << benchmark[benchmarkindex]);
}
std::cout << "SUCCESS!\n";
}
template <typename M, size_t L, size_t I, VRandGenQueryMode QM>
struct GenFromMatrix;
template <size_t L, size_t I, VRandGenQueryMode QM>
struct GenFromMatrix<MT19937Matrix, L, I, QM>
{
typedef VMT19937<L, QM, I> gen_t;
};
template <size_t L, size_t I, VRandGenQueryMode QM>
struct GenFromMatrix<SFMT19937Matrix, L, I, QM>
{
typedef VSFMT19937<L, QM, I> gen_t;
};
template <size_t L, size_t I, VRandGenQueryMode QM, typename M>
void equivalenceTests3(const JumpMatrix<M>& jumpSmall, const JumpMatrix<M>& jumpBig)
{
JumpMatrix<M> noJump{};
if constexpr (I <= L && I <= SIMD_N_BITS) {
using Gen = typename GenFromMatrix<M, L, I, QM>::gen_t;
if constexpr (QM != QM_Any) {
testEquivalence<Gen>(0, noJump, noJump);
testEquivalence<Gen>(1, jumpSmall, noJump);
testEquivalence<Gen>(2, jumpSmall, noJump);
testEquivalence<Gen>(1, jumpBig, noJump);
if constexpr (L > 32) {
testEquivalence<Gen>(1, jumpBig, jumpBig);
testEquivalence<Gen>(2, jumpBig, jumpBig);
testEquivalence<Gen>(0, noJump, jumpBig);
}
}
else {
// we repeat this test multiple times, as there are random number involved
for (size_t i = 0; i < 10; ++i)
testEquivalence<Gen>(0, noJump, noJump);
}
}
}
template <size_t L, size_t I, VRandGenQueryMode...QMs, typename M>
void equivalenceTests2(const JumpMatrix<M>& commonJump, const JumpMatrix<M>& seqJump)
{
(equivalenceTests3<L, I, QMs>(commonJump, seqJump), ...);
}
template <size_t L, size_t...Is, typename M>
void equivalenceTests1(const JumpMatrix<M>& commonJump, const JumpMatrix<M>& seqJump)
{
(equivalenceTests2<L, Is, QM_Scalar, QM_Block16, QM_StateSize, QM_Any>(commonJump, seqJump), ...);
}
template <size_t...Ls, typename M>
void equivalenceTests0(const JumpMatrix<M>& jumpSmall, const JumpMatrix<M>& jumpBig)
{
(equivalenceTests1<Ls, 32, 128, 256, 512>(jumpSmall, jumpBig), ...);
}
void test_VMT19937()
{
startTest("VMT19937");
generateBenchmark_MT19937();
typedef MT19937Matrix matrix_t;
typedef JumpMatrix<matrix_t> pmatrix_t;
pmatrix_t noJump;
pmatrix_t jumpMatrix1(new matrix_t, 1); // jump ahead 1 element
pmatrix_t jumpMatrix512(new matrix_t(std::string("./dat/mt/F00009.bits")), 512); // jump ahead 2^9 (512) elements
pmatrix_t jumpMatrixPeriod(new matrix_t(std::string("./dat/mt/F19937.bits")), 1); // jump ahead 2^19937 elements
equivalenceTests0<32, 128, 256, 512>(jumpMatrix1, jumpMatrix512);
// since the period is 2^19937-1, after applying a jump matrix of 2^19937, we restart the sequence from step 1
std::cout << "VMT19937: a jump of size 2^19937 is equivalent to a jump of size 1\n";
testEquivalence<VMT19937<32, QM_Scalar>>(1, jumpMatrixPeriod, noJump);
}
void test_VSFMT19937()
{
std::cout << "\nTest VSFMT19937\n";
generateBenchmark_SFMT19937();
typedef SFMT19937Matrix matrix_t;
typedef JumpMatrix<matrix_t> pmatrix_t;
pmatrix_t noJump;
pmatrix_t jumpMatrix4(new matrix_t, 4); // jump ahead 1 element
pmatrix_t jumpMatrix512(new matrix_t(std::string("./dat/sfmt/F00009.bits")), 512); // jump ahead 2^9 (1024) elements
equivalenceTests0<128, 256, 512>(jumpMatrix4, jumpMatrix512);
}
int main()
{
try {
testEncoding();
testSquareMatrix();
test_VMT19937();
test_VSFMT19937();
}
catch (const std::exception& e) {
std::cout << e.what() << "\n";
return -1;
}
return 0;
}