Debian Codegraph @

src/c-blosc2/bench/trunc_prec_schunk.c

raw
1 /*
2 Copyright (c) 2021 Blosc Development Team <[email protected]>
3 https://blosc.org
4 License: BSD 3-Clause (see LICENSE.txt)
5
6 Benchmark showing Blosc TRUNC_PREC filter from C code.
7
8 To compile this program:
9
10 $ gcc -O3 trunc_prec_schunk.c -o trunc_prec_schunk -lblosc2
11
12 */
13
14 #include <stdio.h>
15 #include <stdint.h>
16 #include <assert.h>
17 #include <math.h>
18 #include "blosc2.h"
19
20
21 #define KB 1024
22 #define MB (1024*KB)
23 #define GB (1024*MB)
24
25 #define NCHUNKS 200
26 #define CHUNKSIZE (500 * 1000)
27 #define NTHREADS 4
28
29
30 void fill_buffer(double *buffer, int nchunk) {
31 double incx = 10. / (NCHUNKS * CHUNKSIZE);
32
33 for (int i = 0; i < CHUNKSIZE; i++) {
34 double x = incx * (nchunk * CHUNKSIZE + i);
35 buffer[i] = (x - .25) * (x - 4.45) * (x - 8.95);
36 //buffer[i] = x;
37 }
38 }
39
40
41 int main(void) {
42 blosc2_schunk *schunk;
43 int32_t isize = CHUNKSIZE * sizeof(double);
44 int dsize;
45 int64_t nbytes, cbytes;
46 int nchunk;
47 int64_t nchunks = 0;
48 blosc_timestamp_t last, current;
49 double totaltime;
50 float totalsize = (float)(isize * NCHUNKS);
51 double *data_buffer = malloc(CHUNKSIZE * sizeof(double));
52 double *rec_buffer = malloc(CHUNKSIZE * sizeof(double));
53
54 printf("Blosc version info: %s (%s)\n",
55 BLOSC2_VERSION_STRING, BLOSC2_VERSION_DATE);
56
57 /* Initialize the Blosc compressor */
58 blosc2_init();
59
60 /* Create a super-chunk container */
61 blosc2_cparams cparams = BLOSC2_CPARAMS_DEFAULTS;
62 cparams.filters[0] = BLOSC_TRUNC_PREC;
63 cparams.filters_meta[0] = 23; // treat doubles as floats
64 cparams.typesize = sizeof(double);
65 // DELTA makes compression ratio quite worse in this case
66 //cparams.filters[1] = BLOSC_DELTA;
67 // BLOSC_BITSHUFFLE is not compressing better and it quite slower here
68 //cparams.filters[BLOSC2_MAX_FILTERS - 1] = BLOSC_BITSHUFFLE;
69 // Good codec params for this dataset
70 cparams.compcode = BLOSC_BLOSCLZ;
71 cparams.clevel = 9;
72 cparams.nthreads = NTHREADS;
73 blosc2_storage storage = {.cparams=&cparams, .contiguous=true};
74 schunk = blosc2_schunk_new(&storage);
75
76 /* Append the chunks */
77 blosc_set_timestamp(&last);
78 for (nchunk = 0; nchunk < NCHUNKS; nchunk++) {
79 fill_buffer(data_buffer, nchunk);
80 nchunks = blosc2_schunk_append_buffer(schunk, data_buffer, isize);
81 }
82 blosc_set_timestamp(&current);
83 totaltime = blosc_elapsed_secs(last, current);
84 printf("[Compr] Elapsed time:\t %6.3f s."
85 " Processed data: %.3f GB (%.3f GB/s)\n",
86 totaltime, totalsize / GB, totalsize / (GB * totaltime));
87
88 /* Gather some info */
89 nbytes = schunk->nbytes;
90 cbytes = schunk->cbytes;
91 printf("Compression super-chunk: %ld -> %ld (%.1fx)\n",
92 (long)nbytes, (long)cbytes, (1. * (double)nbytes) / (double)cbytes);
93
94 /* Retrieve and decompress the chunks */
95 blosc_set_timestamp(&last);
96 for (nchunk = 0; nchunk < NCHUNKS; nchunk++) {
97 dsize = blosc2_schunk_decompress_chunk(schunk, nchunk, rec_buffer, isize);
98 if (dsize < 0) {
99 printf("Decompression error. Error code: %d\n", dsize);
100 return dsize;
101 }
102 assert (dsize == (int)isize);
103 }
104 blosc_set_timestamp(&current);
105 totaltime = blosc_elapsed_secs(last, current);
106 totalsize = (float)(isize * nchunks);
107 printf("[Decompr] Elapsed time:\t %6.3f s."
108 " Processed data: %.3f GB (%.3f GB/s)\n",
109 totaltime, totalsize / GB, totalsize / (GB * totaltime));
110
111 /* Check that all the values are in the precision range */
112 blosc_set_timestamp(&last);
113 for (nchunk = 0; nchunk < NCHUNKS; nchunk++) {
114 dsize = blosc2_schunk_decompress_chunk(schunk, nchunk, (void *) rec_buffer, isize);
115 if (dsize < 0) {
116 printf("Decompression error. Error code: %d\n", dsize);
117 return dsize;
118 }
119 assert (dsize == (int)isize);
120 fill_buffer(data_buffer, nchunk);
121 for (int i = 0; i < CHUNKSIZE; i++) {
122 if (fabs(data_buffer[i] - rec_buffer[i]) > 1e-5) {
123 printf("Value not in tolerance margin: ");
124 printf("%g - %g: %g, (nchunk: %d, nelem: %d)\n",
125 data_buffer[i], rec_buffer[i],
126 (data_buffer[i] - rec_buffer[i]), nchunk, i);
127 return -1;
128 }
129 }
130 }
131 printf("All data did a good roundtrip!\n");
132
133 /* Free resources */
134 free(data_buffer);
135 free(rec_buffer);
136 /* Destroy the super-chunk */
137 blosc2_schunk_free(schunk);
138 /* Destroy the Blosc environment */
139 blosc2_destroy();
140
141 return 0;
142 }