Debian Codegraph @

src/SZ3/tools/sz3/sz3.cpp

raw
1 #include <cstdio>
2 #include <cstdlib>
3 #include <cmath>
4 #include "SZ3/api/sz.hpp"
5
6
7 #define SZ_FLOAT 0
8 #define SZ_DOUBLE 1
9 #define SZ_UINT8 2
10 #define SZ_INT8 3
11 #define SZ_UINT16 4
12 #define SZ_INT16 5
13 #define SZ_UINT32 6
14 #define SZ_INT32 7
15 #define SZ_UINT64 8
16 #define SZ_INT64 9
17
18 void usage() {
19 printf("Note: SZ3 command line arguments are backward compatible with SZ2, \n");
20 printf(" use -h2 to show the supported SZ2 command line arguments. \n");
21 printf("Usage: sz3 <options>\n");
22 printf("Options:\n");
23 printf("* general options:\n");
24 printf(" -h: print the help information\n");
25 printf(" -h2: print the help information for SZ2 style command line\n");
26 printf(" -v: print the version number\n");
27 printf(" -a : print compression results such as distortions\n");
28 printf("* input and output:\n");
29 printf(" -i <path> : original binary input file\n");
30 printf(" -o <path> : compressed output file, default in binary format\n");
31 printf(" -z <path> : compressed output (w -i) or input (w/o -i) file\n");
32 printf(" -t : store compressed output file in text format\n");
33 // printf(" -p: print meta data (configuration info)\n");
34 printf("* data type:\n");
35 printf(" -f: single precision (float type)\n");
36 printf(" -d: double precision (double type)\n");
37 printf(" -I <width>: integer type (width = 32 or 64)\n");
38 printf("* configuration file: \n");
39 printf(" -c <configuration file> : configuration file sz.config\n");
40 printf("* error control: (the error control parameters here will overwrite the setting in sz.config)\n");
41 printf(" -M <error control mode> <error bound (optional)> \n");
42 printf(" error control mode as follows: \n");
43 printf(" ABS (absolute error bound)\n");
44 printf(" REL (value range based error bound, so a.k.a., VR_REL)\n");
45 printf(" PSNR (peak signal-to-noise ratio)\n");
46 printf(" NORM (norm2 error : sqrt(sum(xi-xi')^2)\n");
47 printf(" ABS_AND_REL (using min{ABS, REL})\n");
48 printf(" ABS_OR_REL (using max{ABS, REL})\n");
49 printf(" error bound can be set directly after the error control mode, or separately with the following options:\n");
50 printf(" -A <absolute error bound>: specifying absolute error bound\n");
51 printf(" -R <value_range based relative error bound>: specifying relative error bound\n");
52 // printf(" -P <point-wise relative error bound>: specifying point-wise relative error bound\n");
53 printf(" -S <PSNR>: specifying PSNR\n");
54 printf(" -N <normErr>: specifying normErr\n");
55 printf("* dimensions: \n");
56 printf(" -1 <nx> : dimension for 1D data such as data[nx]\n");
57 printf(" -2 <nx> <ny> : dimensions for 2D data such as data[ny][nx]\n");
58 printf(" -3 <nx> <ny> <nz> : dimensions for 3D data such as data[nz][ny][nx] \n");
59 printf(" -4 <nx> <ny> <nz> <np>: dimensions for 4D data such as data[np][nz][ny][nx] \n");
60 printf("* examples: \n");
61 printf(" sz -f -i test.dat -z test.dat.sz -3 8 8 128 -M ABS 1e-3 \n");
62 printf(" sz -f -z test.dat.sz -o test.dat.sz.out -3 8 8 128 -M REL 1e-3 -a \n");
63 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");
64 printf(" sz -f -i test.dat -o test.dat.sz.out -3 8 8 128 -c sz.config \n");
65 printf(" sz -f -i test.dat -o test.dat.sz.out -3 8 8 128 -c sz.config -M ABS 1e-3 -a\n");
66 exit(0);
67 }
68
69 void usage_sz2() {
70 printf("Note: below are the supported command line arguments in SZ2 style\n");
71 printf("Usage: sz <options>\n");
72 printf("Options:\n");
73 printf("* operation type:\n");
74 printf(" -z <compressed file>: the compression operation with an optionally specified output file.\n");
75 printf(" (the compressed file will be named as <input_file>.sz if not specified)\n");
76 printf(" -x <decompressed file>: the decompression operation with an optionally specified output file\n");
77 printf(" (the decompressed file will be named as <cmpred_file>.out if not specified)\n");
78 // printf(" -p: print meta data (configuration info)\n");
79 printf(" -h: print the help information\n");
80 printf(" -v: print the version number\n");
81 printf("* data type:\n");
82 printf(" -f: single precision (float type)\n");
83 printf(" -d: double precision (double type)\n");
84 printf("* configuration file: \n");
85 printf(" -c <configuration file> : configuration file sz.config\n");
86 printf("* error control: (the error control parameters here will overwrite the setting in sz.config)\n");
87 printf(" -M <error bound mode> : 10 options as follows. \n");
88 printf(" ABS (absolute error bound)\n");
89 printf(" REL (value range based error bound, so a.k.a., VR_REL)\n");
90 printf(" ABS_AND_REL (using min{ABS, REL})\n");
91 printf(" ABS_OR_REL (using max{ABS, REL})\n");
92 printf(" PSNR (peak signal-to-noise ratio)\n");
93 printf(" NORM (norm2 error : sqrt(sum(xi-xi')^2)\n");
94 // printf(" PW_REL (point-wise relative error bound)\n");
95 printf(" -A <absolute error bound>: specifying absolute error bound\n");
96 printf(" -R <value_range based relative error bound>: specifying relative error bound\n");
97 // printf(" -P <point-wise relative error bound>: specifying point-wise relative error bound\n");
98 printf(" -S <PSNR>: specifying PSNR\n");
99 printf(" -N <normErr>: specifying normErr\n");
100 printf("* input data file:\n");
101 printf(" -i <original data file> : original data file\n");
102 printf(" -s <compressed data file> : compressed data file in decompression\n");
103 printf("* output type of decompressed file: \n");
104 printf(" -b (by default) : decompressed file stored in binary format\n");
105 printf(" -t : decompreadded file stored in text format\n");
106 // printf(" -T : pre-processing with Tucker Tensor Decomposition\n");
107 printf("* dimensions: \n");
108 printf(" -1 <nx> : dimension for 1D data such as data[nx]\n");
109 printf(" -2 <nx> <ny> : dimensions for 2D data such as data[ny][nx]\n");
110 printf(" -3 <nx> <ny> <nz> : dimensions for 3D data such as data[nz][ny][nx] \n");
111 printf(" -4 <nx> <ny> <nz> <np>: dimensions for 4D data such as data[np][nz][ny][nx] \n");
112 printf("* print compression results: \n");
113 printf(" -a : print compression results such as distortions\n");
114 printf("* examples: \n");
115 printf(" sz -z -f -c sz.config -i testdata/x86/testfloat_8_8_128.dat -3 8 8 128\n");
116 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");
117 printf(" sz -x -f -s testdata/x86/testfloat_8_8_128.dat.sz -3 8 8 128\n");
118 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");
119 printf(" sz -z -d -c sz.config -i testdata/x86/testdouble_8_8_128.dat -3 8 8 128\n");
120 printf(" sz -x -d -s testdata/x86/testdouble_8_8_128.dat.sz -3 8 8 128\n");
121 printf(" sz -p -s testdata/x86/testdouble_8_8_128.dat.sz\n");
122 exit(0);
123 }
124
125 template<class T>
126 void compress(char *inPath, char *cmpPath, SZ::Config conf) {
127 T *data = new T[conf.num];
128 SZ::readfile<T>(inPath, conf.num, data);
129
130 size_t outSize;
131 SZ::Timer timer(true);
132 char *bytes = SZ_compress<T>(conf, data, outSize);
133 double compress_time = timer.stop();
134
135 char outputFilePath[1024];
136 if (cmpPath == nullptr) {
137 sprintf(outputFilePath, "%s.sz", inPath);
138 } else {
139 strcpy(outputFilePath, cmpPath);
140 }
141 SZ::writefile(outputFilePath, bytes, outSize);
142
143 printf("compression ratio = %.2f \n", conf.num * 1.0 * sizeof(T) / outSize);
144 printf("compression time = %f\n", compress_time);
145 printf("compressed data file = %s\n", outputFilePath);
146
147 delete[]data;
148 delete[]bytes;
149 }
150
151 template<class T>
152 void decompress(char *inPath, char *cmpPath, char *decPath,
153 SZ::Config conf,
154 int binaryOutput, int printCmpResults) {
155
156 size_t cmpSize;
157 auto cmpData = SZ::readfile<char>(cmpPath, cmpSize);
158
159 SZ::Timer timer(true);
160 T *decData = SZ_decompress<T>(conf, cmpData.get(), cmpSize);
161 double compress_time = timer.stop();
162
163 char outputFilePath[1024];
164 if (decPath == nullptr) {
165 sprintf(outputFilePath, "%s.out", cmpPath);
166 } else {
167 strcpy(outputFilePath, decPath);
168 }
169 if (binaryOutput == 1) {
170 SZ::writefile<T>(outputFilePath, decData, conf.num);
171 } else {
172 SZ::writeTextFile<T>(outputFilePath, decData, conf.num);
173 }
174 if (printCmpResults) {
175 //compute the distortion / compression errors...
176 size_t totalNbEle;
177 auto ori_data = SZ::readfile<T>(inPath, totalNbEle);
178 assert(totalNbEle == conf.num);
179 SZ::verify<T>(ori_data.get(), decData, conf.num);
180 }
181 delete[]decData;
182
183 printf("compression ratio = %f\n", conf.num * sizeof(T) * 1.0 / cmpSize);
184 printf("decompression time = %f seconds.\n", compress_time);
185 printf("decompressed file = %s\n", outputFilePath);
186 }
187
188 int main(int argc, char *argv[]) {
189 bool binaryOutput = true;
190 int printCmpResults = 0;
191 bool compression = false;
192 bool decompression = false;
193 int dataType = SZ_FLOAT;
194 char *inPath = nullptr;
195 char *cmpPath = nullptr;
196 char *conPath = nullptr;
197 char *decPath = nullptr;
198 bool delCmpPath = false;
199
200 char *errBoundMode = nullptr;
201 char *errBound = nullptr;
202 char *absErrorBound = nullptr;
203 char *relErrorBound = nullptr;
204 char *pwrErrorBound = nullptr;
205 char *psnrErrorBound = nullptr;
206 char *normErrorBound = nullptr;
207
208 bool sz2mode = false;
209
210 size_t r4 = 0;
211 size_t r3 = 0;
212 size_t r2 = 0;
213 size_t r1 = 0;
214
215 size_t i = 0;
216 int status;
217 if (argc == 1)
218 usage();
219 int width = -1;
220
221 for (i = 1; i < argc; i++) {
222 if (argv[i][0] != '-' || argv[i][2]) {
223 if (argv[i][1] == 'h' && argv[i][2] == '2') {
224 usage_sz2();
225 } else {
226 usage();
227 }
228 }
229 switch (argv[i][1]) {
230 case 'h':
231 usage();
232 exit(0);
233 case 'v':
234 printf("version: %s\n", SZ3_VER);
235 exit(0);
236 case 'b':
237 binaryOutput = true;
238 break;
239 case 't':
240 binaryOutput = false;
241 break;
242 case 'a':
243 printCmpResults = 1;
244 break;
245 case 'z':
246 compression = true;
247 if (i + 1 < argc) {
248 cmpPath = argv[i + 1];
249 if (cmpPath[0] != '-')
250 i++;
251 else
252 cmpPath = nullptr;
253 }
254 break;
255 case 'x':
256 sz2mode = true;
257 decompression = true;
258 if (i + 1 < argc) {
259 decPath = argv[i + 1];
260 if (decPath[0] != '-')
261 i++;
262 else
263 decPath = nullptr;
264 }
265 break;
266 case 'f':
267 dataType = SZ_FLOAT;
268 break;
269 case 'd':
270 dataType = SZ_DOUBLE;
271 break;
272 case 'I':
273 if (++i == argc || sscanf(argv[i], "%d", &width) != 1) {
274 usage();
275 }
276 if (width == 32) {
277 dataType = SZ_INT32;
278 } else if (width == 64) {
279 dataType = SZ_INT64;
280 } else {
281 usage();
282 }
283 break;
284 case 'i':
285 if (++i == argc)
286 usage();
287 inPath = argv[i];
288 break;
289 case 'o':
290 if (++i == argc)
291 usage();
292 decPath = argv[i];
293 break;
294 case 's':
295 sz2mode = true;
296 if (++i == argc)
297 usage();
298 cmpPath = argv[i];
299 break;
300 case 'c':
301 if (++i == argc)
302 usage();
303 conPath = argv[i];
304 break;
305 case '1':
306 if (++i == argc || sscanf(argv[i], "%zu", &r1) != 1)
307 usage();
308 break;
309 case '2':
310 if (++i == argc || sscanf(argv[i], "%zu", &r1) != 1 ||
311 ++i == argc || sscanf(argv[i], "%zu", &r2) != 1)
312 usage();
313 break;
314 case '3':
315 if (++i == argc || sscanf(argv[i], "%zu", &r1) != 1 ||
316 ++i == argc || sscanf(argv[i], "%zu", &r2) != 1 ||
317 ++i == argc || sscanf(argv[i], "%zu", &r3) != 1)
318 usage();
319 break;
320 case '4':
321 if (++i == argc || sscanf(argv[i], "%zu", &r1) != 1 ||
322 ++i == argc || sscanf(argv[i], "%zu", &r2) != 1 ||
323 ++i == argc || sscanf(argv[i], "%zu", &r3) != 1 ||
324 ++i == argc || sscanf(argv[i], "%zu", &r4) != 1)
325 usage();
326 break;
327 case 'M':
328 if (++i == argc)
329 usage();
330 errBoundMode = argv[i];
331 if (i + 1 < argc && argv[i + 1][0] != '-') {
332 errBound = argv[++i];
333 }
334 break;
335 case 'A':
336 if (++i == argc)
337 usage();
338 absErrorBound = argv[i];
339 break;
340 case 'R':
341 if (++i == argc)
342 usage();
343 relErrorBound = argv[i];
344 break;
345 // case 'P':
346 // if (++i == argc)
347 // usage();
348 // pwrErrorBound = argv[i];
349 // break;
350 case 'N':
351 if (++i == argc)
352 usage();
353 normErrorBound = argv[i];
354 break;
355 case 'S':
356 if (++i == argc)
357 usage();
358 psnrErrorBound = argv[i];
359 break;
360 default:
361 usage();
362 break;
363 }
364 }
365
366 if ((inPath == nullptr) && (cmpPath == nullptr)) {
367 printf("Error: you need to specify either a raw binary data file or a compressed data file as input\n");
368 usage();
369 exit(0);
370 }
371
372 if (!sz2mode && inPath != nullptr && cmpPath != nullptr) {
373 compression = true;
374 }
375 if (cmpPath != nullptr && decPath != nullptr) {
376 decompression = true;
377 }
378 char cmpPathTmp[1024];
379 if (inPath != nullptr && cmpPath == nullptr && decPath != nullptr) {
380 compression = true;
381 decompression = true;
382 sprintf(cmpPathTmp, "%s.sz.tmp", inPath);
383 cmpPath = cmpPathTmp;
384 delCmpPath = true;
385 }
386 if (inPath == nullptr || errBoundMode == nullptr) {
387 compression = false;
388 }
389 if (!compression && !decompression) {
390 usage();
391 exit(0);
392 }
393
394 SZ::Config conf;
395 if (r2 == 0) {
396 conf = SZ::Config(r1);
397 } else if (r3 == 0) {
398 conf = SZ::Config(r2, r1);
399 } else if (r4 == 0) {
400 conf = SZ::Config(r3, r2, r1);
401 } else {
402 conf = SZ::Config(r4, r3, r2, r1);
403 }
404 if (compression && conPath != nullptr) {
405 conf.loadcfg(conPath);
406 }
407
408 if (errBoundMode != nullptr) {
409 {
410 // backward compatible with SZ2
411 if (relErrorBound != nullptr) {
412 conf.relErrorBound = atof(relErrorBound);
413 }
414 if (absErrorBound != nullptr) {
415 conf.absErrorBound = atof(absErrorBound);
416 }
417 if (psnrErrorBound != nullptr) {
418 conf.psnrErrorBound = atof(psnrErrorBound);
419 }
420 if (normErrorBound != nullptr) {
421 conf.l2normErrorBound = atof(normErrorBound);
422 }
423 }
424 if (strcmp(errBoundMode, SZ::EB_STR[SZ::EB_ABS]) == 0) {
425 conf.errorBoundMode = SZ::EB_ABS;
426 if (errBound != nullptr) {
427 conf.absErrorBound = atof(errBound);
428 }
429 } else if (strcmp(errBoundMode, SZ::EB_STR[SZ::EB_REL]) == 0 || strcmp(errBoundMode, "VR_REL") == 0) {
430 conf.errorBoundMode = SZ::EB_REL;
431 if (errBound != nullptr) {
432 conf.relErrorBound = atof(errBound);
433 }
434 } else if (strcmp(errBoundMode, SZ::EB_STR[SZ::EB_PSNR]) == 0) {
435 conf.errorBoundMode = SZ::EB_PSNR;
436 if (errBound != nullptr) {
437 conf.psnrErrorBound = atof(errBound);
438 }
439 } else if (strcmp(errBoundMode, SZ::EB_STR[SZ::EB_L2NORM]) == 0) {
440 conf.errorBoundMode = SZ::EB_L2NORM;
441 if (errBound != nullptr) {
442 conf.l2normErrorBound = atof(errBound);
443 }
444 } else if (strcmp(errBoundMode, SZ::EB_STR[SZ::EB_ABS_AND_REL]) == 0) {
445 conf.errorBoundMode = SZ::EB_ABS_AND_REL;
446 } else if (strcmp(errBoundMode, SZ::EB_STR[SZ::EB_ABS_OR_REL]) == 0) {
447 conf.errorBoundMode = SZ::EB_ABS_OR_REL;
448 } else {
449 printf("Error: wrong error bound mode setting by using the option '-M'\n");
450 usage();
451 exit(0);
452 }
453 }
454
455 if (compression) {
456
457 if (dataType == SZ_FLOAT) {
458 compress<float>(inPath, cmpPath, conf);
459 } else if (dataType == SZ_DOUBLE) {
460 compress<double>(inPath, cmpPath, conf);
461 } else if (dataType == SZ_INT32) {
462 compress<int32_t>(inPath, cmpPath, conf);
463 } else if (dataType == SZ_INT64) {
464 compress<int64_t>(inPath, cmpPath, conf);
465 } else {
466 printf("Error: data type not supported \n");
467 usage();
468 exit(0);
469 }
470 }
471 if (decompression) {
472 if (printCmpResults && inPath == nullptr) {
473 printf("Error: Since you add -a option (analysis), please specify the original data path by -i <path>.\n");
474 exit(0);
475 }
476
477 if (dataType == SZ_FLOAT) {
478 decompress<float>(inPath, cmpPath, decPath, conf, binaryOutput, printCmpResults);
479 } else if (dataType == SZ_DOUBLE) {
480 decompress<double>(inPath, cmpPath, decPath, conf, binaryOutput, printCmpResults);
481 } else if (dataType == SZ_INT32) {
482 decompress<int32_t>(inPath, cmpPath, decPath, conf, binaryOutput, printCmpResults);
483 } else if (dataType == SZ_INT64) {
484 decompress<int64_t>(inPath, cmpPath, decPath, conf, binaryOutput, printCmpResults);
485 } else {
486 printf("Error: data type not supported \n");
487 usage();
488 exit(0);
489 }
490 }
491 if (delCmpPath) {
492 remove(cmpPath);
493 }
494 return 0;
495 }