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 WORK_LOOPS_H
25 #define WORK_LOOPS_H
26 
28 #include "pxr/pxr.h"
29 #include "pxr/base/work/threadLimits.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.h>
36 
37 PXR_NAMESPACE_OPEN_SCOPE
38 
51 template<typename Fn>
52 void
53 WorkSerialForN(size_t n, Fn &&fn)
54 {
55  std::forward<Fn>(fn)(0, n);
56 }
57 
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 (WorkGetConcurrencyLimit() > 1) {
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 
127 template <typename Fn>
128 void
129 WorkParallelForN(size_t n, Fn &&callback)
130 {
131  WorkParallelForN(n, std::forward<Fn>(callback), 1);
132 }
133 
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 
156 PXR_NAMESPACE_CLOSE_SCOPE
157 
158 #endif // WORK_LOOPS_H
void WorkSerialForN(size_t n, Fn &&fn)
WorkSerialForN(size_t n, CallbackType callback)
Definition: loops.h:53
void WorkParallelForN(size_t n, Fn &&callback, size_t grainSize)
WorkParallelForN(size_t n, CallbackType callback, size_t grainSize = 1)
Definition: loops.h:75
WORK_API unsigned WorkGetConcurrencyLimit()
Return the current concurrency limit, always &gt;= 1.
void WorkParallelForEach(InputIterator first, InputIterator last, Fn &&fn)
WorkParallelForEach(Iterator first, Iterator last, CallbackType callback)
Definition: loops.h:149