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 <oneapi/tbb/task.h>
22 #include <oneapi/tbb/task_group.h>
23 #include <oneapi/tbb/parallel_for.h>
24 #include <utility>
25 
26 class 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  tbb::task::current_context()->cancel_group_execution();
84  }
85 };
86 
87 class UT_TaskGroup : private tbb::task_group
88 {
89  typedef tbb::task_group me;
90 
91 public:
93  : tbb::task_group()
94  {
95  }
97  {
98  }
99 
100  template <typename F>
101  void run(F &&f)
102  {
103  me::run(std::forward<F>(f));
104  }
105 
106  template <typename F>
107  void runAndWait(const F &f)
108  {
109  me::run_and_wait(f);
110  }
111 
112  tbb::task_group_status wait() { return me::wait(); }
113  void cancel() { me::cancel(); }
114 };
115 
116 /// Spawn a UTparallelFor() task to a UT_TaskGroup
117 /// @see UTparallelFor(), UT_TaskGroup
118 template <typename RANGE, typename BODY>
119 static inline void
120 UTparallelForRunInTaskGroup(
121  UT_TaskGroup &task_group,
122  RANGE &&range,
123  BODY &&body)
124 {
125  // We forward range and body into the lambda so that if they were movable
126  // temporaries, this can avoid an extra copy.
127  task_group.run(
128  [range = std::forward<RANGE>(range), body = std::forward<BODY>(body)]()
129  {
131  tbb::parallel_for(range, body, tbb::simple_partitioner());
132  });
133 }
134 
135 #ifdef UT_TASKGROUP_RESTORE_DEPRECATION_MESSAGE
136  #undef __TBB_show_deprecation_message_task_H
137  #undef UT_TASKGROUP_RESTORE_DEPRECATION_MESSAGE
138 #endif
139 
140 #endif // __UT_TASKGROUP_H_INCLUDED__
GLenum GLint * range
Definition: glcorearb.h:1925
UT_TaskGroupContext(KindType relation_to_parent=BOUND)
Definition: UT_TaskGroup.h:45
~UT_TaskGroupContext()=default
bool isGroupExecutionCancelled()
Definition: UT_TaskGroup.h:75
static bool isThreadingEnabled()
tbb::task_group_status wait()
Definition: UT_TaskGroup.h:112
void runAndWait(const F &f)
Definition: UT_TaskGroup.h:107
GLfloat f
Definition: glcorearb.h:1926
GLboolean reset
Definition: glad.h:5138
void run(F &&f)
Definition: UT_TaskGroup.h:101
OIIO_UTIL_API void parallel_for(int32_t begin, int32_t end, function_view< void(int32_t)> task, paropt opt=0)
*tasks wait()
bool cancelGroupExecution()
Definition: UT_TaskGroup.h:69
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
static void cancelCurrentGroupExecution()
Cancel the entire current task group context when run within a task.
Definition: UT_TaskGroup.h:81