Debian Codegraph @

src/c-blosc2/bench/b2nd/bench_get_slice.c

raw
1 /*********************************************************************
2 Blosc - Blocked Shuffling and Compression Library
3
4 Copyright (c) 2021 Blosc Development Team <[email protected]>
5 https://blosc.org
6 License: BSD 3-Clause (see LICENSE.txt)
7
8 See LICENSE.txt for details about copyright and rights to use.
9 **********************************************************************/
10
11 #define DATA_TYPE int64_t
12
13 # include <b2nd.h>
14
15 int main() {
16 blosc_timestamp_t t0, t1;
17
18 blosc2_init();
19
20 int nslices = 10;
21
22 int8_t ndim = 3;
23 int32_t itemsize = sizeof(DATA_TYPE);
24
25 int64_t shape[] = {1250, 745, 400};
26
27 int32_t chunkshape[] = {50, 150, 100};
28 int32_t blockshape[] = {13, 21, 30};
29
30 int64_t nbytes = itemsize;
31 for (int i = 0; i < ndim; ++i) {
32 nbytes *= shape[i];
33 }
34
35 DATA_TYPE *src = malloc(nbytes);
36 for (int i = 0; i < nbytes / itemsize; ++i) {
37 src[i] = i;
38 }
39
40 blosc2_cparams cparams = BLOSC2_CPARAMS_DEFAULTS;
41 cparams.nthreads = 4;
42 cparams.typesize = itemsize;
43 blosc2_storage b2_storage = {.cparams=&cparams};
44 b2nd_context_t *ctx = b2nd_create_ctx(&b2_storage, ndim, shape, chunkshape, blockshape, NULL, 0,
45 NULL, 0);
46
47 b2nd_array_t *arr;
48 blosc_set_timestamp(&t0);
49 BLOSC_ERROR(b2nd_from_cbuffer(ctx, &arr, src, nbytes));
50 blosc_set_timestamp(&t1);
51 printf("from_buffer: %.4f s\n", blosc_elapsed_secs(t0, t1));
52
53 blosc_set_timestamp(&t0);
54
55 for (int dim = 0; dim < ndim; ++dim) {
56 int64_t slice_start[B2ND_MAX_DIM], slice_stop[B2ND_MAX_DIM], slice_shape[B2ND_MAX_DIM];
57 int64_t buffersize = itemsize;
58 for (int j = 0; j < ndim; ++j) {
59 slice_start[j] = 0;
60 slice_stop[j] = j == dim ? 1 : shape[j];
61 slice_shape[j] = slice_stop[j] - slice_start[j];
62 buffersize *= slice_shape[j];
63 }
64
65 DATA_TYPE *buffer = malloc(buffersize);
66
67 for (int slice = 0; slice < nslices; ++slice) {
68 slice_start[dim] = rand() % shape[dim];
69 slice_stop[dim] = slice_start[dim] + 1;
70 BLOSC_ERROR(b2nd_get_slice_cbuffer(arr, slice_start, slice_stop, buffer, slice_shape, buffersize));
71 }
72 free(buffer);
73 }
74
75 blosc_set_timestamp(&t1);
76 printf("get_slice: %.4f s\n", blosc_elapsed_secs(t0, t1));
77
78 free(src);
79
80 BLOSC_ERROR(b2nd_free(arr));
81 BLOSC_ERROR(b2nd_free_ctx(ctx));
82
83 blosc2_destroy();
84
85 return 0;
86 }