1 /****************************************************************************
2 * Copyright (C) 2014-2016 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__) || defined(__APPLE__)
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
;
60 std::vector
<Core
> cores
;
63 typedef std::vector
<NumaNode
> CPUNumaNodes
;
65 void CalculateProcessorTopology(CPUNumaNodes
& out_nodes
, uint32_t& out_numThreadsPerProcGroup
)
68 out_numThreadsPerProcGroup
= 0;
72 std::vector
<KAFFINITY
> threadMaskPerProcGroup
;
75 std::lock_guard
<std::mutex
> l(m
);
79 BOOL ret
= GetLogicalProcessorInformationEx(RelationProcessorCore
, nullptr, &bufSize
);
80 SWR_ASSERT(ret
== FALSE
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
);
82 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX pBufferMem
= (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX
)malloc(bufSize
);
83 SWR_ASSERT(pBufferMem
);
85 ret
= GetLogicalProcessorInformationEx(RelationProcessorCore
, pBufferMem
, &bufSize
);
86 SWR_ASSERT(ret
!= FALSE
, "Failed to get Processor Topology Information");
88 uint32_t count
= bufSize
/ pBufferMem
->Size
;
89 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX pBuffer
= pBufferMem
;
91 for (uint32_t i
= 0; i
< count
; ++i
)
93 SWR_ASSERT(pBuffer
->Relationship
== RelationProcessorCore
);
94 for (uint32_t g
= 0; g
< pBuffer
->Processor
.GroupCount
; ++g
)
96 auto& gmask
= pBuffer
->Processor
.GroupMask
[g
];
97 uint32_t threadId
= 0;
98 uint32_t procGroup
= gmask
.Group
;
100 Core
* pCore
= nullptr;
102 uint32_t numThreads
= (uint32_t)_mm_popcount_sizeT(gmask
.Mask
);
104 while (BitScanForwardSizeT((unsigned long*)&threadId
, gmask
.Mask
))
107 KAFFINITY threadMask
= KAFFINITY(1) << threadId
;
108 gmask
.Mask
&= ~threadMask
;
110 if (procGroup
>= threadMaskPerProcGroup
.size())
112 threadMaskPerProcGroup
.resize(procGroup
+ 1);
115 if (threadMaskPerProcGroup
[procGroup
] & threadMask
)
117 // Already seen this mask. This means that we are in 32-bit mode and
118 // have seen more than 32 HW threads for this procGroup
121 SWR_INVALID("Shouldn't get here in 64-bit mode");
126 threadMaskPerProcGroup
[procGroup
] |= (KAFFINITY(1) << threadId
);
130 PROCESSOR_NUMBER procNum
= {};
131 procNum
.Group
= WORD(procGroup
);
132 procNum
.Number
= UCHAR(threadId
);
134 ret
= GetNumaProcessorNodeEx(&procNum
, (PUSHORT
)&numaId
);
138 if (out_nodes
.size() <= numaId
)
140 out_nodes
.resize(numaId
+ 1);
142 auto& numaNode
= out_nodes
[numaId
];
143 numaNode
.numaId
= numaId
;
147 if (nullptr == pCore
)
149 numaNode
.cores
.push_back(Core());
150 pCore
= &numaNode
.cores
.back();
151 pCore
->procGroup
= procGroup
;
153 pCore
->threadIds
.push_back(threadId
);
156 out_numThreadsPerProcGroup
++;
160 pBuffer
= PtrAdd(pBuffer
, pBuffer
->Size
);
166 #elif defined(__linux__) || defined (__gnu_linux__)
168 // Parse /proc/cpuinfo to get full topology
169 std::ifstream
input("/proc/cpuinfo");
172 uint32_t procId
= uint32_t(-1);
173 uint32_t coreId
= uint32_t(-1);
174 uint32_t physId
= uint32_t(-1);
176 while (std::getline(input
, line
))
178 if (line
.find("processor") != std::string::npos
)
180 auto data_start
= line
.find(": ") + 2;
181 procId
= std::strtoul(&line
.c_str()[data_start
], &c
, 10);
184 if (line
.find("core id") != std::string::npos
)
186 auto data_start
= line
.find(": ") + 2;
187 coreId
= std::strtoul(&line
.c_str()[data_start
], &c
, 10);
190 if (line
.find("physical id") != std::string::npos
)
192 auto data_start
= line
.find(": ") + 2;
193 physId
= std::strtoul(&line
.c_str()[data_start
], &c
, 10);
196 if (line
.length() == 0)
198 if (physId
+ 1 > out_nodes
.size())
199 out_nodes
.resize(physId
+ 1);
200 auto& numaNode
= out_nodes
[physId
];
201 numaNode
.numaId
= physId
;
203 if (coreId
+ 1 > numaNode
.cores
.size())
204 numaNode
.cores
.resize(coreId
+ 1);
205 auto& core
= numaNode
.cores
[coreId
];
206 core
.procGroup
= coreId
;
207 core
.threadIds
.push_back(procId
);
211 out_numThreadsPerProcGroup
= 0;
212 for (auto &node
: out_nodes
)
214 for (auto &core
: node
.cores
)
216 out_numThreadsPerProcGroup
+= core
.threadIds
.size();
220 #elif defined(__APPLE__)
224 #error Unsupported platform
228 // Prune empty cores and numa nodes
229 for (auto node_it
= out_nodes
.begin(); node_it
!= out_nodes
.end(); )
231 // Erase empty cores (first)
232 for (auto core_it
= node_it
->cores
.begin(); core_it
!= node_it
->cores
.end(); )
234 if (core_it
->threadIds
.size() == 0)
236 core_it
= node_it
->cores
.erase(core_it
);
244 // Erase empty numa nodes (second)
245 if (node_it
->cores
.size() == 0)
247 node_it
= out_nodes
.erase(node_it
);
257 void bindThread(SWR_CONTEXT
* pContext
, uint32_t threadId
, uint32_t procGroupId
= 0, bool bindProcGroup
=false)
259 // Only bind threads when MAX_WORKER_THREADS isn't set.
260 if (pContext
->threadInfo
.SINGLE_THREADED
|| (pContext
->threadInfo
.MAX_WORKER_THREADS
&& bindProcGroup
== false))
267 GROUP_AFFINITY affinity
= {};
268 affinity
.Group
= procGroupId
;
273 // Hopefully we don't get here. Logic in CreateThreadPool should prevent this.
274 SWR_INVALID("Shouldn't get here");
276 // In a 32-bit process on Windows it is impossible to bind
277 // to logical processors 32-63 within a processor group.
278 // In this case set the mask to 0 and let the system assign
279 // the processor. Hopefully it will make smart choices.
285 // If MAX_WORKER_THREADS is set, only bind to the proc group,
286 // Not the individual HW thread.
287 if (!pContext
->threadInfo
.MAX_WORKER_THREADS
)
289 affinity
.Mask
= KAFFINITY(1) << threadId
;
293 SetThreadGroupAffinity(GetCurrentThread(), &affinity
, nullptr);
295 #elif defined(__linux__) || defined(__gnu_linux__)
298 pthread_t thread
= pthread_self();
300 CPU_SET(threadId
, &cpuset
);
302 int err
= pthread_setaffinity_np(thread
, sizeof(cpu_set_t
), &cpuset
);
305 fprintf(stderr
, "pthread_setaffinity_np failure for tid %u: %s\n", threadId
, strerror(err
));
312 uint32_t GetEnqueuedDraw(SWR_CONTEXT
*pContext
)
314 return pContext
->dcRing
.GetHead();
318 DRAW_CONTEXT
*GetDC(SWR_CONTEXT
*pContext
, uint32_t drawId
)
320 return &pContext
->dcRing
[(drawId
-1) % pContext
->MAX_DRAWS_IN_FLIGHT
];
324 bool IDComparesLess(uint32_t a
, uint32_t b
)
326 // Use signed delta to ensure that wrap-around to 0 is correctly handled.
327 int32_t delta
= int32_t(a
- b
);
331 // returns true if dependency not met
333 bool CheckDependency(SWR_CONTEXT
*pContext
, DRAW_CONTEXT
*pDC
, uint32_t lastRetiredDraw
)
335 return pDC
->dependent
&& IDComparesLess(lastRetiredDraw
, pDC
->drawId
- 1);
338 bool CheckDependencyFE(SWR_CONTEXT
*pContext
, DRAW_CONTEXT
*pDC
, uint32_t lastRetiredDraw
)
340 return pDC
->dependentFE
&& IDComparesLess(lastRetiredDraw
, pDC
->drawId
- 1);
343 //////////////////////////////////////////////////////////////////////////
344 /// @brief Update client stats.
345 INLINE
void UpdateClientStats(SWR_CONTEXT
* pContext
, uint32_t workerId
, DRAW_CONTEXT
* pDC
)
347 if ((pContext
->pfnUpdateStats
== nullptr) || (GetApiState(pDC
).enableStatsBE
== false))
352 DRAW_DYNAMIC_STATE
& dynState
= pDC
->dynState
;
353 OSALIGNLINE(SWR_STATS
) stats
{ 0 };
355 // Sum up stats across all workers before sending to client.
356 for (uint32_t i
= 0; i
< pContext
->NumWorkerThreads
; ++i
)
358 stats
.DepthPassCount
+= dynState
.pStats
[i
].DepthPassCount
;
360 stats
.PsInvocations
+= dynState
.pStats
[i
].PsInvocations
;
361 stats
.CsInvocations
+= dynState
.pStats
[i
].CsInvocations
;
365 pContext
->pfnUpdateStats(GetPrivateState(pDC
), &stats
);
368 INLINE
void ExecuteCallbacks(SWR_CONTEXT
* pContext
, uint32_t workerId
, DRAW_CONTEXT
* pDC
)
370 UpdateClientStats(pContext
, workerId
, pDC
);
372 if (pDC
->retireCallback
.pfnCallbackFunc
)
374 pDC
->retireCallback
.pfnCallbackFunc(pDC
->retireCallback
.userData
,
375 pDC
->retireCallback
.userData2
,
376 pDC
->retireCallback
.userData3
);
380 // inlined-only version
381 INLINE
int32_t CompleteDrawContextInl(SWR_CONTEXT
* pContext
, uint32_t workerId
, DRAW_CONTEXT
* pDC
)
383 int32_t result
= static_cast<int32_t>(InterlockedDecrement(&pDC
->threadsDone
));
384 SWR_ASSERT(result
>= 0);
386 AR_FLUSH(pDC
->drawId
);
390 ExecuteCallbacks(pContext
, workerId
, pDC
);
392 // Cleanup memory allocations
393 pDC
->pArena
->Reset(true);
396 pDC
->pTileMgr
->initialize();
398 if (pDC
->cleanupState
)
400 pDC
->pState
->pArena
->Reset(true);
405 pContext
->dcRing
.Dequeue(); // Remove from tail
411 // available to other translation modules
412 int32_t CompleteDrawContext(SWR_CONTEXT
* pContext
, DRAW_CONTEXT
* pDC
)
414 return CompleteDrawContextInl(pContext
, 0, pDC
);
417 INLINE
bool FindFirstIncompleteDraw(SWR_CONTEXT
* pContext
, uint32_t workerId
, uint32_t& curDrawBE
, uint32_t& drawEnqueued
)
419 // increment our current draw id to the first incomplete draw
420 drawEnqueued
= GetEnqueuedDraw(pContext
);
421 while (IDComparesLess(curDrawBE
, drawEnqueued
))
423 DRAW_CONTEXT
*pDC
= &pContext
->dcRing
[curDrawBE
% pContext
->MAX_DRAWS_IN_FLIGHT
];
425 // If its not compute and FE is not done then break out of loop.
426 if (!pDC
->doneFE
&& !pDC
->isCompute
) break;
428 bool isWorkComplete
= pDC
->isCompute
?
429 pDC
->pDispatch
->isWorkComplete() :
430 pDC
->pTileMgr
->isWorkComplete();
435 CompleteDrawContextInl(pContext
, workerId
, pDC
);
443 // If there are no more incomplete draws then return false.
444 return IDComparesLess(curDrawBE
, drawEnqueued
);
447 //////////////////////////////////////////////////////////////////////////
448 /// @brief If there is any BE work then go work on it.
449 /// @param pContext - pointer to SWR context.
450 /// @param workerId - The unique worker ID that is assigned to this thread.
451 /// @param curDrawBE - This tracks the draw contexts that this thread has processed. Each worker thread
452 /// has its own curDrawBE counter and this ensures that each worker processes all the
454 /// @param lockedTiles - This is the set of tiles locked by other threads. Each thread maintains its
455 /// own set and each time it fails to lock a macrotile, because its already locked,
456 /// then it will add that tile to the lockedTiles set. As a worker begins to work
457 /// on future draws the lockedTiles ensure that it doesn't work on tiles that may
458 /// still have work pending in a previous draw. Additionally, the lockedTiles is
459 /// hueristic that can steer a worker back to the same macrotile that it had been
460 /// working on in a previous draw.
461 /// @returns true if worker thread should shutdown
463 SWR_CONTEXT
*pContext
,
466 TileSet
& lockedTiles
,
470 bool bShutdown
= false;
472 // Find the first incomplete draw that has pending work. If no such draw is found then
473 // return. FindFirstIncompleteDraw is responsible for incrementing the curDrawBE.
474 uint32_t drawEnqueued
= 0;
475 if (FindFirstIncompleteDraw(pContext
, workerId
, curDrawBE
, drawEnqueued
) == false)
480 uint32_t lastRetiredDraw
= pContext
->dcRing
[curDrawBE
% pContext
->MAX_DRAWS_IN_FLIGHT
].drawId
- 1;
482 // Reset our history for locked tiles. We'll have to re-learn which tiles are locked.
485 // Try to work on each draw in order of the available draws in flight.
486 // 1. If we're on curDrawBE, we can work on any macrotile that is available.
487 // 2. If we're trying to work on draws after curDrawBE, we are restricted to
488 // working on those macrotiles that are known to be complete in the prior draw to
489 // maintain order. The locked tiles provides the history to ensures this.
490 for (uint32_t i
= curDrawBE
; IDComparesLess(i
, drawEnqueued
); ++i
)
492 DRAW_CONTEXT
*pDC
= &pContext
->dcRing
[i
% pContext
->MAX_DRAWS_IN_FLIGHT
];
494 if (pDC
->isCompute
) return false; // We don't look at compute work.
496 // First wait for FE to be finished with this draw. This keeps threading model simple
497 // but if there are lots of bubbles between draws then serializing FE and BE may
498 // need to be revisited.
499 if (!pDC
->doneFE
) return false;
501 // If this draw is dependent on a previous draw then we need to bail.
502 if (CheckDependency(pContext
, pDC
, lastRetiredDraw
))
507 // Grab the list of all dirty macrotiles. A tile is dirty if it has work queued to it.
508 auto ¯oTiles
= pDC
->pTileMgr
->getDirtyTiles();
510 for (auto tile
: macroTiles
)
512 uint32_t tileID
= tile
->mId
;
514 // Only work on tiles for this numa node
516 pDC
->pTileMgr
->getTileIndices(tileID
, x
, y
);
517 if (((x
^ y
) & numaMask
) != numaNode
)
522 if (!tile
->getNumQueued())
527 // can only work on this draw if it's not in use by other threads
528 if (lockedTiles
.find(tileID
) != lockedTiles
.end())
537 AR_BEGIN(WorkerFoundWork
, pDC
->drawId
);
539 uint32_t numWorkItems
= tile
->getNumQueued();
540 SWR_ASSERT(numWorkItems
);
542 pWork
= tile
->peek();
544 if (pWork
->type
== DRAW
)
546 pContext
->pHotTileMgr
->InitializeHotTiles(pContext
, pDC
, workerId
, tileID
);
548 else if (pWork
->type
== SHUTDOWN
)
553 while ((pWork
= tile
->peek()) != nullptr)
555 pWork
->pfnWork(pDC
, workerId
, tileID
, &pWork
->desc
);
558 AR_END(WorkerFoundWork
, numWorkItems
);
562 pDC
->pTileMgr
->markTileComplete(tileID
);
564 // Optimization: If the draw is complete and we're the last one to have worked on it then
565 // we can reset the locked list as we know that all previous draws before the next are guaranteed to be complete.
566 if ((curDrawBE
== i
) && (bShutdown
|| pDC
->pTileMgr
->isWorkComplete()))
568 // We can increment the current BE and safely move to next draw since we know this draw is complete.
570 CompleteDrawContextInl(pContext
, workerId
, pDC
);
585 // 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.
586 lockedTiles
.insert(tileID
);
594 //////////////////////////////////////////////////////////////////////////
595 /// @brief Called when FE work is complete for this DC.
596 INLINE
void CompleteDrawFE(SWR_CONTEXT
* pContext
, uint32_t workerId
, DRAW_CONTEXT
* pDC
)
598 if (pContext
->pfnUpdateStatsFE
&& GetApiState(pDC
).enableStatsFE
)
600 SWR_STATS_FE
& stats
= pDC
->dynState
.statsFE
;
602 AR_EVENT(FrontendStatsEvent(pDC
->drawId
,
603 stats
.IaVertices
, stats
.IaPrimitives
, stats
.VsInvocations
, stats
.HsInvocations
,
604 stats
.DsInvocations
, stats
.GsInvocations
, stats
.GsPrimitives
, stats
.CInvocations
, stats
.CPrimitives
,
605 stats
.SoPrimStorageNeeded
[0], stats
.SoPrimStorageNeeded
[1], stats
.SoPrimStorageNeeded
[2], stats
.SoPrimStorageNeeded
[3],
606 stats
.SoNumPrimsWritten
[0], stats
.SoNumPrimsWritten
[1], stats
.SoNumPrimsWritten
[2], stats
.SoNumPrimsWritten
[3]
608 AR_EVENT(FrontendDrawEndEvent(pDC
->drawId
));
610 pContext
->pfnUpdateStatsFE(GetPrivateState(pDC
), &stats
);
613 if (pContext
->pfnUpdateSoWriteOffset
)
615 for (uint32_t i
= 0; i
< MAX_SO_BUFFERS
; ++i
)
617 if ((pDC
->dynState
.SoWriteOffsetDirty
[i
]) &&
618 (pDC
->pState
->state
.soBuffer
[i
].soWriteEnable
))
620 pContext
->pfnUpdateSoWriteOffset(GetPrivateState(pDC
), i
, pDC
->dynState
.SoWriteOffset
[i
]);
625 // Ensure all streaming writes are globally visible before marking this FE done
629 InterlockedDecrement(&pContext
->drawsOutstandingFE
);
632 void WorkOnFifoFE(SWR_CONTEXT
*pContext
, uint32_t workerId
, uint32_t &curDrawFE
)
634 // Try to grab the next DC from the ring
635 uint32_t drawEnqueued
= GetEnqueuedDraw(pContext
);
636 while (IDComparesLess(curDrawFE
, drawEnqueued
))
638 uint32_t dcSlot
= curDrawFE
% pContext
->MAX_DRAWS_IN_FLIGHT
;
639 DRAW_CONTEXT
*pDC
= &pContext
->dcRing
[dcSlot
];
640 if (pDC
->isCompute
|| pDC
->doneFE
)
642 CompleteDrawContextInl(pContext
, workerId
, pDC
);
651 uint32_t lastRetiredFE
= curDrawFE
- 1;
652 uint32_t curDraw
= curDrawFE
;
653 while (IDComparesLess(curDraw
, drawEnqueued
))
655 uint32_t dcSlot
= curDraw
% pContext
->MAX_DRAWS_IN_FLIGHT
;
656 DRAW_CONTEXT
*pDC
= &pContext
->dcRing
[dcSlot
];
658 if (!pDC
->isCompute
&& !pDC
->FeLock
)
660 if (CheckDependencyFE(pContext
, pDC
, lastRetiredFE
))
665 uint32_t initial
= InterlockedCompareExchange((volatile uint32_t*)&pDC
->FeLock
, 1, 0);
668 // successfully grabbed the DC, now run the FE
669 pDC
->FeWork
.pfnWork(pContext
, pDC
, workerId
, &pDC
->FeWork
.desc
);
671 CompleteDrawFE(pContext
, workerId
, pDC
);
678 //////////////////////////////////////////////////////////////////////////
679 /// @brief If there is any compute work then go work on it.
680 /// @param pContext - pointer to SWR context.
681 /// @param workerId - The unique worker ID that is assigned to this thread.
682 /// @param curDrawBE - This tracks the draw contexts that this thread has processed. Each worker thread
683 /// has its own curDrawBE counter and this ensures that each worker processes all the
686 SWR_CONTEXT
*pContext
,
690 uint32_t drawEnqueued
= 0;
691 if (FindFirstIncompleteDraw(pContext
, workerId
, curDrawBE
, drawEnqueued
) == false)
696 uint32_t lastRetiredDraw
= pContext
->dcRing
[curDrawBE
% pContext
->MAX_DRAWS_IN_FLIGHT
].drawId
- 1;
698 for (uint64_t i
= curDrawBE
; IDComparesLess(i
, drawEnqueued
); ++i
)
700 DRAW_CONTEXT
*pDC
= &pContext
->dcRing
[i
% pContext
->MAX_DRAWS_IN_FLIGHT
];
701 if (pDC
->isCompute
== false) return;
703 // check dependencies
704 if (CheckDependency(pContext
, pDC
, lastRetiredDraw
))
709 SWR_ASSERT(pDC
->pDispatch
!= nullptr);
710 DispatchQueue
& queue
= *pDC
->pDispatch
;
712 // Is there any work remaining?
713 if (queue
.getNumQueued() > 0)
715 void* pSpillFillBuffer
= nullptr;
716 void* pScratchSpace
= nullptr;
717 uint32_t threadGroupId
= 0;
718 while (queue
.getWork(threadGroupId
))
720 queue
.dispatch(pDC
, workerId
, threadGroupId
, pSpillFillBuffer
, pScratchSpace
);
721 queue
.finishedWork();
724 // Ensure all streaming writes are globally visible before moving onto the next draw
730 template<bool IsFEThread
, bool IsBEThread
>
731 DWORD
workerThreadMain(LPVOID pData
)
733 THREAD_DATA
*pThreadData
= (THREAD_DATA
*)pData
;
734 SWR_CONTEXT
*pContext
= pThreadData
->pContext
;
735 uint32_t threadId
= pThreadData
->threadId
;
736 uint32_t workerId
= pThreadData
->workerId
;
738 bindThread(pContext
, threadId
, pThreadData
->procGroupId
, pThreadData
->forceBindProcGroup
);
742 sprintf_s(threadName
,
744 "SWRWorker_%02d_NUMA%d_Core%02d_T%d",
746 // linux pthread name limited to 16 chars (including \0)
747 "w%03d-n%d-c%03d-t%d",
749 workerId
, pThreadData
->numaId
, pThreadData
->coreId
, pThreadData
->htId
);
750 SetCurrentThreadName(threadName
);
753 RDTSC_INIT(threadId
);
755 uint32_t numaNode
= pThreadData
->numaId
;
756 uint32_t numaMask
= pContext
->threadPool
.numaMask
;
758 // flush denormals to 0
759 _mm_setcsr(_mm_getcsr() | _MM_FLUSH_ZERO_ON
| _MM_DENORMALS_ZERO_ON
);
761 // Track tiles locked by other threads. If we try to lock a macrotile and find its already
762 // locked then we'll add it to this list so that we don't try and lock it again.
765 // each worker has the ability to work on any of the queued draws as long as certain
766 // conditions are met. the data associated
767 // with a draw is guaranteed to be active as long as a worker hasn't signaled that he
768 // has moved on to the next draw when he determines there is no more work to do. The api
769 // thread will not increment the head of the dc ring until all workers have moved past the
771 // the logic to determine what to work on is:
772 // 1- try to work on the FE any draw that is queued. For now there are no dependencies
773 // on the FE work, so any worker can grab any FE and process in parallel. Eventually
774 // we'll need dependency tracking to force serialization on FEs. The worker will try
775 // to pick an FE by atomically incrementing a counter in the swr context. he'll keep
776 // trying until he reaches the tail.
777 // 2- BE work must be done in strict order. we accomplish this today by pulling work off
778 // the oldest draw (ie the head) of the dcRing. the worker can determine if there is
779 // any work left by comparing the total # of binned work items and the total # of completed
780 // work items. If they are equal, then there is no more work to do for this draw, and
781 // the worker can safely increment its oldestDraw counter and move on to the next draw.
782 std::unique_lock
<std::mutex
> lock(pContext
->WaitLock
, std::defer_lock
);
784 auto threadHasWork
= [&](uint32_t curDraw
) { return curDraw
!= pContext
->dcRing
.GetHead(); };
786 uint32_t curDrawBE
= 0;
787 uint32_t curDrawFE
= 0;
789 bool bShutdown
= false;
793 if (bShutdown
&& !threadHasWork(curDrawBE
))
799 while (loop
++ < KNOB_WORKER_SPIN_LOOP_COUNT
&& !threadHasWork(curDrawBE
))
804 if (!threadHasWork(curDrawBE
))
808 // check for thread idle condition again under lock
809 if (threadHasWork(curDrawBE
))
815 pContext
->FifosNotEmpty
.wait(lock
);
821 AR_BEGIN(WorkerWorkOnFifoBE
, 0);
822 bShutdown
|= WorkOnFifoBE(pContext
, workerId
, curDrawBE
, lockedTiles
, numaNode
, numaMask
);
823 AR_END(WorkerWorkOnFifoBE
, 0);
825 WorkOnCompute(pContext
, workerId
, curDrawBE
);
830 WorkOnFifoFE(pContext
, workerId
, curDrawFE
);
834 curDrawBE
= curDrawFE
;
841 template<> DWORD workerThreadMain
<false, false>(LPVOID
) = delete;
843 template <bool IsFEThread
, bool IsBEThread
>
844 DWORD
workerThreadInit(LPVOID pData
)
850 return workerThreadMain
<IsFEThread
, IsBEThread
>(pData
);
854 __except(EXCEPTION_CONTINUE_SEARCH
)
862 template<> DWORD workerThreadInit
<false, false>(LPVOID pData
) = delete;
864 //////////////////////////////////////////////////////////////////////////
865 /// @brief Creates thread pool info but doesn't launch threads.
866 /// @param pContext - pointer to context
867 /// @param pPool - pointer to thread pool object.
868 void CreateThreadPool(SWR_CONTEXT
* pContext
, THREAD_POOL
* pPool
)
870 bindThread(pContext
, 0);
873 uint32_t numThreadsPerProcGroup
= 0;
874 CalculateProcessorTopology(nodes
, numThreadsPerProcGroup
);
876 uint32_t numHWNodes
= (uint32_t)nodes
.size();
877 uint32_t numHWCoresPerNode
= (uint32_t)nodes
[0].cores
.size();
878 uint32_t numHWHyperThreads
= (uint32_t)nodes
[0].cores
[0].threadIds
.size();
880 // Calculate num HW threads. Due to asymmetric topologies, this is not
881 // a trivial multiplication.
882 uint32_t numHWThreads
= 0;
883 for (auto& node
: nodes
)
885 for (auto& core
: node
.cores
)
887 numHWThreads
+= (uint32_t)core
.threadIds
.size();
891 uint32_t numNodes
= numHWNodes
;
892 uint32_t numCoresPerNode
= numHWCoresPerNode
;
893 uint32_t numHyperThreads
= numHWHyperThreads
;
895 if (pContext
->threadInfo
.MAX_NUMA_NODES
)
897 numNodes
= std::min(numNodes
, pContext
->threadInfo
.MAX_NUMA_NODES
);
900 if (pContext
->threadInfo
.MAX_CORES_PER_NUMA_NODE
)
902 numCoresPerNode
= std::min(numCoresPerNode
, pContext
->threadInfo
.MAX_CORES_PER_NUMA_NODE
);
905 if (pContext
->threadInfo
.MAX_THREADS_PER_CORE
)
907 numHyperThreads
= std::min(numHyperThreads
, pContext
->threadInfo
.MAX_THREADS_PER_CORE
);
910 #if defined(_WIN32) && !defined(_WIN64)
911 if (!pContext
->threadInfo
.MAX_WORKER_THREADS
)
913 // Limit 32-bit windows to bindable HW threads only
914 if ((numCoresPerNode
* numHWHyperThreads
) > 32)
916 numCoresPerNode
= 32 / numHWHyperThreads
;
921 // Calculate numThreads
922 uint32_t numThreads
= numNodes
* numCoresPerNode
* numHyperThreads
;
923 numThreads
= std::min(numThreads
, numHWThreads
);
925 if (pContext
->threadInfo
.MAX_WORKER_THREADS
)
927 uint32_t maxHWThreads
= numHWNodes
* numHWCoresPerNode
* numHWHyperThreads
;
928 numThreads
= std::min(pContext
->threadInfo
.MAX_WORKER_THREADS
, maxHWThreads
);
931 uint32_t numAPIReservedThreads
= 1;
936 // If only 1 worker threads, try to move it to an available
937 // HW thread. If that fails, use the API thread.
938 if (numCoresPerNode
< numHWCoresPerNode
)
942 else if (numHyperThreads
< numHWHyperThreads
)
946 else if (numNodes
< numHWNodes
)
952 pContext
->threadInfo
.SINGLE_THREADED
= true;
957 // Save HW threads for the API if we can
958 if (numThreads
> numAPIReservedThreads
)
960 numThreads
-= numAPIReservedThreads
;
964 numAPIReservedThreads
= 0;
968 if (pContext
->threadInfo
.SINGLE_THREADED
)
973 // Initialize DRAW_CONTEXT's per-thread stats
974 for (uint32_t dc
= 0; dc
< pContext
->MAX_DRAWS_IN_FLIGHT
; ++dc
)
976 pContext
->dcRing
[dc
].dynState
.pStats
= (SWR_STATS
*)AlignedMalloc(sizeof(SWR_STATS
) * numThreads
, 64);
977 memset(pContext
->dcRing
[dc
].dynState
.pStats
, 0, sizeof(SWR_STATS
) * numThreads
);
980 if (pContext
->threadInfo
.SINGLE_THREADED
)
982 pContext
->NumWorkerThreads
= 1;
983 pContext
->NumFEThreads
= 1;
984 pContext
->NumBEThreads
= 1;
985 pPool
->numThreads
= 0;
990 pPool
->numThreads
= numThreads
;
991 pContext
->NumWorkerThreads
= pPool
->numThreads
;
993 pPool
->pThreadData
= (THREAD_DATA
*)malloc(pPool
->numThreads
* sizeof(THREAD_DATA
));
996 pPool
->pThreads
= new THREAD_PTR
[pPool
->numThreads
];
998 if (pContext
->threadInfo
.MAX_WORKER_THREADS
)
1000 bool bForceBindProcGroup
= (numThreads
> numThreadsPerProcGroup
);
1001 uint32_t numProcGroups
= (numThreads
+ numThreadsPerProcGroup
- 1) / numThreadsPerProcGroup
;
1002 // When MAX_WORKER_THREADS is set we don't bother to bind to specific HW threads
1003 // But Windows will still require binding to specific process groups
1004 for (uint32_t workerId
= 0; workerId
< numThreads
; ++workerId
)
1006 pPool
->pThreadData
[workerId
].workerId
= workerId
;
1007 pPool
->pThreadData
[workerId
].procGroupId
= workerId
% numProcGroups
;
1008 pPool
->pThreadData
[workerId
].threadId
= 0;
1009 pPool
->pThreadData
[workerId
].numaId
= 0;
1010 pPool
->pThreadData
[workerId
].coreId
= 0;
1011 pPool
->pThreadData
[workerId
].htId
= 0;
1012 pPool
->pThreadData
[workerId
].pContext
= pContext
;
1013 pPool
->pThreadData
[workerId
].forceBindProcGroup
= bForceBindProcGroup
;
1015 pContext
->NumBEThreads
++;
1016 pContext
->NumFEThreads
++;
1021 // numa distribution assumes workers on all nodes
1022 bool useNuma
= true;
1023 if (numCoresPerNode
* numHyperThreads
== 1)
1027 pPool
->numaMask
= numNodes
- 1; // Only works for 2**n numa nodes (1, 2, 4, etc.)
1029 pPool
->numaMask
= 0;
1032 uint32_t workerId
= 0;
1033 for (uint32_t n
= 0; n
< numNodes
; ++n
)
1035 auto& node
= nodes
[n
];
1036 uint32_t numCores
= numCoresPerNode
;
1037 for (uint32_t c
= 0; c
< numCores
; ++c
)
1039 if (c
>= node
.cores
.size())
1044 auto& core
= node
.cores
[c
];
1045 for (uint32_t t
= 0; t
< numHyperThreads
; ++t
)
1047 if (t
>= core
.threadIds
.size())
1052 if (numAPIReservedThreads
)
1054 --numAPIReservedThreads
;
1058 SWR_ASSERT(workerId
< numThreads
);
1060 pPool
->pThreadData
[workerId
].workerId
= workerId
;
1061 pPool
->pThreadData
[workerId
].procGroupId
= core
.procGroup
;
1062 pPool
->pThreadData
[workerId
].threadId
= core
.threadIds
[t
];
1063 pPool
->pThreadData
[workerId
].numaId
= useNuma
? n
: 0;
1064 pPool
->pThreadData
[workerId
].coreId
= c
;
1065 pPool
->pThreadData
[workerId
].htId
= t
;
1066 pPool
->pThreadData
[workerId
].pContext
= pContext
;
1068 pContext
->NumBEThreads
++;
1069 pContext
->NumFEThreads
++;
1075 SWR_ASSERT(workerId
== pContext
->NumWorkerThreads
);
1079 //////////////////////////////////////////////////////////////////////////
1080 /// @brief Launches worker threads in thread pool.
1081 /// @param pContext - pointer to context
1082 /// @param pPool - pointer to thread pool object.
1083 void StartThreadPool(SWR_CONTEXT
* pContext
, THREAD_POOL
* pPool
)
1085 if (pContext
->threadInfo
.SINGLE_THREADED
)
1090 for (uint32_t workerId
= 0; workerId
< pContext
->NumWorkerThreads
; ++workerId
)
1092 pPool
->pThreads
[workerId
] = new std::thread(workerThreadInit
<true, true>, &pPool
->pThreadData
[workerId
]);
1096 //////////////////////////////////////////////////////////////////////////
1097 /// @brief Destroys thread pool.
1098 /// @param pContext - pointer to context
1099 /// @param pPool - pointer to thread pool object.
1100 void DestroyThreadPool(SWR_CONTEXT
*pContext
, THREAD_POOL
*pPool
)
1102 if (!pContext
->threadInfo
.SINGLE_THREADED
)
1104 // Wait for all threads to finish
1105 SwrWaitForIdle(pContext
);
1107 // Wait for threads to finish and destroy them
1108 for (uint32_t t
= 0; t
< pPool
->numThreads
; ++t
)
1110 // Detach from thread. Cannot join() due to possibility (in Windows) of code
1111 // in some DLLMain(THREAD_DETATCH case) blocking the thread until after this returns.
1112 pPool
->pThreads
[t
]->detach();
1113 delete(pPool
->pThreads
[t
]);
1116 delete [] pPool
->pThreads
;
1118 // Clean up data used by threads
1119 free(pPool
->pThreadData
);