Documente Academic
Documente Profesional
Documente Cultură
Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit
Moores Law
Transistor count still rising Clock speed flattening sharply
memory
cache
cache
Bus
cache
Bus
shared memory
cache
Bus
shared memory
Why do we care?
Time no longer cures software bloat
The free ride is over
1.8x
User code
3.6x
Traditional Uniprocessor
1.8x
3.6x
User code
Multicore
1.8x
User code
2x
2.9x
Multicore
Real-World programming
Architectures Techniques
Art of Multiprocessor Programming 11
Real-World programming
Architectures Techniques
Art of Multiprocessor Programming 12
Sequential Computation
thread memory
object
Art of Multiprocessor Programming
object
13
Concurrent Computation
memory
object
Art of Multiprocessor Programming
object
14
Asynchrony
Cache misses (short) Page faults (long) Scheduling quantum used up (really long)
Art of Multiprocessor Programming 15
Model Summary
Multiple threads
Sometimes called processes
16
Road Map
We are going to focus on principles first, then practice
Start with idealized models Look at simplistic problems Emphasize correctness over pragmatism Correctness may be theoretical, but incorrectness has practical impact
Art of Multiprocessor Programming 17
Concurrency Jargon
Hardware
Processors
Software
Threads, processes
18
Given
Ten-processor multiprocessor One thread per processor
Goal
Get ten-fold speedup (or close)
Art of Multiprocessor Programming 19
Load Balancing
1
P0
109 2109
P1
1010
P9
20
21
Issues
Higher ranges have fewer primes Yet larger numbers harder to test Thread workloads
Uneven Hard to predict
22
Issues
Higher ranges have fewer primes Yet larger numbers harder to test Thread workloads
Uneven Hard to predict
Shared Counter
19 18
17
Art of Multiprocessor Programming 24
code
cache
cache
Bus
Local variables
cache
Bus
shared memory
shared counter
Art of Multiprocessor Programming 27
29
Counter Implementation
public class Counter { private long value; public long getAndIncrement() { return value++; }
30
Counter Implementation
public class Counter { private long value; public long getAndIncrement() { return value++; }
31
What It Means
public class Counter { private long value; public long getAndIncrement() { return value++; }
32
What It Means
public class Counter { private long value; public long getAndIncrement() { return value++; temp = value; } value = value + 1; } return temp;
33
Not so good
Value 1 2
read write 1 2 read 2
3
write 3
read 1
time
Art of Multiprocessor Programming
write 2
34
write
write
read
35
Challenge
public class Counter { private long value; public long getAndIncrement() { temp = value; value = temp + 1; return temp; } }
Art of Multiprocessor Programming 36
Challenge
public class Counter { private long value; public long getAndIncrement() { temp = value; value = temp + 1; return temp; Make these } }
Art of Multiprocessor Programming
Hardware Solution
public class Counter { private long value; public long getAndIncrement() { temp = value; value = temp + 1; return temp; } }
An Aside: Java
public class Counter { private long value; public long getAndIncrement() { synchronized { temp = value; value = temp + 1; } return temp; } }
Art of Multiprocessor Programming 39
An Aside: Java
public class Counter { private long value; public long getAndIncrement() { synchronized { temp = value; value = temp + 1; } return temp; } Synchronized block
Art of Multiprocessor Programming 40
An Aside: Java
public class Counter { private long value; public long getAndIncrement() { synchronized { temp = value; value = temp + 1; } return temp; } }
Art of Multiprocessor Programming 41
Mutual Exclusion
42
43
44
The Problem
A B
45
Liveness Properties
Something good happens eventually
46
No Deadlock
if only one wants in, it gets in if both want in, one gets in. This is a liveness property
Art of Multiprocessor Programming 47
Simple Protocol
Idea
Just look at the pond
Gotcha
Trees obscure the view
48
Interpretation
Threads cant see what other threads are doing Explicit communication required for coordination
49
Gotcha
Bob takes shower Alice recharges battery Bob out shopping for pet food
50
Interpretation
Message-passing doesnt work Recipient might not be
Listening There at all
Communication must be
Persistent (like writing) Not transient (like speaking)
Art of Multiprocessor Programming 51
Can Protocol
cola
cola
52
53
54
Can Protocol
Idea
Cans on Alices windowsill Strings lead to Bobs house Bob pulls strings, knocks over cans
Gotcha
Cans cannot be reused Bob runs out of cans
Art of Multiprocessor Programming 55
Interpretation
Cannot solve mutual exclusion with interrupts
Sender sets fixed bit in receivers space Receiver resets bit when ready Requires unbounded number of inturrupt bits
56
Flag Protocol
A B
57
58
59
Alices Protocol
Raise flag Wait until Bobs flag is down Unleash pet Lower flag when pet returns
60
Bobs Protocol
Raise flag Wait until Alices flag is down Unleash pet Lower flag when pet returns
61
62
Bobs Protocol
Raise flag While Alices flag is up
63
64
Consider the last time Alice and Bob each looked before letting the pets in Without loss of generality assume Alice was the last to look
Art of Multiprocessor Programming 65
Proof
Bob last raised flag Alice last raised her flag Alices last look Bobs last look
time
Art Bobs Flag. A Contradiction 66 Alice must have seen of Multiprocessor Programming
Proof of No Deadlock
If only one pet wants in, it gets in.
67
Proof of No Deadlock
If only one pet wants in, it gets in. Deadlock requires both continually trying to get in.
68
Proof of No Deadlock
If only one pet wants in, it gets in. Deadlock requires both continually trying to get in. If Bob sees Alices flag, he gives her priority (a gentleman)
69
Remarks
Protocol is unfair
Protocol uses waiting
Bobs pet might never get in If Bob is eaten by his pet, Alices pet might never get in
70
Moral of Story
Mutual Exclusion cannot be solved by It can be solved by
transient communication (cell phones) interrupts (cans) one-bit shared variables that can be read or written
71
Pick a point
Art of Multiprocessor Programming 72
73
74
75
76
77
Producer/Consumer
Alice and Bob cant meet
Each has restraining order on other So he puts food in the pond And later, she releases the pets
Avoid
Releasing pets when theres no food Putting out food if uneaten food remains
Art of Multiprocessor Programming 78
Producer/Consumer
Need a mechanism so that
Bob lets Alice know when food has been put out Alice lets Bob know when to put out more food
79
Surprise Solution
A
cola
80
81
82
83
84
Pseudocode
while (true) { while (can.isUp()){}; pet.release(); pet.recapture(); can.reset(); }
Alices code
Art of Multiprocessor Programming 85
Pseudocode
while (true) { while (can.isUp()){}; pet.release(); pet.recapture(); can.reset(); while (true) { } while (can.isDown()){}; pond.stockWithFood(); can.knockOver();
Bobs code
Alices code
}
Art of Multiprocessor Programming 86
Correctness
Mutual Exclusion
Pets and Bob never together in pond
87
Correctness
Mutual Exclusion
Pets and Bob never together in pond
No Starvation
if Bob always willing to feed, and pets always famished, then pets eat infinitely often.
88
Correctness
Mutual Exclusion No Starvation
Producer/Consumer
if Bob always willing to feed, and pets always famished, then pets eat infinitely often. The pets never enter pond unless there is food, and Bob never provides food if there is unconsumed food.
Art of Multiprocessor Programming 89
90
Waiting
Both solutions use waiting
while(mumble){}
Waiting is problematic
If one participant is delayed So is everyone else But delays are common & unpredictable
91
92
93
94
B D A C E
3 2 1 3 Art of Multiprocessor Programming
Letter Tiles
W A S
4 1
B D A C E
3 2 1 3 Art of Multiprocessor Programming
1 96
To post a message
W A S H T H E C A R
4 1 1 4 1 4 1 3 1
whe w
97
S E L L
1 1 1
L A V A
1 1 4
98
Uh-Oh
S E L L
1 1 1
T H E
1 4
C A R
3 1
OK
Art of Multiprocessor Programming 99
Readers/Writers
Devise a protocol so that
Writer writes one letter at a time Reader reads one letter at a time Reader sees
Old message or new message No mixed messages
100
Readers/Writers (continued)
Easy with mutual exclusion But mutual exclusion requires waiting
One waits for the other Everyone executes sequentially
Remarkably
We can solve R/W without mutual exclusion
Art of Multiprocessor Programming 101
Why do we care?
We want as much of the code as possible to execute concurrently (in parallel) A larger sequential part implies reduced performance Amdahls law: this relation is not linear
Art of Multiprocessor Programming 102
Amdahls Law
OldExecutionTime NewExecutionTime
Speedup=
of computation given
n CPUs instead of 1
103
Amdahls Law
Speedup=
p 1p n
104
Amdahls Law
Speedup=
p 1p n
105
Parallel fraction
Amdahls Law
Sequential fraction
Speedup=
p 1p n
106
Parallel fraction
Amdahls Law
Sequential fraction
Speedup=
Number of processors
p 1p n
107
Parallel fraction
Example
Ten processors 60% concurrent, 40% sequential How close to 10-fold speedup?
108
Example
Ten processors 60% concurrent, 40% sequential How close to 10-fold speedup?
Speedup=2.17=
0.6 1 0.6 10
109
Example
Ten processors 80% concurrent, 20% sequential How close to 10-fold speedup?
110
Example
Ten processors 80% concurrent, 20% sequential How close to 10-fold speedup?
Speedup=3.57=
0.8 1 0.8 10
111
Example
Ten processors 90% concurrent, 10% sequential How close to 10-fold speedup?
112
Example
Ten processors 90% concurrent, 10% sequential How close to 10-fold speedup?
Speedup=5.26=
0.9 1 0.9 10
113
Example
Ten processors 99% concurrent, 01% sequential How close to 10-fold speedup?
114
Example
Ten processors 99% concurrent, 01% sequential How close to 10-fold speedup?
Speedup=9.17=
0.99 1 0.99 10
115
The Moral
Making good use of our multiple processors (cores) means Finding ways to effectively parallelize our code
Minimize sequential parts Reduce idle time in which threads wait without
Art of Multiprocessor Programming 116
Multicore Programming
This is what this course is about
The % that is not easy to make concurrent yet may have a large impact on overall speedup
Next week:
A more serious look at mutual exclusion
117
118