1 /*
2 Copyright (C) 2021 The Blosc Developers <blosc@blosc.org>
3 https://blosc.org
4 License: BSD 3-Clause (see LICENSE.txt)
5
6 Example program demonstrating use of the Blosc filter from C code.
7
8 To compile this program:
9
10 $ gcc schunk_simple.c -o schunk_simple -lblosc2
11
12 To run:
13
14 $ ./schunk_simple
15 Blosc version info: 2.0.0-beta.1 ($Date:: 2019-08-09 #$)
16 Compression ratio: 381.5 MB -> 12.2 MB (31.2x)
17 Compression time: 0.119 s, 3192.9 MB/s
18 Decompression time: 0.035 s, 10888.3 MB/s
19 Successful roundtrip data <-> schunk !
20
21 */
22
23 #include <stdio.h>
24 #include <blosc2.h>
25
26 #define KB 1024.
27 #define MB (1024*KB)
28 #define GB (1024*MB)
29
30 #define CHUNKSIZE (1000 * 1000)
31 #define NCHUNKS 100
32 #define NTHREADS 4
33
main(void)34 int main(void) {
35 static int32_t data[CHUNKSIZE];
36 static int32_t data_dest[CHUNKSIZE];
37 int32_t isize = CHUNKSIZE * sizeof(int32_t);
38 int dsize;
39 int64_t nbytes, cbytes;
40 blosc2_cparams cparams = BLOSC2_CPARAMS_DEFAULTS;
41 blosc2_dparams dparams = BLOSC2_DPARAMS_DEFAULTS;
42 blosc2_schunk* schunk;
43 int i, nchunk;
44 blosc_timestamp_t last, current;
45 double ttotal;
46
47 printf("Blosc version info: %s (%s)\n", blosc_get_version_string(), BLOSC_VERSION_DATE);
48
49 /* Create a super-chunk container */
50 cparams.typesize = sizeof(int32_t);
51 cparams.clevel = 9;
52 cparams.nthreads = NTHREADS;
53 dparams.nthreads = NTHREADS;
54 blosc2_storage storage = {.cparams=&cparams, .dparams=&dparams};
55 schunk = blosc2_schunk_new(&storage);
56
57 blosc_set_timestamp(&last);
58 for (nchunk = 0; nchunk < NCHUNKS; nchunk++) {
59 for (i = 0; i < CHUNKSIZE; i++) {
60 data[i] = i * nchunk;
61 }
62 int nchunks = blosc2_schunk_append_buffer(schunk, data, isize);
63 if (nchunks != nchunk + 1) {
64 printf("Unexpected nchunks!");
65 return -1;
66 }
67 }
68 /* Gather some info */
69 nbytes = schunk->nbytes;
70 cbytes = schunk->cbytes;
71 blosc_set_timestamp(¤t);
72 ttotal = blosc_elapsed_secs(last, current);
73 printf("Compression ratio: %.1f MB -> %.1f MB (%.1fx)\n",
74 nbytes / MB, cbytes / MB, (1. * nbytes) / cbytes);
75 printf("Compression time: %.3g s, %.1f MB/s\n",
76 ttotal, nbytes / (ttotal * MB));
77
78 /* Retrieve and decompress the chunks (0-based count) */
79 blosc_set_timestamp(&last);
80 for (nchunk = NCHUNKS-1; nchunk >= 0; nchunk--) {
81 dsize = blosc2_schunk_decompress_chunk(schunk, nchunk, data_dest, isize);
82 if (dsize < 0) {
83 printf("Decompression error. Error code: %d\n", dsize);
84 return dsize;
85 }
86 }
87 blosc_set_timestamp(¤t);
88 ttotal = blosc_elapsed_secs(last, current);
89 printf("Decompression time: %.3g s, %.1f MB/s\n",
90 ttotal, nbytes / (ttotal * MB));
91
92 /* Check integrity of the second chunk (made of non-zeros) */
93 blosc2_schunk_decompress_chunk(schunk, 1, data_dest, isize);
94 for (i = 0; i < CHUNKSIZE; i++) {
95 if (data_dest[i] != i) {
96 printf("Decompressed data differs from original %d, %d!\n",
97 i, data_dest[i]);
98 return -1;
99 }
100 }
101
102 printf("Successful roundtrip data <-> schunk !\n");
103
104 /* Free resources */
105 /* Destroy the super-chunk */
106 blosc2_schunk_free(schunk);
107
108 return 0;
109 }
110