dragged kicking and screaming: source multicore · © 2007 valve corporation. all rights reserved....
TRANSCRIPT
![Page 1: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/1.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Dragged Kicking and Screaming:
Source Multicore
Tom Leonard, Valve9 March 2007
![Page 2: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/2.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Multicore
Most significant development since consumer 3D
![Page 3: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/3.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Multicore
Most significant development since consumer 3DExplicit parallelism
Hardware problem becoming software problem will require new techniques
![Page 4: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/4.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Introduction
The decisions faced with multiple coresHow we are approaching multiple coresAlgorithms and paradigms
![Page 5: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/5.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Goals
Integrate multicore across Valve’s business
Expose to game programmers, licensees and MOD authors
![Page 6: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/6.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Goals
Integrate multicore across Valve’s businessScale to cores without recompile
![Page 7: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/7.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Goals
Integrate multicore across Valve’s businessScale to cores without recompileCreate value beyond framerate
Apply cores to new gameplay
![Page 8: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/8.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Challenges
Games want maximal CPU utilizationGames are inherently serialDecades of experience in single threaded optimizationMillions of lines of code written for single threading
![Page 9: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/9.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Strategies
Threading modelThreading framework
![Page 10: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/10.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Threading Models
Fine grained threadingCoarse threadingHybrid threading
![Page 11: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/11.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Diving In
ClientUser inputRenderingGraphics simulation
ServerAIPhysicsGame logic
![Page 12: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/12.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Diving In
Experiment: run client and server each on own core
![Page 13: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/13.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Diving In
Experiment: run client and server each on own coreBenefits: forced to confront systems that are not thread safe or not thread efficient
![Page 14: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/14.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Discoveries
Problem: shared data accessGlobal dataStatic data (optimizations/function local state)Singleton objects
![Page 15: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/15.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Discoveries
Problem: shared data accessThread safety is easy!
![Page 16: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/16.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Discoveries
Problem: shared data accessThread safety is easy!
Slap on a mutex/critical section
![Page 17: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/17.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Discoveries
Problem: shared data accessBad thread safety is easy!
Slap on a mutex/critical sectionThe simple thing is the worst thing
Mutexes are terribleExcessive waitsError proneFail to scale
Establish slow but stable baseline
![Page 18: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/18.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Discoveries
Efficient thread safetyNo synchronization (“wait-free”)
Each thread has a private copy of all the data needed to perform operation:
Threads working on independent problemsReplace globals with thread private dataReorient to pipeline
Example: Source “Spatial Partition”
![Page 19: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/19.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Discoveries
Server Objects Client Objects
Static Objects
Spatial Partition
![Page 20: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/20.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Discoveries
Server Objects Client Objects
Static Objects
Spatial Partition
Static Objects
![Page 21: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/21.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Discoveries
Efficient thread safetyNo synchronization (“wait-free”)Better synchronization tools, techniques
Analyze data accessExample: symbol table using read/write lock
Decouple using queued function calls
![Page 22: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/22.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Discoveries
What if you can’t eliminate contention over shared resources?
![Page 23: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/23.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Results
Can approach 2x in contrived maps
![Page 24: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/24.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Results
![Page 25: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/25.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Results
![Page 26: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/26.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Results
Can approach 2x in contrived mapsMore like 1.2x in real single playerApplicable to 360 Team Fortress 2
![Page 27: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/27.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Hybrid threading
Use the appropriate tool for the jobSome systems on cores (e.g. sound)Some systems split internally in a coarse mannerSplit expensive iterations across cores fine grainedQueue some work to run when a core goes idle
Need strong toolsMaximal core utilization
![Page 28: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/28.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Hybrid threading: Rendering
Render
Skybox Main View Monitor Etc.
Scene List
For each object
Particles
Character
Sim & Draw
Bone Setup
Etc
Draw
![Page 29: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/29.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Hybrid threading: Rendering
ProblemsPer-view scene construction limits opportunityArbitrary object type orderArbitrary code execution
Simulation and Rendering interleavedLazy calculation optimizations
![Page 30: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/30.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Hybrid threading: Rendering
Iterative Transition: Skeletal AnimationParallelize lazy calculation triggersRefactor bone setup into single pass per viewRefactor into single pass for all viewsSame pattern for other CPU-intensive stages
![Page 31: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/31.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Hybrid threading: Rendering
Revised pipelineConstruct scene rendering lists for multiple scenes in parallel (e.g., the world and its reflection in water)Overlap graphics simulationCompute character bone transformations for all characters in all scenes in parallelAllow multiple threads to draw in parallelSerialize drawing operations on another core
![Page 32: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/32.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Threading Tools
Implementing Hybrid Threading
Programmers solve game development problems, not threading problemsEmpower all programmers to leverage cores
Operating system: too low levelCompiler extensions (OpenMP): too opaqueTailored tools: correct abstraction
![Page 33: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/33.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Tailored tools: Game Threading Infrastructure
Custom work management systemIntuitive for programmersFocus on keeping cores busyThread pool: N-1 threads for N coresSupport hybrid threading
Function threadingArray parallelismQueued and immediate execution
![Page 34: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/34.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Tailored tools: Game Threading Infrastructure
Goal: make system easy to use, hard to mess upExample: compiler generated functors
Uses templates to package up functions and data, point of call looks very similarCall arrives on other end as if called normallySaves time, reduces error, encourages experimentation
![Page 35: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/35.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Tailored tools: Game Threading Infrastructure
One-off push to another core
if ( !IsEngineThreaded() )
_Host_RunFrame_Server( numticks );
else
ThreadExecute( _Host_RunFrame_Server, numticks );
![Page 36: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/36.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Tailored tools: Game Threading Infrastructure
Parallel loop
void ProcessPSystem( CParticleEffect *pEffect );
ParallelProcess( particlesToSimulate.Base(),
particlesToSimulate.Count(),
ProcessPSystem );
![Page 37: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/37.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Tailored tools: Game Threading Infrastructure
Queue up a bunch of work items, wait for them to complete
BeginExecuteParallel();
ExecuteParallel( g_pParticleSystem,
&CParticleSystem::Update, time );
ExecuteParallel( &UpdateRopes, time );
EndExecuteParallel();
Low level APIs for the brave
![Page 38: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/38.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Contention
What if you can’t eliminate contention over shared resources?Example: Allocator
Heavily usedMultiple pools of fixed sized blocks with a custom spin lock mutex per-poolMutex limiting scaleDidn’t want per-thread allocators
![Page 39: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/39.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Contention
Lock-free algorithmsNo thread can block system regardless of scheduling or stateUnder the hood of all services and data structuresRelies on atomic write instructions, “compare-and-swap”
![Page 40: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/40.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Contention
bool CompareAndSwap(int *pDest, int newValue, int oldValue)
{
Lock( pDest );
bool success = false;
if ( *pDest == oldValue )
{
*pDest = newValue;
success = true;
}
Unlock( pDest );
return success;
}
![Page 41: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/41.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Contention
bool CompareAndSwap(int *pDest, int newValue, int oldValue)
{
__asm
{
mov eax,oldValue
mov ecx,pDest
mov edx,newValue
lock cmpxchg [ecx],edxmov eax,0
setz al
}
}
![Page 42: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/42.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Contention
Use lock-free algorithm in allocatorReplace mutex and traditional free list per-pool with a lock-free list per-poolWindows API/XDK SList
![Page 43: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/43.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Lock-free example: singly linked list
Compare-and-swap“If head is equal to what I think it is, assign with my new head”ABA Problem: is it the same head?Use a serial number as a discriminating field
![Page 44: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/44.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Lock-free example: singly linked list
class CSList
{
public:
CSList()
void Push( SListNode_t *pNode );
SListNode_t *Pop();
SListNode_t *Detach();
int Count() const;
private:
SListHead_t m_Head;
};
![Page 45: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/45.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Lock-free example: singly linked list
struct SListNode_t
{
SListNode_t *pNext;
};
union SListHead_t
{
struct Value_t
{
SListNode_t *pNext;
int16 iDepth;
int16 iSequence;
} value;
int64 value64;
};
![Page 46: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/46.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Lock-free example: singly linked list
Void Push( SListNode_t *pNode )
{
SListHead_t oldHead, newHead;
for (;;)
{
oldHead.value64 = m_Head.value64;
newHead.value.iDepth = oldHead.value.iDepth + 1;
newHead.value.iSequence = oldHead.value.iSequence + 1;
newHead.value.Next = pNode;
pNode->pNext = oldHead.value.pNext;
if ( ThreadInterlockedAssignIf64( &m_Head.value64,
newHead.value64, oldHead.value64 ) )
{
return;
}
}
}
![Page 47: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/47.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Lock-free example: singly linked list
Lock-free list exceptionally usefulKeep pools of context structures when impractical to give every thread a contextEfficiently gather results of a parallel process for later handlingBuild up lists of data to operate on using Push(), then use Detach() (a.k.a “Flush”) to grab the data in another thread in a single operation
![Page 48: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/48.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Exampleextern Vector trace_start;
extern Vector trace_end;
// etc...
struct cbrush_t
{
int contents;
unsigned short numsides;
unsigned short firstbrushside;
int checkcount; // to avoid repeated testings
};
///////////////////////////////
void BeginTrace()
{
g_CModelMutex.Lock();
++s_nCheckCount;
}
![Page 49: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/49.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Examplestruct TraceInfo_t
{
Vector m_start;
Vector m_end;
// etc...
CVisitBitVec m_BrushVisits;
};
CTraceInfoPool g_TraceInfoPool;
TraceInfo_t *BeginTrace()
{
TraceInfo_t *pTraceInfo;
if ( !g_TraceInfoPool.PopItem( &pTraceInfo ) )
pTraceInfo = new TraceInfo_t;
return pTraceInfo;
}
![Page 50: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/50.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Lock-free algorithms
Thread pool work distribution queueDerived from HL2 asynchronous I/O queueDesigned for one provider, one consumerSimple prioritized queue with mutexArbitrary priorityOne queue for all threads
![Page 51: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/51.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Lock-free algorithms
SolutionsUse lock-free queue (Fober, et. al.)Rework interface to fixed priorities, one queue per-priority
Interfaces critical
Queues per core in addition to a shared queueUse atomic operations to get “ticket”, actual work done may differ
![Page 52: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/52.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Lock-free algorithms
Locks permit a stable realityLock-free permits reality to change instruction to instructionLeverage inference rather than locks to know part of the system is stable
Wait-free is always better
![Page 53: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/53.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Looking Forward
Why so much up-front investment?
![Page 54: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/54.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Looking Forward
Why so much up-front investment?Steam
Communicate with customers Tap markets not available via retail
Dramatic change is underwayCore count double every 18 monthsCPU/GPU/PPU/AIPU/etc not the futureMany homogeneous coresDivision of computing power a software problem
![Page 55: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/55.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Call to action
Build or acquire strong tools, new techniques
Embrace lock-free mechanisms to move work and data to and from wait-free code
Prepare for decomposition of features over many cores
Use accessible solutions to empower all programmers, not just systems programmers
Support even higher level threading framed in terms of game problems
![Page 56: Dragged Kicking and Screaming: Source Multicore · © 2007 Valve Corporation. All Rights Reserved. Dragged Kicking and Screaming: Source Multicore Tom Leonard, Valve 9 March 2007](https://reader035.vdocument.in/reader035/viewer/2022071019/5fd2d3644b7f4e740555d5db/html5/thumbnails/56.jpg)
© 2007 Valve Corporation. All Rights Reserved.
Summary
Started with a stable but bad threadingIteratively eliminated bad cases using variety of techniques, usually lock-freeDuring iterations, expanded toolset to meet newly discovered needsFocused on ease-of-use for other programmersNow being applied by others at higher levels