Games at Bolton
Programming multiple cores
Andrew Williams
http://www.bolton.ac.uk/staff/adw1
Games at Bolton
Introduction
Cores Processes Threads Symmetric Multiprocessing (SMP) Asymmetric Multiprocessing
Games at Bolton
Processes versus threads
Processes– MS Word– Powerpoint– Firefox
Threads (within Firefox)– Read data from a website– Render page to screen
Games at Bolton
Processes versus Threads
Processes usually independent of each other
Every process contains at least one thread (… of execution)
Threads tend to be doing different aspects of the same task
Processes have separate, protected address spaces and call stacks– Context switches tend to be slow
Games at Bolton
Processes versus Threads
Processes communicate through operating system calls– Inter-process communication– Typically a socket or a pipe– My chess program communicates with
its GUI like this
Games at Bolton
Inter-process communication - Xboard Launch command (shell command on Unix):
xboard -fcp "./postmodernist xboard" -size Middling -animateMoving False -tc 1400 -inc 0 -td 0.6 -showCoords True -showThinking True $1 $2 $3 $4 $5
Checking for input from Xboard (from xbd.c):retValue = PeekNamedPipe(stdInputHandle,
NULL, 0, NULL, &dw, NULL);if(!retValue || dw == 0)
return 0;else
return 1;
Games at Bolton
Multiple CPU motherboards
“Back in the day” approach Multiple CPUs on the motherboard Two, four, even eight CPU boards Very popular in computer chess
circles!
Dual-Celeron MB from http://tinyurl.com/392zwu
Games at Bolton
Multiple CPU motherboards
These were SMP Problems with this approach:
– Communication between CPUs• Information transferred over slower, external
bus
– Complex, expensive motherboards– Needed to buy two (or four or eight)
CPUs
Games at Bolton
Modern approaches
On 26/9/2007, Dell’s cheapest model for home customers was the Inspiron 531 (£269 without display)– This features an AMD® Athlon™ 64 X2
Dual Core Processor 3800+ Modern consumer CPUs have
multiple cores:– Intel Core 2 Duo– AMD X2
Games at Bolton
Multiple Cores in Consoles
We are used to this PS2 used the Emotion engine
– Q: Symmetric or Asymmetric? XBox360’s CPU is an IBM-designed
triple-core product– See Ars Technica’s coverage
PS3 uses the CELL chip– Q: Symmetric or Asymmetric?
Games at Bolton
Xbox360’s Xenon CPU
Image reproduced from Ars Technica, op cit
Games at Bolton
Xbox360’s Xenon CPU
Questions:– Is the Xenon symmetric or asymmetric?– The marketing material for the Xenon
says: “Two hardware threads per core; six
hardware threads total”
– How is this achieved?
Games at Bolton
Making use of Multiple Cores
–6α=-6
β=+inf
+16α=+16β=+inf
-9α=-9β=-6
+12α=+12β=+inf
+12 +2
+9α=+6β=+12
+5 +9
-16α=-16β=-9
+16α=+16β=+inf
+3 +16
+36α=+36β=+16
+36
A game-tree in a two-person, perfect information game
Games at Bolton
Making use of Multiple Cores
–6α=-6
β=+inf
+16α=+16β=+inf
-9α=-9β=-6
+12α=+12β=+inf
+12 +2
+9α=+6β=+12
+5 +9
-16α=-16β=-9
+16α=+16β=+inf
+3 +16
+36α=+36β=+16
+36
Q: How do we parallelize this process? Give me some options!
Games at Bolton
Making use of Multiple Cores
Typical game loop (pseudocode):Initialize_game();
while(!quit) {Get_user_input();
Do_ai();
Do_physics();
Render();
}
Shutdown_game();
Q: How do we parallelize that?
Give me some options!
Games at Bolton
Task Parallelism
The question is, to what extent are (eg) physics and AI separate things?
Can we sensibly do them both at once?
What if one of them needs data produced by the other?– which is pretty likely if you think about it
even for a moment How up-to-date does this information
really need to be?
Games at Bolton
Data parallelism
Suppose you have 200 NPCs running around the screen (eg in an RTS)
Divide them up among the available cores?
So four “spare” cores do the AI and physics for 50 NPCs each?– Simultaneously?– What if two of them bump into each other?
• Fine if they are both “on” the same core?
Games at Bolton
Mixed approaches
What about a mixture between task parallelism and data parallelism?
We have six cores:– Two for our NPCs, so give each core 100– Two for rendering?– Two for game logic and user input?
Note that if you think like this, you’re committing yourself to the Xbox360 implementation– What about PS3 or PC or Wii?