Games Task Scheduler (GTS)
A multi-processor scheduling framework for games engines
2_parallel_reduce.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 "gts/micro_scheduler/WorkerPool.h"
25 #include "gts/micro_scheduler/MicroScheduler.h"
26 #include "gts/micro_scheduler/patterns/ParallelReduce.h"
27 #include "gts/micro_scheduler/patterns/Partitioners.h"
28 #include "gts/micro_scheduler/patterns/Range1d.h"
29 
30 using namespace gts;
31 
32 namespace gts_examples {
33 
34 //------------------------------------------------------------------------------
35 void fullParallelReduce()
36 {
37  printf ("================\n");
38  printf ("fullParallelReduce\n");
39  printf ("================\n");
40 
41  // Init boilerplate
42  WorkerPool workerPool;
43  bool result = workerPool.initialize(1);
44  GTS_ASSERT(result);
45  MicroScheduler microScheduler;
46  result = microScheduler.initialize(&workerPool);
47  GTS_ASSERT(result);
48 
49  size_t const elementCount = 1 << 2;
50 
51  // Create the array to reduce.
52  std::vector<uint32_t> vec(elementCount);
53  for (size_t ii = 0; ii < elementCount; ++ii)
54  {
55  vec[ii] = uint32_t(ii);
56  }
57 
58  // Make a parallel-reduce object for this scheduler. We do this because
59  // there can be multiple scheduler objects.
60  ParallelReduce parallelReduce(microScheduler);
61 
62  auto partitionerType = AdaptivePartitioner();
63 
64  uint32_t reduction = parallelReduce(
65 
66  // The 1D iterator range parallel-for will iterate over.
67  Range1d<std::vector<uint32_t>::iterator>(vec.begin(), vec.end(), 1),
68 
69  // The function parallel-reduce will execute on each block of the range.
70  // It returns the reduction of the block.
71  [](Range1d<std::vector<uint32_t>::iterator>& range, void*, TaskContext const&) -> uint32_t
72  {
73  uint32_t result = 0;
74  for (auto ii = range.begin(); ii != range.end(); ++ii)
75  {
76  result += *ii;
77  }
78  return result;
79  },
80 
81  // The function that combines the block reductions.
82  [](uint32_t const& lhs, uint32_t const& rhs, void*, TaskContext const&) -> uint32_t
83  {
84  return lhs + rhs;
85  },
86 
87  // The initial value of the reduction.
88  0,
89 
90  // The partitioner object.
91  partitionerType);
92 
93  microScheduler.shutdown();
94  workerPool.shutdown();
95 }
96 
97 } // namespace gts_examples
Adaptively subdivides a TRange based on demand from the scheduler.
Definition: AdaptivePartitioner.h:28
A work-stealing task scheduler. The scheduler is executed by the WorkerPool it is initialized with.
Definition: MicroScheduler.h:81
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....
A construct that maps parallel-reduce behavior to a MicroScheduler.
Definition: ParallelReduce.h:48
An iteration range over a 1D data set. Splits divide the range in two based unless the minimum size i...
Definition: Range1d.h:56
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