87
Multicore Programming Nir Shavit Tel Aviv University

Multicore Programming Nir Shavit Tel Aviv University

Embed Size (px)

Citation preview

Page 1: Multicore Programming Nir Shavit Tel Aviv University

Multicore Programming

Nir ShavitTel Aviv University

Page 2: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 2

Moore’s Law

Clock speed

flattening sharply

Transistor count still

rising

Page 3: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 3

Vanishing from your Desktops: The Uniprocesor

memory

cpu

Page 4: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 4

Your Server: The Shared Memory

Multiprocessor(SMP)

cache

BusBus

shared memory

cachecache

Page 5: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 5

Your New Server or Desktop: The Multicore Processor

(CMP)

cache

BusBus

shared memory

cachecacheAll on the same chip

Sun T2000Niagara

Page 6: Multicore Programming Nir Shavit Tel Aviv University

6

From the 2008 press……Intel has announced a press conference in San Francisco on November 17th, where it will officially launch the Core i7 Nehalem processor…

…Sun’s next generation Enterprise T5140 and T5240 servers, based on the 3rd Generation UltraSPARC T2 Plus processor, were released two days ago…

Page 7: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

7

Why is Kunle Smiling?

Niagara 1

Page 8: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 9

Traditional Scaling Process

User code

TraditionalUniprocessor

Speedup1.8x1.8x

7x7x

3.6x3.6x

Time: Moore’s law

Page 9: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 10

Multicore Scaling Process

User code

Multicore

Speedup 1.8x1.8x

7x7x

3.6x3.6x

Unfortunately, not so simple…

Page 10: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 11

Real-World Scaling Process

1.8x1.8x 2x2x 2.9x2.9x

User code

Multicore

Speedup

Parallelization and Synchronization require great care…

Page 11: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

12

Sequential Computation

memory

object object

thread

Page 12: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

13

Concurrent Computation

memory

object object

thre

ads

Page 13: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 14

Asynchrony

• Sudden unpredictable delays– Cache misses (short)– Page faults (long)– Scheduling quantum used up (really

long)

Page 14: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 15

Concurrency Jargon

• Hardware– Processors

• Software– Threads, processes

• Sometimes OK to confuse them, sometimes not.

Page 15: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 16

Parallel Primality Testing

• Challenge– Print primes from 1 to 1010

• Given– Ten-processor multiprocessor– One thread per processor

• Goal– Get ten-fold speedup (or close)

Page 16: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 17

Load Balancing

• Split the work evenly• Each thread tests range of 109

…109 10102·1091

P0 P1 P9

Page 17: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

18

Procedure for Thread i

void primePrint { int i = ThreadID.get(); // IDs in {0..9} for (j = i*109+1, j<(i+1)*109; j++) { if (isPrime(j)) print(j); }}

Page 18: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 19

Issues

• Higher ranges have fewer primes• Yet larger numbers harder to test• Thread workloads

– Uneven– Hard to predict

Page 19: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 20

Issues

• Higher ranges have fewer primes• Yet larger numbers harder to test• Thread workloads

– Uneven– Hard to predict

• Need dynamic load balancingre

jected

Page 20: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

21

17

18

19

Shared Counter

each thread takes a number

Page 21: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

22

Procedure for Thread i

int counter = new Counter(1); void primePrint { long j = 0; while (j < 1010) { j = counter.getAndIncrement(); if (isPrime(j)) print(j); }}

Page 22: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

23

Counter counter = new Counter(1); void primePrint { long j = 0; while (j < 1010) { j = counter.getAndIncrement(); if (isPrime(j)) print(j); }}

Procedure for Thread i

Shared counterobject

Page 23: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 24

Where Things Reside

cache

BusBus

cachecache

1

shared counter

shared memory

void primePrint { int i = ThreadID.get(); // IDs in {0..9} for (j = i*109+1, j<(i+1)*109; j++) { if (isPrime(j)) print(j); }}

code

Local variables

Page 24: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

25

Procedure for Thread i

Counter counter = new Counter(1); void primePrint { long j = 0; while (j < 1010) { j = counter.getAndIncrement(); if (isPrime(j)) print(j); }}

Stop when every value

taken

Page 25: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

26

Counter counter = new Counter(1); void primePrint { long j = 0; while (j < 1010) { j = counter.getAndIncrement(); if (isPrime(j)) print(j); }}

Procedure for Thread i

Increment & return each new value

Page 26: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

27

Counter Implementation

public class Counter { private long value;

public long getAndIncrement() { return value++; }}

Page 27: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

28

Counter Implementation

public class Counter { private long value;

public long getAndIncrement() { return value++; }} OK for single thread,

not for concurrent

threads

Page 28: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

29

What It Means

public class Counter { private long value;

public long getAndIncrement() { return value++; }}

Page 29: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

30

What It Means

public class Counter { private long value;

public long getAndIncrement() { return value++; }}

temp = value; value = temp + 1; return temp;

Page 30: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

31

time

Not so good…

Value… 1

read 1

read 1

write 2

read 2

write 3

write 2

2 3 2

Page 31: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

32

Is this problem inherent?

If we could only glue reads and writes together…

read

write read

write

!! !!

Page 32: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

33

Challenge

public class Counter { private long value;

public long getAndIncrement() { temp = value; value = temp + 1; return temp; }}

Page 33: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

34

Challenge

public class Counter { private long value;

public long getAndIncrement() { temp = value; value = temp + 1; return temp; }}

Make these steps atomic (indivisible)

Page 34: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

35

Hardware Solution

public class Counter { private long value;

public long getAndIncrement() { temp = value; value = temp + 1; return temp; }} ReadModifyWrit

e()instruction

Page 35: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

36

An Aside: Java™

public class Counter { private long value;

public long getAndIncrement() { synchronized { temp = value; value = temp + 1; } return temp; }}

Page 36: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

37

An Aside: Java™

public class Counter { private long value;

public long getAndIncrement() { synchronized { temp = value; value = temp + 1; } return temp; }}

Synchronized block

Page 37: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

38

An Aside: Java™

public class Counter { private long value;

public long getAndIncrement() { synchronized { temp = value; value = temp + 1; } return temp; }}

Mutual Exclusion

Page 38: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

39

Mutual Exclusion or “Alice & Bob share a pond”

A B

Page 39: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

40

Alice has a pet

A B

Page 40: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

41

Bob has a pet

A B

Page 41: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

42

The Problem

A B

The pets don’tget along

Page 42: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 43

Formalizing the Problem

• Two types of formal properties in asynchronous computation:

• Safety Properties– Nothing bad happens ever

• Liveness Properties – Something good happens eventually

Page 43: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 44

Formalizing our Problem

• Mutual Exclusion– Both pets never in pond

simultaneously– This is a safety property

• No Deadlock– if only one wants in, it gets in– if both want in, one gets in.– This is a liveness property

Page 44: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 45

Simple Protocol

• Idea– Just look at the pond

• Gotcha– Not atomic– Trees obscure the view

Page 45: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 46

Interpretation

• Threads can’t “see” what other threads are doing

• Explicit communication required for coordination

Page 46: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 47

Cell Phone Protocol

• Idea– Bob calls Alice (or vice-versa)

• Gotcha– Bob takes shower– Alice recharges battery– Bob out shopping for pet food …

Page 47: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 48

Interpretation

• Message-passing doesn’t work• Recipient might not be

– Listening– There at all

• Communication must be– Persistent (like writing)– Not transient (like speaking)

Page 48: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

49

Can Protocol

cola

cola

Page 49: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

50

Bob conveys a bit

A B

cola

Page 50: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

51

Bob conveys a bit

A B

cola

Page 51: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 52

Can Protocol

• Idea– Cans on Alice’s windowsill– Strings lead to Bob’s house– Bob pulls strings, knocks over cans

• Gotcha– Cans cannot be reused– Bob runs out of cans

Page 52: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 53

Interpretation

• Cannot solve mutual exclusion with interrupts– Sender sets fixed bit in receiver’s

space– Receiver resets bit when ready– Requires unbounded number of

interrupt bits

Page 53: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

54

Flag Protocol

A B

Page 54: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

55

Alice’s Protocol (sort of)

A B

Page 55: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

56

Bob’s Protocol (sort of)

A B

Page 56: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 57

Alice’s Protocol

• Raise flag• Wait until Bob’s flag is down• Unleash pet• Lower flag when pet returns

Page 57: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 58

Bob’s Protocol

• Raise flag• Wait until Alice’s flag is down• Unleash pet• Lower flag when pet returns

dang

er!

Page 58: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 59

Bob’s Protocol (2nd try)

• Raise flag• While Alice’s flag is up

– Lower flag– Wait for Alice’s flag to go down– Raise flag

• Unleash pet• Lower flag when pet returns

Page 59: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 60

Bob’s Protocol

• Raise flag• While Alice’s flag is up

– Lower flag– Wait for Alice’s flag to go down– Raise flag

• Unleash pet• Lower flag when pet returns

Bob defers to

Alice

Page 60: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 61

The Flag Principle

• Raise the flag• Look at other’s flag• Flag Principle:

– If each raises and looks, then– Last to look must see both flags up

Page 61: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 62

Proof of Mutual Exclusion

• Assume both pets in pond– Derive a contradiction– By reasoning backwards

• 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…

Page 62: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

63

Proof

time

Alice’s last look

Alice last raised her flag

Bob’s last look

QED

Alice must have seen Bob’s Flag. A Contradiction

Bob last raised flag

Page 63: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 64

Proof of No Deadlock

• If only one pet wants in, it gets in.

Page 64: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 65

Proof of No Deadlock

• If only one pet wants in, it gets in.• Deadlock requires both continually

trying to get in.

Page 65: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 66

Proof of No Deadlock

• If only one pet wants in, it gets in.• Deadlock requires both continually

trying to get in.• If Bob sees Alice’s flag, he gives

her priority (a gentleman…)

QED

Page 66: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 68

Moral of Story

•Mutual Exclusion cannot be solved by–transient communication (cell phones)– interrupts (cans)

•It can be solved by– one-bit shared variables – that can be read or written

Page 67: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

69

Add Mutex Herepublic class Counter { private long value;

public long getAndIncrement() { mutex.begin() temp = value; value = temp + 1; mutex.end() return temp; }}

Page 68: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

70

Not Here

int counter = new Counter(1); void primePrint { long j = 0; while (j < 1010) { mutex.begin() j = counter.getAndIncrement(); if (isPrime(j)) print(j); mutex.end() }}

Page 69: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 71

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

• Amdahl’s law: this relation is not linear…

Page 70: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 72

Amdahl’s Law

OldExecutionTimeNewExecutionTimeSpeedup=

…of computation given n CPUs instead of 1

Page 71: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 73

Amdahl’s Law

p

pn

1

1Speedup=

Page 72: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 74

Amdahl’s Law

p

pn

1

1Speedup=

Parallel fraction

Page 73: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 75

Amdahl’s Law

p

pn

1

1Speedup=

Parallel fraction

Sequential fraction

Page 74: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 76

Amdahl’s Law

p

pn

1

1Speedup=

Parallel fraction

Number of

processors

Sequential fraction

Page 75: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

77

Example

• Ten processors• 60% concurrent, 40% sequential• How close to 10-fold speedup?

Page 76: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

78

Example

• Ten processors• 60% concurrent, 40% sequential• How close to 10-fold speedup?

106.0

6.01

1

Speedup = 2.17=

Page 77: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

79

Example

• Ten processors• 80% concurrent, 20% sequential• How close to 10-fold speedup?

Page 78: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

80

Example

• Ten processors• 80% concurrent, 20% sequential• How close to 10-fold speedup?

108.0

8.01

1

Speedup = 3.57=

Page 79: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

81

Example

• Ten processors• 90% concurrent, 10% sequential• How close to 10-fold speedup?

Page 80: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

82

Example

• Ten processors• 90% concurrent, 10% sequential• How close to 10-fold speedup?

109.0

9.01

1

Speedup = 5.26=

Page 81: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

83

Example

• Ten processors• 99% concurrent, 01% sequential• How close to 10-fold speedup?

Page 82: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

84

Example

• Ten processors• 99% concurrent, 01% sequential• How close to 10-fold speedup?

1099.0

99.01

1

Speedup = 9.17=

Page 83: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 85

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

Page 84: Multicore Programming Nir Shavit Tel Aviv University

Back to Real-World Multicore Scaling

86

1.8x1.8x 2x2x 2.9x2.9x

User code

Multicore

Speedup

Must not be managing to reduce sequential % of code

Page 85: Multicore Programming Nir Shavit Tel Aviv University

A next generation Intel machine… You pay for N = 8 cores SequentialPart = 25%

Amhal Speedup = only 2.9 times!

In Our Example

As num cores grows the effect of 25% sequential becomes more accute 42.3, 82.9, 16 3.4, 323.7….

Page 86: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming 89

Multicore Computing

• This is what this my research is about…

• Inventing tools and techniques that simplify the task of parallelizing the % that is not easy to make concurrent yet may have a large impact on overall speedup

Page 87: Multicore Programming Nir Shavit Tel Aviv University

Art of Multiprocessor Programming

90

         This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.

• You are free:– to Share — to copy, distribute and transmit the work – to Remix — to adapt the work

• Under the following conditions:– Attribution. You must attribute the work to “The Art of

Multiprocessor Programming” (but not in any way that suggests that the authors endorse you or your use of the work).

– Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license.

• For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to– http://creativecommons.org/licenses/by-sa/3.0/.

• Any of the above conditions can be waived if you get permission from the copyright holder.

• Nothing in this license impairs or restricts the author's moral rights.