Top Banner
Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit
58

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

Dec 16, 2015

Download

Documents

Skylar Howison
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Introduction

Companion slides forThe Art of Multiprocessor

Programmingby Maurice Herlihy & Nir Shavit

Page 2: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming 2

Moore’s Law

Clock speed

flattening sharply

Transistor count still

rising

Page 3: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming 3

Still on some of your desktops: The Uniprocesor

memory

cpu

Page 4: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming 4

In the Enterprise: The Shared Memory

Multiprocessor(SMP)

cache

BusBus

shared memory

cachecache

Page 5: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming 5

Your New Desktop: The Multicore Processor

(CMP)

cache

BusBus

shared memory

cachecacheAll on the same chip

Sun T2000Niagara

Page 6: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming 6

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 7: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

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: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming 8

Traditional Scaling Process

User code

TraditionalUniprocessor

Speedup1.8x1.8x

7x7x

3.6x3.6x

Time: Moore’s law

Page 9: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming 9

Multicore Scaling Process

User code

Multicore

Speedup 1.8x1.8x

7x7x

3.6x3.6x

Unfortunately, not so simple…

Page 10: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming 10

Real-World Scaling Process

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

User code

Multicore

Speedup

Parallelization and Synchronization require great care…

Page 11: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming 11

Multicore Programming: Course Overview

• Fundamentals– Models, algorithms, impossibility

• Real-World programming– Architectures– Techniques

Page 12: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming 12

Multicore Programming: Course Overview

• Fundamentals– Models, algorithms, impossibility

• Real-World programming– Architectures– Techniques

We don’t necessarily

want to m

ake

you experts…

Page 13: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

13

Sequential Computation

memory

object object

thread

Page 14: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

14

Concurrent Computation

memory

object object

thre

ads

Page 15: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming 15

Asynchrony

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

long)

Page 16: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming 16

Model Summary

• Multiple threads– Sometimes called processes

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

delays

Page 17: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

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: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming 18

Concurrency Jargon

• Hardware– Processors

• Software– Threads, processes

• Sometimes OK to confuse them, sometimes not.

Page 19: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

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: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming 20

Load Balancing

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

…109 10102·1091

P0 P1 P9

Page 21: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

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: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

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: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

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 balancingre

jected

Page 24: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

24

17

18

19

Shared Counter

each thread takes a number

Page 25: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

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: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

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 counterobject

Page 27: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming 27

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 28: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

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: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

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: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

30

Counter Implementation

public class Counter { private long value;

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

Page 31: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

31

Counter Implementation

public class Counter { private long value;

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

not for concurrent threads

Page 32: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

32

What It Means

public class Counter { private long value;

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

Page 33: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

33

What It Means

public class Counter { private long value;

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

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

Page 34: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

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: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

35

Is this problem inherent?

If we could only glue reads and writes…

read

write read

write

Page 36: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

36

Challenge

public class Counter { private long value;

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

Page 37: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

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: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

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: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

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: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

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: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

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: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming 42

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 43: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming 43

Amdahl’s Law

OldExecutionTimeNewExecutionTimeSpeedup=

…of computation given n CPUs instead of 1

Page 44: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming 44

Amdahl’s Law

p

pn

1

1Speedup=

Page 45: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming 45

Amdahl’s Law

p

pn

1

1Speedup=

Parallel fraction

Page 46: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming 46

Amdahl’s Law

p

pn

1

1Speedup=

Parallel fraction

Sequential fraction

Page 47: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming 47

Amdahl’s Law

p

pn

1

1Speedup=

Parallel fraction

Number of

processors

Sequential fraction

Page 48: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

48

Example

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

Page 49: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

49

Example

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

106.0

6.01

1

Speedup=2.17=

Page 50: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

50

Example

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

Page 51: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

51

Example

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

108.0

8.01

1

Speedup=3.57=

Page 52: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

52

Example

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

Page 53: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

53

Example

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

109.0

9.01

1

Speedup=5.26=

Page 54: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

54

Example

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

Page 55: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

55

Example

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

1099.0

99.01

1

Speedup=9.17=

Page 56: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming 56

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 57: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming 57

Multicore Programming

• This is what this course is about… – The % that is not easy to make

concurrent yet may have a large impact on overall speedup

• Next week: – A more serious look at mutual

exclusion

Page 58: Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

58

         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.