it ain’t the meat, it’s the notion why theory is essential to teaching concurrent programming...

35
It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Upload: sydney-davis

Post on 19-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

It Ain’t the Meat, it’s the Notion

Why Theory is Essential to Teaching

Concurrent Programming

Maurice HerlihyBrown University

Page 2: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Multicore Programming Education

2

In many areas of CS

• Systems research fought all the hard battles– FORTRAN complexity theory– UDP/IP network algorithms

• Theory researchers arrived later to shoot the wounded

• But concurrency is different

Page 3: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Multicore Programming Education

3

Vive la Différence

• Concurrent programmers are made, not born (mostly)

• Students have little experience with concurrency (today)

• Develop intuition by– Idealized examples first– Logical reasoning

Page 4: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Multicore Programming Education

4

Homework Challenge

Devise Wait-Free 1-Enqueuer, 1-Dequeuer Buffer using only reads

& writes

Page 5: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Multicore Programming Education

5

Another Homework Challenge

How about two dequeuers?

Page 6: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Multicore Programming Education

66

Shared Memory

Atomic reads and writes to individual variables

read

write

Page 7: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Multicore Programming Education

77

Snapshot Challenge

Wait-Free algorithm to read multiple

variables atomically?

Read both

Read both

Page 8: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Multicore Programming Education

88

Snapshot Challenge

Wait-Free algorithm to

write multiple variables

atomically?writeboth

write both

Page 9: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Multicore Programming Education

99

Bakery Algorithmclass Bakery implements Lock { boolean[] flag; Label[] label; public Bakery (int n) { flag = new boolean[n]; label = new Label[n]; for (int i = 0; i < n; i++) { flag[i] = false; label[i] = 0; } } …

n-10

f f f f t ft

2

f

0 0 0 0 5 04 0

6

Critical Section

Classic, first-come-first served mutual

exclusion algorithm

Page 10: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Multicore Programming Education

10

Is Locked?isLocked(

)

Bakery Lock

True means was locked at some instant, false means was free at some instant.

Page 11: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Multicore Programming Education

11

tryLock()

Bakery Lock

Acquire lock if free & return true, otherwise

immediately return false.

I Can Has Lock?

Page 12: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Multicore Programming Education

12

Our Experience Teaching Concurrency

• Brown (10 years)• Tel-Aviv (10 years)• Industry

Page 13: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Multicore Programming Education

13

Mutual Exclusion

• Peterson, Bakery, space lower bounds

• Teaches– Deadlock– Livelock– Fairness, first-come-first-served, …

Page 14: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Multicore Programming Education

14

Linearizability

• How to describe a concurrent object?– By “equivalence” to sequential

• What is a concurrent API?• How do we reason about correctness?

– Even informally!

• Learn the rules before you break them

Page 15: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Multicore Programming Education

15

Consensus Numbers

• Certain synchronization primitives are mathematically stronger than others

• No, you can’t build lock-free X from Y– So do not waste your time trying

• Yes, you can build lock-free X from Z– The rest is optimization

Page 16: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Multicore Programming Education

16

Uh, Oh, Spin Locks

• Inexplicable in idealized model

• Well, kids …– We didn’t actually

lie to you– But the architects

gave us caches– And you have to

outwit them …

bad

less bad

Test&set

Test&test&set

Page 17: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Multicore Programming Education

17

Then What?

• Implement simple T&S lock– Dreadful performance

• Implement T&T&S lock– Improved performance

Page 18: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Multicore Programming Education

18

Homework Challenge

Devise Wait-Free 1-Enqueuer, 1-Dequeuer Buffer using only reads

& writes

Page 19: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Multicore Programming Education

19

Yes,

you ca

n

Homework Challenge

Page 20: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Multicore Programming Education

20

Another Homework Challenge

How about two dequeuers?

Page 21: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Multicore Programming Education

21

Maybe?

“hence [any wait-free object] can be written without a CAS/LL/SC. Instead LL/SC is used to make it more efficient but LL/SC

isn't *required* to make it wait-free.”

Found in a blog

Page 22: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Multicore Programming Education

22

Alas, no, you can’t

Another Homework Challenge

Page 23: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Multicore Programming Education

23

What If we have Test&Set?

Page 24: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Multicore Programming Education

24

What If we have Test&Set?

No, you still can’t

Page 25: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Multicore Programming Education

25

What If we have Compare&Swap?

Page 26: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Multicore Programming Education

26

What if We Have Compare&Sswap?

Yes,

you ca

n

Page 27: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Multicore Programming Education

2727

Snapshot Challenge

Reads from 0 and

1

Reads from 1 and

2

Wait-Free algorithm to read multiple

variables atomically?

Page 28: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Multicore Programming Education

2828

Snapshot Challenge

Reads from 0 and

1

Reads from 1 and

2

Yes,

you ca

n

Wait-Free algorithm to read multiple

variables atomically?

Page 29: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Multicore Programming Education

2929

Multiple Assignment Challenge

Writes to 0 and 1

Writes to 1 and 2

Wait-Free algorithm to

write multiple variables

atomically?

Page 30: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Multicore Programming Education

3030

Writes to 0 and 1

Writes to 1 and 2

Wait-Free algorithm to

write multiple variables

atomically?

Multiple Assignment Challenge

No, you can’t

Page 31: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Multicore Programming Education

31

Is Locked?isLocked(

)

Bakery Lock

True means was locked at some instant, false means was free at some instant.

Page 32: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Multicore Programming Education

32

isLocked()

Bakery Lock

True means was locked at some instant, false means was free at some instant.

Yes,

you ca

n

Is Locked?

Page 33: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Multicore Programming Education

33

I Can Has Lock?

tryLock()

Bakery Lock

Acquire lock if free & return true, otherwise

immediately return false.

Page 34: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Multicore Programming Education

34

tryLock()

Bakery Lock

Acquire lock if free & return true, otherwise

immediately return false.

I Can Has Lock?

No, you can’t

Page 35: It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University

Multicore Programming Education

35

My Very Last Slide

• Teaching theory first– Develops intuition– Imposes order on chaos

• Idealized problems– Prepare for realistic, complicated

ones

• Impossibility results– Save time and effort