HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_TaskGroup.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_TaskGroup.h (UT Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __UT_TASKGROUP_H_INCLUDED__
12 #define __UT_TASKGROUP_H_INCLUDED__
13 
14 #ifndef __TBB_show_deprecation_message_task_H
15  #define __TBB_show_deprecation_message_task_H
16  #define UT_TASKGROUP_RESTORE_DEPRECATION_MESSAGE
17 #endif
18 
19 #include "UT_API.h"
20 #include "UT_Thread.h"
21 #include <tbb/parallel_for.h>
22 #include <tbb/task.h>
23 #include <tbb/task_group.h>
24 #include <utility>
25 
26 class UT_API UT_TaskGroupContext : private tbb::task_group_context
27 {
28  typedef tbb::task_group_context me;
29 
30 public:
31 
32  enum KindType
33  {
34  ISOLATED = me::isolated,
35  BOUND = me::bound
36  };
37 
38  /// Constructs an empty task_group_context.
39  ///
40  /// If relation_to_parent is bound, the task_group_context will become a
41  /// child of the innermost running task's group when it is first spawned.
42  /// If this call is made directly from the user thread, the effect will be
43  /// as if relation_to_parent were isolated. If relation_to_parent is
44  /// isolated, it has no parent task_group_context.
45  UT_TaskGroupContext(KindType relation_to_parent = BOUND)
46  : task_group_context((kind_type)relation_to_parent)
47  {
48  }
49 
50  /// Destroys an empty task_group_context. The behavior is undefined if
51  /// there are still extant tasks associated with this task_group_context.
52  ~UT_TaskGroupContext() = default;
53 
54  /// Reinitializes this to uncancelled state.
55  ///
56  /// @warning This method is only safe to call once all tasks associated with
57  /// the group's subordinate groups have completed. This method must not be
58  /// invoked concurrently by multiple threads.
59  void reset()
60  {
61  me::reset();
62  }
63 
64  /// Requests that tasks in group be cancelled.
65  ///
66  /// @return False if group is already cancelled; true otherwise. If
67  /// concurrently called by multiple threads, exactly one call
68  /// returns true and the rest return false.
70  {
71  return me::cancel_group_execution();
72  }
73 
74  /// @return True if group has received cancellation.
76  {
77  return me::is_group_execution_cancelled();
78  }
79 
80  /// Cancel the entire current task group context when run within a task
82  {
83  // TODO: In oneTBB, this becomes,
84  // current_context()->cancel_group_execution()
85  tbb::task::self().cancel_group_execution();
86  }
87 };
88 
89 class UT_API UT_TaskGroup : private tbb::task_group
90 {
91  typedef tbb::task_group me;
92 
93 public:
95  : tbb::task_group()
96  {
97  }
99  {
100  }
101 
102  template <typename F>
103  void run(F &&f)
104  {
105  me::run(std::forward<F>(f));
106  }
107 
108  template <typename F>
109  void run(tbb::task_handle<F> &f)
110  {
111  me::run(f);
112  }
113 
114  template <typename F>
115  void runAndWait(const F &f)
116  {
117  me::run_and_wait(f);
118  }
119 
120  template <typename F>
121  void runAndWait(tbb::task_handle<F> &f)
122  {
123  me::run_and_wait(f);
124  }
125 
126  // American English
127  bool isCanceling() { return me::is_canceling(); }
128  // British English
129  bool isCancelling() { return me::is_canceling(); }
130 
131  tbb::task_group_status wait() { return me::wait(); }
132  void cancel() { me::cancel(); }
133 };
134 
135 /// Spawn a UTparallelFor() task to a UT_TaskGroup
136 /// @see UTparallelFor(), UT_TaskGroup
137 template <typename RANGE, typename BODY>
138 void
140  UT_TaskGroup &task_group,
141  RANGE &&range,
142  BODY &&body)
143 {
144  // We forward range and body into the lambda so that if they were movable
145  // temporaries, this can avoid an extra copy.
146  task_group.run(
147  [range = std::forward<RANGE>(range), body = std::forward<BODY>(body)]()
148  {
150  tbb::parallel_for(range, body, tbb::simple_partitioner());
151  });
152 }
153 
154 #ifdef UT_TASKGROUP_RESTORE_DEPRECATION_MESSAGE
155  #undef __TBB_show_deprecation_message_task_H
156  #undef UT_TASKGROUP_RESTORE_DEPRECATION_MESSAGE
157 #endif
158 
159 #endif // __UT_TASKGROUP_H_INCLUDED__
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
bool isCancelling()
Definition: UT_TaskGroup.h:129
GLenum GLint * range
Definition: glcorearb.h:1925
UT_TaskGroupContext(KindType relation_to_parent=BOUND)
Definition: UT_TaskGroup.h:45
#define UT_API
Definition: UT_API.h:14
static bool isThreadingEnabled()
void runAndWait(tbb::task_handle< F > &f)
Definition: UT_TaskGroup.h:121
tbb::task_group_status wait()
Definition: UT_TaskGroup.h:131
void runAndWait(const F &f)
Definition: UT_TaskGroup.h:115
GLfloat f
Definition: glcorearb.h:1926
GLboolean reset
Definition: glad.h:5138
void run(F &&f)
Definition: UT_TaskGroup.h:103
void UTparallelForRunInTaskGroup(UT_TaskGroup &task_group, RANGE &&range, BODY &&body)
Definition: UT_TaskGroup.h:139
void run(tbb::task_handle< F > &f)
Definition: UT_TaskGroup.h:109
*tasks wait()
bool cancelGroupExecution()
Definition: UT_TaskGroup.h:69
bool isCanceling()
Definition: UT_TaskGroup.h:127
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
bool isGroupExecutionCancelled() const
Definition: UT_TaskGroup.h:75
static void cancelCurrentGroupExecution()
Cancel the entire current task group context when run within a task.
Definition: UT_TaskGroup.h:81