Debian Codegraph @

src/c-blosc2/bench/b2nd/bench_stack_append.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 // Benchmark for appending data to a b2nd array. A new accelerated path has been
12 // added to b2nd_append() that allows for faster appending of data to a b2nd array
13 // when data to append is of the same size as the chunkshape. This benchmark
14 // compares the performance of the new accelerated path with the old one.
15
16 #include <inttypes.h>
17 #include "blosc2.h"
18 #include "b2nd.h"
19
20
21 int main() {
22 blosc2_init();
23 const int width = 512;
24 const int height = 256;
25 const int nimages_inbuf = 10;
26 const int64_t buffershape[] = {nimages_inbuf, height, width};
27 int64_t N_images = 1000;
28
29 // Shapes of the b2nd array
30 int64_t shape[] = {N_images, height, width};
31 int32_t chunkshape[] = {nimages_inbuf, height, width};
32 int32_t blockshape[] = {1, height, width};
33
34 // Determine the buffer size of the image (in bytes)
35 const int64_t buffersize = nimages_inbuf * height * width * (int64_t)sizeof(uint16_t);
36 uint16_t* image = malloc(buffersize);
37
38 // Generate data
39 for (int j = 0; j < nimages_inbuf * height * width; j++) {
40 image[j] = j;
41 }
42
43 char *urlpath = "bench_stack_append.b2nd";
44 blosc2_cparams cparams = BLOSC2_CPARAMS_DEFAULTS;
45 cparams.typesize = sizeof(image[0]);
46 blosc2_storage storage = BLOSC2_STORAGE_DEFAULTS;
47 //storage.contiguous = true; // for a single file in output
48 //storage.urlpath = urlpath;
49 storage.cparams = &cparams;
50
51 char *accel_str = "non-accel";
52 for (int accel=0; accel < 2; accel++) {
53 if (accel) {
54 shape[0] = 0;
55 accel_str = "accel";
56 }
57
58 blosc2_remove_urlpath(urlpath);
59
60 b2nd_context_t *ctx = b2nd_create_ctx(&storage, 3,
61 shape, chunkshape, blockshape,
62 "|u2", DTYPE_NUMPY_FORMAT,
63 NULL, 0);
64 b2nd_array_t *src;
65 if (b2nd_empty(ctx, &src) < 0) {
66 printf("Error in b2nd_empty\n");
67 return -1;
68 }
69
70 // loop through all images
71 blosc_timestamp_t t0, t1;
72 blosc_set_timestamp(&t0);
73 for (int i = 0; i < N_images / nimages_inbuf; i++) {
74 if (accel) {
75 if (b2nd_append(src, image, buffersize, 0) < 0) {
76 printf("Error in b2nd_append\n");
77 return -1;
78 }
79 } else {
80 int64_t start[] = {i, 0, 0};
81 int64_t stop[] = {i + nimages_inbuf, height, width};
82 if (b2nd_set_slice_cbuffer(image, buffershape, buffersize, start, stop, src) < 0) {
83 printf("Error in b2nd_append\n");
84 return -1;
85 }
86 }
87 }
88 blosc_set_timestamp(&t1);
89 printf("Time to append (%s): %.4f s\n", accel_str, blosc_elapsed_secs(t0, t1));
90 printf("Number of chunks: %" PRId64 "\n", src->sc->nchunks);
91 printf("Shape of array: (%" PRId64 ", %" PRId64 ", %" PRId64 ")\n",
92 src->shape[0], src->shape[1], src->shape[2]);
93
94 b2nd_free(src);
95 b2nd_free_ctx(ctx);
96 }
97 free(image);
98
99 blosc2_destroy();
100 return 0;
101 }