62
Programming Paradigms for Concurrency Lecture 8 – Transactional Memory 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 Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

Programming Paradigms for Concurrency Lecture 8 – Transactional Memory

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 Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

2

Beyond the State of the Art

So far, we covered...

Best practices …

New and clever ideas …

And common-sense observations.

Page 3: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

3

So far, we covered ...

Best practices …

New and clever ideas …

And common-sense observations.

Nevertheless …

Concurrent programming is still too hard …

Next, we explore why this is ….

and what we can do about it.

Beyond the State of the Art

Page 4: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

4

Locking

Page 5: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

5

Coarse-Grained Locking

Easily made correct …

But not scalable.

Page 6: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

6

Fine-Grained Locking

Can be tricky …

Page 7: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

7

Locks are not Robust

If a thread holding

a lock is delayed …

No one else can

make progress

Page 8: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

Locking Relies on Conventions

• Relation between

– Lock bit and object bits

– Exists only in programmer’s mind

/*

* When a locked buffer is visible to the I/O layer

* BH_Launder is set. This means before unlocking

* we must clear BH_Launder,mb() on alpha and then

* clear BH_Lock, so no reader can see BH_Launder set

* on an unlocked buffer and then risk to deadlock.

*/

Actual comment

from Linux Kernel (hat tip: Bradley Kuszmaul)

Page 9: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

9

Simple Problems are hard

enq(x) enq(y) double-ended queue

No interference if

ends “far apart” Interference OK if

queue is small Clean solution is

publishable result: [Michael & Scott PODC 97]

Page 10: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

10

Locks Not Composable

Transfer item from one

queue to another Must be atomic :

No duplicate or missing items

Page 11: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

11

Locks Not Composable

Lock source

Lock target

Unlock source

& target

Page 12: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

12

Locks Not Composable

Lock source

Lock target

Unlock source &

target

Methods cannot provide

internal synchronization Objects must expose

locking protocols to clients

Clients must devise and

follow protocols Abstraction broken!

Page 13: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

13

Monitor Wait and Signal

zzz

Empty

buffer Yes!

If buffer is empty,

wait for item to show up

Page 14: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

14

Wait and Signal do not Compose

empty

empty zzz… Wait for either?

Page 15: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

15 15

The Transactional Manifesto

• Current practice inadequate

– to meet the multicore challenge

• Alternative Programming Paradigm

– Replace locking with a transactional API

– Design languages or libraries

– Implement efficient run-times

Page 16: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

16 16

Transactions

Block of code ….

Atomic: appears to happen

instantaneously

Serializable: all appear to

happen in one-at-a-time

order Commit: takes effect

(atomically)

Abort: has no effect

(typically restarted)

Page 17: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

17 17

atomic { x.remove(3); y.add(3); } atomic { y = null; }

Atomic Blocks

Page 18: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

18 18

atomic { x.remove(3); y.add(3); } atomic { y = null; }

Atomic Blocks

No data race

Page 19: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

19 19

public void LeftEnq(item x) { Qnode q = new Qnode(x); q.left = this.left; this.left.right = q; this.left = q; }

A Double-Ended Queue

Write sequential Code

Page 20: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

20 20

public void LeftEnq(item x) atomic { Qnode q = new Qnode(x); q.left = this.left; this.left.right = q; this.left = q; } }

A Double-Ended Queue

Page 21: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

21 21

public void LeftEnq(item x) { atomic { Qnode q = new Qnode(x); q.left = this.left; this.left.right = q; this.left = q; } }

A Double-Ended Queue

Enclose in atomic block

Page 22: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

22 22

Warning

• Not always this simple

– Conditional waits

– Enhanced concurrency

– Complex patterns

• But often it is…

Page 23: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

23

Composition?

Page 24: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

24

Composition?

public void Transfer(Queue<T> q1, q2) { atomic { T x = q1.deq(); q2.enq(x); } }

Trivial or what?

Page 25: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

25 25

public T LeftDeq() { atomic { if (this.left == null) retry; … } }

Conditional Waiting

Roll back transaction

and restart when

something changes

Page 26: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

26 26

Composable Conditional Waiting

atomic { x = q1.deq(); } orElse { x = q2.deq(); }

Run 1st method. If it retries … Run 2nd method. If it retries …

Entire statement retries

Page 27: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

27

Simple Lock-Based STM

• STMs come in different forms

– Lock-based

– Lock-free

• Here : a simple lock-based STM

Page 28: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

28

Synchronization

• Transaction keeps

– Read set: locations & values read

– Write set: locations & values to be written

• Deferred update

– Changes installed at commit

• Lazy conflict detection

– Conflicts detected at commit

Page 29: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

29 29

STM: Transactional Locking

Map

Application

Memory

V#

V#

V#

Array of

version #s &

locks

Page 30: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

30 30

Reading an Object Mem

Locks

V#

V#

V#

V#

V#

Add version numbers

& values to read set

Page 31: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

31 31

To Write an Object Mem

Locks

V#

V#

V#

V#

V#

Add version numbers &

new values to write set

Page 32: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

32 32

To Commit Mem

Locks

V#

V#

V#

V#

V#

X

Y

V#+1

V#+1

Acquire write locks

Check version numbers

unchanged Install new values

Increment version numbers

Unlock.

Page 33: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

Problem: Internal

Inconsistency

• A Zombie is an active transaction destined to

abort.

• If Zombies see inconsistent states bad things

can happen

Page 34: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

34

Internal Consistency

x

y

4

2

8

4

Invariant: x = 2y

Transaction A: reads x = 4

Transaction B: writes

8 to x, 4 to y, aborts A )

Transaction A: (zombie)

reads y = 4

computes 1/(x-y)

Divide by zero FAIL!

Page 35: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

35

Solution: The Global Clock

• Have one shared global clock

• Incremented by (small subset of) writing

transactions

• Read by all transactions

• Used to validate that state worked on is

always consistent

Page 36: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

100

36 36

Read-Only Transactions Mem

Locks

12

32

56

19

17

100

Shared Version

Clock

Private Read

Version (RV)

Copy version clock to local

read version clock

Page 37: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

100

37 37

Read-Only Transactions Mem

Locks

12

32

56

19

17

100

Shared Version

Clock

Private Read

Version (RV)

Copy version clock to local

read version clock Read lock, version #, and

memory

Page 38: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

100

38 38

Read-Only Transactions Mem

Locks

12

32

56

19

17

100

Shared Version

Clock

Private Read

Version (RV)

Copy version clock to local

read version clock Read lock, version #, and

memory On Commit:

check unlocked &

version # unchanged

Page 39: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

39 39

Read-Only Transactions Mem

Locks

12

32

56

19

17

100

Shared Version

Clock

Private Read

Version (RV)

Copy version clock to local

read version clock Read lock, version #, and

memory On Commit:

check unlocked &

version # unchanged

Check that version #s less than

local read clock

100

Page 40: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

40 40

Read-Only Transactions Mem

Locks

12

32

56

19

17

100

Shared Version

Clock

Private Read

Version (RV)

Copy version clock to local

read version clock Read lock, version #, and

memory On Commit:

check unlocked &

version # unchanged

Check that version #s less than

local read clock

100

We have taken a snapshot without

keeping an explicit read set!

Page 41: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

100

41 41

Regular Transactions Mem

Locks

12

32

56

19

17

100

Shared Version

Clock

Private Read

Version (RV)

Copy version clock to local

read version clock

Page 42: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

100

42 42

Regular Transactions Mem

Locks

12

32

56

19

17

100

Shared Version

Clock

Private Read

Version (RV)

Copy version clock to local

read version clock On read/write, check:

Unlocked & version # < RV

Add to R/W set

Page 43: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

43 43

On Commit Mem

Locks

100

Shared Version

Clock

100

12

32

56

19

17 Private Read

Version (RV)

Acquire write locks

Page 44: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

44 44

On Commit Mem

Locks

100

Shared Version

Clock

100 101

12

32

56

19

17 Private Read

Version (RV)

Acquire write locks

Increment Version Clock

Page 45: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

45 45

On Commit Mem

Locks

100

Shared Version

Clock

100 101

12

32

56

19

17 Private Read

Version (RV)

Acquire write locks

Increment Version Clock

Check version numbers ≤ RV

Page 46: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

46 46

On Commit Mem

Locks

100

Shared Version

Clock

100 101

12

32

56

19

17 Private Read

Version (RV)

Acquire write locks

Increment Version Clock

Check version numbers ≤ RV Update memory x

y

Page 47: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

47 47

On Commit Mem

Locks

100

Shared Version

Clock

100 101

12

32

56

19

17 Private Read

Version (RV)

Acquire write locks

Increment Version Clock

Check version numbers ≤ RV Update memory

Update write version #s

x

y

100

100

Page 48: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

48

TM Design Issues

• Implementation

choices

• Language design

issues

• Semantic issues

Page 49: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

49

Granularity

• Object

– managed languages, Java, C#, Scala, …

– Easy to control interactions between

transactional & non-trans threads

• Word

– C, C++, …

– Hard to control interactions between

transactional & non-trans threads

Page 50: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

50

Direct/Deferred Update

• Deferred

– modify private copies & install on commit

– Commit requires work

– Consistency easier

• Direct

– Modify in place, roll back on abort

– Makes commit efficient

– Consistency harder

Page 51: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

51

Conflict Detection

• Eager

– Detect before conflict arises

– “Contention manager” module resolves

• Lazy

– Detect on commit/abort

• Mixed

– Eager write/write, lazy read/write …

Page 52: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

52

Conflict Detection

• Eager detection may abort transactions

that could have committed.

• Lazy detection discards more

computation.

Page 53: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

53

Contention Management &

Scheduling

• How to resolve

conflicts?

• Who moves forward

and who rolls back?

• Lots of empirical

work but formal

work in infancy

Page 54: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

54

Contention Manager Strategies

• Exponential backoff

• Priority to

– Oldest?

– Most work?

– Non-waiting?

• None Dominates

• But needed anyway Judgment of Solomon

Page 55: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

55

I/O & System Calls?

• Some I/O revocable

– Provide transaction-

safe libraries

– Undoable file

system/DB calls

• Some not

– Opening cash

drawer

– Firing missile

Page 56: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

56

I/O & System Calls

• One solution: make transaction irrevocable

– If transaction tries I/O, switch to irrevocable mode.

• There can be only one …

– Requires serial execution

• No explicit aborts

– In irrevocable transactions

Page 57: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

57

Exceptions

int i = 0;

try {

atomic {

i++;

node = new Node();

}

} catch (Exception e) {

print(i);

}

Page 58: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

58

Exceptions

int i = 0; try { atomic { i++; node = new Node(); } } catch (Exception e) { print(i); }

Throws OutOfMemoryException!

Page 59: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

59

Exceptions

int i = 0;

try {

atomic {

i++;

node = new Node();

}

} catch (Exception e) {

print(i);

}

Throws OutOfMemoryException!

What is

printed?

Page 60: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

60

Unhandled Exceptions

• Aborts transaction

– Preserves invariants

– Safer

• Commits transaction

– Like locking semantics

– What if exception object refers to values

modified in transaction?

Page 61: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

61

Nested Transactions

atomic void foo() {

bar();

}

atomic void bar() {

}

Page 62: Programming Paradigms for Concurrencywies/teaching/ppc-14/material/... · 2014. 3. 31. · Concurrent programming is still too hard … Next, we explore why this is …. and what

62

Nested Transactions

• Needed for modularity

– Who knew that cosine() contained a transaction?

• Flat nesting

– If child aborts, so does parent

• First-class nesting

– If child aborts, partial rollback of child only