1 /* minimal code example showing how to call the zfp (de)compressor */
2
3 #include <math.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include "zfp.h"
8
9 /* compress or decompress array */
10 static int
compress(double * array,int nx,int ny,int nz,double tolerance,int decompress)11 compress(double* array, int nx, int ny, int nz, double tolerance, int decompress)
12 {
13 int status = 0; /* return value: 0 = success */
14 zfp_type type; /* array scalar type */
15 zfp_field* field; /* array meta data */
16 zfp_stream* zfp; /* compressed stream */
17 void* buffer; /* storage for compressed stream */
18 size_t bufsize; /* byte size of compressed buffer */
19 bitstream* stream; /* bit stream to write to or read from */
20 size_t zfpsize; /* byte size of compressed stream */
21
22 /* allocate meta data for the 3D array a[nz][ny][nx] */
23 type = zfp_type_double;
24 field = zfp_field_3d(array, type, nx, ny, nz);
25
26 /* allocate meta data for a compressed stream */
27 zfp = zfp_stream_open(NULL);
28
29 /* set compression mode and parameters via one of three functions */
30 /* zfp_stream_set_rate(zfp, rate, type, 3, 0); */
31 /* zfp_stream_set_precision(zfp, precision); */
32 zfp_stream_set_accuracy(zfp, tolerance);
33
34 /* allocate buffer for compressed data */
35 bufsize = zfp_stream_maximum_size(zfp, field);
36 buffer = malloc(bufsize);
37
38 /* associate bit stream with allocated buffer */
39 stream = stream_open(buffer, bufsize);
40 zfp_stream_set_bit_stream(zfp, stream);
41 zfp_stream_rewind(zfp);
42
43 /* compress or decompress entire array */
44 if (decompress) {
45 /* read compressed stream and decompress array */
46 zfpsize = fread(buffer, 1, bufsize, stdin);
47 if (!zfp_decompress(zfp, field)) {
48 fprintf(stderr, "decompression failed\n");
49 status = 1;
50 }
51 }
52 else {
53 /* compress array and output compressed stream */
54 zfpsize = zfp_compress(zfp, field);
55 if (!zfpsize) {
56 fprintf(stderr, "compression failed\n");
57 status = 1;
58 }
59 else
60 fwrite(buffer, 1, zfpsize, stdout);
61 }
62
63 /* clean up */
64 zfp_field_free(field);
65 zfp_stream_close(zfp);
66 stream_close(stream);
67 free(buffer);
68 free(array);
69
70 return status;
71 }
72
main(int argc,char * argv[])73 int main(int argc, char* argv[])
74 {
75 /* use -d to decompress rather than compress data */
76 int decompress = (argc == 2 && !strcmp(argv[1], "-d"));
77
78 /* allocate 100x100x100 array of doubles */
79 int nx = 100;
80 int ny = 100;
81 int nz = 100;
82 double* array = malloc(nx * ny * nz * sizeof(double));
83
84 if (!decompress) {
85 /* initialize array to be compressed */
86 int i, j, k;
87 for (k = 0; k < nz; k++)
88 for (j = 0; j < ny; j++)
89 for (i = 0; i < nx; i++) {
90 double x = 2.0 * i / nx;
91 double y = 2.0 * j / ny;
92 double z = 2.0 * k / nz;
93 array[i + nx * (j + ny * k)] = exp(-(x * x + y * y + z * z));
94 }
95 }
96
97 /* compress or decompress array */
98 return compress(array, nx, ny, nz, 1e-3, decompress);
99 }
100