forked from fast-pack/JavaFastPFOR
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBenchmarkSkippable.java
More file actions
339 lines (310 loc) · 13.7 KB
/
Copy pathBenchmarkSkippable.java
File metadata and controls
339 lines (310 loc) · 13.7 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
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
/**
* This code is released under the
* Apache License Version 2.0 http://www.apache.org/licenses/.
*
* (c) Daniel Lemire, http://lemire.me/en/
*/
package me.lemire.integercompression.benchmarktools;
import me.lemire.integercompression.BinaryPacking;
import me.lemire.integercompression.FastPFOR;
import me.lemire.integercompression.FastPFOR128;
import me.lemire.integercompression.IntWrapper;
import me.lemire.integercompression.JustCopy;
import me.lemire.integercompression.NewPFD;
import me.lemire.integercompression.NewPFDS16;
import me.lemire.integercompression.NewPFDS9;
import me.lemire.integercompression.OptPFD;
import me.lemire.integercompression.OptPFDS16;
import me.lemire.integercompression.OptPFDS9;
import me.lemire.integercompression.Simple16;
import me.lemire.integercompression.Simple9;
import me.lemire.integercompression.SkippableComposition;
import me.lemire.integercompression.SkippableIntegerCODEC;
import me.lemire.integercompression.VariableByte;
import me.lemire.integercompression.differential.Delta;
import me.lemire.integercompression.differential.IntegratedBinaryPacking;
import me.lemire.integercompression.differential.IntegratedVariableByte;
import me.lemire.integercompression.differential.SkippableIntegratedComposition;
import me.lemire.integercompression.differential.SkippableIntegratedIntegerCODEC;
import me.lemire.integercompression.synth.ClusteredDataGenerator;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Arrays;
/**
*
* Simple class meant to compare the speed of different schemes.
*
* @author Daniel Lemire
*
*/
public class BenchmarkSkippable {
private static int compressWithSkipTable(Object c, int[] data,
int[] output, IntWrapper outpos, int[] metadata, int blocksize) {
int metapos = 0;
metadata[metapos++] = data.length;
IntWrapper inpos = new IntWrapper();
int initvalue = 0;
IntWrapper ival = new IntWrapper(initvalue);
while (inpos.get() < data.length) {
metadata[metapos++] = outpos.get();
metadata[metapos++] = initvalue;
if (c instanceof SkippableIntegerCODEC) {
int size = blocksize > data.length - inpos.get() ? data.length
- inpos.get() : blocksize;
initvalue = Delta.delta(data, inpos.get(), size, initvalue);
((SkippableIntegerCODEC) c).headlessCompress(data, inpos,
blocksize, output, outpos);
} else if (c instanceof SkippableIntegratedIntegerCODEC) {
ival.set(initvalue);
((SkippableIntegratedIntegerCODEC) c).headlessCompress(data,
inpos, blocksize, output, outpos, ival);
initvalue = ival.get();
} else {
throw new RuntimeException("Unrecognized codec " + c);
}
}
return metapos;
}
private static int decompressFromSkipTable(Object c, int[] compressed,
IntWrapper compressedpos, int[] metadata, int blocksize, int[] data) {
int metapos = 0;
int length = metadata[metapos++];
IntWrapper uncomppos = new IntWrapper();
IntWrapper ival = new IntWrapper();
while (uncomppos.get() < length) {
int num = blocksize;
if (num > length - uncomppos.get())
num = length - uncomppos.get();
int location = metadata[metapos++];
int initvalue = metadata[metapos++];
int outputlocation = uncomppos.get();
if (location != compressedpos.get())
throw new RuntimeException("Bug " + location + " "
+ compressedpos.get() + " codec " + c);
if (c instanceof SkippableIntegerCODEC) {
((SkippableIntegerCODEC) c).headlessUncompress(compressed,
compressedpos, compressed.length - uncomppos.get(),
data, uncomppos, num);
initvalue = Delta.fastinverseDelta(data, outputlocation, num,
initvalue);
} else if (c instanceof SkippableIntegratedIntegerCODEC) {
ival.set(initvalue);
((SkippableIntegratedIntegerCODEC) c).headlessUncompress(
compressed, compressedpos, compressed.length
- uncomppos.get(), data, uncomppos, num, ival);
} else {
throw new RuntimeException("Unrecognized codec " + c);
}
}
return length;
}
/**
* Standard benchmark
*
* @param csvLog
* Writer for CSV log.
* @param c
* the codec
* @param data
* arrays of input data
* @param repeat
* How many times to repeat the test
* @param verbose
* whether to output result on screen
*/
private static void testCodec(PrintWriter csvLog, int sparsity, Object c,
int[][] data, int repeat, boolean verbose) {
if (verbose) {
System.out.println("# " + c.toString());
System.out
.println("# bits per int, compress speed (mis), decompression speed (mis) ");
}
int N = data.length;
int totalSize = 0;
int maxLength = 0;
for (int k = 0; k < N; ++k) {
totalSize += data[k].length;
if (data[k].length > maxLength) {
maxLength = data[k].length;
}
}
// 4x + 1024 to account for the possibility of some negative
// compression.
int[] compressBuffer = new int[4 * maxLength + 1024];
int[] decompressBuffer = new int[maxLength + 1024];
int[] metadataBuffer = new int[maxLength];
final int blocksize = 1024;
// These variables hold time in microseconds (10^-6).
double compressTime = 0;
double decompressTime = 0;
final int times = 5;
int size = 0;
for (int r = 0; r < repeat; ++r) {
size = 0;
for (int k = 0; k < N; ++k) {
int[] backupdata = Arrays.copyOf(data[k], data[k].length);
// compress data.
long beforeCompress = System.nanoTime() / 1000;
IntWrapper outpos = new IntWrapper();
compressWithSkipTable(c, backupdata, compressBuffer, outpos,
metadataBuffer, blocksize);
long afterCompress = System.nanoTime() / 1000;
// measure time of compression.
compressTime += afterCompress - beforeCompress;
final int thiscompsize = outpos.get();
size += thiscompsize;
// dry run
int volume = 0;
{
IntWrapper compressedpos = new IntWrapper(0);
volume = decompressFromSkipTable(c, compressBuffer,
compressedpos, metadataBuffer, blocksize,
decompressBuffer);
// let us check the answer
if (volume != backupdata.length)
throw new RuntimeException(
"Bad output size with codec " + c);
for (int j = 0; j < volume; ++j) {
if (data[k][j] != decompressBuffer[j])
throw new RuntimeException("bug in codec " + c);
}
}
// extract (uncompress) data
long beforeDecompress = System.nanoTime() / 1000;
for (int t = 0; t < times; ++t) {
IntWrapper compressedpos = new IntWrapper(0);
volume = decompressFromSkipTable(c, compressBuffer,
compressedpos, metadataBuffer, blocksize,
decompressBuffer);
}
long afterDecompress = System.nanoTime() / 1000;
// measure time of extraction (uncompression).
decompressTime += (afterDecompress - beforeDecompress) / times;
if (volume != data[k].length)
throw new RuntimeException("we have a bug (diff length) "
+ c + " expected " + data[k].length + " got "
+ volume);
// verify: compare original array with
// compressed and
// uncompressed.
for (int m = 0; m < outpos.get(); ++m) {
if (decompressBuffer[m] != data[k][m]) {
throw new RuntimeException(
"we have a bug (actual difference), expected "
+ data[k][m] + " found "
+ decompressBuffer[m] + " at " + m);
}
}
}
}
if (verbose) {
double bitsPerInt = size * 32.0 / totalSize;
long compressSpeed = Math
.round(totalSize * repeat / (compressTime));
long decompressSpeed = Math.round(totalSize * repeat
/ (decompressTime));
System.out.println(String.format("\t%1$.2f\t%2$d\t%3$d",
bitsPerInt, compressSpeed, decompressSpeed));
csvLog.format("\"%1$s\",%2$d,%3$.2f,%4$d,%5$d\n", c.toString(),
sparsity, bitsPerInt, compressSpeed, decompressSpeed);
csvLog.flush();
}
}
/**
* Main method.
*
* @param args
* command-line arguments
* @throws FileNotFoundException
* when we fail to create a new file
*/
public static void main(String args[]) throws FileNotFoundException {
System.out.println("# benchmark based on the ClusterData model from:");
System.out.println("# Vo Ngoc Anh and Alistair Moffat. ");
System.out.println("# Index compression using 64-bit words.");
System.out
.println("# Softw. Pract. Exper.40, 2 (February 2010), 131-147. ");
System.out.println();
PrintWriter writer = null;
try {
File csvFile = new File(String.format(
"benchmark-%1$tY%1$tm%1$tdT%1$tH%1$tM%1$tS.csv",
System.currentTimeMillis()));
writer = new PrintWriter(csvFile);
System.out.println("# Results will be written into a CSV file: "
+ csvFile.getName());
System.out.println();
test(writer, 20, 18, 10);
System.out.println();
System.out.println("Results were written into a CSV file: "
+ csvFile.getName());
} finally {
if (writer != null) {
writer.close();
}
}
}
/**
* Generate test data.
*
* @param N
* How many input arrays to generate
* @param nbr
* How big (in log2) should the arrays be
* @param sparsity
* How sparse test data generated
*/
private static int[][] generateTestData(ClusteredDataGenerator dataGen,
int N, int nbr, int sparsity) {
final int[][] data = new int[N][];
final int dataSize = (1 << (nbr + sparsity));
for (int i = 0; i < N; ++i) {
data[i] = dataGen.generateClustered((1 << nbr), dataSize);
}
return data;
}
static Object[] codecs = {
new SkippableIntegratedComposition(new IntegratedBinaryPacking(),
new IntegratedVariableByte()), new JustCopy(), new VariableByte(),
new SkippableComposition(new BinaryPacking(), new VariableByte()),
new SkippableComposition(new NewPFD(), new VariableByte()),
new SkippableComposition(new NewPFDS9(), new VariableByte()),
new SkippableComposition(new NewPFDS16(), new VariableByte()),
new SkippableComposition(new OptPFD(), new VariableByte()),
new SkippableComposition(new OptPFDS9(), new VariableByte()),
new SkippableComposition(new OptPFDS16(), new VariableByte()),
new SkippableComposition(new FastPFOR(), new VariableByte()),
new SkippableComposition(new FastPFOR128(), new VariableByte()),
new Simple9(), new Simple16() };
/**
* Generates data and calls other tests.
*
* @param csvLog
* Writer for CSV log.
* @param N
* How many input arrays to generate
* @param nbr
* how big (in log2) should the arrays be
* @param repeat
* How many times should we repeat tests.
*/
private static void test(PrintWriter csvLog, int N, int nbr, int repeat) {
csvLog.format("\"Algorithm\",\"Sparsity\",\"Bits per int\",\"Compress speed (MiS)\",\"Decompress speed (MiS)\"\n");
ClusteredDataGenerator cdg = new ClusteredDataGenerator();
final int max_sparsity = 31 - nbr;
for (int sparsity = 1; sparsity < max_sparsity; sparsity+=4) {
System.out.println("# sparsity " + sparsity);
System.out.println("# generating random data...");
int[][] data = generateTestData(cdg, N, nbr, sparsity);
System.out.println("# generating random data... ok.");
for (Object c : codecs) {
testCodec(csvLog, sparsity, c, data, repeat, false);
testCodec(csvLog, sparsity, c, data, repeat, false);
testCodec(csvLog, sparsity, c, data, repeat, true);
testCodec(csvLog, sparsity, c, data, repeat, true);
testCodec(csvLog, sparsity, c, data, repeat, true);
}
}
}
}