Games Task Scheduler (GTS)
A multi-processor scheduling framework for games engines
5_task_recycling.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 4_scheduler_bypassing
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 ParallelFibTask4 : 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  return nullptr;
78  }
79  else
80  {
81  // Create the continuation task with the join function.
83  pContinuationTask->addRef(2, gts::memory_order::relaxed);
84  setContinuationTask(pContinuationTask);
85 
86  // Fork f(n-1)
87  Task* pLeftChild = ctx.pMicroScheduler->allocateTask<ParallelFibTask4>(fibN - 1, &pContinuationTask->l);
88  pContinuationTask->addChildTaskWithoutRef(pLeftChild);
89  ctx.pMicroScheduler->spawnTask(pLeftChild);
90 
91  // Fork f(n-2)
92 
93  // Since the right task has exactly the same form as a this task,
94  // we can simply reuse it and skip the allocator!
95  recycle();
96  pContinuationTask->addChildTaskWithoutRef(this);
97 
98  // Reset the values for the right child.
99  fibN -= 2;
100  sum = &pContinuationTask->r;
101 
102  // Now we just bypass with this task!
103  return this;
104  }
105  }
106 };
107 
108 //------------------------------------------------------------------------------
109 void taskRecycling(uint32_t fibN)
110 {
111  printf ("================\n");
112  printf ("taskRecycling\n");
113  printf ("================\n");
114 
115  // Init boilerplate
116  WorkerPool workerPool;
117  bool result = workerPool.initialize();
118  GTS_ASSERT(result);
119  MicroScheduler microScheduler;
120  result = microScheduler.initialize(&workerPool);
121  GTS_ASSERT(result);
122 
123  uint64_t fibVal = 0;
124 
125  auto start = std::chrono::high_resolution_clock::now();
126 
127  // Create the fib task.
128  Task* pTask = microScheduler.allocateTask<ParallelFibTask4>(fibN, &fibVal);
129 
130  // Queue and wait for the task to complete.
131  microScheduler.spawnTaskAndWait(pTask);
132  // NOTE: wait ^^^^ does NOT mean that this thread is idle. This thread will
133  // actively execute any tasks in the scheduler until pTask completes.
134 
135  auto end = std::chrono::high_resolution_clock::now();
136 
137  std::cout << "Fib " << fibN << " is: " << fibVal << std::endl;
138  std::cout << "Time (ms): " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << std::endl;
139 
140  microScheduler.shutdown();
141  workerPool.shutdown();
142 }
143 
144 } // 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: 5_task_recycling.h:40
Definition: 5_task_recycling.h:62