All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
reduce.h File Reference
#include "pxr/pxr.h"
#include "pxr/base/work/threadLimits.h"
#include "pxr/base/work/api.h"
#include <tbb/blocked_range.h>
#include <tbb/parallel_reduce.h>
#include <tbb/task.h>
+ Include dependency graph for reduce.h:

Go to the source code of this file.


template<typename Fn , typename Rn , typename V >
PXR_NAMESPACE_OPEN_SCOPEWorkParallelReduceN (const V &identity, size_t n, Fn &&loopCallback, Rn &&reductionCallback, size_t grainSize)
template<typename Fn , typename Rn , typename V >
WorkParallelReduceN (const V &identity, size_t n, Fn &&loopCallback, Rn &&reductionCallback)

Function Documentation

template<typename Fn , typename Rn , typename V >
PXR_NAMESPACE_OPEN_SCOPE V WorkParallelReduceN ( const V &  identity,
size_t  n,
Fn &&  loopCallback,
Rn &&  reductionCallback,
size_t  grainSize 

Recursively splits the range [0, n) into subranges, which are then reduced by invoking loopCallback in parallel. Each invocation of loopCallback returns a single value that is the result of joining the elements in the respective subrange. These values are then further joined using the binary operator reductionCallback, until only a single value remains. This single value is then the result of joining all elements over the entire range [0, n).

The loopCallback must be of the form:

V LoopCallback(size_t begin, size_t end, const V &identity);

The reductionCallback must be of the form:

V ReductionCallback(const V &lhs, const V &rhs);

For example, the following code reduces an array of mesh points into a single bounding box:


// Get the mesh points from which we are going to generate the bounding box. const std::vector<Vector3> &points = GetMeshPoints();

// Generate the bounding box by parallel reducing the points. BoundingBox bbox = WorkParallelReduceN( BoundingBox(), points.size(), [&points](size_t b, size_t e, const BoundingBox &identity){ BoundingBox bbox(identity);

// Insert each point in this subrange into the local bounding box. for (size_t i = b; i != e; ++i) { bbox.InsertPoint(points[i]); }

// Return the local bounding box, which now encapsulates all the // points in this subrange. return bbox; }, [](const BoundingBox &lhs, const BoundingBox &rhs){ // Join two bounding boxes into a single bounding box. The // algorithm will apply this reduction step recursively until there // is only a single bounding box left. BoundingBox bbox(lhs); bbox.UnionWith(rhs); return bbox; } );


grainSize specifies a minimum amount of work to be done per-thread. There is overhead to launching a task and a typical guideline is that you want to have at least 10,000 instructions to count for the overhead of launching that task.

Definition at line 100 of file reduce.h.

template<typename Fn , typename Rn , typename V >
V WorkParallelReduceN ( const V &  identity,
size_t  n,
Fn &&  loopCallback,
Rn &&  reductionCallback 

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

This overload does not accept a grain size parameter and instead attempts to automatically deduce a grain size that is optimal for the current resource utilization and provided workload.

Definition at line 157 of file reduce.h.