Games Task Scheduler (GTS)
A multi-processor scheduling framework for games engines
4_scheduler_bypassing.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 3_continuation_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 ParallelFibTask3 : 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  setContinuationTask(pContinuationTask);
84  pContinuationTask->addRef(2, gts::memory_order::relaxed);
85 
86  // Fork f(n-1)
87  Task* pLeftChild = ctx.pMicroScheduler->allocateTask<ParallelFibTask3>(fibN - 1, &pContinuationTask->l);
88  pContinuationTask->addChildTaskWithoutRef(pLeftChild);
89  ctx.pMicroScheduler->spawnTask(pLeftChild);
90 
91  // Fork f(n-2)
92  Task* pRightChild = ctx.pMicroScheduler->allocateTask<ParallelFibTask3>(fibN - 2, &pContinuationTask->r);
93  pContinuationTask->addChildTaskWithoutRef(pRightChild);
94  // Don't queue the right child! return it.
95 
96  // We return right child. The makes the task execute immediately,
97  // bypassing the more expensive scheduler operations.
98  return pRightChild;
99  }
100  }
101 };
102 
103 //------------------------------------------------------------------------------
104 void schedulerBypassing(uint32_t fibN)
105 {
106  printf ("================\n");
107  printf ("schedulerBypassing\n");
108  printf ("================\n");
109 
110  // Init boilerplate
111  WorkerPool workerPool;
112  bool result = workerPool.initialize();
113  GTS_ASSERT(result);
114  MicroScheduler microScheduler;
115  result = microScheduler.initialize(&workerPool);
116  GTS_ASSERT(result);
117 
118  uint64_t fibVal = 0;
119 
120  auto start = std::chrono::high_resolution_clock::now();
121 
122  // Create the fib task.
123  Task* pTask = microScheduler.allocateTask<ParallelFibTask3>(fibN, &fibVal);
124 
125  // Queue and wait for the task to complete.
126  microScheduler.spawnTaskAndWait(pTask);
127 
128  auto end = std::chrono::high_resolution_clock::now();
129 
130  // NOTE: wait ^^^^ does NOT mean that this thread is idle. This thread will
131  // actively execute any tasks in the scheduler until pTask completes.
132 
133  std::cout << "Fib " << fibN << " is: " << fibVal << std::endl;
134  std::cout << "Time (ms): " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << std::endl;
135 
136  microScheduler.shutdown();
137  workerPool.shutdown();
138 }
139 
140 } // 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: 4_scheduler_bypassing.h:40
Definition: 4_scheduler_bypassing.h:62