14 #ifndef NANOVDB_UTIL_REDUCE_H_HAS_BEEN_INCLUDED 15 #define NANOVDB_UTIL_REDUCE_H_HAS_BEEN_INCLUDED 19 #ifdef NANOVDB_USE_TBB 20 #include <tbb/parallel_reduce.h> 42 template <
typename RangeT,
typename T,
typename FuncT,
typename JoinT>
43 inline T
reduce(RangeT range,
const T& identity,
const FuncT &func,
const JoinT &join)
45 if (range.empty())
return identity;
46 #ifdef NANOVDB_USE_TBB 47 return tbb::parallel_reduce(range, identity, func, join);
48 #else// naive and likely slow alternative based on std::future 49 if (
const size_t threadCount = std::thread::hardware_concurrency()>>1) {
50 std::vector<RangeT> rangePool{ range };
51 while(rangePool.size() < threadCount) {
52 const size_t oldSize = rangePool.size();
53 for (
size_t i = 0; i < oldSize && rangePool.size() < threadCount; ++i) {
54 auto &r = rangePool[i];
55 if (r.is_divisible()) rangePool.push_back(RangeT(r,
Split()));
57 if (rangePool.size() == oldSize)
break;
59 std::vector< std::future<T> > futurePool;
60 for (
auto &r : rangePool) {
61 auto task = std::async(std::launch::async, [&](){
return func(r, identity);});
62 futurePool.push_back( std::move(task) );
65 for (
auto &f : futurePool) {
66 result = join(result, f.get());
70 return static_cast<T
>(func(range, identity));
77 template <
typename T,
typename FuncT,
typename JoinT>
78 inline T
reduce(
size_t begin,
size_t end,
size_t grainSize,
const T& identity,
const FuncT& func,
const JoinT& join)
80 Range1D range(begin, end, grainSize);
81 return reduce( range, identity, func, join );
85 template <
template<
typename...>
class ContainerT,
typename... ArgT,
typename T,
typename FuncT,
typename JoinT >
86 inline T
reduce(
const ContainerT<ArgT...> &c,
const T& identity,
const FuncT& func,
const JoinT& join)
89 return reduce( range, identity, func, join );
94 template <
template<
typename...>
class ContainerT,
typename... ArgT,
typename T,
typename FuncT,
typename JoinT >
95 inline T
reduce(
const ContainerT<ArgT...> &c,
size_t grainSize,
const T& identity,
const FuncT& func,
const JoinT& join)
97 Range1D range(0, c.size(), grainSize);
98 return reduce( range, identity, func, join );
104 template <
typename T,
typename FuncT,
typename JoinT>
105 [[deprecated(
"Use nanovdb::util::reduce instead")]]
106 inline T
reduce(
size_t begin,
size_t end,
size_t grainSize,
const T& identity,
const FuncT& func,
const JoinT& join)
113 template <
template<
typename...>
class ContainerT,
typename... ArgT,
typename T,
typename FuncT,
typename JoinT >
114 [[deprecated(
"Use nanovdb::util::reduce instead")]]
115 inline T
reduce(
const ContainerT<ArgT...> &c,
const T& identity,
const FuncT& func,
const JoinT& join)
123 template <
template<
typename...>
class ContainerT,
typename... ArgT,
typename T,
typename FuncT,
typename JoinT >
124 [[deprecated(
"Use nanovdb::util::reduce instead")]]
125 T
reduce(
const ContainerT<ArgT...> &c,
size_t grainSize,
const T& identity,
const FuncT& func,
const JoinT& join)
133 #endif // NANOVDB_UTIL_REDUCE_H_HAS_BEEN_INCLUDED T reduce(RangeT range, const T &identity, const FuncT &func, const JoinT &join)
Definition: Reduce.h:43
Definition: GridHandle.h:27
Custom Range class that is compatible with the tbb::blocked_range classes.