Skip to content

Commit

Permalink
Merge pull request #11 from TimoMaarse/develop
Browse files Browse the repository at this point in the history
Fix incorrect inference in `bulk::sum` and `bulk::product`
  • Loading branch information
jwbuurlage committed Oct 9, 2020
2 parents 5855b48 + 0eaceb7 commit b793202
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ Versioning](https://semver.org/spec/v2.0.0.html).

- `bulk::index` now represents indices as `size_t`

### Fixed

- Prevent `bulk::sum` and `bulk::product` from silently truncating values (@TimoMaarse, #11)

## [2.0.0] - 2020-03-27

### Breaking changes
Expand Down
4 changes: 2 additions & 2 deletions include/bulk/algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,13 @@ T min(bulk::world& world, T t) {
template <typename T>
T sum(bulk::world& world, T t) {
auto xs = bulk::gather_all(world, t);
return std::accumulate(xs.begin(), xs.end(), 0);
return std::accumulate(xs.begin(), xs.end(), T{});
}

template <typename T>
T product(bulk::world& world, T t) {
auto xs = bulk::gather_all(world, t);
return std::accumulate(xs.begin(), xs.end(), 1,
return std::accumulate(xs.begin(), xs.end(), (T)1,
[](auto& lhs, auto& rhs) { return lhs * rhs; });
}

Expand Down
10 changes: 10 additions & 0 deletions test/algorithm.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <chrono>
#include <limits>
#include <thread>

#include "bulk_test_common.hpp"
Expand Down Expand Up @@ -75,6 +76,15 @@ void test_algorithm() {
BULK_CHECK(bulk::min(xs) == 1, "min of coarray");
BULK_CHECK(bulk::sum(xs) == 2 * p * (2 * p + 1) / 2,
"sum of coarray ");

BULK_CHECK(bulk::sum(world, (int64_t)std::numeric_limits<int32_t>::max()) ==
(int64_t)p * (int64_t)std::numeric_limits<int32_t>::max(),
"sum of large values");
BULK_CHECK(bulk::product(world, s == 0 ?
(int64_t)std::numeric_limits<int32_t>::max() + 1 :
1) ==
(int64_t)std::numeric_limits<int32_t>::max() + 1,
"large product");
}
});
}

0 comments on commit b793202

Please sign in to comment.