Skip to content

Commit 6841149

Browse files
committed
First version that is doing concatenation correctly (at least in 2d)
1 parent 9669020 commit 6841149

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

blosc/b2nd.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1376,7 +1376,11 @@ int b2nd_concatenate(b2nd_context_t *ctx, const b2nd_array_t *src1,
13761376
for (int8_t i = 0; i < src2->ndim; ++i) {
13771377
chunkshape[i] = src2->chunkshape[i];
13781378
}
1379-
blosc2_unidim_to_multidim(src2->ndim, chunkshape, nchunk, nchunk_ndim);
1379+
int64_t chunks_in_dim[B2ND_MAX_DIM] = {0};
1380+
for (int8_t i = 0; i < src2->ndim; ++i) {
1381+
chunks_in_dim[i] = (src2->extshape[i] + src2->chunkshape[i] - 1) / src2->chunkshape[i];
1382+
}
1383+
blosc2_unidim_to_multidim(src2->ndim, chunks_in_dim, nchunk, nchunk_ndim);
13801384

13811385
// Set positions for each dimension
13821386
for (int8_t i = 0; i < src2->ndim; ++i) {

tests/b2nd/test_b2nd_concatenate.c

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,11 @@ CUTEST_TEST_TEST(concatenate) {
140140
for (int i = 0; i < shapes.ndim; ++i) {
141141
if (i == axis) {
142142
helpershape[i] = shapes.shape1[i] + shapes.shape2[i];
143-
buffersize *= (size_t) (shapes.shape1[i] + shapes.shape2[i]);
144143
}
145144
else {
146145
helpershape[i] = shapes.shape1[i];
147-
buffersize *= (size_t) shapes.shape1[i];
148146
}
147+
buffersize *= helpershape[i]; // Multiply by each dimension
149148
}
150149
// Allocate a buffer for the concatenated array
151150
uint8_t *helperbuffer = malloc(buffersize);
@@ -221,18 +220,20 @@ CUTEST_TEST_TEST(concatenate) {
221220

222221
// Set up the region to fill (corresponding to src2's position)
223222
for (int i = 0; i < shapes.ndim; i++) {
224-
if (i == axis) {
225-
start_src2[i] = shapes.shape1[i]; // src2 starts after src1
226-
stop_src2[i] = shapes.shape1[i] + shapes.shape2[i];
227-
} else {
228-
start_src2[i] = 0;
229-
stop_src2[i] = shapes.shape2[i];
230-
}
223+
if (i == axis) {
224+
start_src2[i] = shapes.shape1[i]; // src2 starts after src1
225+
stop_src2[i] = shapes.shape1[i] + shapes.shape2[i];
226+
} else {
227+
start_src2[i] = 0;
228+
// Use the minimum of shape1 and shape2 for non-axis dimensions
229+
stop_src2[i] = (shapes.shape1[i] < shapes.shape2[i]) ?
230+
shapes.shape1[i] : shapes.shape2[i];
231+
}
231232
}
232233

233-
// Fill the region with the value
234-
fill_buffer_region(helperbuffer, helpershape, shapes.ndim,
235-
start_src2, stop_src2, value, typesize);
234+
// Fill the region with the value
235+
fill_buffer_region(helperbuffer, helpershape, shapes.ndim,
236+
start_src2, stop_src2, value, typesize);
236237

237238
// Check the shape of the concatenated array
238239
for (int i = 0; i < ctx->ndim; ++i) {
@@ -259,13 +260,15 @@ CUTEST_TEST_TEST(concatenate) {
259260
int64_t start[B2ND_MAX_DIM] = {0};
260261
int64_t stop[B2ND_MAX_DIM] = {0};
261262
int64_t buffershape[B2ND_MAX_DIM] = {0};
263+
size_t elementcount = 1; // Count of elements
262264
for (int i = 0; i < ctx->ndim; ++i) {
263265
start[i] = 0;
264266
stop[i] = array->shape[i];
265267
buffershape[i] = stop[i] - start[i];
266-
buffersize *= buffershape[i];
268+
elementcount *= buffershape[i];
267269
}
268-
uint8_t *buffer = malloc(buffersize);
270+
size_t buffersize2 = elementcount * typesize;
271+
uint8_t *buffer = malloc(buffersize2);
269272
B2ND_TEST_ASSERT(b2nd_get_slice_cbuffer(array, start, stop, buffer, buffershape, buffersize));
270273
// Check if the data in the concatenated array matches the helperbuffer
271274
uint8_t *buffer_fill = malloc(typesize);
@@ -293,11 +296,11 @@ CUTEST_TEST_TEST(concatenate) {
293296
if (!is_true) {
294297
// Print the raw byte values for better debugging
295298
fprintf(stderr, "Data mismatch at index %ld: buffer bytes = ", i);
296-
for (int b = 0; b < typesize + 10; b++) {
299+
for (int b = 0; b < typesize; b++) {
297300
fprintf(stderr, "%02x ", buffer[i * typesize + b]);
298301
}
299302
fprintf(stderr, ", helperbuffer bytes = ");
300-
for (int b = 0; b < typesize + 10; b++) {
303+
for (int b = 0; b < typesize; b++) {
301304
fprintf(stderr, "%02x ", helperbuffer[i * typesize + b]);
302305
}
303306
fprintf(stderr, "\n");

0 commit comments

Comments
 (0)