OpenVDB  9.0.1
Reduce.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 
4 /*!
5  \file Reduce.h
6 
7  \author Ken Museth
8 
9  \date March 4, 2021
10 
11  \brief A unified wrapper for tbb::parallel_reduce and a naive std::future analog
12 */
13 
14 #ifndef NANOVDB_REDUCE_H_HAS_BEEN_INCLUDED
15 #define NANOVDB_REDUCE_H_HAS_BEEN_INCLUDED
16 
17 #include "Range.h"// for Range1D
18 
19 #ifdef NANOVDB_USE_TBB
20 #include <tbb/parallel_reduce.h>
21 #else
22 #include <thread>
23 #include <future>
24 #include <vector>
25 #endif
26 
27 namespace nanovdb {
28 
29 /// @return reduction
30 ///
31 /// @param range RangeT can be Range<dim,T>, CoordBBox, tbb::blocked_range, blocked_range2D, or blocked_range3D.
32 /// @param func functor with signature T FuncT::operator()(const RangeT& range, const T& a) const
33 /// @param join functor with the signature T JoinT::operator()(const T& a, const T& b) const
34 /// @code
35 /// std::vector<int> array(100, 1);
36 /// auto func = [&array](auto &r, int a){for (auto i=r.begin(); i!=r.end(); ++i) a+=array[i]; return a;};
37 /// int sum = reduce(array, 0, func, [](int a, int b){return a + b;});
38 /// @endcode
39 
40 template <typename RangeT, typename T, typename FuncT, typename JoinT>
41 inline T reduce(RangeT range, const T& identity, const FuncT &func, const JoinT &join)
42 {
43  if (range.empty()) return identity;
44 #ifdef NANOVDB_USE_TBB
45  return tbb::parallel_reduce(range, identity, func, join);
46 #else// naive and likely slow alternative based on std::future
47  if (const size_t threadCount = std::thread::hardware_concurrency()>>1) {
48  std::vector<RangeT> rangePool{ range };
49  while(rangePool.size() < threadCount) {
50  const size_t oldSize = rangePool.size();
51  for (size_t i = 0; i < oldSize && rangePool.size() < threadCount; ++i) {
52  auto &r = rangePool[i];
53  if (r.is_divisible()) rangePool.push_back(RangeT(r, Split()));
54  }
55  if (rangePool.size() == oldSize) break;// none of the ranges were divided so stop
56  }
57  std::vector< std::future<T> > futurePool;
58  for (auto &r : rangePool) {
59  auto task = std::async(std::launch::async, [&](){return func(r, identity);});
60  futurePool.push_back( std::move(task) );// launch tasks
61  }
62  T result = identity;
63  for (auto &f : futurePool) {
64  result = join(result, f.get());// join results
65  }
66  return result;
67  } else {// serial
68  return static_cast<T>(func(range, identity));
69  }
70 #endif
71  return identity;// should never happen
72 }
73 
74 /// @brief Simple wrapper to the function defined above
75 template <typename T, typename FuncT, typename JoinT >
76 inline T reduce(size_t begin, size_t end, size_t grainSize, const T& identity, const FuncT& func, const JoinT& join)
77 {
78  Range1D range(begin, end, grainSize);
79  return reduce( range, identity, func, join );
80 }
81 
82 /// @brief Simple wrapper that works with std::containers
83 template <template<typename...> class ContainerT, typename... ArgT, typename T, typename FuncT, typename JoinT >
84 inline T reduce(const ContainerT<ArgT...> &c, const T& identity, const FuncT& func, const JoinT& join)
85 {
86  Range1D range(0, c.size(), 1);
87  return reduce( range, identity, func, join );
88 
89 }
90 
91 /// @brief Simple wrapper that works with std::containers
92 template <template<typename...> class ContainerT, typename... ArgT, typename T, typename FuncT, typename JoinT >
93 inline T reduce(const ContainerT<ArgT...> &c, size_t grainSize, const T& identity, const FuncT& func, const JoinT& join)
94 {
95  Range1D range(0, c.size(), grainSize);
96  return reduce( range, identity, func, join );
97 }
98 
99 }// namespace nanovdb
100 
101 #endif // NANOVDB_REDUCE_H_HAS_BEEN_INCLUDED
Definition: NanoVDB.h:184
Custom Range class that is compatible with the tbb::blocked_range classes.
T reduce(RangeT range, const T &identity, const FuncT &func, const JoinT &join)
Definition: Reduce.h:41
Definition: Range.h:28
Definition: Range.h:25