44inline T
reduce(RangeT range,
const T& identity,
const FuncT &func,
const JoinT &join)
46 if (range.empty())
return identity;
48 return tbb::parallel_reduce(range, identity, func, join);
50 if (
const size_t threadCount = std::thread::hardware_concurrency()>>1) {
51 std::vector<RangeT> rangePool{ range };
52 while(rangePool.size() < threadCount) {
53 const size_t oldSize = rangePool.size();
54 for (
size_t i = 0; i < oldSize && rangePool.size() < threadCount; ++i) {
55 auto &r = rangePool[i];
56 if (r.is_divisible()) rangePool.push_back(RangeT(r,
Split()));
58 if (rangePool.size() == oldSize)
break;
60 std::vector< std::future<T> > futurePool;
61 for (
auto &r : rangePool) {
62 auto task = std::async(std::launch::async, [&](){
return func(r, identity);});
63 futurePool.push_back( std::move(task) );
66 for (
auto &f : futurePool) {
67 result = join(result, f.get());
71 return static_cast<T
>(func(range, identity));