14 #ifndef NANOVDB_UTIL_FOREACH_H_HAS_BEEN_INCLUDED 15 #define NANOVDB_UTIL_FOREACH_H_HAS_BEEN_INCLUDED 19 #ifdef NANOVDB_USE_TBB 20 #include <tbb/parallel_for.h> 41 template <
typename RangeT,
typename FuncT>
42 inline void forEach(RangeT range,
const FuncT &func)
44 if (range.empty())
return;
45 #ifdef NANOVDB_USE_TBB 46 tbb::parallel_for(range, func);
47 #else// naive and likely slow alternative based on std::thread 48 if (
const size_t threadCount = std::thread::hardware_concurrency()>>1) {
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::thread> threadPool;
59 for (
auto &r : rangePool) threadPool.emplace_back(func, r);
60 for (
auto &t : threadPool) t.join();
68 template <
typename FuncT>
69 inline void forEach(
size_t begin,
size_t end,
size_t grainSize,
const FuncT& func)
75 template <
template<
typename...>
class ContainerT,
typename... T,
typename FuncT>
76 inline void forEach(
const ContainerT<T...> &c,
const FuncT& func)
82 template <
template<
typename...>
class ContainerT,
typename... T,
typename FuncT>
83 inline void forEach(
const ContainerT<T...> &c,
size_t grainSize,
const FuncT& func)
91 template <
typename FuncT>
92 [[deprecated(
"Use nanovdb::util::forEach instead")]]
93 inline void forEach(
size_t begin,
size_t end,
size_t grainSize,
const FuncT& func)
99 template <
template<
typename...>
class ContainerT,
typename... T,
typename FuncT>
100 [[deprecated(
"Use nanovdb::util::forEach instead")]]
101 inline void forEach(
const ContainerT<T...> &c,
const FuncT& func)
107 template <
template<
typename...>
class ContainerT,
typename... T,
typename FuncT>
108 [[deprecated(
"Use nanovdb::util::forEach instead")]]
109 inline void forEach(
const ContainerT<T...> &c,
size_t grainSize,
const FuncT& func)
116 #endif // NANOVDB_UTIL_FOREACH_H_HAS_BEEN_INCLUDED
Definition: GridHandle.h:27
Range< 1, size_t > Range1D
Definition: Range.h:33
Custom Range class that is compatible with the tbb::blocked_range classes.
void forEach(RangeT range, const FuncT &func)
simple wrapper for tbb::parallel_for with a naive std fallback
Definition: ForEach.h:42