14 #ifndef NANOVDB_REDUCE_H_HAS_BEEN_INCLUDED
15 #define NANOVDB_REDUCE_H_HAS_BEEN_INCLUDED
19 #ifdef NANOVDB_USE_TBB
20 #include <tbb/parallel_reduce.h>
41 template <
typename RangeT,
typename T,
typename FuncT,
typename JoinT>
44 if (range.empty())
return identity;
45 #ifdef NANOVDB_USE_TBB
46 return tbb::parallel_reduce(range, identity, func, join);
47 #else// naive and likely slow alternative based on std::future
49 std::vector<RangeT> rangePool{ range };
50 while(rangePool.size() < threadCount) {
51 const size_t oldSize = rangePool.size();
52 for (
size_t i = 0; i < oldSize && rangePool.size() < threadCount; ++i) {
53 auto &
r = rangePool[i];
54 if (
r.is_divisible()) rangePool.push_back(RangeT(
r,
Split()));
56 if (rangePool.size() == oldSize)
break;
58 std::vector< std::future<T> > futurePool;
59 for (
auto &
r : rangePool) {
60 auto task = std::async(std::launch::async, [&](){
return func(
r, identity);});
61 futurePool.push_back( std::move(task) );
64 for (
auto &
f : futurePool) {
65 result =
join(result,
f.get());
69 return static_cast<T>(
func(range, identity));
76 template <
typename T,
typename FuncT,
typename JoinT >
80 return reduce( range, identity, func, join );
84 template <
template<
typename...>
class ContainerT,
typename... ArgT,
typename T,
typename FuncT,
typename JoinT >
85 inline T reduce(
const ContainerT<ArgT...> &
c,
const T& identity,
const FuncT&
func,
const JoinT&
join)
93 template <
template<
typename...>
class ContainerT,
typename... ArgT,
typename T,
typename FuncT,
typename JoinT >
94 inline T reduce(
const ContainerT<ArgT...> &
c,
size_t grainSize,
const T& identity,
const FuncT&
func,
const JoinT&
join)
102 #endif // NANOVDB_REDUCE_H_HAS_BEEN_INCLUDED
OIIO_API unsigned int hardware_concurrency()
**But if you need a result
T reduce(RangeT range, const T &identity, const FuncT &func, const JoinT &join)
Custom Range class that is compatible with the tbb::blocked_range classes.
PcpNodeRef_ChildrenIterator begin(const PcpNodeRef::child_const_range &r)
Support for range-based for loops for PcpNodeRef children ranges.