Bugfix: NDArary/NDView overflow in calculating number of elements (#322)
Build on RHEL9 / build (push) Successful in 2m38s
Build on RHEL8 / build (push) Successful in 3m10s
Run tests using data on local RHEL8 / build (push) Successful in 3m59s
Build on local RHEL8 / build (push) Successful in 2m45s

Fixed number of elements calculation that caused integer overflow. Now
return size_t instead of int.
This commit is contained in:
Erik Fröjdh
2026-06-10 12:24:18 +02:00
committed by GitHub
parent b78ea64ea7
commit 17d04083a7
3 changed files with 21 additions and 2 deletions
+1 -1
View File
@@ -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
+1 -1
View File
@@ -48,7 +48,7 @@ Shape<Ndim - 1> drop_first_dim(const Shape<Ndim> &shape) {
* @return The number of elements in and NDArray/NDView of that shape.
*/
template <size_t Ndim> size_t num_elements(const Shape<Ndim> &shape) {
return std::accumulate(shape.begin(), shape.end(), 1,
return std::accumulate(shape.begin(), shape.end(), size_t{1},
std::multiplies<size_t>());
}
+19
View File
@@ -8,8 +8,27 @@
#include <vector>
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<int> vec;
for (int i = 0; i != 10; ++i) {