Debian Codegraph @

src/c-blosc2/bench/create_frame.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 Simple benchmark for frame creation.
7
8 To run:
9
10 $ ./create_frame
11
12 *** Creating uninitialized ***
13 *** Using fill method! ***
14
15 *** Creating *contiguous* super-chunk for *blosclz*
16 Compression ratio: 186.26 GB -> 0.17 KB (1162790697.7x)
17 Compression time: 8.5e-06 s, 21399.9 TB/s
18 Decompression time: 0.0628 s, 2965.1 GB/s
19
20 *** Creating *sparse* super-chunk for *blosclz*
21 Compression ratio: 186.26 GB -> 3906.29 KB (49999.5x)
22 Compression time: 0.00294 s, 61.8 TB/s
23 Decompression time: 0.00424 s, 43959.2 GB/s
24
25 *** Creating *contiguous* super-chunk for *lz4*
26 Compression ratio: 186.26 GB -> 0.17 KB (1162790697.7x)
27 Compression time: 4.71e-06 s, 38636.1 TB/s
28 Decompression time: 0.0701 s, 2656.7 GB/s
29
30 *** Creating *sparse* super-chunk for *lz4*
31 Compression ratio: 186.26 GB -> 3906.29 KB (49999.5x)
32 Compression time: 0.00311 s, 58.5 TB/s
33 Decompression time: 0.0101 s, 18516.4 GB/s
34
35 Process finished with exit code 0
36
37 */
38
39 #include <stdio.h>
40 #include <blosc2.h>
41 #include <inttypes.h>
42
43 #define KB (1024.)
44 #define MB (1024 * KB)
45 #define GB (1024 * MB)
46 #define TB (1024 * GB)
47
48 #define CHUNKSHAPE (500 * 1000)
49 #define NCHUNKS 100000
50 #define NTHREADS 1 // curiously, using 1 single thread is better for the uninitialized values
51
52 // For exercising the optimized chunk creators (un)comment the lines below as you please
53 //#define CREATE_ZEROS
54 #define CREATE_FILL
55 //#define CREATE_LOOP
56
57 int create_cframe(const char* compname, bool contiguous) {
58 int32_t isize = CHUNKSHAPE * sizeof(int32_t);
59 int32_t* data = malloc(isize);
60 int32_t* data_dest = malloc(isize);
61 int32_t* data_dest2 = malloc(isize);
62 int64_t nbytes, cbytes;
63 int nchunk;
64 blosc_timestamp_t last, current;
65 double ttotal;
66 int compcode = blosc2_compname_to_compcode(compname);
67 printf("\n*** Creating *%s* super-chunk for *%s*\n",
68 contiguous ? "contiguous" : "sparse", compname);
69
70 /* Create a super-chunk container */
71 blosc2_cparams cparams = BLOSC2_CPARAMS_DEFAULTS;
72 cparams.typesize = sizeof(int32_t);
73 cparams.compcode = compcode;
74 cparams.clevel = 9;
75 cparams.nthreads = NTHREADS;
76 //cparams.blocksize = 1024;
77 blosc2_dparams dparams = BLOSC2_DPARAMS_DEFAULTS;
78 dparams.nthreads = NTHREADS;
79 char filename[64];
80 sprintf(filename, "frame_simple-%s.b2frame", compname);
81 blosc2_storage storage = {.cparams=&cparams, .dparams=&dparams,
82 .urlpath=NULL, .contiguous=contiguous};
83 blosc2_schunk* schunk = blosc2_schunk_new(&storage);
84
85 #ifdef CREATE_ZEROS
86 // Precompute chunk of zeros
87 int ret = blosc2_chunk_zeros(cparams, isize, data_dest, isize);
88 #else
89 int ret = blosc2_chunk_uninit(cparams, isize, data_dest, isize);
90 #endif
91 if (ret < 0) {
92 printf("Creation error in special chunk. Error code: %d\n", ret);
93 return ret;
94 }
95
96 // Add some data
97 blosc_set_timestamp(&last);
98
99 int64_t nitems;
100 #ifdef CREATE_FILL
101 // Make nitems a non-divisible number of CHUNKSHAPE
102 nitems = (int64_t)NCHUNKS * CHUNKSHAPE + 1;
103 #ifdef CREATE_ZEROS
104 // Precompute chunk of zeros
105 int special_value = BLOSC2_SPECIAL_ZERO;
106 #else
107 int special_value = BLOSC2_SPECIAL_UNINIT;
108 #endif
109 int64_t rc = blosc2_schunk_fill_special(schunk, nitems, special_value, isize);
110 if (rc < 0) {
111 printf("Error in fill special. Error code: %" PRId64 "\n", rc);
112 return (int)rc;
113 }
114 #else
115 // In these methods, nitems can only be an actual multiple of CHUNKSHAPE
116 nitems = (int64_t)NCHUNKS * CHUNKSHAPE;
117 for (nchunk = 0; nchunk < NCHUNKS; nchunk++) {
118 #ifdef CREATE_LOOP
119 int nchunks = blosc2_schunk_append_chunk(schunk, (uint8_t *) data_dest, true);
120 if (nchunks != nchunk + 1) {
121 printf("Compression error in append chunk. Error code: %d\n", nchunks);
122 return nchunk;
123 }
124 #else
125 for (int i = 0; i < CHUNKSHAPE; i++) {
126 // Different data patterns
127 // data[i] = i * nchunk;
128 // data[i] = nchunk;
129 data[i] = 0;
130 }
131 int nchunks = blosc2_schunk_append_buffer(schunk, data, isize);
132 if (nchunks != nchunk + 1) {
133 printf("Compression error appending in schunk. Error code: %d\n", nchunks);
134 return nchunk;
135 }
136 #endif
137 }
138 #endif
139 blosc_set_timestamp(&current);
140
141 /* Gather some info */
142 nbytes = schunk->nbytes;
143 cbytes = blosc2_schunk_frame_len(schunk);
144 ttotal = blosc_elapsed_secs(last, current);
145 printf("Compression ratio: %.2f GB -> %.2f KB (%4.1fx)\n",
146 (double)nbytes / GB, (double)cbytes / KB, (double)nbytes / (double)cbytes);
147 printf("Compression time: %.3g s, %.1f TB/s\n",
148 ttotal, (double)nbytes / (ttotal * TB));
149
150 /* Retrieve and decompress the chunks from the super-chunks and compare values */
151 blosc_set_timestamp(&last);
152 int32_t leftover_bytes = (int32_t)((nitems % CHUNKSHAPE) * sizeof(int32_t));
153 int32_t nchunks = leftover_bytes ? NCHUNKS + 1 : NCHUNKS;
154 for (nchunk = 0; nchunk < nchunks; nchunk++) {
155 int32_t dsize = blosc2_schunk_decompress_chunk(schunk, nchunk, data_dest, isize);
156 if (dsize < 0) {
157 printf("Decompression error in schunk. Error code: %d\n", dsize);
158 return dsize;
159 }
160 if ((nchunk == nchunks - 1) && (leftover_bytes > 0)) {
161 if (dsize != leftover_bytes) {
162 printf("Wrong size for last chunk. It is %d and should be: %d\n", dsize, leftover_bytes);
163 return dsize;
164 }
165 }
166 }
167 blosc_set_timestamp(&current);
168 ttotal = blosc_elapsed_secs(last, current);
169 printf("Decompression time: %.3g s, %.1f GB/s\n",
170 ttotal, (double)nbytes / (ttotal * GB));
171
172 /* Free resources */
173 blosc2_schunk_free(schunk);
174 free(data);
175 free(data_dest);
176 free(data_dest2);
177
178 return 0;
179 }
180
181
182 int main(void) {
183 blosc2_init();
184
185 #ifdef CREATE_ZEROS
186 printf("\n *** Creating zeros ***\n");
187 #else
188 printf("\n *** Creating uninitialized ***\n");
189 #endif
190 #ifdef CREATE_FILL
191 printf(" *** Using fill method! ***\n");
192 #elif defined(CREATE_LOOP)
193 printf(" *** Using loop method! ***\n");
194 #else
195 printf(" *** Using not optimized method! ***\n");
196 #endif
197
198 create_cframe("blosclz", true);
199 create_cframe("blosclz", false);
200 create_cframe("lz4", true);
201 create_cframe("lz4", false);
202
203 blosc2_destroy();
204 }