117
Art of Multiprocessor Programming Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming

Introduction

Companion slides for The Art of Multiprocessor

Programming by Maurice Herlihy & Nir Shavit

Page 2: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 2

Moore’s Law

Clock speed flattening

sharply

Transistor count still

rising

Page 3: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 3

Vanishing from your Desktops: The Uniprocesor

memory

cpu

Page 4: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 4

Your Server: The Shared Memory Multiprocessor

(SMP)

cache

Bus Bus

shared memory

cache cache

Page 5: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 5

Your New Server or Desktop: The Multicore Processor

(CMP)

cache Bus Bus

shared memory

cache cache All on the same chip

Sun T2000 Niagara

Page 6: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 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: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 7

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 8: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 8

Traditional Scaling Process

User code

Traditional Uniprocessor

Speedup 1.8x

7x 3.6x

Time: Moore’s law

Page 9: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 9

Multicore Scaling Process

User code

Multicore

Speedup 1.8x

7x 3.6x

Unfortunately, not so simple…

Page 10: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 10

Real-World Scaling Process

1.8x 2x 2.9x

User code

Multicore

Speedup

Parallelization and Synchronization require great care…

Page 11: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 11

Multicore Programming: Course Overview

•  Fundamentals – Models, algorithms, impossibility

•  Real-World programming – Architectures – Techniques

Page 12: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 12

Multicore Programming: Course Overview

•  Fundamentals – Models, algorithms, impossibility

•  Real-World programming – Architectures – Techniques

Page 13: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 13

Sequential Computation

memory

object object

thread

Page 14: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 14

Concurrent Computation

memory

object object

Page 15: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 15

Asynchrony

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

Page 16: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 16

Model Summary •  Multiple threads

–  Sometimes called processes •  Single shared memory •  Objects live in memory •  Unpredictable asynchronous delays

Page 17: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 17

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 18: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 18

Concurrency Jargon •  Hardware

–  Processors •  Software

–  Threads, processes •  Sometimes OK to confuse them,

sometimes not.

Page 19: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 19

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 20: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 20

Load Balancing

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

… 109 1010 2·109 1

P0 P1 P9

Page 21: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 21

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 22: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 22

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

–  Uneven – Hard to predict

Page 23: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 23

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

–  Uneven – Hard to predict

•  Need dynamic load balancing

Page 24: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 24

17

18

19

Shared Counter

each thread takes a number

Page 25: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 25

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 26: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

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

Shared counter object

Page 27: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 27

Where Things Reside

cache

Bus Bus

cache cache

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 28: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 28

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 29: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 29

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 30: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 30

Counter Implementation

public class Counter { private long value; public long getAndIncrement() { return value++; } }

Page 31: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 31

Counter Implementation

public class Counter { private long value; public long getAndIncrement() { return value++; } } OK for

single thread

,

not for conc

urrent threa

ds

Page 32: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 32

What It Means

public class Counter { private long value; public long getAndIncrement() { return value++; } }

Page 33: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 33

What It Means

public class Counter { private long value; public long getAndIncrement() { return value++; } }

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

Page 34: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 34

time

Not so good…

Value… 1

read 1

read 1

write 2

read 2

write 3

write 2

2 3 2

Page 35: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 35

Is this problem inherent?

If we could only glue reads and writes together…

read

write read

write !! !!

Page 36: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 36

Challenge

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

Page 37: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 37

Challenge

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

Make these steps atomic (indivisible)

Page 38: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 38

Hardware Solution

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

instruction

Page 39: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

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; } }

Page 40: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

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; } }

Synchronized block

Page 41: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 41

An Aside: Java™

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

Mutual Exclusion

Page 42: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 42

Mutual Exclusion or “Alice & Bob share a pond”

A B

Page 43: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 43

Alice has a pet

A B

Page 44: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 44

Bob has a pet

A B

Page 45: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 45

The Problem

A B

The pets don’t get along

Page 46: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 46

Formalizing the Problem •  Two types of formal properties in

asynchronous computation: •  Safety Properties

– Nothing bad happens ever •  Liveness Properties

–  Something good happens eventually

Page 47: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 47

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 48: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 48

Simple Protocol •  Idea

–  Just look at the pond •  Gotcha

– Not atomic –  Trees obscure the view

Page 49: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 49

Interpretation •  Threads can’t “see” what other

threads are doing •  Explicit communication required for

coordination

Page 50: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 50

Cell Phone Protocol •  Idea

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

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

Page 51: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 51

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 52: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 52

Can Protocol

cola

cola

Page 53: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 53

Bob conveys a bit

A B

cola

Page 54: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 54

Bob conveys a bit

A B

cola

Page 55: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 55

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 56: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 56

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 57: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 57

Flag Protocol

A B

Page 58: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 58

Alice’s Protocol (sort of)

A B

Page 59: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 59

Bob’s Protocol (sort of)

A B

Page 60: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 60

Alice’s Protocol

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

Page 61: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 61

Bob’s Protocol

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

Page 62: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 62

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 63: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 63

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 64: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 64

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 65: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 65

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 66: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming Art of Multiprocessor Programming

66

Proof

time

Alice’s last look

Alice last raised her flag

Bob’s last look

Alice must have seen Bob’s Flag. A Contradiction

Bob last raised flag

Page 67: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 67

Proof of No Deadlock •  If only one pet wants in, it gets in.

Page 68: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 68

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

trying to get in.

Page 69: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 69

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

Page 70: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 70

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 71: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 71

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 72: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 72

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

Page 73: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 73

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 74: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 74

Bob Puts Food in the Pond

A

Page 75: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 75

mmm…

Alice releases her pets to Feed

B mmm…

Page 76: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 76

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 77: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 77

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 78: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 78

Surprise Solution

A B

cola

Page 79: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 79

Bob puts food in Pond

A B

cola

Page 80: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 80

Bob knocks over Can

A B

cola

Page 81: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 81

Alice Releases Pets

A B

cola

yum… B yum…

Page 82: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 82

Alice Resets Can when Pets are Fed

A B

cola

Page 83: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 83

Pseudocode

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

Alice’s code

Page 84: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 84

Pseudocode

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

Alice’s code

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

Bob’s code

Page 85: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 85

Correctness •  Mutual Exclusion

–  Pets and Bob never together in pond

Page 86: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 86

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.

Page 87: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 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.

•  Producer/Consumer The pets never enter pond unless there is

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

safety

liveness

safety

Page 88: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 88

Waiting •  Both solutions use waiting

– while(mumble){} •  In some cases waiting is problematic

–  If one participant is delayed –  So is everyone else –  But delays are common & unpredictable

Page 89: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 89

The Fable drags on … •  Bob and Alice still have issues

Page 90: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 90

The Fable drags on … •  Bob and Alice still have issues •  So they need to communicate

Page 91: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 91

The Fable drags on … •  Bob and Alice still have issues •  So they need to communicate •  They agree to use billboards …

Page 92: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming Art of Multiprocessor Programming

92

E 1

D 2 C

3

Billboards are Large

B 3 A

1

Letter Tiles

From Scrabble™ box

Page 93: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 93

E 1

D 2 C

3

Write One Letter at a Time …

B 3 A

1

W 4 A

1 S

1

H 4

Page 94: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 94

To post a message

W 4 A

1 S

1 H

4 A

1 C

3 R

1 T

1 H

4 E

1

whew

Page 95: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 95

S 1

Let’s send another message

S 1 E

1 L

1 L

1 L

1 V

4

L 1 A

1

M 3

A 1

A 1

P 3

Page 96: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 96

Uh-Oh

A 1

C 3

R 1

T 1 H

4 E

1 S

1 E

1 L

1 L

1

L 1

OK

Page 97: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 97

Readers/Writers •  Devise a protocol so that

– Writer writes one letter at a time –  Reader reads one letter at a time –  Reader sees “snapshot”

• Old message or new message • No mixed messages

Page 98: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 98

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 99: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 99

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 100: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 100

Amdahl’s Law

OldExecutionTimeNewExecutionTimeSpeedup=

…of computation given n CPUs instead of 1

Page 101: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 101

Amdahl’s Law

− +ppn

1

1Speedup=

Page 102: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 102

Amdahl’s Law

− +ppn

1

1Speedup=

Parallel fraction

Page 103: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 103

Amdahl’s Law

− +ppn

1

1Speedup=

Parallel fraction

Sequential fraction

Page 104: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 104

Amdahl’s Law

− +ppn

1

1Speedup=

Parallel fraction

Number of processors

Sequential fraction

Page 105: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 105

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

Page 106: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 106

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

106.06.01

1

+−Speedup = 2.17=

Page 107: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 107

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

Page 108: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 108

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

108.08.01

1

+−Speedup = 3.57=

Page 109: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 109

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

Page 110: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 110

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

109.09.01

1

+−Speedup = 5.26=

Page 111: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 111

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

Page 112: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 112

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

1099.099.01

1

+−Speedup = 9.17=

Page 113: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming

Back to Real-World Multicore Scaling

113

1.8x 2x 2.9x

User code

Multicore

Speedup

Must not be managing to reduce sequential % of code

Page 114: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming

Back to Real-World Multicore Scaling

114

1.8x 2x 2.9x

User code

Multicore

Speedup

Not reducing sequential % of code

Page 115: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming

Diminishing Returns

0

1

2

3

4

5

4 8 16 32

infinite

speedup

Page 116: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

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: –  A more serious look at mutual exclusion

Page 117: ch01herlihy - PUC-Rionoemi/pcp-13/aula1/ch01herlihy.pdf · Title: ch01herlihy.ppt Author: Noemi Rodriguez Created Date: 8/14/2013 1:02:01 AM

Art of Multiprocessor Programming 117

          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.