OpenVDB: nanovdb/util/Reduce.h Source File

OpenVDB 10.0.1

Loading...
Searching...
No Matches
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
27namespace 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 identity initial value
33/// @param func functor with signature T FuncT::operator()(const RangeT& range, const T& a) const
34/// @param join functor with the signature T JoinT::operator()(const T& a, const T& b) const
35/// @code
36/// std::vector<int> array(100, 1);
37/// auto func = [&array](auto &r, int a){for (auto i=r.begin(); i!=r.end(); ++i) a+=array[i]; return a;};
38/// int sum = reduce(array, 0, func, [](int a, int b){return a + b;});
39/// @endcode
40
41template <typename RangeT, typename T, typename FuncT, typename JoinT>
42 inline T reduce(RangeT range, const T& identity, const FuncT &func, const JoinT &join)
43{
44 if (range.empty()) return identity;
45#ifdef NANOVDB_USE_TBB
46 return tbb::parallel_reduce(range, identity, func, join);
47#else// naive and likely slow alternative based on std::future
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()));
55 }
56 if (rangePool.size() == oldSize) break;// none of the ranges were divided so stop
57 }
58 std::vector< std::future<T> > futurePool;
59 for (auto &r : rangePool) {
60 auto task = std::async(std::launch::async, [&](){return func(r, identity);});
61 futurePool.push_back( std::move(task) );// launch tasks
62 }
63 T result = identity;
64 for (auto &f : futurePool) {
65 result = join(result, f.get());// join results
66 }
67 return result;
68 } else {// serial
69 return static_cast<T>(func(range, identity));
70 }
71#endif
72 return identity;// should never happen
73}
74
75/// @brief Simple wrapper to the function defined above
76template <typename T, typename FuncT, typename JoinT >
77 inline T reduce(size_t begin, size_t end, size_t grainSize, const T& identity, const FuncT& func, const JoinT& join)
78{
79 Range1D range(begin, end, grainSize);
80 return reduce( range, identity, func, join );
81}
82
83/// @brief Simple wrapper that works with std::containers
84template <template<typename...> class ContainerT, typename... ArgT, typename T, typename FuncT, typename JoinT >
85 inline T reduce(const ContainerT<ArgT...> &c, const T& identity, const FuncT& func, const JoinT& join)
86{
87 Range1D range(0, c.size(), 1);
88 return reduce( range, identity, func, join );
89
90}
91
92/// @brief Simple wrapper that works with std::containers
93template <template<typename...> class ContainerT, typename... ArgT, typename T, typename FuncT, typename JoinT >
94 inline T reduce(const ContainerT<ArgT...> &c, size_t grainSize, const T& identity, const FuncT& func, const JoinT& join)
95{
96 Range1D range(0, c.size(), grainSize);
97 return reduce( range, identity, func, join );
98}
99
100}// namespace nanovdb
101
102#endif // NANOVDB_REDUCE_H_HAS_BEEN_INCLUDED
Range.h
Custom Range class that is compatible with the tbb::blocked_range classes.
nanovdb::DenseGridHandle
Definition DenseGrid.h:402
nanovdb::DenseGridHandle::size
uint64_t size() const
Returns the size in bytes of the raw memory buffer managed by this DenseGridHandle's allocator.
Definition DenseGrid.h:440
nanovdb::Split
Definition Range.h:25
nanovdb
Definition NanoVDB.h:208
nanovdb::reduce
T reduce(RangeT range, const T &identity, const FuncT &func, const JoinT &join)
Definition Reduce.h:42