HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
loops.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_BASE_WORK_LOOPS_H
25 #define PXR_BASE_WORK_LOOPS_H
26 
27 /// \file work/loops.h
28 #include "pxr/pxr.h"
30 #include "pxr/base/work/api.h"
31 
32 #include <tbb/blocked_range.h>
33 #include <tbb/parallel_for.h>
34 #include <tbb/parallel_for_each.h>
35 #include <tbb/task_group.h>
36 
38 
39 ///////////////////////////////////////////////////////////////////////////////
40 ///
41 /// WorkSerialForN(size_t n, CallbackType callback)
42 ///
43 /// A serial version of WorkParallelForN as a drop in replacement to
44 /// selectively turn off multithreading for a single parallel loop for easier
45 /// debugging.
46 ///
47 /// Callback must be of the form:
48 ///
49 /// void LoopCallback(size_t begin, size_t end);
50 ///
51 template<typename Fn>
52 void
53 WorkSerialForN(size_t n, Fn &&fn)
54 {
55  std::forward<Fn>(fn)(0, n);
56 }
57 
58 ///////////////////////////////////////////////////////////////////////////////
59 ///
60 /// WorkParallelForN(size_t n, CallbackType callback, size_t grainSize = 1)
61 ///
62 /// Runs \p callback in parallel over the range 0 to n.
63 ///
64 /// Callback must be of the form:
65 ///
66 /// void LoopCallback(size_t begin, size_t end);
67 ///
68 /// grainSize specifies a minimum amount of work to be done per-thread. There
69 /// is overhead to launching a thread (or task) and a typical guideline is that
70 /// you want to have at least 10,000 instructions to count for the overhead of
71 /// launching a thread.
72 ///
73 template <typename Fn>
74 void
75 WorkParallelForN(size_t n, Fn &&callback, size_t grainSize)
76 {
77  if (n == 0)
78  return;
79 
80  // Don't bother with parallel_for, if concurrency is limited to 1.
81  if (WorkHasConcurrency()) {
82 
83  class Work_ParallelForN_TBB
84  {
85  public:
86  Work_ParallelForN_TBB(Fn &fn) : _fn(fn) { }
87 
88  void operator()(const tbb::blocked_range<size_t> &r) const {
89  // Note that we std::forward _fn using Fn in order get the
90  // right operator().
91  // We maintain the right type in this way:
92  // If Fn is T&, then reference collapsing gives us T& for _fn
93  // If Fn is T, then std::forward correctly gives us T&& for _fn
94  std::forward<Fn>(_fn)(r.begin(), r.end());
95  }
96 
97  private:
98  Fn &_fn;
99  };
100 
101  // In most cases we do not want to inherit cancellation state from the
102  // parent context, so we create an isolated task group context.
103  tbb::task_group_context ctx(tbb::task_group_context::isolated);
104  tbb::parallel_for(tbb::blocked_range<size_t>(0,n,grainSize),
105  Work_ParallelForN_TBB(callback),
106  ctx);
107 
108  } else {
109 
110  // If concurrency is limited to 1, execute serially.
111  WorkSerialForN(n, std::forward<Fn>(callback));
112 
113  }
114 }
115 
116 ///////////////////////////////////////////////////////////////////////////////
117 ///
118 /// WorkParallelForN(size_t n, CallbackType callback, size_t grainSize = 1)
119 ///
120 /// Runs \p callback in parallel over the range 0 to n.
121 ///
122 /// Callback must be of the form:
123 ///
124 /// void LoopCallback(size_t begin, size_t end);
125 ///
126 ///
127 template <typename Fn>
128 void
129 WorkParallelForN(size_t n, Fn &&callback)
130 {
131  WorkParallelForN(n, std::forward<Fn>(callback), 1);
132 }
133 
134 ///////////////////////////////////////////////////////////////////////////////
135 ///
136 /// WorkParallelForEach(Iterator first, Iterator last, CallbackType callback)
137 ///
138 /// Callback must be of the form:
139 ///
140 /// void LoopCallback(T elem);
141 ///
142 /// where the type T is deduced from the type of the InputIterator template
143 /// argument.
144 ///
145 ///
146 ///
147 template <typename InputIterator, typename Fn>
148 inline void
150  InputIterator first, InputIterator last, Fn &&fn)
151 {
152  tbb::task_group_context ctx(tbb::task_group_context::isolated);
153  tbb::parallel_for_each(first, last, std::forward<Fn>(fn), ctx);
154 }
155 
157 
158 #endif // PXR_BASE_WORK_LOOPS_H
GLint first
Definition: glcorearb.h:405
void parallel_for(int64_t start, int64_t end, std::function< void(int64_t index)> &&task, parallel_options opt=parallel_options(0, Split_Y, 1))
Definition: parallel.h:127
PXR_NAMESPACE_OPEN_SCOPE void WorkSerialForN(size_t n, Fn &&fn)
Definition: loops.h:53
void WorkParallelForN(size_t n, Fn &&callback, size_t grainSize)
Definition: loops.h:75
GLdouble n
Definition: glcorearb.h:2008
UnaryFunction parallel_for_each(InputIt first, InputIt last, UnaryFunction f, parallel_options opt=parallel_options(0, Split_Y, 1))
Definition: parallel.h:161
WORK_API bool WorkHasConcurrency()
void WorkParallelForEach(InputIterator first, InputIterator last, Fn &&fn)
Definition: loops.h:149
__hostdev__ uint64_t last(uint32_t i) const
Definition: NanoVDB.h:5976
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
GLboolean r
Definition: glcorearb.h:1222