HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_TaskList.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_TaskList.h (UT Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __UT_TASKLIST_H_INCLUDED__
12 #define __UT_TASKLIST_H_INCLUDED__
13 
14 #include "UT_Assert.h"
15 #include "UT_TaskScope.h"
16 #include "UT_Thread.h"
17 #include <tbb/parallel_for.h>
18 #include <tbb/task_group.h>
19 #include <functional>
20 #include <type_traits>
21 #include <utility>
22 #include <vector>
23 
24 /// @note Consider using UT_TaskGroup instead
26 {
27 public:
28 
29  /// Returns true when number of tasks is non-zero
30  bool isEmpty() const
31  {
32  return myTasks.empty();
33  }
34 
35  /// Removes all tasks
36  void clear()
37  {
38  myTasks.clear();
39  }
40 
41  /// Append a task
42  template <typename Body>
43  void append(Body&& task)
44  {
45  myTasks.emplace_back(std::forward<Body>(task));
46  }
47 
48  /// Spawn this task list as root tasks and wait
50  {
51  tbb::task_group tg;
52  const UT_TaskScope *parent = UT_TaskScope::getCurrent();
53  for (auto&& task : myTasks)
54  {
55  tg.run([parent, task]() {
56  UT_TaskScope task_scope(parent);
57  task();
58  });
59  }
60  (void) tg.wait();
61  }
62 
63  /// Run this task list in serial
64  void runSerial()
65  {
66  for (auto&& task : myTasks)
67  task();
68  }
69 
70 private:
71  // Cannot use UT_Array here since std::function is not trivially
72  // relocatable on any platform that has small buffer optimizations.
73  std::vector<std::function<void()>> myTasks;
74 };
75 
76 /// Append a UTparallelFor() task to a UT_TaskList for later spawning en masse.
77 /// @see UTparallelFor(), UT_TaskList
78 template <typename RANGE, typename BODY>
79 void
81  UT_TaskList &task_list,
82  RANGE &&range,
83  BODY &&body)
84 {
85  // We forward range and body into the lambda so that if they were movable
86  // temporaries, this can avoid an extra copy.
87  task_list.append(
88  [range = std::forward<RANGE>(range), body = std::forward<BODY>(body)]()
89  {
91  tbb::parallel_for(range, body, tbb::simple_partitioner());
92  });
93 }
94 
95 #endif // __UT_TASKLIST_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
void spawnRootAndWait()
Spawn this task list as root tasks and wait.
Definition: UT_TaskList.h:49
void
Definition: png.h:1083
static const UT_TaskScope * getCurrent()
Definition: UT_TaskScope.h:94
void append(Body &&task)
Append a task.
Definition: UT_TaskList.h:43
static bool isThreadingEnabled()
void UTparallelForAppendToTaskList(UT_TaskList &task_list, RANGE &&range, BODY &&body)
Definition: UT_TaskList.h:80
void clear()
Removes all tasks.
Definition: UT_TaskList.h:36
bool isEmpty() const
Returns true when number of tasks is non-zero.
Definition: UT_TaskList.h:30
GLenum GLint * range
Definition: glcorearb.h:1924
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:171
void runSerial()
Run this task list in serial.
Definition: UT_TaskList.h:64