Games Task Scheduler (GTS)
A multi-processor scheduling framework for games engines
3_continuation_join.h
1 /*******************************************************************************
2  * Copyright 2019 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files(the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions :
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  ******************************************************************************/
22 #pragma once
23 
24 #include <cassert>
25 #include <iostream>
26 #include <chrono>
27 
28 #include <gts/micro_scheduler/WorkerPool.h>
29 #include <gts/micro_scheduler/MicroScheduler.h>
30 
31 using namespace gts;
32 
33 namespace gts_examples {
34 
35 // Builds on 2_blocking_join
36 
37 //------------------------------------------------------------------------------
38 // A task that explicitly represent a join.
40 {
42  uint32_t l,
43  uint32_t r,
44  uint64_t* sum)
45  : l(l)
46  , r(r)
47  , sum(sum) {}
48 
49  uint64_t l;
50  uint64_t r;
51  uint64_t* sum;
52 
53  virtual Task* execute(gts::TaskContext const&) override
54  {
55  *(sum) = l + r;
56  return nullptr;
57  }
58 };
59 
60 //------------------------------------------------------------------------------
61 struct ParallelFibTask2 : public Task
62 {
63  uint32_t fibN;
64  uint64_t* sum;
65 
67  uint32_t fibN,
68  uint64_t* sum)
69  : fibN(fibN)
70  , sum(sum) {}
71 
72  virtual Task* execute(gts::TaskContext const& ctx) override
73  {
74  if (fibN <= 2)
75  {
76  *sum = 1;
77  }
78  else
79  {
80  // Create the continuation task with the join function.
81  ParallelFibContinuationTask* pContinuationTask =
83 
84  // We now replace pThisTask in the task graph with the continuation task.
85  /*
86  ---------- Child_R
87  / \ /
88  <---Cont <-- pThisTask ---
89  \
90  Child_L
91  */
92  setContinuationTask(pContinuationTask);
93 
94  // This time add the ref count to the continuation.
95  pContinuationTask->addRef(2, gts::memory_order::relaxed);
96 
97  // Fork f(n-1)
98  Task* pLeftChild = ctx.pMicroScheduler->allocateTask<ParallelFibTask2>(fibN - 1, &pContinuationTask->l);
99  // Add the task to the continuation.
100  pContinuationTask->addChildTaskWithoutRef(pLeftChild);
101  ctx.pMicroScheduler->spawnTask(pLeftChild);
102 
103  // Fork f(n-2)
104  Task* pRightChild = ctx.pMicroScheduler->allocateTask<ParallelFibTask2>(fibN - 2, &pContinuationTask->r);
105  // Add the task to the continuation.
106  pContinuationTask->addChildTaskWithoutRef(pRightChild);
107  ctx.pMicroScheduler->spawnTask(pRightChild);
108 
109  // We don't have to explicitly wait for the left and right children
110  // to join as before. Now the last child to complete will automatically
111  // join by executing the continuation task.
112  }
113 
114  return nullptr;
115  }
116 };
117 
118 //------------------------------------------------------------------------------
119 void continuationJoin(uint32_t fibN)
120 {
121  printf ("================\n");
122  printf ("continuationJoin\n");
123  printf ("================\n");
124 
125  // Init boilerplate
126  WorkerPool workerPool;
127  bool result = workerPool.initialize();
128  GTS_ASSERT(result);
129  MicroScheduler microScheduler;
130  result = microScheduler.initialize(&workerPool);
131  GTS_ASSERT(result);
132 
133  uint64_t fibVal = 0;
134 
135  auto start = std::chrono::high_resolution_clock::now();
136 
137  // Create the fib task.
138  Task* pTask = microScheduler.allocateTask<ParallelFibTask2>(fibN, &fibVal);
139 
140  // Queue and wait for the task to complete.
141  microScheduler.spawnTaskAndWait(pTask);
142  // NOTE: wait ^^^^ does NOT mean that this thread is idle. This thread will
143  // actively execute any tasks in the scheduler until pTask completes.
144 
145  auto end = std::chrono::high_resolution_clock::now();
146 
147  std::cout << "Fib " << fibN << " is: " << fibVal << std::endl;
148  std::cout << "Time (ms): " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << std::endl;
149 
150  microScheduler.shutdown();
151  workerPool.shutdown();
152 }
153 
154 } // namespace gts_examples
A work-stealing task scheduler. The scheduler is executed by the WorkerPool it is initialized with.
Definition: MicroScheduler.h:81
void spawnTaskAndWait(Task *pTask, uint32_t priority=0)
Spawns the specified 'pTask' to be executed by the scheduler and then waits for its reference count t...
void spawnTask(Task *pTask, uint32_t priority=0)
Spawns the specified 'pTask' to be executed by the scheduler. Spawned tasks are executed in LIFO orde...
bool initialize(WorkerPool *pWorkerPool)
Initializes the MicroScheduler and attaches it to pWorkPool, where each worker in pWorkPool will exec...
void shutdown()
Stops the MicroScheduler and destroys all resources. The TaskSchuduler is now in an unusable state....
GTS_INLINE TTask * allocateTask(TArgs &&... args)
Allocates a new Task object of type TTask.
Definition: MicroScheduler.h:145
A Task payload that embeds TFunc and TArgs into the Task's data. It makes it easy to construct a Task...
Definition: Task.h:120
GTS_INLINE int32_t addRef(int32_t count=1, gts::memory_order order=gts::memory_order::seq_cst)
Definition: Task.inl:84
GTS_INLINE void addChildTaskWithoutRef(Task *pChild)
Definition: Task.inl:30
A collection of running Worker threads that a MicroScheduler can be run on.
Definition: WorkerPool.h:54
bool initialize(uint32_t threadCount=0)
#define GTS_ASSERT(expr)
Causes execution to break when expr is false.
Definition: Assert.h:144
The context associated with the task being executed.
Definition: MicroSchedulerTypes.h:54
MicroScheduler * pMicroScheduler
The MicroScheduler executing the Task.
Definition: MicroSchedulerTypes.h:59
Definition: 3_continuation_join.h:40
Definition: 3_continuation_join.h:62