Skip to content

Commit

Permalink
Support repeated values larger than 8-bit, also for n-dim arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancescAlted committed Feb 18, 2025
1 parent ec9009d commit ba78acd
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 42 deletions.
2 changes: 1 addition & 1 deletion bench/b2nd/bench_get_slice.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ int main() {
int nslices = 10;

int8_t ndim = 3;
uint8_t itemsize = sizeof(DATA_TYPE);
int32_t itemsize = sizeof(DATA_TYPE);

int64_t shape[] = {1250, 745, 400};

Expand Down
29 changes: 11 additions & 18 deletions blosc/b2nd_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// copyNdim where N = {2-8} - specializations of copy loops to be used by b2nd_copy_buffer
// since we don't have c++ templates, substitute manual specializations for up to known B2ND_MAX_DIM (8)
// it's not pretty, but it substantially reduces overhead vs. the generic method
void copy8dim(const uint8_t itemsize,
void copy8dim(const int32_t itemsize,
const int64_t *copy_shape,
const uint8_t *bsrc, const int64_t *src_strides,
uint8_t *bdst, const int64_t *dst_strides) {
Expand Down Expand Up @@ -57,7 +57,7 @@ void copy8dim(const uint8_t itemsize,
} while (copy_start[0] < copy_shape[0]);
}

void copy7dim(const uint8_t itemsize,
void copy7dim(const int32_t itemsize,
const int64_t *copy_shape,
const uint8_t *bsrc, const int64_t *src_strides,
uint8_t *bdst, const int64_t *dst_strides) {
Expand Down Expand Up @@ -95,7 +95,7 @@ void copy7dim(const uint8_t itemsize,
} while (copy_start[0] < copy_shape[0]);
}

void copy6dim(const uint8_t itemsize,
void copy6dim(const int32_t itemsize,
const int64_t *copy_shape,
const uint8_t *bsrc, const int64_t *src_strides,
uint8_t *bdst, const int64_t *dst_strides) {
Expand Down Expand Up @@ -129,7 +129,7 @@ void copy6dim(const uint8_t itemsize,
} while (copy_start[0] < copy_shape[0]);
}

void copy5dim(const uint8_t itemsize,
void copy5dim(const int32_t itemsize,
const int64_t *copy_shape,
const uint8_t *bsrc, const int64_t *src_strides,
uint8_t *bdst, const int64_t *dst_strides) {
Expand Down Expand Up @@ -159,7 +159,7 @@ void copy5dim(const uint8_t itemsize,
} while (copy_start[0] < copy_shape[0]);
}

void copy4dim(const uint8_t itemsize,
void copy4dim(const int32_t itemsize,
const int64_t *copy_shape,
const uint8_t *bsrc, const int64_t *src_strides,
uint8_t *bdst, const int64_t *dst_strides) {
Expand All @@ -185,7 +185,7 @@ void copy4dim(const uint8_t itemsize,
} while (copy_start[0] < copy_shape[0]);
}

void copy3dim(const uint8_t itemsize,
void copy3dim(const int32_t itemsize,
const int64_t *copy_shape,
const uint8_t *bsrc, const int64_t *src_strides,
uint8_t *bdst, const int64_t *dst_strides) {
Expand All @@ -207,7 +207,7 @@ void copy3dim(const uint8_t itemsize,
} while (copy_start[0] < copy_shape[0]);
}

void copy2dim(const uint8_t itemsize,
void copy2dim(const int32_t itemsize,
const int64_t *copy_shape,
const uint8_t *bsrc, const int64_t *src_strides,
uint8_t *bdst, const int64_t *dst_strides) {
Expand All @@ -223,7 +223,7 @@ void copy2dim(const uint8_t itemsize,


void copy_ndim_fallback(const int8_t ndim,
const uint8_t itemsize,
const int32_t itemsize,
int64_t *copy_shape,
const uint8_t *bsrc, int64_t *src_strides,
uint8_t *bdst, int64_t *dst_strides) {
Expand Down Expand Up @@ -268,13 +268,13 @@ int b2nd_copy_buffer2(int8_t ndim,
}

// Compute the strides
int64_t src_strides[B2ND_MAX_DIM];
int64_t src_strides[B2ND_MAX_DIM] = {0};
src_strides[ndim - 1] = 1;
for (int i = ndim - 2; i >= 0; --i) {
src_strides[i] = src_strides[i + 1] * src_pad_shape[i + 1];
}

int64_t dst_strides[B2ND_MAX_DIM];
int64_t dst_strides[B2ND_MAX_DIM] = {0};
dst_strides[ndim - 1] = 1;
for (int i = ndim - 2; i >= 0; --i) {
dst_strides[i] = dst_strides[i + 1] * dst_pad_shape[i + 1];
Expand Down Expand Up @@ -332,14 +332,7 @@ int b2nd_copy_buffer(int8_t ndim,
const void *src, const int64_t *src_pad_shape,
const int64_t *src_start, const int64_t *src_stop,
void *dst, const int64_t *dst_pad_shape,
const int64_t *dst_start) __attribute__((deprecated("Use b2nd_copy_buffer2 instead")));

int b2nd_copy_buffer(int8_t ndim,
uint8_t itemsize,
const void *src, const int64_t *src_pad_shape,
const int64_t *src_start, const int64_t *src_stop,
void *dst, const int64_t *dst_pad_shape,
const int64_t *dst_start) {
const int64_t *dst_start) __attribute__((deprecated("Use b2nd_copy_buffer2 instead"))) {
// Simply cast itemsize to int32_t and delegate
return b2nd_copy_buffer2(ndim, (int32_t)itemsize, src, src_pad_shape,
src_start, src_stop, dst, dst_pad_shape, dst_start);
Expand Down
45 changes: 22 additions & 23 deletions tests/b2nd/test_b2nd_full.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,33 @@ CUTEST_TEST_SETUP(full) {

// Add parametrizations
CUTEST_PARAMETRIZE(typesize, int32_t, CUTEST_DATA(
//1, 2, 4, 8,
256, //257, 256 * 256,
// 256*256*256, 256*256*256*8, // these should work for small shapes as well, but are too slow
1, 2, 4, 8, 16,
255, 256, 257, // useful to testing typesizes equal or larger than 255
// 256 * 256, // this should work for all shapes as well, but is too slow
// 256*256*256, 256*256*256*8, // this should for work small shapes, but is way sloooower
));
CUTEST_PARAMETRIZE(shapes, _test_shapes, CUTEST_DATA(
// {0, {0}, {0}, {0}}, // 0-dim
// {1, {5}, {3}, {2}}, // 1-idim
// {2, {20, 0}, {7, 0}, {3, 0}}, // 0-shape
//{2, {20, 10}, {20, 10}, {10, 10}}, // funciona sempre
//{2, {20, 10}, {10, 5}, {10, 5}}, // falla
//{2, {4, 1}, {2, 1}, {2, 1}}, // falla
{2, {1, 3}, {1, 2}, {1, 2}}, // falla
//{1, {4}, {2}, {2}}, // funciona sempre
//{2, {20, 10}, {8, 6}, {7, 5}}, // falla
//{2, {20, 10}, {7, 5}, {3, 5}}, // falla
//{2, {14, 10}, {8, 5}, {2, 2}}, // general,
//{3, {12, 10, 14}, {3, 5, 9}, {3, 4, 4}}, // general
//{3, {10, 21, 20, 5}, {8, 7, 15, 3}, {5, 5, 10, 1}}, // general,
{0, {0}, {0}, {0}}, // 0-dim
{1, {5}, {3}, {2}}, // 1-idim
{2, {20, 0}, {7, 0}, {3, 0}}, // 0-shape
{2, {20, 10}, {20, 10}, {10, 10}}, // simple 2-dim
{2, {20, 10}, {10, 5}, {10, 5}}, // non-contiguous
{2, {4, 1}, {2, 1}, {2, 1}},
{2, {1, 3}, {1, 2}, {1, 2}},
{2, {20, 10}, {8, 6}, {7, 5}},
{2, {20, 10}, {7, 5}, {3, 5}},
{2, {14, 10}, {8, 5}, {2, 2}},
{3, {12, 10, 14}, {3, 5, 9}, {3, 4, 4}}, // 3-dim
{4, {10, 21, 20, 5}, {8, 7, 15, 3}, {5, 5, 10, 1}}, // 4-dim
));
CUTEST_PARAMETRIZE(backend, _test_backend, CUTEST_DATA(
{false, false},
// {true, false},
// {true, true},
// {false, true},
{true, false},
{true, true},
{false, true},
));
CUTEST_PARAMETRIZE(fill_value, int8_t, CUTEST_DATA(
//3, 113, 33, -5
3
3, 113, 33, -5
));
}

Expand Down Expand Up @@ -131,9 +130,9 @@ CUTEST_TEST_TEST(full) {
}
// Compare buffer_fill with buffer_dest
is_true = memcmp(buffer_fill, buffer_dest, typesize) == 0;
// print the 10 first bytes of buffer_fill and buffer_dest
// print the N first bytes of buffer_fill and buffer_dest
// printf("buffer_fill: ");
// for (int j = 0; j < 10; ++j) {
// for (int j = 0; j < 3; ++j) {
// printf("%d vs %d ", buffer_fill[j], buffer_dest[j]);
// }
free(buffer_fill);
Expand Down

0 comments on commit ba78acd

Please sign in to comment.