Call this function the same way you would with std::accumulate:
std::vector<int> v{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sum = parallelAccumulate(v.begin(), v.end(), 0);Call this function the same way you would with std::accumulate:
std::vector<int> v{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sum = parallelAccumulate(v.begin(), v.end(), 0);| #include <numeric> // std::accumulate | |
| #include "tbb/parallel_reduce.h" | |
| #include "tbb/blocked_range.h" | |
| template <class Summable, class SummableIt> | |
| Summable parallelAccumulate( | |
| SummableIt beginIt, | |
| SummableIt endIt, | |
| const Summable initial | |
| ) const { | |
| Summable* start = &(*beginIt); | |
| Summable* end = &(*endIt); | |
| return tbb::parallel_reduce( | |
| tbb::blocked_range<Summable*>(start, end), // range | |
| initial, // identity; initial value | |
| [](const tbb::blocked_range<Summable*>& chunk, Summable initial)->Summable { | |
| return std::accumulate(chunk.begin(), chunk.end(), initial); | |
| }, // function to accumulate subrange | |
| std::plus<Summable>() // reduction operator | |
| ); | |
| } | |