#include <stdio.h>
#include <blosc2.h>
#define KB 1024.
#define MB (1024*KB)
#define GB (1024*MB)
#define CHUNKSIZE (1000 * 1000)
#define NCHUNKS 100
#define NTHREADS 4
int main(void) {
blosc2_init();
static int32_t data[CHUNKSIZE];
int32_t isize = CHUNKSIZE * sizeof(int32_t);
int64_t nbytes, cbytes;
blosc2_cparams cparams = BLOSC2_CPARAMS_DEFAULTS;
blosc2_dparams dparams = BLOSC2_DPARAMS_DEFAULTS;
blosc2_schunk* schunk;
int i, nchunk;
blosc_timestamp_t last, current;
double ttotal;
printf("Blosc version info: %s (%s)\n", blosc2_get_version_string(), BLOSC2_VERSION_DATE);
cparams.typesize = sizeof(int32_t);
cparams.clevel = 9;
cparams.nthreads = NTHREADS;
dparams.nthreads = NTHREADS;
blosc2_storage storage = {.cparams=&cparams, .dparams=&dparams};
schunk = blosc2_schunk_new(&storage);
blosc_set_timestamp(&last);
for (nchunk = 0; nchunk < NCHUNKS; nchunk++) {
for (i = 0; i < CHUNKSIZE; i++) {
data[i] = i * nchunk;
}
int64_t nchunks = blosc2_schunk_append_buffer(schunk, data, isize);
if (nchunks != nchunk + 1) {
printf("Unexpected nchunks!");
return -1;
}
}
nbytes = schunk->nbytes;
cbytes = schunk->cbytes;
blosc_set_timestamp(¤t);
ttotal = blosc_elapsed_secs(last, current);
printf("Compression ratio: %.1f MB -> %.1f MB (%.1fx)\n",
(double)nbytes / MB, (double)cbytes / MB, (1. * (double)nbytes) / (double)cbytes);
printf("Compression time: %.3g s, %.1f MB/s\n",
ttotal, (double)nbytes / (ttotal * MB));
int64_t start = CHUNKSIZE + 3;
int64_t stop = CHUNKSIZE * 2 + 7;
int32_t *buffer = malloc((stop - start) * schunk->typesize);
for (i = 0; i < (stop - start); ++i) {
buffer[i] = i + NCHUNKS * CHUNKSIZE;
}
blosc_set_timestamp(&last);
int rc = blosc2_schunk_set_slice_buffer(schunk, start, stop, buffer);
if (rc < 0) {
printf("ERROR: %d, cannot set slice correctly.\n", rc);
return rc;
}
blosc_set_timestamp(¤t);
ttotal = blosc_elapsed_secs(last, current);
printf("set_slice_buffer time: %.3g s, %.1f MB/s\n",
ttotal, (double)nbytes / (ttotal * MB));
int32_t *res = malloc((stop - start) * schunk->typesize);
blosc_set_timestamp(&last);
rc = blosc2_schunk_get_slice_buffer(schunk, start, stop, res);
if (rc < 0) {
printf("ERROR: %d, cannot get slice correctly.\n", rc);
return rc;
}
blosc_set_timestamp(¤t);
ttotal = blosc_elapsed_secs(last, current);
printf("get_slice_buffer time: %.3g s, %.1f MB/s\n",
ttotal, (double)nbytes / (ttotal * MB));
for (i = 0; i < (stop - start); ++i) {
if(buffer[i] != res[i]) {
printf("Bad roundtrip\n");
return -1;
}
}
printf("Successful roundtrip data <-> schunk !\n");
blosc2_schunk_free(schunk);
free(buffer);
free(res);
blosc2_destroy();
return 0;
}