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