1 /*
2  * Copyright (C) 2018 Francesc Alted, Aleix Alcacer.
3  * Copyright (C) 2019-present Blosc Development team <blosc@blosc.org>
4  * All rights reserved.
5  *
6  * This source code is licensed under both the BSD-style license (found in the
7  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
8  * in the COPYING file in the root directory of this source tree).
9  * You may select, at your option, one of the above-listed licenses.
10  */
11 
12 #define DATA_TYPE int64_t
13 
14 # include <caterva.h>
15 
main()16 int main() {
17     blosc_timestamp_t t0, t1;
18 
19     int nslices = 10;
20 
21     int8_t ndim = 3;
22     uint8_t itemsize = sizeof(DATA_TYPE);
23 
24     int64_t shape[] = {1250, 745, 400};
25 
26     int32_t chunkshape[] = {50, 150, 100};
27     int32_t blockshape[] = {13, 21, 30};
28 
29     int64_t nbytes = itemsize;
30     for (int i = 0; i < ndim; ++i) {
31         nbytes *= shape[i];
32     }
33 
34     DATA_TYPE *src = malloc(nbytes);
35     for (int i = 0; i < nbytes / itemsize; ++i) {
36         src[i] = i;
37     }
38 
39     caterva_config_t cfg = CATERVA_CONFIG_DEFAULTS;
40     cfg.nthreads = 4;
41 
42     caterva_ctx_t *ctx;
43     caterva_ctx_new(&cfg, &ctx);
44 
45     caterva_params_t params;
46     params.itemsize = itemsize;
47     params.ndim = ndim;
48     for (int i = 0; i < ndim; ++i) {
49         params.shape[i] = shape[i];
50     }
51 
52     caterva_storage_t storage = {0};
53     for (int i = 0; i < ndim; ++i) {
54         storage.chunkshape[i] = chunkshape[i];
55         storage.blockshape[i] = blockshape[i];
56     }
57 
58     caterva_array_t *arr;
59     blosc_set_timestamp(&t0);
60     CATERVA_ERROR(caterva_from_buffer(ctx, src, nbytes, &params, &storage, &arr));
61     blosc_set_timestamp(&t1);
62     printf("from_buffer: %.4f s\n", blosc_elapsed_secs(t0, t1));
63 
64     blosc_set_timestamp(&t0);
65 
66     for (int dim = 0; dim < ndim; ++dim) {
67         int64_t slice_start[CATERVA_MAX_DIM], slice_stop[CATERVA_MAX_DIM], slice_shape[CATERVA_MAX_DIM];
68         int64_t buffersize = itemsize;
69         for (int j = 0; j < ndim; ++j) {
70             slice_start[j] = 0;
71             slice_stop[j] = j == dim ? 1 : shape[j];
72             slice_shape[j] = slice_stop[j] - slice_start[j];
73             buffersize *= slice_shape[j];
74         }
75 
76         DATA_TYPE *buffer = malloc(buffersize);
77 
78         for (int slice = 0; slice < nslices; ++slice) {
79             slice_start[dim] = rand() % shape[dim];
80             slice_stop[dim] = slice_start[dim] + 1;
81             CATERVA_ERROR(caterva_get_slice_buffer(ctx, arr, slice_start, slice_stop, buffer, slice_shape, buffersize));
82         }
83         free(buffer);
84     }
85 
86     blosc_set_timestamp(&t1);
87     printf("get_slice: %.4f s\n", blosc_elapsed_secs(t0, t1));
88 
89     free(src);
90 
91     caterva_free(ctx, &arr);
92     caterva_ctx_free(&ctx);
93 
94     return 0;
95 }
96