14 #ifndef NANOVDB_FOREACH_H_HAS_BEEN_INCLUDED 15 #define NANOVDB_FOREACH_H_HAS_BEEN_INCLUDED 19 #ifdef NANOVDB_USE_TBB 20 #include <tbb/parallel_for.h> 39 template <
typename RangeT,
typename FuncT>
40 inline void forEach(RangeT range,
const FuncT &func)
42 if (range.empty())
return;
43 #ifdef NANOVDB_USE_TBB 44 tbb::parallel_for(range, func);
45 #else// naive and likely slow alternative based on std::thread 46 if (
const size_t threadCount = std::thread::hardware_concurrency()>>1) {
47 std::vector<RangeT> rangePool{ range };
48 while(rangePool.size() < threadCount) {
49 const size_t oldSize = rangePool.size();
50 for (
size_t i = 0; i < oldSize && rangePool.size() < threadCount; ++i) {
51 auto &r = rangePool[i];
52 if (r.is_divisible()) rangePool.push_back(RangeT(r,
Split()));
54 if (rangePool.size() == oldSize)
break;
56 std::vector<std::thread> threadPool;
57 for (
auto &r : rangePool) threadPool.emplace_back(func, r);
58 for (
auto &t : threadPool) t.join();
66 template <
typename FuncT>
67 inline void forEach(
size_t begin,
size_t end,
size_t grainSize,
const FuncT& func)
73 template <
template<
typename...>
class ContainerT,
typename... T,
typename FuncT>
74 inline void forEach(
const ContainerT<T...> &c,
const FuncT& func)
80 template <
template<
typename...>
class ContainerT,
typename... T,
typename FuncT>
81 inline void forEach(
const ContainerT<T...> &c,
size_t grainSize,
const FuncT& func)
88 #endif // NANOVDB_FOREACH_H_HAS_BEEN_INCLUDED void forEach(RangeT range, const FuncT &func)
simple wrapper for tbb::parallel_for with a naive std fallback
Definition: ForEach.h:40
Definition: NanoVDB.h:184
Custom Range class that is compatible with the tbb::blocked_range classes.
Range< 1, size_t > Range1D
Definition: Range.h:30