-
Notifications
You must be signed in to change notification settings - Fork 29
/
sz3.cpp
495 lines (468 loc) · 18.6 KB
/
sz3.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include "SZ3/api/sz.hpp"
#define SZ_FLOAT 0
#define SZ_DOUBLE 1
#define SZ_UINT8 2
#define SZ_INT8 3
#define SZ_UINT16 4
#define SZ_INT16 5
#define SZ_UINT32 6
#define SZ_INT32 7
#define SZ_UINT64 8
#define SZ_INT64 9
void usage() {
printf("Note: SZ3 command line arguments are backward compatible with SZ2, \n");
printf(" use -h2 to show the supported SZ2 command line arguments. \n");
printf("Usage: sz3 <options>\n");
printf("Options:\n");
printf("* general options:\n");
printf(" -h: print the help information\n");
printf(" -h2: print the help information for SZ2 style command line\n");
printf(" -v: print the version number\n");
printf(" -a : print compression results such as distortions\n");
printf("* input and output:\n");
printf(" -i <path> : original binary input file\n");
printf(" -o <path> : compressed output file, default in binary format\n");
printf(" -z <path> : compressed output (w -i) or input (w/o -i) file\n");
printf(" -t : store compressed output file in text format\n");
// printf(" -p: print meta data (configuration info)\n");
printf("* data type:\n");
printf(" -f: single precision (float type)\n");
printf(" -d: double precision (double type)\n");
printf(" -I <width>: integer type (width = 32 or 64)\n");
printf("* configuration file: \n");
printf(" -c <configuration file> : configuration file sz.config\n");
printf("* error control: (the error control parameters here will overwrite the setting in sz.config)\n");
printf(" -M <error control mode> <error bound (optional)> \n");
printf(" error control mode as follows: \n");
printf(" ABS (absolute error bound)\n");
printf(" REL (value range based error bound, so a.k.a., VR_REL)\n");
printf(" PSNR (peak signal-to-noise ratio)\n");
printf(" NORM (norm2 error : sqrt(sum(xi-xi')^2)\n");
printf(" ABS_AND_REL (using min{ABS, REL})\n");
printf(" ABS_OR_REL (using max{ABS, REL})\n");
printf(" error bound can be set directly after the error control mode, or separately with the following options:\n");
printf(" -A <absolute error bound>: specifying absolute error bound\n");
printf(" -R <value_range based relative error bound>: specifying relative error bound\n");
// printf(" -P <point-wise relative error bound>: specifying point-wise relative error bound\n");
printf(" -S <PSNR>: specifying PSNR\n");
printf(" -N <normErr>: specifying normErr\n");
printf("* dimensions: \n");
printf(" -1 <nx> : dimension for 1D data such as data[nx]\n");
printf(" -2 <nx> <ny> : dimensions for 2D data such as data[ny][nx]\n");
printf(" -3 <nx> <ny> <nz> : dimensions for 3D data such as data[nz][ny][nx] \n");
printf(" -4 <nx> <ny> <nz> <np>: dimensions for 4D data such as data[np][nz][ny][nx] \n");
printf("* examples: \n");
printf(" sz -f -i test.dat -z test.dat.sz -3 8 8 128 -M ABS 1e-3 \n");
printf(" sz -f -z test.dat.sz -o test.dat.sz.out -3 8 8 128 -M REL 1e-3 -a \n");
printf(" sz -f -i test.dat -o test.dat.sz.out -3 8 8 128 -M ABS_AND_REL -A 1 -R 1e-3 -a \n");
printf(" sz -f -i test.dat -o test.dat.sz.out -3 8 8 128 -c sz.config \n");
printf(" sz -f -i test.dat -o test.dat.sz.out -3 8 8 128 -c sz.config -M ABS 1e-3 -a\n");
exit(0);
}
void usage_sz2() {
printf("Note: below are the supported command line arguments in SZ2 style\n");
printf("Usage: sz <options>\n");
printf("Options:\n");
printf("* operation type:\n");
printf(" -z <compressed file>: the compression operation with an optionally specified output file.\n");
printf(" (the compressed file will be named as <input_file>.sz if not specified)\n");
printf(" -x <decompressed file>: the decompression operation with an optionally specified output file\n");
printf(" (the decompressed file will be named as <cmpred_file>.out if not specified)\n");
// printf(" -p: print meta data (configuration info)\n");
printf(" -h: print the help information\n");
printf(" -v: print the version number\n");
printf("* data type:\n");
printf(" -f: single precision (float type)\n");
printf(" -d: double precision (double type)\n");
printf("* configuration file: \n");
printf(" -c <configuration file> : configuration file sz.config\n");
printf("* error control: (the error control parameters here will overwrite the setting in sz.config)\n");
printf(" -M <error bound mode> : 10 options as follows. \n");
printf(" ABS (absolute error bound)\n");
printf(" REL (value range based error bound, so a.k.a., VR_REL)\n");
printf(" ABS_AND_REL (using min{ABS, REL})\n");
printf(" ABS_OR_REL (using max{ABS, REL})\n");
printf(" PSNR (peak signal-to-noise ratio)\n");
printf(" NORM (norm2 error : sqrt(sum(xi-xi')^2)\n");
// printf(" PW_REL (point-wise relative error bound)\n");
printf(" -A <absolute error bound>: specifying absolute error bound\n");
printf(" -R <value_range based relative error bound>: specifying relative error bound\n");
// printf(" -P <point-wise relative error bound>: specifying point-wise relative error bound\n");
printf(" -S <PSNR>: specifying PSNR\n");
printf(" -N <normErr>: specifying normErr\n");
printf("* input data file:\n");
printf(" -i <original data file> : original data file\n");
printf(" -s <compressed data file> : compressed data file in decompression\n");
printf("* output type of decompressed file: \n");
printf(" -b (by default) : decompressed file stored in binary format\n");
printf(" -t : decompreadded file stored in text format\n");
// printf(" -T : pre-processing with Tucker Tensor Decomposition\n");
printf("* dimensions: \n");
printf(" -1 <nx> : dimension for 1D data such as data[nx]\n");
printf(" -2 <nx> <ny> : dimensions for 2D data such as data[ny][nx]\n");
printf(" -3 <nx> <ny> <nz> : dimensions for 3D data such as data[nz][ny][nx] \n");
printf(" -4 <nx> <ny> <nz> <np>: dimensions for 4D data such as data[np][nz][ny][nx] \n");
printf("* print compression results: \n");
printf(" -a : print compression results such as distortions\n");
printf("* examples: \n");
printf(" sz -z -f -c sz.config -i testdata/x86/testfloat_8_8_128.dat -3 8 8 128\n");
printf(" sz -z -f -c sz.config -M ABS -A 1E-3 -i testdata/x86/testfloat_8_8_128.dat -3 8 8 128\n");
printf(" sz -x -f -s testdata/x86/testfloat_8_8_128.dat.sz -3 8 8 128\n");
printf(" sz -x -f -s testdata/x86/testfloat_8_8_128.dat.sz -i testdata/x86/testfloat_8_8_128.dat -3 8 8 128 -a\n");
printf(" sz -z -d -c sz.config -i testdata/x86/testdouble_8_8_128.dat -3 8 8 128\n");
printf(" sz -x -d -s testdata/x86/testdouble_8_8_128.dat.sz -3 8 8 128\n");
printf(" sz -p -s testdata/x86/testdouble_8_8_128.dat.sz\n");
exit(0);
}
template<class T>
void compress(char *inPath, char *cmpPath, SZ::Config conf) {
T *data = new T[conf.num];
SZ::readfile<T>(inPath, conf.num, data);
size_t outSize;
SZ::Timer timer(true);
char *bytes = SZ_compress<T>(conf, data, outSize);
double compress_time = timer.stop();
char outputFilePath[1024];
if (cmpPath == nullptr) {
sprintf(outputFilePath, "%s.sz", inPath);
} else {
strcpy(outputFilePath, cmpPath);
}
SZ::writefile(outputFilePath, bytes, outSize);
printf("compression ratio = %.2f \n", conf.num * 1.0 * sizeof(T) / outSize);
printf("compression time = %f\n", compress_time);
printf("compressed data file = %s\n", outputFilePath);
delete[]data;
delete[]bytes;
}
template<class T>
void decompress(char *inPath, char *cmpPath, char *decPath,
SZ::Config conf,
int binaryOutput, int printCmpResults) {
size_t cmpSize;
auto cmpData = SZ::readfile<char>(cmpPath, cmpSize);
SZ::Timer timer(true);
T *decData = SZ_decompress<T>(conf, cmpData.get(), cmpSize);
double compress_time = timer.stop();
char outputFilePath[1024];
if (decPath == nullptr) {
sprintf(outputFilePath, "%s.out", cmpPath);
} else {
strcpy(outputFilePath, decPath);
}
if (binaryOutput == 1) {
SZ::writefile<T>(outputFilePath, decData, conf.num);
} else {
SZ::writeTextFile<T>(outputFilePath, decData, conf.num);
}
if (printCmpResults) {
//compute the distortion / compression errors...
size_t totalNbEle;
auto ori_data = SZ::readfile<T>(inPath, totalNbEle);
assert(totalNbEle == conf.num);
SZ::verify<T>(ori_data.get(), decData, conf.num);
}
delete[]decData;
printf("compression ratio = %f\n", conf.num * sizeof(T) * 1.0 / cmpSize);
printf("decompression time = %f seconds.\n", compress_time);
printf("decompressed file = %s\n", outputFilePath);
}
int main(int argc, char *argv[]) {
bool binaryOutput = true;
int printCmpResults = 0;
bool compression = false;
bool decompression = false;
int dataType = SZ_FLOAT;
char *inPath = nullptr;
char *cmpPath = nullptr;
char *conPath = nullptr;
char *decPath = nullptr;
bool delCmpPath = false;
char *errBoundMode = nullptr;
char *errBound = nullptr;
char *absErrorBound = nullptr;
char *relErrorBound = nullptr;
char *pwrErrorBound = nullptr;
char *psnrErrorBound = nullptr;
char *normErrorBound = nullptr;
bool sz2mode = false;
size_t r4 = 0;
size_t r3 = 0;
size_t r2 = 0;
size_t r1 = 0;
size_t i = 0;
int status;
if (argc == 1)
usage();
int width = -1;
for (i = 1; i < argc; i++) {
if (argv[i][0] != '-' || argv[i][2]) {
if (argv[i][1] == 'h' && argv[i][2] == '2') {
usage_sz2();
} else {
usage();
}
}
switch (argv[i][1]) {
case 'h':
usage();
exit(0);
case 'v':
printf("version: %s\n", SZ3_VER);
exit(0);
case 'b':
binaryOutput = true;
break;
case 't':
binaryOutput = false;
break;
case 'a':
printCmpResults = 1;
break;
case 'z':
compression = true;
if (i + 1 < argc) {
cmpPath = argv[i + 1];
if (cmpPath[0] != '-')
i++;
else
cmpPath = nullptr;
}
break;
case 'x':
sz2mode = true;
decompression = true;
if (i + 1 < argc) {
decPath = argv[i + 1];
if (decPath[0] != '-')
i++;
else
decPath = nullptr;
}
break;
case 'f':
dataType = SZ_FLOAT;
break;
case 'd':
dataType = SZ_DOUBLE;
break;
case 'I':
if (++i == argc || sscanf(argv[i], "%d", &width) != 1) {
usage();
}
if (width == 32) {
dataType = SZ_INT32;
} else if (width == 64) {
dataType = SZ_INT64;
} else {
usage();
}
break;
case 'i':
if (++i == argc)
usage();
inPath = argv[i];
break;
case 'o':
if (++i == argc)
usage();
decPath = argv[i];
break;
case 's':
sz2mode = true;
if (++i == argc)
usage();
cmpPath = argv[i];
break;
case 'c':
if (++i == argc)
usage();
conPath = argv[i];
break;
case '1':
if (++i == argc || sscanf(argv[i], "%zu", &r1) != 1)
usage();
break;
case '2':
if (++i == argc || sscanf(argv[i], "%zu", &r1) != 1 ||
++i == argc || sscanf(argv[i], "%zu", &r2) != 1)
usage();
break;
case '3':
if (++i == argc || sscanf(argv[i], "%zu", &r1) != 1 ||
++i == argc || sscanf(argv[i], "%zu", &r2) != 1 ||
++i == argc || sscanf(argv[i], "%zu", &r3) != 1)
usage();
break;
case '4':
if (++i == argc || sscanf(argv[i], "%zu", &r1) != 1 ||
++i == argc || sscanf(argv[i], "%zu", &r2) != 1 ||
++i == argc || sscanf(argv[i], "%zu", &r3) != 1 ||
++i == argc || sscanf(argv[i], "%zu", &r4) != 1)
usage();
break;
case 'M':
if (++i == argc)
usage();
errBoundMode = argv[i];
if (i + 1 < argc && argv[i + 1][0] != '-') {
errBound = argv[++i];
}
break;
case 'A':
if (++i == argc)
usage();
absErrorBound = argv[i];
break;
case 'R':
if (++i == argc)
usage();
relErrorBound = argv[i];
break;
// case 'P':
// if (++i == argc)
// usage();
// pwrErrorBound = argv[i];
// break;
case 'N':
if (++i == argc)
usage();
normErrorBound = argv[i];
break;
case 'S':
if (++i == argc)
usage();
psnrErrorBound = argv[i];
break;
default:
usage();
break;
}
}
if ((inPath == nullptr) && (cmpPath == nullptr)) {
printf("Error: you need to specify either a raw binary data file or a compressed data file as input\n");
usage();
exit(0);
}
if (!sz2mode && inPath != nullptr && cmpPath != nullptr) {
compression = true;
}
if (cmpPath != nullptr && decPath != nullptr) {
decompression = true;
}
char cmpPathTmp[1024];
if (inPath != nullptr && cmpPath == nullptr && decPath != nullptr) {
compression = true;
decompression = true;
sprintf(cmpPathTmp, "%s.sz.tmp", inPath);
cmpPath = cmpPathTmp;
delCmpPath = true;
}
if (inPath == nullptr || errBoundMode == nullptr) {
compression = false;
}
if (!compression && !decompression) {
usage();
exit(0);
}
SZ::Config conf;
if (r2 == 0) {
conf = SZ::Config(r1);
} else if (r3 == 0) {
conf = SZ::Config(r2, r1);
} else if (r4 == 0) {
conf = SZ::Config(r3, r2, r1);
} else {
conf = SZ::Config(r4, r3, r2, r1);
}
if (compression && conPath != nullptr) {
conf.loadcfg(conPath);
}
if (errBoundMode != nullptr) {
{
// backward compatible with SZ2
if (relErrorBound != nullptr) {
conf.relErrorBound = atof(relErrorBound);
}
if (absErrorBound != nullptr) {
conf.absErrorBound = atof(absErrorBound);
}
if (psnrErrorBound != nullptr) {
conf.psnrErrorBound = atof(psnrErrorBound);
}
if (normErrorBound != nullptr) {
conf.l2normErrorBound = atof(normErrorBound);
}
}
if (strcmp(errBoundMode, SZ::EB_STR[SZ::EB_ABS]) == 0) {
conf.errorBoundMode = SZ::EB_ABS;
if (errBound != nullptr) {
conf.absErrorBound = atof(errBound);
}
} else if (strcmp(errBoundMode, SZ::EB_STR[SZ::EB_REL]) == 0 || strcmp(errBoundMode, "VR_REL") == 0) {
conf.errorBoundMode = SZ::EB_REL;
if (errBound != nullptr) {
conf.relErrorBound = atof(errBound);
}
} else if (strcmp(errBoundMode, SZ::EB_STR[SZ::EB_PSNR]) == 0) {
conf.errorBoundMode = SZ::EB_PSNR;
if (errBound != nullptr) {
conf.psnrErrorBound = atof(errBound);
}
} else if (strcmp(errBoundMode, SZ::EB_STR[SZ::EB_L2NORM]) == 0) {
conf.errorBoundMode = SZ::EB_L2NORM;
if (errBound != nullptr) {
conf.l2normErrorBound = atof(errBound);
}
} else if (strcmp(errBoundMode, SZ::EB_STR[SZ::EB_ABS_AND_REL]) == 0) {
conf.errorBoundMode = SZ::EB_ABS_AND_REL;
} else if (strcmp(errBoundMode, SZ::EB_STR[SZ::EB_ABS_OR_REL]) == 0) {
conf.errorBoundMode = SZ::EB_ABS_OR_REL;
} else {
printf("Error: wrong error bound mode setting by using the option '-M'\n");
usage();
exit(0);
}
}
if (compression) {
if (dataType == SZ_FLOAT) {
compress<float>(inPath, cmpPath, conf);
} else if (dataType == SZ_DOUBLE) {
compress<double>(inPath, cmpPath, conf);
} else if (dataType == SZ_INT32) {
compress<int32_t>(inPath, cmpPath, conf);
} else if (dataType == SZ_INT64) {
compress<int64_t>(inPath, cmpPath, conf);
} else {
printf("Error: data type not supported \n");
usage();
exit(0);
}
}
if (decompression) {
if (printCmpResults && inPath == nullptr) {
printf("Error: Since you add -a option (analysis), please specify the original data path by -i <path>.\n");
exit(0);
}
if (dataType == SZ_FLOAT) {
decompress<float>(inPath, cmpPath, decPath, conf, binaryOutput, printCmpResults);
} else if (dataType == SZ_DOUBLE) {
decompress<double>(inPath, cmpPath, decPath, conf, binaryOutput, printCmpResults);
} else if (dataType == SZ_INT32) {
decompress<int32_t>(inPath, cmpPath, decPath, conf, binaryOutput, printCmpResults);
} else if (dataType == SZ_INT64) {
decompress<int64_t>(inPath, cmpPath, decPath, conf, binaryOutput, printCmpResults);
} else {
printf("Error: data type not supported \n");
usage();
exit(0);
}
}
if (delCmpPath) {
remove(cmpPath);
}
return 0;
}