forked from marbl/canu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unaryEncodingTester.C
228 lines (176 loc) · 5.9 KB
/
unaryEncodingTester.C
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
/******************************************************************************
*
* This file is part of canu, a software program that assembles whole-genome
* sequencing reads into contigs.
*
* This software is based on:
* 'Celera Assembler' (http://wgs-assembler.sourceforge.net)
* the 'kmer package' (http://kmer.sourceforge.net)
* both originally distributed by Applera Corporation under the GNU General
* Public License, version 2.
*
* Canu branched from Celera Assembler at its revision 4587.
* Canu branched from the kmer project at its revision 1994.
*
* Modifications by:
*
* Brian P. Walenz from 2004-APR-27 to 2004-OCT-10
* are Copyright 2004 Brian P. Walenz, and
* are subject to the GNU General Public License version 2
*
* Brian P. Walenz from 2008-JAN-29 to 2014-APR-11
* are Copyright 2008,2014 J. Craig Venter Institute, and
* are subject to the GNU General Public License version 2
*
* File 'README.licenses' in the root directory of this distribution contains
* full conditions and disclaimers for each license.
*/
#include "util++.H"
uint64 numLoops = 1;
uint64 numNums = 4000000;
uint64 numSize = 300;
// The space in bits that we can play with, and the pointer to said space.
//
uint64 spa = 128 * 1024 * 1024 * 8;
uint64 *ptr = 0L;
uint64 *rnd = 0L;
void
testUnary(void) {
uint64 pos = uint64ZERO;
uint64 siz = uint64ZERO;
uint64 val = uint64ZERO;
uint64 i = uint64ZERO;
for (i=0; i<numNums; i++) {
setUnaryEncodedNumber(ptr, pos, &siz, rnd[i]);
pos += siz;
if (pos + 1000 >= spa) {
fprintf(stderr, "ERROR: Ran out of space in testUnary at number "uint64FMT" out of "uint64FMT"\n", i, numNums);
exit(1);
}
}
//fprintf(stderr, "unaryEncodedNumbers used "uint64FMT"MB of storage out of "uint64FMT"MB.\n", pos >> 23, spa >> 23);
pos = uint64ZERO;
for (i=0; i<numNums; i++) {
val = getUnaryEncodedNumber(ptr, pos, &siz);
if (val != rnd[i]) {
fprintf(stderr, "Number "uint64FMT" at bitpos "uint64FMT" failed. Desired "uint64FMT" got "uint64FMT"\n", i, pos, rnd[i], val);
exit(1);
}
pos += siz;
}
fprintf(stderr, "unary encoded numbers OK!\n");
}
void
testGeneralizedUnary(void) {
uint64 pos = uint64ZERO;
uint64 siz = uint64ZERO;
uint64 val = uint64ZERO;
uint64 i = uint64ZERO;
for (i=0; i<numNums; i++) {
setGeneralizedUnaryEncodedNumber(ptr, pos, &siz, rnd[i]);
pos += siz;
if (pos + 1000 >= spa) {
fprintf(stderr, "ERROR: Ran out of space in testGeneralizedUnary at number "uint64FMT" out of "uint64FMT"\n", i, numNums);
exit(1);
}
}
//fprintf(stderr, "generalizedUnaryEncodedNumbers used "uint64FMT"MB of storage out of "uint64FMT"MB.\n", pos >> 23, spa >> 23);
pos = uint64ZERO;
for (i=0; i<numNums; i++) {
val = getGeneralizedUnaryEncodedNumber(ptr, pos, &siz);
if (val != rnd[i]) {
fprintf(stderr, "Number "uint64FMT" at bitpos "uint64FMT" failed. Desired "uint64FMT" got "uint64FMT"\n", i, pos, rnd[i], val);
exit(1);
}
pos += siz;
}
fprintf(stderr, "generalized unary encoded numbers OK!\n");
}
void
testEliasGamma(void) {
uint64 pos = uint64ZERO;
uint64 siz = uint64ZERO;
uint64 val = uint64ZERO;
uint64 i = uint64ZERO;
for (i=0; i<numNums; i++) {
setEliasGammaEncodedNumber(ptr, pos, &siz, rnd[i]);
pos += siz;
if (pos + 1000 >= spa) {
fprintf(stderr, "ERROR: Ran out of space in testGeneralizedUnary at number "uint64FMT" out of "uint64FMT"\n", i, numNums);
exit(1);
}
}
//fprintf(stderr, "eliasGammaEncodedNumbers used "uint64FMT"MB of storage out of "uint64FMT"MB.\n", pos >> 23, spa >> 23);
pos = uint64ZERO;
for (i=0; i<numNums; i++) {
val = getEliasGammaEncodedNumber(ptr, pos, &siz);
if (val != rnd[i]) {
fprintf(stderr, "Number "uint64FMT" at bitpos "uint64FMT" failed. Desired "uint64FMT" got "uint64FMT"\n", i, pos, rnd[i], val);
exit(1);
}
pos += siz;
}
fprintf(stderr, "Elias gamma encoded numbers OK!\n");
}
void
testEliasDelta(void) {
uint64 pos = uint64ZERO;
uint64 siz = uint64ZERO;
uint64 val = uint64ZERO;
uint64 i = uint64ZERO;
for (i=0; i<numNums; i++) {
setEliasDeltaEncodedNumber(ptr, pos, &siz, rnd[i]);
pos += siz;
if (pos + 1000 >= spa) {
fprintf(stderr, "ERROR: Ran out of space in testGeneralizedUnary at number "uint64FMT" out of "uint64FMT"\n", i, numNums);
exit(1);
}
}
//fprintf(stderr, "eliasDeltaEncodedNumbers used "uint64FMT"MB of storage out of "uint64FMT"MB.\n", pos >> 23, spa >> 23);
pos = uint64ZERO;
for (i=0; i<numNums; i++) {
val = getEliasDeltaEncodedNumber(ptr, pos, &siz);
if (val != rnd[i]) {
fprintf(stderr, "Number "uint64FMT" at bitpos "uint64FMT" failed. Desired "uint64FMT" got "uint64FMT"\n", i, pos, rnd[i], val);
exit(1);
}
pos += siz;
}
fprintf(stderr, "Elias delta encoded numbers OK!\n");
}
int
main(int argc, char **argv) {
if (argc != 3) {
fprintf(stderr, "usage: %s <num-loops> <num-nums-per-loop>\n", argv[0]);
fprintf(stderr, " -> DEFAULTS USED <-\n");
} else {
numLoops = strtouint32(argv[1], 0L);
numNums = strtouint32(argv[2], 0L);
}
rnd = new uint64 [numNums];
ptr = new uint64 [spa >> 6];
mt_s *ctx = mtInit(time(NULL));
// Generate some random numbers to store
//
while (numLoops--) {
// Test out unary encodings on small numbers
//
for (uint64 i=0; i<numNums; i++)
rnd[i] = mtRandom32(ctx) % numSize;
testUnary();
// Generalized unary encoding can handle larger numbers
//
for (uint64 i=0; i<numNums; i++)
rnd[i] = mtRandom32(ctx);
testGeneralizedUnary();
// Elias Gamma and Delta codes are probably pretty good
//
for (uint64 i=0; i<numNums; i++)
rnd[i] = mtRandom64(ctx);
testEliasGamma();
testEliasDelta();
}
delete [] rnd;
delete [] ptr;
exit(0);
}