HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
loops_impl.h
Go to the documentation of this file.
1 //
2 // Copyright 2025 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_TBB_LOOPS_IMPL_H
8 #define PXR_BASE_WORK_TBB_LOOPS_IMPL_H
9 
10 #include "pxr/pxr.h"
11 
12 #include <tbb/blocked_range.h>
13 #include <tbb/parallel_for.h>
14 #include <tbb/parallel_for_each.h>
15 #include <tbb/task_group.h>
16 
17 
18 /// If this is not defined then WorkImpl_ParallelForTBBRange will default
19 /// to a WorkDispatcher based WorkParallelForTBBRange that is provided
20 /// in work/loops.h
21 #define WORK_IMPL_HAS_PARALLEL_FOR_TBB_RANGE
22 
24 
25 /// TBB Parallel For Implementation
26 ///
27 /// Implements ParallelForN
28 ///
29 template <typename Fn>
30 void
31 WorkImpl_ParallelForN(size_t n, Fn &&callback, size_t grainSize)
32 {
33  class Work_ParallelForN_TBB
34  {
35  public:
36  Work_ParallelForN_TBB(Fn &fn) : _fn(fn) { }
37 
38  void operator()(const tbb::blocked_range<size_t> &r) const {
39  // Note that we std::forward _fn using Fn in order get the
40  // right operator().
41  // We maintain the right type in this way:
42  // If Fn is T&, then reference collapsing gives us T& for _fn
43  // If Fn is T, then std::forward correctly gives us T&& for _fn
44  std::forward<Fn>(_fn)(r.begin(), r.end());
45  }
46 
47  private:
48  Fn &_fn;
49  };
50 
51  // In most cases we do not want to inherit cancellation state from the
52  // parent context, so we create an isolated task group context.
53  tbb::task_group_context ctx(tbb::task_group_context::isolated);
54  tbb::parallel_for(tbb::blocked_range<size_t>(0,n,grainSize),
55  Work_ParallelForN_TBB(callback),
56  ctx);
57 }
58 
59 /// Implements WorkParallelForTBBRange
60 ///
61 template <typename RangeType, typename Fn>
62 inline void
63 WorkImpl_ParallelForTBBRange(const RangeType &range, Fn &&callback)
64 {
65  tbb::task_group_context ctx(tbb::task_group_context::isolated);
66  tbb::parallel_for(range, std::forward<Fn>(callback), ctx);
67 }
68 
69 /// Implements WorkParallelForEach
70 ///
71 template <typename InputIterator, typename Fn>
72 inline void
74  InputIterator first, InputIterator last, Fn &&fn)
75 {
76  tbb::task_group_context ctx(tbb::task_group_context::isolated);
77  tbb::parallel_for_each(first, last, std::forward<Fn>(fn), ctx);
78 }
79 
81 
82 #endif // PXR_BASE_WORK_TBB_LOOPS_IMPL_H
GLint first
Definition: glcorearb.h:405
GLenum GLint * range
Definition: glcorearb.h:1925
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
void WorkImpl_ParallelForTBBRange(const RangeType &range, Fn &&callback)
Definition: loops_impl.h:63
void WorkImpl_ParallelForEach(InputIterator first, InputIterator last, Fn &&fn)
Definition: loops_impl.h:73
GLdouble n
Definition: glcorearb.h:2008
OIIO_UTIL_API void parallel_for(int32_t begin, int32_t end, function_view< void(int32_t)> task, paropt opt=0)
__hostdev__ uint64_t last(uint32_t i) const
Definition: NanoVDB.h:5976
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
PXR_NAMESPACE_OPEN_SCOPE void WorkImpl_ParallelForN(size_t n, Fn &&callback, size_t grainSize)
Definition: loops_impl.h:31
GLboolean r
Definition: glcorearb.h:1222