Skip to content

Commit 1829f09

Browse files
author
Luke Shaw
committed
Change decompress_chunk to get_chunk into cbuffer
1 parent 40783eb commit 1829f09

File tree

3 files changed

+111
-51
lines changed

3 files changed

+111
-51
lines changed

blosc/b2nd.c

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -776,10 +776,6 @@ int get_set_slice(void *buffer, int64_t buffersize, const int64_t *start, const
776776
}
777777

778778
uint8_t *buffer_b = buffer;
779-
const int64_t *buffer_start = start;
780-
const int64_t *buffer_stop = stop;
781-
const int64_t *buffer_shape = shape;
782-
783779
int8_t ndim = array->ndim;
784780

785781
// 0-dim case
@@ -884,11 +880,11 @@ int get_set_slice(void *buffer, int64_t buffersize, const int64_t *start, const
884880
int64_t update_nchunks = 1;
885881
for (int i = 0; i < ndim; ++i) {
886882
int64_t pos = 0;
887-
while (pos <= buffer_start[i]) {
883+
while (pos <= start[i]) {
888884
pos += array->chunkshape[i];
889885
}
890886
update_start[i] = pos / array->chunkshape[i] - 1;
891-
while (pos < buffer_stop[i]) {
887+
while (pos < stop[i]) {
892888
pos += array->chunkshape[i];
893889
}
894890
update_shape[i] = pos / array->chunkshape[i] - update_start[i];
@@ -916,7 +912,7 @@ int get_set_slice(void *buffer, int64_t buffersize, const int64_t *start, const
916912
}
917913
bool chunk_empty = false;
918914
for (int i = 0; i < ndim; ++i) {
919-
chunk_empty |= (chunk_stop[i] <= buffer_start[i] || chunk_start[i] >= buffer_stop[i]);
915+
chunk_empty |= (chunk_stop[i] <= start[i] || chunk_start[i] >= stop[i]);
920916
}
921917
if (chunk_empty) {
922918
continue;
@@ -927,7 +923,7 @@ int get_set_slice(void *buffer, int64_t buffersize, const int64_t *start, const
927923
// Check if all the chunk is going to be updated and avoid the decompression
928924
bool decompress_chunk = false;
929925
for (int i = 0; i < ndim; ++i) {
930-
decompress_chunk |= (chunk_start[i] < buffer_start[i] || chunk_stop[i] > buffer_stop[i]);
926+
decompress_chunk |= (chunk_start[i] < start[i] || chunk_stop[i] > stop[i]);
931927
}
932928

933929
if (decompress_chunk) {
@@ -1022,8 +1018,8 @@ int get_set_slice(void *buffer, int64_t buffersize, const int64_t *start, const
10221018
// compute the start of the slice inside the block
10231019
int64_t slice_start[B2ND_MAX_DIM] = {0};
10241020
for (int i = 0; i < ndim; ++i) {
1025-
if (block_start[i] < buffer_start[i]) {
1026-
slice_start[i] = buffer_start[i] - block_start[i];
1021+
if (block_start[i] < start[i]) {
1022+
slice_start[i] = start[i] - block_start[i];
10271023
} else {
10281024
slice_start[i] = 0;
10291025
}
@@ -1032,8 +1028,8 @@ int get_set_slice(void *buffer, int64_t buffersize, const int64_t *start, const
10321028

10331029
int64_t slice_stop[B2ND_MAX_DIM] = {0};
10341030
for (int i = 0; i < ndim; ++i) {
1035-
if (block_stop[i] > buffer_stop[i]) {
1036-
slice_stop[i] = block_shape[i] - (block_stop[i] - buffer_stop[i]);
1031+
if (block_stop[i] > stop[i]) {
1032+
slice_stop[i] = block_shape[i] - (block_stop[i] - stop[i]);
10371033
} else {
10381034
slice_stop[i] = block_stop[i] - block_start[i];
10391035
}
@@ -1046,13 +1042,12 @@ int get_set_slice(void *buffer, int64_t buffersize, const int64_t *start, const
10461042
}
10471043

10481044
uint8_t *src = &buffer_b[0];
1049-
const int64_t *src_pad_shape = buffer_shape;
10501045

10511046
int64_t src_start[B2ND_MAX_DIM] = {0};
10521047
int64_t src_stop[B2ND_MAX_DIM] = {0};
10531048
for (int i = 0; i < ndim; ++i) {
1054-
src_start[i] = slice_start[i] - buffer_start[i];
1055-
src_stop[i] = slice_stop[i] - buffer_start[i];
1049+
src_start[i] = slice_start[i] - start[i];
1050+
src_stop[i] = slice_stop[i] - start[i];
10561051
}
10571052

10581053
uint8_t *dst = &data[nblock * array->blocknitems * array->sc->typesize];
@@ -1070,12 +1065,12 @@ int get_set_slice(void *buffer, int64_t buffersize, const int64_t *start, const
10701065

10711066
if (set_slice) {
10721067
b2nd_copy_buffer2(ndim, array->sc->typesize,
1073-
src, src_pad_shape, src_start, src_stop,
1068+
src, shape, src_start, src_stop,
10741069
dst, dst_pad_shape, dst_start);
10751070
} else {
10761071
b2nd_copy_buffer2(ndim, array->sc->typesize,
10771072
dst, dst_pad_shape, dst_start, dst_stop,
1078-
src, src_pad_shape, src_start);
1073+
src, shape, src_start);
10791074
}
10801075
}
10811076

@@ -1390,11 +1385,6 @@ int b2nd_concatenate(b2nd_context_t *ctx, const b2nd_array_t *src1, const b2nd_a
13901385
void *buffer = malloc(src2->sc->typesize * src2->extchunknitems);
13911386
BLOSC_ERROR_NULL(buffer, BLOSC2_ERROR_MEMORY_ALLOC);
13921387
for (int64_t nchunk = 0; nchunk < src2->sc->nchunks; ++nchunk) {
1393-
if (blosc2_schunk_decompress_chunk(src2->sc, nchunk, buffer,
1394-
src2->sc->typesize * (int32_t)src2->extchunknitems) <= 0) {
1395-
BLOSC_TRACE_ERROR("Error decompressing chunk");
1396-
goto cleanup;
1397-
}
13981388
// Get multidimensional chunk position
13991389
int64_t nchunk_ndim[B2ND_MAX_DIM] = {0};
14001390
int64_t chunkshape[B2ND_MAX_DIM] = {0};
@@ -1414,21 +1404,20 @@ int b2nd_concatenate(b2nd_context_t *ctx, const b2nd_array_t *src1, const b2nd_a
14141404
if (stop[i] > src2->shape[i]) {
14151405
stop[i] = src2->shape[i]; // Handle boundary chunks
14161406
}
1417-
1418-
// Apply offset only for concatenation axis
1419-
if (i == axis) {
1420-
start[i] += src1_shape[i];
1421-
stop[i] += src1_shape[i];
1422-
}
14231407
}
1408+
// Load chunk into buffer
1409+
BLOSC_ERROR(b2nd_get_slice_cbuffer(src2, start, stop, buffer, chunkshape, src2->sc->chunksize));
1410+
1411+
// Apply chunk offset only for concatenation axis
1412+
start[axis] += src1_shape[axis];
1413+
stop[axis] += src1_shape[axis];
14241414

14251415
// Copy the chunk to the correct position
14261416
BLOSC_ERROR(b2nd_set_slice_cbuffer(buffer, chunkshape,
14271417
src2->sc->typesize * src2->extchunknitems,
14281418
start, stop, *array));
14291419
}
14301420

1431-
cleanup:
14321421
free(buffer);
14331422

14341423
return BLOSC2_ERROR_SUCCESS;

blosc/directories.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@
5454
sprintf(fname, "%s\\%s", dir_path, cfile.name);
5555

5656
ret = remove(fname);
57-
free(fname);
5857
if (ret < 0) {
5958
BLOSC_TRACE_ERROR("Could not remove file %s", fname);
6059
_findclose(file);
6160
return BLOSC2_ERROR_FAILURE;
6261
}
62+
free(fname);
6363
}
6464

6565
rmdir(dir_path);

tests/b2nd/test_b2nd_concatenate.c

Lines changed: 92 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,46 @@ static int fill_recursive_region(uint8_t *buffer,
5151
}
5252

5353
/**
54-
* Fill a region of a multidimensional buffer with a constant value.
54+
* Helper function for recursive region filling with arange
55+
*/
56+
static int fill_recursive_arange(uint8_t *buffer,
57+
int64_t *strides,
58+
int8_t ndim,
59+
int64_t *start,
60+
int64_t *stop,
61+
int64_t value,
62+
uint8_t typesize,
63+
int dim,
64+
int64_t current_offset) {
65+
if (dim == ndim) {
66+
// We've reached the innermost dimension, copy the value
67+
memcpy(buffer + (current_offset * typesize), &value, typesize);
68+
// Increment value for arange
69+
return value + 1;
70+
}
71+
72+
// Iterate through the current dimension within the region
73+
int64_t val = value;
74+
for (int64_t i = start[dim]; i < stop[dim]; i++) {
75+
int64_t offset = current_offset + i * strides[dim];
76+
val = fill_recursive_arange(buffer, strides, ndim, start, stop,
77+
val, typesize, dim + 1, offset);
78+
if (val < 0) return val;
79+
}
80+
return val;
81+
}
82+
83+
/**
84+
* Fill a region of a multidimensional buffer with a constant value or linspace.
5585
*/
5686
int fill_buffer_region(uint8_t *buffer,
5787
int64_t *buffer_shape,
5888
int8_t ndim,
5989
int64_t *start,
6090
int64_t *stop,
6191
const void *value,
62-
uint8_t typesize) {
92+
uint8_t typesize,
93+
bool arange) {
6394
// Calculate strides for the buffer
6495
int64_t strides[B2ND_MAX_DIM];
6596
strides[ndim - 1] = 1;
@@ -68,8 +99,15 @@ int fill_buffer_region(uint8_t *buffer,
6899
}
69100

70101
// Start the recursive filling
71-
return fill_recursive_region(buffer, strides, ndim, start, stop,
72-
value, typesize, 0, 0);
102+
if (arange) {
103+
// If arange is true, fill with increasing values
104+
return fill_recursive_arange(buffer, strides, ndim, start, stop,
105+
0, typesize, 0, 0);
106+
}
107+
else{
108+
return fill_recursive_region(buffer, strides, ndim, start, stop,
109+
value, typesize, 0, 0);
110+
}
73111
}
74112

75113

@@ -79,8 +117,8 @@ CUTEST_TEST_SETUP(concatenate) {
79117
// Add parametrization
80118
CUTEST_PARAMETRIZE(typesize, uint8_t, CUTEST_DATA(
81119
1,
82-
// 2,
83-
// 4,
120+
2,
121+
4,
84122
8,
85123
13,
86124
));
@@ -100,9 +138,9 @@ CUTEST_TEST_SETUP(concatenate) {
100138
{1, 0, {50}, {25}, {5}, {20}, {25}, {5}},
101139
{1, 0, {2}, {25}, {5}, {49}, {25}, {5}},
102140
// 2-dim
103-
{2, 0, {50, 50}, {25, 13}, {5, 8}, {50, 50}, {25, 13}, {5, 8}},
104-
{2, 1, {50, 50}, {25, 13}, {5, 8}, {50, 50}, {25, 13}, {5, 8}},
105-
{2, 0, {25, 50}, {25, 25}, {5, 5}, {2, 50}, {25, 25}, {5, 5}},
141+
{2, 0, {10, 10}, {2, 2}, {1, 1}, {4, 10}, {2, 2}, {1, 1}},
142+
{2, 1, {10, 8}, {2, 2}, {1, 1}, {10, 8}, {2, 2}, {1, 1}},
143+
{2, 0, {4, 4}, {4, 4}, {2, 2}, {4, 4}, {4, 4}, {2, 2}}, //Fails
106144
{2, 1, {25, 50}, {25, 25}, {5, 5}, {25, 5}, {25, 25}, {5, 5}},
107145
// 3-dim
108146
{3, 0, {50, 5, 50}, {25, 13, 10}, {5, 8, 5}, {50, 5, 50}, {25, 13, 10}, {5, 8, 5}},
@@ -111,9 +149,9 @@ CUTEST_TEST_SETUP(concatenate) {
111149
{3, 0, {5, 5, 50}, {25, 13, 10}, {5, 8, 5}, {51, 5, 50}, {25, 13, 10}, {5, 8, 5}},
112150
// Inner 0-dims are supported
113151
{3, 1, {50, 1, 50}, {25, 13, 10}, {5, 8, 5}, {50, 0, 50}, {25, 13, 10}, {5, 8, 5}},
114-
// TODO: the next is not working yet
152+
// // TODO: the next is not working yet
115153
// {3, 2, {50, 50, 0}, {25, 13, 10}, {5, 8, 5}, {50, 50, 49}, {25, 13, 10}, {5, 8, 5}},
116-
// 4-dim
154+
// // 4-dim
117155
{4, 0, {5, 5, 5, 5}, {2, 5, 10, 5}, {5, 2, 5, 2}, {5, 5, 5, 5}, {5, 5, 10, 5}, {5, 2, 5, 2}},
118156
{4, 1, {5, 5, 5, 5}, {2, 5, 10, 5}, {5, 2, 5, 2}, {5, 5, 5, 5}, {5, 5, 10, 5}, {5, 2, 5, 2}},
119157
{4, 2, {5, 5, 5, 5}, {2, 13, 10, 5}, {5, 8, 5, 2}, {5, 5, 5, 5}, {5, 13, 10, 5}, {5, 8, 5, 2}},
@@ -122,18 +160,22 @@ CUTEST_TEST_SETUP(concatenate) {
122160
{4, 1, {5, 5, 5, 5}, {2, 13, 10, 5}, {5, 8, 5, 2}, {5, 6, 5, 5}, {15, 13, 10, 5}, {5, 8, 5, 2}},
123161
{4, 2, {5, 5, 5, 5}, {2, 13, 10, 5}, {5, 8, 5, 2}, {5, 5, 6, 5}, {15, 13, 10, 5}, {5, 8, 5, 2}},
124162
{4, 3, {5, 5, 5, 5}, {2, 13, 10, 5}, {5, 8, 5, 2}, {5, 5, 5, 6}, {15, 13, 10, 5}, {5, 8, 5, 2}},
125-
163+
//
126164
));
127165
CUTEST_PARAMETRIZE(fill_value, int8_t, CUTEST_DATA(
128166
3,
129167
-5,
130-
// 113,
131-
// 33,
168+
113,
169+
33,
132170
));
133171
CUTEST_PARAMETRIZE(copy, bool, CUTEST_DATA(
134172
true,
135173
false,
136174
));
175+
CUTEST_PARAMETRIZE(arange, bool, CUTEST_DATA(
176+
true,
177+
false,
178+
));
137179

138180
}
139181

@@ -143,6 +185,7 @@ CUTEST_TEST_TEST(concatenate) {
143185
CUTEST_GET_PARAMETER(typesize, uint8_t);
144186
CUTEST_GET_PARAMETER(fill_value, int8_t);
145187
CUTEST_GET_PARAMETER(copy, bool);
188+
CUTEST_GET_PARAMETER(arange, bool);
146189

147190
int axis = shapes.axis;
148191
char *urlpath = "test_concatenate.b2frame";
@@ -188,11 +231,7 @@ CUTEST_TEST_TEST(concatenate) {
188231

189232
/* Create src2 with a value */
190233
b2nd_array_t *src2;
191-
uint8_t *value = malloc(typesize);
192-
// Fill a buffer with fill_value
193-
for (int i = 0; i < typesize; ++i) {
194-
value[i] = fill_value;
195-
}
234+
196235
blosc2_storage b2_storage2 = {.cparams=&cparams};
197236
if (backend.persistent) {
198237
b2_storage2.urlpath = urlpath2;
@@ -201,8 +240,31 @@ CUTEST_TEST_TEST(concatenate) {
201240
b2nd_context_t *ctx2 = b2nd_create_ctx(&b2_storage2, shapes.ndim, shapes.shape2,
202241
shapes.chunkshape2, shapes.blockshape2,
203242
NULL, 0, NULL, 0);
204-
B2ND_TEST_ASSERT(b2nd_full(ctx2, &src2, value));
205243

244+
uint8_t *value = malloc(typesize);
245+
// Fill a buffer with fill_value
246+
for (int i = 0; i < typesize; ++i) {
247+
value[i] = fill_value;
248+
}
249+
250+
if (arange) {
251+
int64_t buffsize = typesize;
252+
for (int i = 0; i < shapes.ndim; ++i) {
253+
buffsize *= shapes.shape2[i]; // Multiply by each dimension
254+
}
255+
// Allocate a buffer for the src2 array
256+
uint8_t *buff = malloc(buffsize);
257+
int64_t start_[B2ND_MAX_DIM] = {0};
258+
259+
fill_buffer_region(buff, shapes.shape2, shapes.ndim,
260+
start_, shapes.shape2, value, typesize, arange); // value is not used in this case
261+
B2ND_TEST_ASSERT(b2nd_from_cbuffer(ctx2, &src2, buff, buffsize));
262+
263+
}
264+
265+
else {
266+
B2ND_TEST_ASSERT(b2nd_full(ctx2, &src2, value));
267+
}
206268
/* Concatenate src1 and src2 */
207269
b2nd_array_t *array = NULL;
208270
blosc2_storage b2_storage = {.cparams=&cparams};
@@ -234,7 +296,7 @@ CUTEST_TEST_TEST(concatenate) {
234296

235297
// Fill the region with the value
236298
fill_buffer_region(helperbuffer, helpershape, shapes.ndim,
237-
start_src2, stop_src2, value, typesize);
299+
start_src2, stop_src2, value, typesize, arange); // value is not used if arange is true
238300

239301
// Check the shape of the concatenated array
240302
for (int i = 0; i < ctx->ndim; ++i) {
@@ -296,6 +358,15 @@ CUTEST_TEST_TEST(concatenate) {
296358
CUTEST_ASSERT("Data in the concatenated array does not match the helperbuffer", is_true);
297359
}
298360

361+
// for (int i = 0; i < buffersize; i++) {
362+
// if (i%typesize == 0) {
363+
// fprintf(stderr, "\n");
364+
// if (i%(typesize*shapes.shape1[0]) == 0) {fprintf(stderr, "--------------------------------\n");}
365+
// fprintf(stderr, "%d ", buffer[i]);
366+
// }
367+
// }
368+
369+
299370
/* Free mallocs */
300371
free(buffer_fill);
301372
free(buffer);

0 commit comments

Comments
 (0)