the art of multiprocessor programming nir shavit, ori shalev cs 0368-4061-01 spring 2007 (based on...

117
“The Art of Multiprocessor Programming” Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

Upload: raymond-sowell

Post on 29-Mar-2015

220 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

“The Art of Multiprocessor Programming”

Nir Shavit, Ori ShalevCS 0368-4061-01

Spring 2007

(Based on the book by Herlihy and Shavit)

Page 2: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 2

Requirements

• Required readings• 6 problem sets

– 20% of grade– See submission instructions later– Late loses 10%/day

• Final exam

Page 3: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 3

From the New York Times …

SAN FRANCISCO, May 7. 2004 - Intel said on Friday that it was scrapping its development of two microprocessors, a move that is a shift in the company's business strategy….

Page 4: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 4

Moore’s Law

Clock speed

flattening sharply

Transistor count still

rising

Page 5: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 5

On Your Desktop: The Uniprocesor

memory

cpu

Page 6: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 6

In the Enterprise: The Shared Memory

Multiprocessor(SMP)

cache

BusBus

shared memory

cachecache

Page 7: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 7

Your New Desktop: The Multicore processor

(CMP)

cache

BusBus

shared memory

cachecacheAll on the same chip

Sun T2000Niagara

Page 8: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 8

Multicores Are Here

• “Intel's Intel ups ante with 4-core chip. New microprocessor, due this year, will be faster, use less electricity...” [San Fran Chronicle]

• “AMD will launch a dual-core version of its Opteron server processor at an event in New York on April 21.” [PC World]

• “Sun’s Niagara…will have eight cores, each core capable of running 4 threads in parallel, for 32 concurrently running threads. ….” [The Inquierer]

Page 9: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 9

Why do we care?

• Time no longer cures software bloat– The “free ride” is over

• When you double your program’s path length– You can’t just wait 6 months– Your software must somehow exploit

twice as much concurrency

Page 10: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 10

Traditional Scaling Process

User code

TraditionalUniprocessor

Speedup1.8x1.8x

7x7x

3.6x3.6x

Time: Moore’s law

Page 11: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 11

Multicore Scaling Process

User code

Multicore

Speedup 1.8x1.8x

7x7x

3.6x3.6x

Unfortunately, not so simple…

Page 12: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 12

Real-World Scaling Process

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

User code

Multicore

Speedup

Parallelization and Synchronization require great care…

Page 13: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 13

Multiprocessor Programming: Course

Overview• Fundamentals

– Models, algorithms, impossibility

• Real-World programming– Architectures– Techniques

Page 14: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 14

Sequential Computation

memory

object object

thread

Page 15: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 15

Concurrent Computation

memory

object object

thre

ads

Page 16: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 16

Asynchrony

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

long)

Page 17: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 17

Model Summary

• Multiple threads– Sometimes called processes

• Single shared memory• Objects live in memory• Unpredictable asynchronous

delays

Page 18: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 18

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”

Page 19: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 19

Concurrency Jargon

• Hardware– Processors

• Software– Threads, processes

• Sometimes OK to confuse them, sometimes not.

Page 20: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 20

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 21: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 21

Load Balancing

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

…109 10102·1091

P0 P1 P9

Page 22: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 22

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 23: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 23

Issues

• Larger numbers imply fewer primes

• Yet larger numbers harder to test• Thread workloads

– Uneven– Hard to predict

• Need dynamic load balancing

Page 24: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 24

Issues

• Larger Num ranges have fewer primes

• Larger numbers harder to test• Thread workloads

– Uneven– Hard to predict

• Need dynamic load balancing

rejected

Page 25: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 25

17

18

19

Shared Counter

each thread takes a number

Page 26: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 26

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 27: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 27

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 28: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 28

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 29: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 29

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 30: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 30

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 31: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 31

Counter Implementation

public class Counter { private long value;

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

Page 32: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 32

Counter Implementation

public class Counter { private long value;

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

not for multiprocessor

Page 33: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 33

What It Means

public class Counter { private long value;

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

Page 34: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 34

What It Means

public class Counter { private long value;

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

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

Page 35: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 35

time

Not so good…

Value… 1

read 1

read 1

write 2

read 2

write 3

write 2

2 3 2

Page 36: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 36

Is this problem inherent?

If we could only glue reads and writes…

read

write read

write

Page 37: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 37

Challenge

public class Counter { private long value;

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

Page 38: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 38

Challenge

public class Counter { private long value;

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

Make these steps atomic (indivisible)

Page 39: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 39

Hardware Solution

public class Counter { private long value;

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

instruction

Page 40: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 40

An Aside: Java™

public class Counter { private long value;

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

Page 41: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 41

An Aside: Java™

public class Counter { private long value;

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

Synchronized block

Page 42: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 42

An Aside: Java™

public class Counter { private long value;

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

Mutual Exclusion

Page 43: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 43

Mutual Exclusion or “Alice & Bob share a pond”

A B

Page 44: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 44

Alice has a pet

A B

Page 45: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 45

Bob has a pet

A B

Page 46: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 46

The Problem

A B

The pets don’tget along

Page 47: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 47

Formalizing the Problem

• Two types of formal properties in asynchronous computation:

• Safety Properties– Nothing bad happens ever

• Liveness Properties – Something good happens eventually

Page 48: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 48

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 49: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 49

Simple Protocol

• Idea– Just look at the pond

• Gotcha– Trees obscure the view

Page 50: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 50

Interpretation

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

• Explicit communication required for coordination

Page 51: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 51

Cell Phone Protocol

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

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

Page 52: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 52

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 53: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 53

Can Protocol

cola

cola

Page 54: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 54

Bob conveys a bit

A B

cola

Page 55: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 55

Bob conveys a bit

A B

cola

Page 56: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 56

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 57: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 57

Interpretation

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

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

inturrupt bits

Page 58: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 58

Flag Protocol

A B

Page 59: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 59

Alice’s Protocol (sort of)

A B

Page 60: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 60

Bob’s Protocol (sort of)

A B

Page 61: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 61

Alice’s Protocol

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

Page 62: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 62

Bob’s Protocol

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

dang

er!

Page 63: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 63

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 64: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 64

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 65: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 65

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 66: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 66

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 67: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 67

Proof

time

Alice’s last look

Alice last raised her flag

Bob’s last looked

QED

Alice must have seen Bob’s Flag. A Contradiction

Bob last raised flag

Page 68: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 68

Proof of No Deadlock

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

Page 69: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 69

Proof of No Deadlock

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

trying to get in.

Page 70: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 70

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 71: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 71

Remarks

• Protocol is unfair– Bob’s pet might never get in

• Protocol uses waiting– If Bob is eaten by his pet, Alice’s pet

might never get in

Page 72: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 72

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 73: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 73

The Arbiter Problem (an aside)

Pick a point

Pick a point

Page 74: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 74

The Fable Continues

• Alice and Bob fall in love & marry

Page 75: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 75

The Fable Continues

• Alice and Bob fall in love & marry• Then they fall out of love & divorce

– She gets the pets– He has to feed them

• Leading to a new coordination problem: Producer-Consumer

Page 76: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 76

Bob Puts Food in the Pond

A

Page 77: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 77

mmm…

Alice releases her pets to Feed

Bmmm…

Page 78: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 78

Producer/Consumer

• Alice and Bob can’t meet– Each has restraining order on other– So he puts food in the pond– And later, she releases the pets

• Avoid– Releasing pets when there’s no food– Putting out food if uneaten food

remains

Page 79: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 79

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

Page 80: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 80

Surprise Solution

A B

cola

Page 81: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 81

Bob puts food in Pond

A B

cola

Page 82: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 82

Bob knocks over Can

A B

cola

Page 83: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 83

Alice Releases Pets

A B

cola

yum… Byum…

Page 84: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 84

Alice Resets Can when Pets are Fed

A B

cola

Page 85: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 85

Pseudocode

while (true) { while (can.isUp()){}; pet.release(); pet.recapture(); can.reset();}

Alice’s code

Page 86: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 86

Pseudocode

while (true) { while (can.isUp()){}; pet.release(); pet.recapture(); can.reset();}

Alice’s code

while (true) { while (can.isDown()){}; pond.stockWithFood(); can.knockOver();}

Bob’s code

Page 87: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 87

Correctness

• Mutual Exclusion– Pets and Bob never together in pond

Page 88: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 88

Correctness• Mutual Exclusion

– Pets and Bob never together in pond

• No Starvationif Bob always willing to feed, and pets

always famished, then pets eat infinitely often.

Page 89: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 89

Correctness

• Mutual Exclusion– Pets and Bob never together in pond

• No Starvationif Bob always willing to feed, and pets

always famished, then pets eat infinitely often.

• Producer/ConsumerThe pets never enter pond unless there

is food, and Bob never provides food if there is unconsumed food.

safety

liveness

safety

Page 90: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 90

Could Also Solve Using Flags

A B

Page 91: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 91

Waiting

• Both solutions use waiting– while(mumble){}

• Waiting is problematic– If one participant is delayed– So is everyone else– But delays are common &

unpredictable

Page 92: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 92

The Fable drags on …

• Bob and Alice still have issues

Page 93: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 93

The Fable drags on …

• Bob and Alice still have issues• So they need to communicate

Page 94: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 94

The Fable drags on …

• Bob and Alice still have issues• So they need to communicate• So they agree to use billboards …

Page 95: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 95

E1

D2C

3

Billboards are Large

B3A

1

LetterTiles

From Scrabble™ box

Page 96: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 96

E1

D2C

3

Write One Letter at a Time …

B3A

1

W4A

1S

1

H4

Page 97: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 97

To post a message

W4A

1S

1H

4A

1C

3R

1T

1H

4E

1

whew

Page 98: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 98

S1

Let’s send another mesage

S1E

1L

1L

1L

1V

4

L1 A

1

M3

A1

A1

P3

Page 99: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 99

Uh-Oh

A1

C3

R1

T1H

4E

1S

1E

1L

1L

1

L1

OK

Page 100: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 100

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

Page 101: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 101

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

Page 102: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 102

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 103: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 103

Amdahl’s Law

OldExecutionTimeNewExecutionTimeSpeedup=

…of computation given n CPUs instead of 1

Page 104: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 104

Amdahl’s Law

p

pn

1

1Speedup=

Page 105: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 105

Amdahl’s Law

p

pn

1

1Speedup=

Parallel fraction

Page 106: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 106

Amdahl’s Law

p

pn

1

1Speedup=

Parallel fraction

Sequential fraction

Page 107: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 107

Amdahl’s Law

p

pn

1

1Speedup=

Parallel fraction

Number of

processors

Sequential fraction

Page 108: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 108

Example

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

Page 109: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 109

Example

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

106.0

6.01

1

Speedup=2.17=

Page 110: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 110

Example

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

Page 111: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 111

Example

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

108.0

8.01

1

Speedup=3.57=

Page 112: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 112

Example

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

Page 113: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 113

Example

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

109.0

9.01

1

Speedup=5.26=

Page 114: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 114

Example

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

Page 115: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 115

Example

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

1099.0

99.01

1

Speedup=9.17=

Page 116: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 116

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 117: The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS 0368-4061-01 Spring 2007 (Based on the book by Herlihy and Shavit)

© 2006 Herlihy and Shavit 117

Multiprocessor 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– Homework Assignment 1 will be

waiting for you on the course web page www.cs.tau.ac.il/~multi tomorrow.