diff --git a/RELEASE.md b/RELEASE.md index 708f4ba..0cf8715 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -14,7 +14,7 @@ ### Bugfixes: - Fixed ``split_task(first, last, n_threads)`` so task ranges now correctly respect the ``first`` offset. Previously, non-zero starting indices could generate incorrect subranges. - +- Fixed overflow issue causing failed allocations for NDArrays abouve ~2GB ## 2026.3.17 diff --git a/include/aare/NDView.hpp b/include/aare/NDView.hpp index 33a5bf6..403d203 100644 --- a/include/aare/NDView.hpp +++ b/include/aare/NDView.hpp @@ -48,7 +48,7 @@ Shape drop_first_dim(const Shape &shape) { * @return The number of elements in and NDArray/NDView of that shape. */ template size_t num_elements(const Shape &shape) { - return std::accumulate(shape.begin(), shape.end(), 1, + return std::accumulate(shape.begin(), shape.end(), size_t{1}, std::multiplies()); } diff --git a/src/NDView.test.cpp b/src/NDView.test.cpp index d24a08a..b89d0cd 100644 --- a/src/NDView.test.cpp +++ b/src/NDView.test.cpp @@ -8,8 +8,27 @@ #include using aare::NDView; +using aare::num_elements; using aare::Shape; +TEST_CASE("Calculate size from a shape") { + Shape<3> shape{2, 3, 4}; + REQUIRE(num_elements(shape) == 24); + + Shape<2> shape2{5, 5}; + REQUIRE(num_elements(shape2) == 25); + + Shape<1> shape3{10}; + REQUIRE(num_elements(shape3) == 10); + + Shape<3> shape4{1000, 512, 1024}; + REQUIRE(num_elements(shape4) == 524288000); + + // 10GB which is more than INT_MAX bytes + Shape<3> shape5{10000, 1024, 1024}; + REQUIRE(num_elements(shape5) == 10485760000); +} + TEST_CASE("Element reference 1D") { std::vector vec; for (int i = 0; i != 10; ++i) {