Games Task Scheduler (GTS)
A multi-processor scheduling framework for games engines
2_1to1_affinity_init.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 #include "gts/platform/Thread.h"
23 
24 #include "gts/micro_scheduler/WorkerPool.h"
25 #include "gts/micro_scheduler/MicroScheduler.h"
26 
27 using namespace gts;
28 
29 namespace gts_examples {
30 
31 void affinityInit()
32 {
33  printf ("================\n");
34  printf ("affinityInit\n");
35  printf ("================\n");
36 
37  // Explicitly create the workers via a description struct. For this example
38  // we will affinitize each worker to a HW thread.
39  WorkerPoolDesc workerPoolDesc;
40 
41  // Get the CPU topology.
42  SystemTopology sysTopo;
44 
45  // Loop through each processor group.
46  for (size_t iGroup = 0; iGroup < sysTopo.groupInfoElementCount; ++iGroup)
47  {
48  ProcessorGroupInfo const& groupInfo = sysTopo.pGroupInfoArray[iGroup];
49 
50  // Loop through each physical core.
51  for (size_t iCore = 0; iCore < groupInfo.coreInfoElementCount; ++iCore)
52  {
53  CpuCoreInfo const& coreInfo = groupInfo.pCoreInfoArray[iCore];
54 
55  // Loop through each thread.
56  for (size_t iThead = 0; iThead < coreInfo.hardwareThreadIdCount; ++iThead)
57  {
58  WorkerThreadDesc workerDesc;
59 
60  // Set the affinity mask.
61  workerDesc.affinity.affinitySet.set(coreInfo.pHardwareThreadIds[iThead]);
62  workerDesc.affinity.group = iGroup;
63 
64  // Add the worker desc to the pool desc.
65  workerPoolDesc.workerDescs.push_back(workerDesc);
66  }
67  }
68  }
69 
70  // Affinitize the Master thread here since the Worker Pool does not own it.
71  AffinitySet masterAffinity;
72  masterAffinity.set(sysTopo.pGroupInfoArray[0].pCoreInfoArray[0].pHardwareThreadIds[0]);
73  gts::ThisThread::setAffinity(0, masterAffinity);
74 
75  // Create a worker pool and init it with the description.
76  WorkerPool workerPool;
77  bool result = workerPool.initialize(workerPoolDesc);
78  GTS_ASSERT(result);
79 
80  // Create a micro scheduler and assign the worker pool to it as before.
81  MicroScheduler microScheduler;
82  result = microScheduler.initialize(&workerPool);
83  GTS_ASSERT(result);
84 
85  // schedule stuff ...
86 
87  microScheduler.shutdown();
88  workerPool.shutdown();
89 }
90 
91 } // namespace gts_examples
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 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
uint32_t * pHardwareThreadIds
The ID for each hardware thread in the core.
Definition: Thread.h:156
CpuCoreInfo * pCoreInfoArray
A array of descriptions for each processor core in the group.
Definition: Thread.h:211
size_t groupInfoElementCount
The number of elements in pGroupInfoArray.
Definition: Thread.h:240
static GTS_INLINE bool setAffinity(size_t groupId, AffinitySet const &affinity)
Sets the processor affinity for the thread and returns the previous affinity.
Definition: Thread.h:497
ProcessorGroupInfo * pGroupInfoArray
A array of descriptions for each processor group.
Definition: Thread.h:238
static GTS_INLINE void getSystemTopology(SystemTopology &out)
Gets the topology of all the processors in the system.
Definition: Thread.h:441
size_t hardwareThreadIdCount
The number of elements in pHardwareThreadIds.
Definition: Thread.h:158
size_t coreInfoElementCount
The number of elements in pCoreInfoArray.
Definition: Thread.h:213
A description of a CPU core.
Definition: Thread.h:149
A description of a processor group.
Definition: Thread.h:202
A description of the system's processor topology.
Definition: Thread.h:231
The description of a WorkerPool.
Definition: MicroSchedulerTypes.h:172
gts::Vector< WorkerThreadDesc > workerDescs
The description of each worker thread.
Definition: MicroSchedulerTypes.h:184
AffinitySet affinitySet
A set of processor affinities.
Definition: MicroSchedulerTypes.h:100
size_t group
The group the the affinities in affinitySet belong too. Groups seem to only be relevant for Windows....
Definition: MicroSchedulerTypes.h:95
The description of a worker Thread.
Definition: MicroSchedulerTypes.h:87
WorkerThreadDesc::GroupAndAffinity affinity
Specifies the thread affinity for each processor group. See gts::Thread::getSystemTopology to identif...
Definition: MicroSchedulerTypes.h:114