From dc769b1f71a6b25a60cf3a196a62f69aac8e98ac Mon Sep 17 00:00:00 2001 From: Sergei Iskakov <439799+iskakoff@users.noreply.github.com> Date: Thu, 12 Jun 2025 10:29:15 -0400 Subject: [PATCH 1/2] Update ndarray.h When array created with reference to an external memory region, the size in internal storage should be equal to number of bytes. --- src/green/ndarray/ndarray.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/green/ndarray/ndarray.h b/src/green/ndarray/ndarray.h index a01e35e..5234d9a 100644 --- a/src/green/ndarray/ndarray.h +++ b/src/green/ndarray/ndarray.h @@ -105,7 +105,7 @@ namespace green::ndarray { template explicit ndarray(T* data, const shape_type& shape) : shape_(get_shape(shape)), strides_(strides_for_shape(shape)), size_(size_for_shape(shape)), offset_(0), - storage_(data, size_) { + storage_(data, sizeof(T) * size_) { if (data) set_value(0.0); } From 28c031b3c422c98db4f6d4781dc32f346b9aa0ed Mon Sep 17 00:00:00 2001 From: Sergei Iskakov <439799+iskakoff@users.noreply.github.com> Date: Thu, 12 Jun 2025 10:30:46 -0400 Subject: [PATCH 2/2] Update ndarray_test.cpp Tests for storage memory size check --- test/ndarray_test.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/ndarray_test.cpp b/test/ndarray_test.cpp index fdc662f..729e312 100644 --- a/test/ndarray_test.cpp +++ b/test/ndarray_test.cpp @@ -118,6 +118,22 @@ TEST_CASE("NDArrayTest") { std::equal(array.begin(), array.end(), array_ref2.begin(), [](double a, double b) { return std::abs(a - b) < 1e-12; })); } + SECTION("Check storage size consistensy") { + std::vector x(200); + // make shape consistent with the vector size + std::array shape{20, 5, 2}; + // create ndarray referencing data in x + ndarray::ndarray array_ref(x.data(), shape); + // check that size assigned in storage is consistent with actual size of data + REQUIRE(array_ref.storage().data().size == x.size() * sizeof(decltype(x)::value_type)); + // create a complex view of an array + auto carray_ref = array_ref.view>(); + // check that new array points to the same memory region + REQUIRE(array_ref.storage().data().ptr == carray_ref.storage().data().ptr); + // check that new array storage size is still correct + REQUIRE(carray_ref.storage().data().size == x.size() * sizeof(decltype(x)::value_type)); + } + SECTION("Slice") { ndarray::ndarray array(1, 2, 3, 4, 5); initialize_array(array);