Loading...
Searching...
No Matches
reduce.h
Go to the documentation of this file.
1//
2// Copyright 2018 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_BASE_WORK_REDUCE_H
8#define PXR_BASE_WORK_REDUCE_H
9
11#include "pxr/pxr.h"
12#include "pxr/base/work/api.h"
13#include "pxr/base/work/impl.h"
15
16PXR_NAMESPACE_OPEN_SCOPE
17
18
78template <typename Fn, typename Rn, typename V>
79V
81 const V &identity,
82 size_t n,
83 Fn &&loopCallback,
84 Rn &&reductionCallback,
85 size_t grainSize)
86{
87 if (n == 0)
88 return identity;
89
90 // Don't bother with parallel_reduce, if concurrency is limited to 1.
91 if (WorkHasConcurrency()) {
92 PXR_WORK_IMPL_NAMESPACE_USING_DIRECTIVE;
93 return WorkImpl_ParallelReduceN(
94 identity,
95 n,
96 std::forward<Fn>(loopCallback),
97 std::forward<Rn>(reductionCallback),
98 grainSize);
99 }
100
101 // If concurrency is limited to 1, execute serially.
102 return std::forward<Fn>(loopCallback)(0, n, identity);
103}
104
113template <typename Fn, typename Rn, typename V>
114V
116 const V &identity,
117 size_t n,
118 Fn &&loopCallback,
119 Rn &&reductionCallback)
120{
121 return WorkParallelReduceN(identity, n, loopCallback, reductionCallback, 1);
122}
123
124PXR_NAMESPACE_CLOSE_SCOPE
125
126#endif // PXR_BASE_WORK_REDUCE_H
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 i...
Definition: reduce.h:80
WORK_API bool WorkHasConcurrency()
Return true if WorkGetPhysicalConcurrencyLimit() returns a number greater than 1 and PXR_WORK_THREAD_...