126
Programming Paradigms for Concurrency Introduction Based on companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Modified by Thomas Wies New York University

Programming Paradigms for Concurrency Introduction

  • Upload
    others

  • View
    17

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Programming Paradigms for Concurrency Introduction

Programming Paradigms for Concurrency Introduction

Based on companion slides for The Art of Multiprocessor Programming

by Maurice Herlihy & Nir Shavit

Modified by Thomas Wies

New York University

TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAAA

Page 2: Programming Paradigms for Concurrency Introduction

2

Moore’s Law

Clock speed flattening

sharply

Transistor count still

rising

Page 3: Programming Paradigms for Concurrency Introduction

Moore’s Law (in practice)

3

Page 4: Programming Paradigms for Concurrency Introduction

4

Nearly Extinct: the Uniprocesor

memory

cpu

Page 5: Programming Paradigms for Concurrency Introduction

5

Endangered: The Shared Memory Multiprocessor

(SMP)

cache

Bus Bus

shared memory

cache cache

Page 6: Programming Paradigms for Concurrency Introduction

6

The New Boss: The Multicore Processor

(CMP)

cache

Bus Bus

shared memory

cache cache All on the same chip

NVidia Tegra 4 with ARM Cortex-A15

Page 7: Programming Paradigms for Concurrency Introduction

7

Why is Mooly Eden Smiling?

Page 8: Programming Paradigms for Concurrency Introduction

9

Traditional Scaling Process

User code

Traditional Uniprocessor

Speedup 1.8x

7x

3.6x

Time: Moore’s law

Page 9: Programming Paradigms for Concurrency Introduction

Ideal Scaling Process

10

User code

Multicore

Speedup 1.8x

7x

3.6x

Unfortunately, not so simple…

Page 10: Programming Paradigms for Concurrency Introduction

Actual Scaling Process

11

1.8x 2x 2.9x

User code

Multicore

Speedup

Parallelization and Synchronization require great care…

Page 11: Programming Paradigms for Concurrency Introduction

12

Course Overview

• Fundamentals – models, algorithms, impossibility

• Real-World programming – architectures

– synchronization primitives • spin-locks, monitors, barriers

– paradigms and techniques • shared memory concurrency

• transactional memories

• message passing

Page 12: Programming Paradigms for Concurrency Introduction

Languages Used

• Java

– shared memory concurrency

• Scala

– software transactional memory

– message passing (actors)

13

Page 13: Programming Paradigms for Concurrency Introduction

Administrative Issues

• Office Hours: Tue 3-4pm, or by appointment

• Office: CIWW 407

• Course web site: http://cs.nyu.edu/wies/teaching/ppc-14

• Mailing list: [email protected]

14

Page 14: Programming Paradigms for Concurrency Introduction

Grading

• Weekly Assignments: 30%

• Term Project: 30%

• Final Exam: 40%

15

Page 15: Programming Paradigms for Concurrency Introduction

16

Sequential Computation

memory

object object

thread

Page 16: Programming Paradigms for Concurrency Introduction

17

Concurrent Computation

memory

object object

Page 17: Programming Paradigms for Concurrency Introduction

18

Asynchrony

• Sudden unpredictable delays

– Cache misses (short)

– Page faults (long)

– Scheduling quantum used up (really long)

Page 18: Programming Paradigms for Concurrency Introduction

19

Model Summary

• Multiple threads

– Sometimes called processes

• Single shared memory

• Objects live in memory

• Unpredictable asynchronous delays

Page 19: Programming Paradigms for Concurrency Introduction

20

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 20: Programming Paradigms for Concurrency Introduction

Toyota Unintended Acceleration Incidents (2009-2010)

• at least one fatal accident

• more than 9 million vehicles recalled

• In a 2013 court case, unprotected critical variables in Toyota’s real-time OS were linked to the incidents

21

Page 21: Programming Paradigms for Concurrency Introduction

22

Concurrency Jargon

• Hardware

– Processors

• Software

– Threads, processes

• Sometimes OK to confuse them, sometimes not.

Page 22: Programming Paradigms for Concurrency Introduction

23

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 23: Programming Paradigms for Concurrency Introduction

24

Load Balancing

• Split the work evenly

• Each thread tests range of 109

… 109 1010 2·109 1

P0 P1 P9

Page 24: Programming Paradigms for Concurrency Introduction

25

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 25: Programming Paradigms for Concurrency Introduction

26

Issues

• Higher ranges have fewer primes

• Yet larger numbers harder to test

• Thread workloads

– Uneven

– Hard to predict

Page 26: Programming Paradigms for Concurrency Introduction

27

Issues

• Higher ranges have fewer primes

• Yet larger numbers harder to test

• Thread workloads

– Uneven

– Hard to predict

• Need dynamic load balancing

Page 27: Programming Paradigms for Concurrency Introduction

28

17

18

19

Shared Counter

each thread takes a number

Page 28: Programming Paradigms for Concurrency Introduction

29

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 29: Programming Paradigms for Concurrency Introduction

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

Shared counter object

Page 30: Programming Paradigms for Concurrency Introduction

31

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 31: Programming Paradigms for Concurrency Introduction

32

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 32: Programming Paradigms for Concurrency Introduction

33

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 33: Programming Paradigms for Concurrency Introduction

34

Counter Implementation

public class Counter {

private long value;

public long getAndIncrement() {

return value++;

}

}

Page 34: Programming Paradigms for Concurrency Introduction

35

Counter Implementation

public class Counter {

private long value;

public long getAndIncrement() {

return value++;

}

}

Page 35: Programming Paradigms for Concurrency Introduction

36

What It Means

public class Counter {

private long value;

public long getAndIncrement() {

return value++;

}

}

Page 36: Programming Paradigms for Concurrency Introduction

37

What It Means

public class Counter {

private long value;

public long getAndIncrement() {

return value++;

}

}

temp = value;

value = temp + 1;

return temp;

Page 37: Programming Paradigms for Concurrency Introduction

38

time

Not so good…

Value… 1

read 1

read 1

write 2

read 2

write 3

write 2

2 3 2

Page 38: Programming Paradigms for Concurrency Introduction

39

Is this problem inherent?

If we could only glue reads and writes together…

read

write read

write

!! !!

Page 39: Programming Paradigms for Concurrency Introduction

40

Challenge

public class Counter {

private long value;

public long getAndIncrement() {

temp = value;

value = temp + 1;

return temp;

}

}

Page 40: Programming Paradigms for Concurrency Introduction

41

Challenge

public class Counter {

private long value;

public long getAndIncrement() {

temp = value;

value = temp + 1;

return temp;

}

}

Make these steps atomic (indivisible)

Page 41: Programming Paradigms for Concurrency Introduction

42

Hardware Solution

public class Counter {

private long value;

public long getAndIncrement() {

temp = value;

value = temp + 1;

return temp;

}

} ReadModifyWrite() instruction

Page 42: Programming Paradigms for Concurrency Introduction

43

An Aside: Java™

public class Counter {

private long value;

public long getAndIncrement() {

synchronized {

temp = value;

value = temp + 1;

}

return temp;

}

}

Page 43: Programming Paradigms for Concurrency Introduction

44

An Aside: Java™

public class Counter {

private long value;

public long getAndIncrement() {

synchronized {

temp = value;

value = temp + 1;

}

return temp;

}

} Synchronized block

Page 44: Programming Paradigms for Concurrency Introduction

45

An Aside: Java™

public class Counter {

private long value;

public long getAndIncrement() {

synchronized {

temp = value;

value = temp + 1;

}

return temp;

}

}

Mutual Exclusion

Page 45: Programming Paradigms for Concurrency Introduction

46

Mutual Exclusion, or “Alice & Bob share a pond”

A B

Page 46: Programming Paradigms for Concurrency Introduction

47

Alice has a pet

A B

Page 47: Programming Paradigms for Concurrency Introduction

48

Bob has a pet

A B

Page 48: Programming Paradigms for Concurrency Introduction

49

The Problem

A B

The pets don’t get along

Page 49: Programming Paradigms for Concurrency Introduction

50

Formalizing the Problem

• Two types of formal properties in asynchronous computation:

• Safety Properties

– Nothing bad happens ever

• Liveness Properties

– Something good happens eventually

Page 50: Programming Paradigms for Concurrency Introduction

51

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 51: Programming Paradigms for Concurrency Introduction

52

Simple Protocol

• Idea

– Just look at the pond

• Gotcha

– Not atomic

– Trees obscure the view

Page 52: Programming Paradigms for Concurrency Introduction

53

Interpretation

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

• Explicit communication required for coordination

Page 53: Programming Paradigms for Concurrency Introduction

54

Cell Phone Protocol

• Idea

– Bob calls Alice (or vice-versa)

• Gotcha

– Bob takes shower

– Alice recharges battery

– Bob out shopping for pet food …

Page 54: Programming Paradigms for Concurrency Introduction

55

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 55: Programming Paradigms for Concurrency Introduction

56

Can Protocol

cola

cola

Page 56: Programming Paradigms for Concurrency Introduction

57

Bob conveys a bit

A B

cola

Page 57: Programming Paradigms for Concurrency Introduction

58

Bob conveys a bit

A B

Page 58: Programming Paradigms for Concurrency Introduction

59

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 59: Programming Paradigms for Concurrency Introduction

60

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 60: Programming Paradigms for Concurrency Introduction

61

Flag Protocol

A B

Page 61: Programming Paradigms for Concurrency Introduction

62

Alice’s Protocol (sort of)

A B

Page 62: Programming Paradigms for Concurrency Introduction

63

Bob’s Protocol (sort of)

A B

Page 63: Programming Paradigms for Concurrency Introduction

64

Alice’s Protocol

• Raise flag

• Wait until Bob’s flag is down

• Unleash pet

• Lower flag when pet returns

Page 64: Programming Paradigms for Concurrency Introduction

65

Bob’s Protocol

• Raise flag

• Wait until Alice’s flag is down

• Unleash pet

• Lower flag when pet returns

Page 65: Programming Paradigms for Concurrency Introduction

66

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 66: Programming Paradigms for Concurrency Introduction

67

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 67: Programming Paradigms for Concurrency Introduction

68

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 68: Programming Paradigms for Concurrency Introduction

69

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 69: Programming Paradigms for Concurrency Introduction

70

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 70: Programming Paradigms for Concurrency Introduction

71

Proof of No Deadlock

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

Page 71: Programming Paradigms for Concurrency Introduction

72

Proof of No Deadlock

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

• Deadlock requires both continually trying to get in.

Page 72: Programming Paradigms for Concurrency Introduction

73

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 73: Programming Paradigms for Concurrency Introduction

74

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 74: Programming Paradigms for Concurrency Introduction

75

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 75: Programming Paradigms for Concurrency Introduction

76

The Arbiter Problem (an aside)

Pick a point

Pick a point

Page 76: Programming Paradigms for Concurrency Introduction

77

The Fable Continues

• Alice and Bob fall in love & marry

Page 77: Programming Paradigms for Concurrency Introduction

78

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 78: Programming Paradigms for Concurrency Introduction

79

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 79: Programming Paradigms for Concurrency Introduction

80

Bob Puts Food in the Pond

A

Page 80: Programming Paradigms for Concurrency Introduction

81

mmm…

Alice releases her pets to Feed

B mmm…

Page 81: Programming Paradigms for Concurrency Introduction

82

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 82: Programming Paradigms for Concurrency Introduction

83

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 83: Programming Paradigms for Concurrency Introduction

84

Surprise Solution

A B

cola

Page 84: Programming Paradigms for Concurrency Introduction

85

Bob puts food in Pond

A B

cola

Page 85: Programming Paradigms for Concurrency Introduction

86

Bob knocks over Can

A B

Page 86: Programming Paradigms for Concurrency Introduction

87

Alice Releases Pets

A B yum… B yum…

Page 87: Programming Paradigms for Concurrency Introduction

88

Alice Resets Can when Pets are Fed

A B

cola

Page 88: Programming Paradigms for Concurrency Introduction

89

Pseudocode

while (true) {

while (can.isUp()){};

pet.release();

pet.recapture();

can.reset();

}

Alice’s code

Page 89: Programming Paradigms for Concurrency Introduction

90

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 90: Programming Paradigms for Concurrency Introduction

91

Correctness

• Mutual Exclusion

– Pets and Bob never together in pond

Page 91: Programming Paradigms for Concurrency Introduction

92

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 92: Programming Paradigms for Concurrency Introduction

93

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 93: Programming Paradigms for Concurrency Introduction

94

Could Also Solve Using Flags

A B

Page 94: Programming Paradigms for Concurrency Introduction

95

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 95: Programming Paradigms for Concurrency Introduction

96

The Fable drags on …

• Bob and Alice still have issues

Page 96: Programming Paradigms for Concurrency Introduction

97

The Fable drags on …

• Bob and Alice still have issues

• So they need to communicate

Page 97: Programming Paradigms for Concurrency Introduction

98

The Fable drags on …

• Bob and Alice still have issues

• So they need to communicate

• They agree to use billboards …

Page 98: Programming Paradigms for Concurrency Introduction

99

E 1

D 2

C 3

Billboards are Large

B 3 A

1

Letter Tiles

From Scrabble™ box

Page 99: Programming Paradigms for Concurrency Introduction

100

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 100: Programming Paradigms for Concurrency Introduction

101

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 101: Programming Paradigms for Concurrency Introduction

102

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 102: Programming Paradigms for Concurrency Introduction

103

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 103: Programming Paradigms for Concurrency Introduction

104

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 104: Programming Paradigms for Concurrency Introduction

105

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 105: Programming Paradigms for Concurrency Introduction

106

Esoteric?

• Java container size() method

• Single shared counter?

– incremented with each add() and

– decremented with each remove()

• Threads wait to exclusively access counter

Page 106: Programming Paradigms for Concurrency Introduction

107

Readers/Writers Solution

• Each thread i has size[i] counter

– only it increments or decrements.

• To get object’s size, a thread reads a “snapshot” of all counters

• This eliminates the bottleneck

Page 107: Programming Paradigms for Concurrency Introduction

108

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 108: Programming Paradigms for Concurrency Introduction

109

Amdahl’s Law

Speedup= 1-thread execution time

n-thread execution time

Page 109: Programming Paradigms for Concurrency Introduction

110

Amdahl’s Law

Speedup= 1

1¡ p+ p

n

Page 110: Programming Paradigms for Concurrency Introduction

1

1¡ p+ p

n

111

Amdahl’s Law

Speedup=

Parallel fraction

Page 111: Programming Paradigms for Concurrency Introduction

1

1¡ p+ p

n

112

Amdahl’s Law

Speedup=

Parallel fraction

Sequential fraction

Page 112: Programming Paradigms for Concurrency Introduction

1

1¡ p+ p

n

113

Amdahl’s Law

Speedup=

Parallel fraction

Sequential fraction

Number of threads

Page 113: Programming Paradigms for Concurrency Introduction

Amdahl’s Law (in practice)

114

Page 114: Programming Paradigms for Concurrency Introduction

115

Example

• Ten processors

• 60% concurrent, 40% sequential

• How close to 10-fold speedup?

Page 115: Programming Paradigms for Concurrency Introduction

116

Example

• Ten processors

• 60% concurrent, 40% sequential

• How close to 10-fold speedup?

10

6.06.01

1

Speedup = 2.17=

Page 116: Programming Paradigms for Concurrency Introduction

117

Example

• Ten processors

• 80% concurrent, 20% sequential

• How close to 10-fold speedup?

Page 117: Programming Paradigms for Concurrency Introduction

118

Example

• Ten processors

• 80% concurrent, 20% sequential

• How close to 10-fold speedup?

10

8.08.01

1

Speedup = 3.57=

Page 118: Programming Paradigms for Concurrency Introduction

119

Example

• Ten processors

• 90% concurrent, 10% sequential

• How close to 10-fold speedup?

Page 119: Programming Paradigms for Concurrency Introduction

120

Example

• Ten processors

• 90% concurrent, 10% sequential

• How close to 10-fold speedup?

10

9.09.01

1

Speedup = 5.26=

Page 120: Programming Paradigms for Concurrency Introduction

121

Example

• Ten processors

• 99% concurrent, 01% sequential

• How close to 10-fold speedup?

Page 121: Programming Paradigms for Concurrency Introduction

122

Example

• Ten processors

• 99% concurrent, 01% sequential

• How close to 10-fold speedup?

10

99.099.01

1

Speedup = 9.17=

Page 122: Programming Paradigms for Concurrency Introduction

Back to Real-World Multicore Scaling

1.8x 2x 2.9x

User code

Multicore

Speedup

Not reducing sequential % of code

123

Page 123: Programming Paradigms for Concurrency Introduction

Shared Data Structures

75% Unshared

25% Shared

Coarse Grained

Fine Grained

75% Unshared

25% Shared

124

Page 124: Programming Paradigms for Concurrency Introduction

Shared Data Structures

75% Unshared

25% Shared

Coarse Grained

Fine Grained

Why only 2.9 speedup

75% Unshared

25% Shared

Honk! Honk!

Honk!

125

Page 125: Programming Paradigms for Concurrency Introduction

Shared Data Structures

75% Unshared

25% Shared

Coarse Grained

Fine Grained

Why fine-grained parallelism maters

75% Unshared

25% Shared

Honk! Honk!

Honk!

126

Page 126: Programming Paradigms for Concurrency Introduction

127

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.