1 /****************************************************************************
2 * Copyright (C) 2014-2015 Intel Corporation. All Rights Reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 ****************************************************************************/
33 #if defined(__linux__) || defined(__gnu_linux__)
39 #include "common/os.h"
43 #include "rasterizer.h"
44 #include "rdtsc_core.h"
53 uint32_t procGroup
= 0;
54 std::vector
<uint32_t> threadIds
;
59 std::vector
<Core
> cores
;
62 typedef std::vector
<NumaNode
> CPUNumaNodes
;
64 void CalculateProcessorTopology(CPUNumaNodes
& out_nodes
, uint32_t& out_numThreadsPerProcGroup
)
67 out_numThreadsPerProcGroup
= 0;
71 SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX buffer
[KNOB_MAX_NUM_THREADS
];
72 DWORD bufSize
= sizeof(buffer
);
74 BOOL ret
= GetLogicalProcessorInformationEx(RelationProcessorCore
, buffer
, &bufSize
);
75 SWR_ASSERT(ret
!= FALSE
, "Failed to get Processor Topology Information");
77 uint32_t count
= bufSize
/ buffer
->Size
;
78 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX pBuffer
= buffer
;
80 for (uint32_t i
= 0; i
< count
; ++i
)
82 SWR_ASSERT(pBuffer
->Relationship
== RelationProcessorCore
);
83 for (uint32_t g
= 0; g
< pBuffer
->Processor
.GroupCount
; ++g
)
85 auto& gmask
= pBuffer
->Processor
.GroupMask
[g
];
86 uint32_t threadId
= 0;
87 uint32_t procGroup
= gmask
.Group
;
89 Core
* pCore
= nullptr;
91 uint32_t numThreads
= (uint32_t)_mm_popcount_sizeT(gmask
.Mask
);
93 while (BitScanForwardSizeT((unsigned long*)&threadId
, gmask
.Mask
))
96 gmask
.Mask
&= ~(KAFFINITY(1) << threadId
);
99 PROCESSOR_NUMBER procNum
= {};
100 procNum
.Group
= WORD(procGroup
);
101 procNum
.Number
= UCHAR(threadId
);
104 ret
= GetNumaProcessorNodeEx(&procNum
, (PUSHORT
)&numaId
);
108 if (out_nodes
.size() <= numaId
) out_nodes
.resize(numaId
+ 1);
109 auto& numaNode
= out_nodes
[numaId
];
113 if (nullptr == pCore
)
115 numaNode
.cores
.push_back(Core());
116 pCore
= &numaNode
.cores
.back();
117 pCore
->procGroup
= procGroup
;
119 coreId
= (uint32_t)numaNode
.cores
.size();
120 if ((coreId
* numThreads
) >= 32)
122 // Windows doesn't return threadIds >= 32 for a processor group correctly
123 // when running a 32-bit application.
124 // Just save -1 as the threadId
125 threadId
= uint32_t(-1);
129 pCore
->threadIds
.push_back(threadId
);
132 out_numThreadsPerProcGroup
++;
136 pBuffer
= PtrAdd(pBuffer
, pBuffer
->Size
);
140 #elif defined(__linux__) || defined (__gnu_linux__)
142 // Parse /proc/cpuinfo to get full topology
143 std::ifstream
input("/proc/cpuinfo");
146 uint32_t threadId
= uint32_t(-1);
147 uint32_t coreId
= uint32_t(-1);
148 uint32_t numaId
= uint32_t(-1);
150 while (std::getline(input
, line
))
152 if (line
.find("processor") != std::string::npos
)
154 if (threadId
!= uint32_t(-1))
157 if (out_nodes
.size() <= numaId
) out_nodes
.resize(numaId
+ 1);
158 auto& numaNode
= out_nodes
[numaId
];
159 if (numaNode
.cores
.size() <= coreId
) numaNode
.cores
.resize(coreId
+ 1);
160 auto& core
= numaNode
.cores
[coreId
];
162 core
.procGroup
= coreId
;
163 core
.threadIds
.push_back(threadId
);
165 out_numThreadsPerProcGroup
++;
168 auto data_start
= line
.find(": ") + 2;
169 threadId
= std::strtoul(&line
.c_str()[data_start
], &c
, 10);
172 if (line
.find("core id") != std::string::npos
)
174 auto data_start
= line
.find(": ") + 2;
175 coreId
= std::strtoul(&line
.c_str()[data_start
], &c
, 10);
178 if (line
.find("physical id") != std::string::npos
)
180 auto data_start
= line
.find(": ") + 2;
181 numaId
= std::strtoul(&line
.c_str()[data_start
], &c
, 10);
186 if (threadId
!= uint32_t(-1))
189 if (out_nodes
.size() <= numaId
) out_nodes
.resize(numaId
+ 1);
190 auto& numaNode
= out_nodes
[numaId
];
191 if (numaNode
.cores
.size() <= coreId
) numaNode
.cores
.resize(coreId
+ 1);
192 auto& core
= numaNode
.cores
[coreId
];
194 core
.procGroup
= coreId
;
195 core
.threadIds
.push_back(threadId
);
196 out_numThreadsPerProcGroup
++;
199 for (uint32_t node
= 0; node
< out_nodes
.size(); node
++) {
200 auto& numaNode
= out_nodes
[node
];
201 auto it
= numaNode
.cores
.begin();
202 for ( ; it
!= numaNode
.cores
.end(); ) {
203 if (it
->threadIds
.size() == 0)
204 numaNode
.cores
.erase(it
);
212 #error Unsupported platform
218 void bindThread(uint32_t threadId
, uint32_t procGroupId
= 0, bool bindProcGroup
=false)
220 // Only bind threads when MAX_WORKER_THREADS isn't set.
221 if (KNOB_MAX_WORKER_THREADS
&& bindProcGroup
== false)
228 GROUP_AFFINITY affinity
= {};
229 affinity
.Group
= procGroupId
;
234 // In a 32-bit process on Windows it is impossible to bind
235 // to logical processors 32-63 within a processor group.
236 // In this case set the mask to 0 and let the system assign
237 // the processor. Hopefully it will make smart choices.
243 // If KNOB_MAX_WORKER_THREADS is set, only bind to the proc group,
244 // Not the individual HW thread.
245 if (!KNOB_MAX_WORKER_THREADS
)
247 affinity
.Mask
= KAFFINITY(1) << threadId
;
251 SetThreadGroupAffinity(GetCurrentThread(), &affinity
, nullptr);
255 pthread_t thread
= pthread_self();
257 CPU_SET(threadId
, &cpuset
);
259 pthread_setaffinity_np(thread
, sizeof(cpu_set_t
), &cpuset
);
264 uint64_t GetEnqueuedDraw(SWR_CONTEXT
*pContext
)
266 return pContext
->dcRing
.GetHead();
270 DRAW_CONTEXT
*GetDC(SWR_CONTEXT
*pContext
, uint64_t drawId
)
272 return &pContext
->dcRing
[(drawId
-1) % KNOB_MAX_DRAWS_IN_FLIGHT
];
275 // returns true if dependency not met
277 bool CheckDependency(SWR_CONTEXT
*pContext
, DRAW_CONTEXT
*pDC
, uint64_t lastRetiredDraw
)
279 return (pDC
->dependency
> lastRetiredDraw
);
282 INLINE
int64_t CompleteDrawContext(SWR_CONTEXT
* pContext
, DRAW_CONTEXT
* pDC
)
284 int64_t result
= InterlockedDecrement64(&pDC
->threadsDone
);
285 SWR_ASSERT(result
>= 0);
289 // Cleanup memory allocations
290 pDC
->pArena
->Reset(true);
291 pDC
->pTileMgr
->initialize();
292 if (pDC
->cleanupState
)
294 pDC
->pState
->pArena
->Reset(true);
299 pContext
->dcRing
.Dequeue(); // Remove from tail
305 INLINE
bool FindFirstIncompleteDraw(SWR_CONTEXT
* pContext
, uint64_t& curDrawBE
)
307 // increment our current draw id to the first incomplete draw
308 uint64_t drawEnqueued
= GetEnqueuedDraw(pContext
);
309 while (curDrawBE
< drawEnqueued
)
311 DRAW_CONTEXT
*pDC
= &pContext
->dcRing
[curDrawBE
% KNOB_MAX_DRAWS_IN_FLIGHT
];
313 // If its not compute and FE is not done then break out of loop.
314 if (!pDC
->doneFE
&& !pDC
->isCompute
) break;
316 bool isWorkComplete
= (pDC
->isCompute
) ?
317 pDC
->pDispatch
->isWorkComplete() : pDC
->pTileMgr
->isWorkComplete();
322 CompleteDrawContext(pContext
, pDC
);
330 // If there are no more incomplete draws then return false.
331 return (curDrawBE
>= drawEnqueued
) ? false : true;
334 //////////////////////////////////////////////////////////////////////////
335 /// @brief If there is any BE work then go work on it.
336 /// @param pContext - pointer to SWR context.
337 /// @param workerId - The unique worker ID that is assigned to this thread.
338 /// @param curDrawBE - This tracks the draw contexts that this thread has processed. Each worker thread
339 /// has its own curDrawBE counter and this ensures that each worker processes all the
341 /// @param lockedTiles - This is the set of tiles locked by other threads. Each thread maintains its
342 /// own set and each time it fails to lock a macrotile, because its already locked,
343 /// then it will add that tile to the lockedTiles set. As a worker begins to work
344 /// on future draws the lockedTiles ensure that it doesn't work on tiles that may
345 /// still have work pending in a previous draw. Additionally, the lockedTiles is
346 /// hueristic that can steer a worker back to the same macrotile that it had been
347 /// working on in a previous draw.
349 SWR_CONTEXT
*pContext
,
352 TileSet
& lockedTiles
,
356 // Find the first incomplete draw that has pending work. If no such draw is found then
357 // return. FindFirstIncompleteDraw is responsible for incrementing the curDrawBE.
358 if (FindFirstIncompleteDraw(pContext
, curDrawBE
) == false)
363 uint64_t lastRetiredDraw
= pContext
->dcRing
[curDrawBE
% KNOB_MAX_DRAWS_IN_FLIGHT
].drawId
- 1;
365 // Reset our history for locked tiles. We'll have to re-learn which tiles are locked.
368 // Try to work on each draw in order of the available draws in flight.
369 // 1. If we're on curDrawBE, we can work on any macrotile that is available.
370 // 2. If we're trying to work on draws after curDrawBE, we are restricted to
371 // working on those macrotiles that are known to be complete in the prior draw to
372 // maintain order. The locked tiles provides the history to ensures this.
373 for (uint64_t i
= curDrawBE
; i
< GetEnqueuedDraw(pContext
); ++i
)
375 DRAW_CONTEXT
*pDC
= &pContext
->dcRing
[i
% KNOB_MAX_DRAWS_IN_FLIGHT
];
377 if (pDC
->isCompute
) return; // We don't look at compute work.
379 // First wait for FE to be finished with this draw. This keeps threading model simple
380 // but if there are lots of bubbles between draws then serializing FE and BE may
381 // need to be revisited.
382 if (!pDC
->doneFE
) return;
384 // If this draw is dependent on a previous draw then we need to bail.
385 if (CheckDependency(pContext
, pDC
, lastRetiredDraw
))
390 // Grab the list of all dirty macrotiles. A tile is dirty if it has work queued to it.
391 std::vector
<uint32_t> ¯oTiles
= pDC
->pTileMgr
->getDirtyTiles();
393 for (uint32_t tileID
: macroTiles
)
395 // Only work on tiles for for this numa node
397 pDC
->pTileMgr
->getTileIndices(tileID
, x
, y
);
398 if (((x
^ y
) & numaMask
) != numaNode
)
403 MacroTileQueue
&tile
= pDC
->pTileMgr
->getMacroTileQueue(tileID
);
405 if (!tile
.getNumQueued())
410 // can only work on this draw if it's not in use by other threads
411 if (lockedTiles
.find(tileID
) != lockedTiles
.end())
420 RDTSC_START(WorkerFoundWork
);
422 uint32_t numWorkItems
= tile
.getNumQueued();
423 SWR_ASSERT(numWorkItems
);
427 if (pWork
->type
== DRAW
)
429 pContext
->pHotTileMgr
->InitializeHotTiles(pContext
, pDC
, tileID
);
432 while ((pWork
= tile
.peek()) != nullptr)
434 pWork
->pfnWork(pDC
, workerId
, tileID
, &pWork
->desc
);
437 RDTSC_STOP(WorkerFoundWork
, numWorkItems
, pDC
->drawId
);
441 pDC
->pTileMgr
->markTileComplete(tileID
);
443 // Optimization: If the draw is complete and we're the last one to have worked on it then
444 // we can reset the locked list as we know that all previous draws before the next are guaranteed to be complete.
445 if ((curDrawBE
== i
) && pDC
->pTileMgr
->isWorkComplete())
447 // We can increment the current BE and safely move to next draw since we know this draw is complete.
449 CompleteDrawContext(pContext
, pDC
);
459 // This tile is already locked. So let's add it to our locked tiles set. This way we don't try locking this one again.
460 lockedTiles
.insert(tileID
);
466 void WorkOnFifoFE(SWR_CONTEXT
*pContext
, uint32_t workerId
, uint64_t &curDrawFE
, uint32_t numaNode
)
468 // Try to grab the next DC from the ring
469 uint64_t drawEnqueued
= GetEnqueuedDraw(pContext
);
470 while (curDrawFE
< drawEnqueued
)
472 uint32_t dcSlot
= curDrawFE
% KNOB_MAX_DRAWS_IN_FLIGHT
;
473 DRAW_CONTEXT
*pDC
= &pContext
->dcRing
[dcSlot
];
474 if (pDC
->isCompute
|| pDC
->doneFE
|| pDC
->FeLock
)
476 CompleteDrawContext(pContext
, pDC
);
485 uint64_t curDraw
= curDrawFE
;
486 while (curDraw
< drawEnqueued
)
488 uint32_t dcSlot
= curDraw
% KNOB_MAX_DRAWS_IN_FLIGHT
;
489 DRAW_CONTEXT
*pDC
= &pContext
->dcRing
[dcSlot
];
491 if (!pDC
->isCompute
&& !pDC
->FeLock
)
493 uint32_t initial
= InterlockedCompareExchange((volatile uint32_t*)&pDC
->FeLock
, 1, 0);
496 // successfully grabbed the DC, now run the FE
497 pDC
->FeWork
.pfnWork(pContext
, pDC
, workerId
, &pDC
->FeWork
.desc
);
507 //////////////////////////////////////////////////////////////////////////
508 /// @brief If there is any compute work then go work on it.
509 /// @param pContext - pointer to SWR context.
510 /// @param workerId - The unique worker ID that is assigned to this thread.
511 /// @param curDrawBE - This tracks the draw contexts that this thread has processed. Each worker thread
512 /// has its own curDrawBE counter and this ensures that each worker processes all the
515 SWR_CONTEXT
*pContext
,
519 if (FindFirstIncompleteDraw(pContext
, curDrawBE
) == false)
524 uint64_t lastRetiredDraw
= pContext
->dcRing
[curDrawBE
% KNOB_MAX_DRAWS_IN_FLIGHT
].drawId
- 1;
526 DRAW_CONTEXT
*pDC
= &pContext
->dcRing
[curDrawBE
% KNOB_MAX_DRAWS_IN_FLIGHT
];
527 if (pDC
->isCompute
== false) return;
529 // check dependencies
530 if (CheckDependency(pContext
, pDC
, lastRetiredDraw
))
535 SWR_ASSERT(pDC
->pDispatch
!= nullptr);
536 DispatchQueue
& queue
= *pDC
->pDispatch
;
538 // Is there any work remaining?
539 if (queue
.getNumQueued() > 0)
541 uint32_t threadGroupId
= 0;
542 while (queue
.getWork(threadGroupId
))
544 ProcessComputeBE(pDC
, workerId
, threadGroupId
);
546 queue
.finishedWork();
551 DWORD
workerThreadMain(LPVOID pData
)
553 THREAD_DATA
*pThreadData
= (THREAD_DATA
*)pData
;
554 SWR_CONTEXT
*pContext
= pThreadData
->pContext
;
555 uint32_t threadId
= pThreadData
->threadId
;
556 uint32_t workerId
= pThreadData
->workerId
;
558 bindThread(threadId
, pThreadData
->procGroupId
, pThreadData
->forceBindProcGroup
);
560 RDTSC_INIT(threadId
);
562 uint32_t numaNode
= pThreadData
->numaId
;
563 uint32_t numaMask
= pContext
->threadPool
.numaMask
;
565 // flush denormals to 0
566 _mm_setcsr(_mm_getcsr() | _MM_FLUSH_ZERO_ON
| _MM_DENORMALS_ZERO_ON
);
568 // Track tiles locked by other threads. If we try to lock a macrotile and find its already
569 // locked then we'll add it to this list so that we don't try and lock it again.
572 // each worker has the ability to work on any of the queued draws as long as certain
573 // conditions are met. the data associated
574 // with a draw is guaranteed to be active as long as a worker hasn't signaled that he
575 // has moved on to the next draw when he determines there is no more work to do. The api
576 // thread will not increment the head of the dc ring until all workers have moved past the
578 // the logic to determine what to work on is:
579 // 1- try to work on the FE any draw that is queued. For now there are no dependencies
580 // on the FE work, so any worker can grab any FE and process in parallel. Eventually
581 // we'll need dependency tracking to force serialization on FEs. The worker will try
582 // to pick an FE by atomically incrementing a counter in the swr context. he'll keep
583 // trying until he reaches the tail.
584 // 2- BE work must be done in strict order. we accomplish this today by pulling work off
585 // the oldest draw (ie the head) of the dcRing. the worker can determine if there is
586 // any work left by comparing the total # of binned work items and the total # of completed
587 // work items. If they are equal, then there is no more work to do for this draw, and
588 // the worker can safely increment its oldestDraw counter and move on to the next draw.
589 std::unique_lock
<std::mutex
> lock(pContext
->WaitLock
, std::defer_lock
);
591 auto threadHasWork
= [&](uint64_t curDraw
) { return curDraw
!= pContext
->dcRing
.GetHead(); };
593 uint64_t curDrawBE
= 0;
594 uint64_t curDrawFE
= 0;
596 while (pContext
->threadPool
.inThreadShutdown
== false)
599 while (loop
++ < KNOB_WORKER_SPIN_LOOP_COUNT
&& !threadHasWork(curDrawBE
))
604 if (!threadHasWork(curDrawBE
))
608 // check for thread idle condition again under lock
609 if (threadHasWork(curDrawBE
))
615 if (pContext
->threadPool
.inThreadShutdown
)
621 RDTSC_START(WorkerWaitForThreadEvent
);
623 pContext
->FifosNotEmpty
.wait(lock
);
626 RDTSC_STOP(WorkerWaitForThreadEvent
, 0, 0);
628 if (pContext
->threadPool
.inThreadShutdown
)
634 RDTSC_START(WorkerWorkOnFifoBE
);
635 WorkOnFifoBE(pContext
, workerId
, curDrawBE
, lockedTiles
, numaNode
, numaMask
);
636 RDTSC_STOP(WorkerWorkOnFifoBE
, 0, 0);
638 WorkOnCompute(pContext
, workerId
, curDrawBE
);
640 WorkOnFifoFE(pContext
, workerId
, curDrawFE
, numaNode
);
646 DWORD
workerThreadInit(LPVOID pData
)
652 return workerThreadMain(pData
);
656 __except(EXCEPTION_CONTINUE_SEARCH
)
665 void CreateThreadPool(SWR_CONTEXT
*pContext
, THREAD_POOL
*pPool
)
670 uint32_t numThreadsPerProcGroup
= 0;
671 CalculateProcessorTopology(nodes
, numThreadsPerProcGroup
);
673 uint32_t numHWNodes
= (uint32_t)nodes
.size();
674 uint32_t numHWCoresPerNode
= (uint32_t)nodes
[0].cores
.size();
675 uint32_t numHWHyperThreads
= (uint32_t)nodes
[0].cores
[0].threadIds
.size();
677 uint32_t numNodes
= numHWNodes
;
678 uint32_t numCoresPerNode
= numHWCoresPerNode
;
679 uint32_t numHyperThreads
= numHWHyperThreads
;
681 if (KNOB_MAX_NUMA_NODES
)
683 numNodes
= std::min(numNodes
, KNOB_MAX_NUMA_NODES
);
686 if (KNOB_MAX_CORES_PER_NUMA_NODE
)
688 numCoresPerNode
= std::min(numCoresPerNode
, KNOB_MAX_CORES_PER_NUMA_NODE
);
691 if (KNOB_MAX_THREADS_PER_CORE
)
693 numHyperThreads
= std::min(numHyperThreads
, KNOB_MAX_THREADS_PER_CORE
);
696 // Calculate numThreads
697 uint32_t numThreads
= numNodes
* numCoresPerNode
* numHyperThreads
;
699 if (KNOB_MAX_WORKER_THREADS
)
701 uint32_t maxHWThreads
= numHWNodes
* numHWCoresPerNode
* numHWHyperThreads
;
702 numThreads
= std::min(KNOB_MAX_WORKER_THREADS
, maxHWThreads
);
705 if (numThreads
> KNOB_MAX_NUM_THREADS
)
707 printf("WARNING: system thread count %u exceeds max %u, "
708 "performance will be degraded\n",
709 numThreads
, KNOB_MAX_NUM_THREADS
);
712 uint32_t numAPIReservedThreads
= 1;
717 // If only 1 worker threads, try to move it to an available
718 // HW thread. If that fails, use the API thread.
719 if (numCoresPerNode
< numHWCoresPerNode
)
723 else if (numHyperThreads
< numHWHyperThreads
)
727 else if (numNodes
< numHWNodes
)
733 pPool
->numThreads
= 0;
734 SET_KNOB(SINGLE_THREADED
, true);
740 // Save HW threads for the API if we can
741 if (numThreads
> numAPIReservedThreads
)
743 numThreads
-= numAPIReservedThreads
;
747 numAPIReservedThreads
= 0;
751 pPool
->numThreads
= numThreads
;
752 pContext
->NumWorkerThreads
= pPool
->numThreads
;
754 pPool
->inThreadShutdown
= false;
755 pPool
->pThreadData
= (THREAD_DATA
*)malloc(pPool
->numThreads
* sizeof(THREAD_DATA
));
758 if (KNOB_MAX_WORKER_THREADS
)
760 bool bForceBindProcGroup
= (numThreads
> numThreadsPerProcGroup
);
761 uint32_t numProcGroups
= (numThreads
+ numThreadsPerProcGroup
- 1) / numThreadsPerProcGroup
;
762 // When MAX_WORKER_THREADS is set we don't bother to bind to specific HW threads
763 // But Windows will still require binding to specific process groups
764 for (uint32_t workerId
= 0; workerId
< numThreads
; ++workerId
)
766 pPool
->pThreadData
[workerId
].workerId
= workerId
;
767 pPool
->pThreadData
[workerId
].procGroupId
= workerId
% numProcGroups
;
768 pPool
->pThreadData
[workerId
].threadId
= 0;
769 pPool
->pThreadData
[workerId
].numaId
= 0;
770 pPool
->pThreadData
[workerId
].pContext
= pContext
;
771 pPool
->pThreadData
[workerId
].forceBindProcGroup
= bForceBindProcGroup
;
772 pPool
->threads
[workerId
] = new std::thread(workerThreadInit
, &pPool
->pThreadData
[workerId
]);
777 pPool
->numaMask
= numNodes
- 1; // Only works for 2**n numa nodes (1, 2, 4, etc.)
779 uint32_t workerId
= 0;
780 for (uint32_t n
= 0; n
< numNodes
; ++n
)
782 auto& node
= nodes
[n
];
784 uint32_t numCores
= numCoresPerNode
;
785 for (uint32_t c
= 0; c
< numCores
; ++c
)
787 auto& core
= node
.cores
[c
];
788 for (uint32_t t
= 0; t
< numHyperThreads
; ++t
)
790 if (numAPIReservedThreads
)
792 --numAPIReservedThreads
;
796 pPool
->pThreadData
[workerId
].workerId
= workerId
;
797 pPool
->pThreadData
[workerId
].procGroupId
= core
.procGroup
;
798 pPool
->pThreadData
[workerId
].threadId
= core
.threadIds
[t
];
799 pPool
->pThreadData
[workerId
].numaId
= n
;
800 pPool
->pThreadData
[workerId
].pContext
= pContext
;
801 pPool
->threads
[workerId
] = new std::thread(workerThreadInit
, &pPool
->pThreadData
[workerId
]);
810 void DestroyThreadPool(SWR_CONTEXT
*pContext
, THREAD_POOL
*pPool
)
812 if (!KNOB_SINGLE_THREADED
)
814 // Inform threads to finish up
815 std::unique_lock
<std::mutex
> lock(pContext
->WaitLock
);
816 pPool
->inThreadShutdown
= true;
818 pContext
->FifosNotEmpty
.notify_all();
821 // Wait for threads to finish and destroy them
822 for (uint32_t t
= 0; t
< pPool
->numThreads
; ++t
)
824 pPool
->threads
[t
]->join();
825 delete(pPool
->threads
[t
]);
828 // Clean up data used by threads
829 free(pPool
->pThreadData
);