art of multiprocessor programming - brown...

112
Transactional Memory Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Upload: others

Post on 16-Aug-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Transactional Memory

Companion slides for

The Art of Multiprocessor Programming

by Maurice Herlihy & Nir Shavit

Page 2: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

2

Our Vision for the Future

In this course, we covered ….

Best practices …

New and clever ideas …

And common-sense observations.

Art of Multiprocessor

Programming

Page 3: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

3

Our Vision for the Future

In this course, we covered ….

Best practices …

New and clever ideas …

And common-sense observations.

Nevertheless …

Concurrent programming is still too hard …

Here we explore why this is ….

And what we can do about it.

Art of Multiprocessor

Programming

Page 4: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

4

Locking

Art of Multiprocessor

Programming

Page 5: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

5

Coarse-Grained Locking

Easily made correct …

But not scalable.

Art of Multiprocessor

Programming

Page 6: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

6

Fine-Grained Locking

Can be tricky …

Art of Multiprocessor

Programming

Page 7: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

7

Locks are not Robust

If a thread holding

a lock is delayed …

No one else can

make progressArt of Multiprocessor

Programming

Page 8: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

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)

Art of Multiprocessor

Programming

Page 9: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

9

Simple Problems are hard

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

No interference if

ends “far apart”Interference OK if

queue is smallClean solution is

publishable result:[Michael & Scott PODC 97]

Art of Multiprocessor

Programming

Page 10: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

10

Locks Not Composable

Transfer item from one

queue to anotherMust be atomic :

No duplicate or missing items

Page 11: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

11

Locks Not Composable

Lock source

Lock target

Unlock source

& target

Page 12: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

12

Locks Not Composable

Lock source

Lock target

Unlock source &

target

Methods cannot provide

internal synchronizationObjects must expose

locking protocols to clients

Clients must devise and

follow protocolsAbstraction broken!

Page 13: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

13

Monitor Wait and Signal

zzz

Empty

bufferYes!

Art of Multiprocessor

Programming

If buffer is empty,

wait for item to show up

Page 14: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

14

Wait and Signal do not Compose

empty

emptyzzz…

Art of Multiprocessor

Programming

Wait for either?

Page 15: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

1515

The Transactional Manifesto

• Current practice inadequate

– to meet the multicore challenge

• Research Agenda

– Replace locking with a transactional API

– Design languages or libraries

– Implement efficient run-times

Page 16: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

1616

Transactions

Block of code ….

Atomic: appears to happen

instantaneously

Serializable: all appear to

happen in one-at-a-time

orderCommit: takes effect

(atomically)

Abort: has no effect

(typically restarted)

Page 17: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

1717

atomic {

x.remove(3);

y.add(3);

}

atomic {

y = null;

}

Atomic Blocks

Page 18: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

1818

atomic {

x.remove(3);

y.add(3);

}

atomic {

y = null;

}

Atomic Blocks

No data race

Page 19: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

1919

public void LeftEnq(item x) {

Qnode q = new Qnode(x);

q.left = left;

left.right = q;

left = q;

}

A Double-Ended Queue

Write sequential Code

Page 20: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

2020

public void LeftEnq(item x)

atomic {

Qnode q = new Qnode(x);

q.left = left;

left.right = q;

left = q;

}

}

A Double-Ended Queue

Page 21: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

2121

public void LeftEnq(item x) {

atomic {

Qnode q = new Qnode(x);

q.left = left;

left.right = q;

left = q;

}

}

A Double-Ended Queue

Enclose in atomic block

Page 22: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

2222

Warning

• Not always this simple

– Conditional waits

– Enhanced concurrency

– Complex patterns

• But often it is…

Page 23: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

23

Composition?

Page 24: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

24

Composition?

public void Transfer(Queue<T> q1, q2)

{

atomic {

T x = q1.deq();

q2.enq(x);

}

}

Trivial or what?

Page 25: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

2525

public T LeftDeq() {

atomic {

if (left == null)

retry;

}

}

Conditional Waiting

Roll back transaction

and restart when

something changes

Page 26: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

2626

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: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

2727

Hardware Transactional

Memory• Exploit Cache coherence

• Already almost does it

– Invalidation

– Consistency checking

• Speculative execution

– Branch prediction =

optimistic synch!

Page 28: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

2828

HW Transactional Memory

Interconnect

caches

memory

read active

T

Page 29: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

2929

Transactional Memory

read

activeTT

active

caches

memory

Page 30: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

3030

Transactional Memory

activeTT

activecommitted

caches

memory

Page 31: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

3131

Transactional Memorywrite

active

committed

TD caches

memory

Page 32: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

3232

Rewind

activeTT

activewriteaborted

D caches

memory

Page 33: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

3333

Transaction Commit

• At commit point

– If no cache conflicts, we win.

• Mark transactional entries

– Read-only: valid

– Modified: dirty (eventually written back)

• That’s all, folks!

– Except for a few details …

Page 34: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

34

Hardware Transactional Memory (HTM)

IBM’s Blue Gene/Q & System Z & Power8

Intel’s Haswell TSX extensions

Page 35: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

if (_xbegin() == _XBEGIN_STARTED) {

speculative code

_xend()

} else {

abort handler

}

Intel RTM

Page 36: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

if (_xbegin() == _XBEGIN_STARTED) {

speculative code

_xend()

} else {

abort handler

}

Intel RTM

start a speculative

transaction

Page 37: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

if (_xbegin() == _XBEGIN_STARTED) {

speculative code

_xend()

} else {

abort handler

}

Intel RTM

If you see this, you are

inside a transaction

Page 38: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

if (_xbegin() == _XBEGIN_STARTED) {

speculative code

_xend()

} else {

abort handler

}

Intel RTM

If you see anything else,

your transaction aborted

Page 39: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

if (_xbegin() == _XBEGIN_STARTED) {

speculative code

_xend()

} else {

abort handler

}

Intel RTM

you could retry the

transaction, or take an

alternative path

Page 40: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

if (_xbegin() == _XBEGIN_STARTED) {

speculative code

} else if (status & _XABORT_EXPLICIT) {

aborted by user code

} else if (status & _XABORT_CONFLICT) {

read-write conflict

} else if (status & _XABORT_CAPACITY) {

cache overflow

} else {

}

Abort codes

Page 41: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

if (_xbegin() == _XBEGIN_STARTED) {

speculative code

} else if (status & _XABORT_EXPLICIT) {

aborted by user code

} else if (status & _XABORT_CONFLICT) {

read-write conflict

} else if (status & _XABORT_CAPACITY) {

cache overflow

} else {

}

Abort codes

speculative code can call

_xabort()

Page 42: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

if (_xbegin() == _XBEGIN_STARTED) {

speculative code

} else if (status & _XABORT_EXPLICIT) {

aborted by user code

} else if (status & _XABORT_CONFLICT) {

read-write conflict

} else if (status & _XABORT_CAPACITY) {

cache overflow

} else {

}

Abort codes

synchronization conflict

occurred (maybe retry)

Page 43: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

if (_xbegin() == _XBEGIN_STARTED) {

speculative code

} else if (status & _XABORT_EXPLICIT) {

aborted by user code

} else if (status & _XABORT_CONFLICT) {

read-write conflict

} else if (status & _XABORT_CAPACITY) {

cache overflow

} else {

}

Abort codes

read/write set too big

(maybe don’t retry)

Page 44: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

if (_xbegin() == _XBEGIN_STARTED) {

speculative code

} else if (status & _XABORT_EXPLICIT) {

aborted by user code

} else if (status & _XABORT_CONFLICT) {

read-write conflict

} else if (status & _XABORT_CAPACITY) {

cache overflow

} else {

}

Abort codes

other abort codes …

Page 45: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Best-effort

Overflow

Unsupported

InstructionsInterrupts

Conflicts

46

Small & Medium

Transactions

Needs software fallback

RTM

Page 46: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

if (_xbegin() == _XBEGIN_STARTED) {

read lock state

if (lock taken) _xabort();

work;

_xend()

} else {

lock->lock();

work;

lock->unlock();

}

Non-Speculative Fallback

Page 47: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

if (_xbegin() == _XBEGIN_STARTED) {

read lock state

if (lock taken) _xabort();

work;

_xend()

} else {

lock->lock();

work;

lock->unlock();

}

Non-Speculative Fallback

reading lock ensures that

transaction will abort if another

thread acquires lock

Page 48: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

if (_xbegin() == _XBEGIN_STARTED) {

read lock state

if (lock taken) _xabort();

work;

_xend()

} else {

lock->lock();

work;

lock->unlock();

}

Non-Speculative Fallback

abort if another thread has

acquired lock

Page 49: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

if (_xbegin() == _XBEGIN_STARTED) {

read lock state

if (lock taken) _xabort();

work;

_xend()

} else {

lock->lock();

work;

lock->unlock();

}

Non-Speculative Fallbackon abort, acquire lock & do work

(aborting concurrent speculative

transactions)

Page 50: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

<HLE_Aquire_Prefix> Lock(L)

<HLE_Release_Prefix> Release(L)

Atomic region executed as a transaction or mutually exclusive on L

Execute optimistically, without any locks

Track Read and Write Sets

51

Abort on memory conflict: rollback acquire lock

Lock Elision

Page 51: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

52

Lock Elision

<HLE acquire prefix> lock();

do work;

<HLE release prefix> unlock()

Page 52: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

53

Lock Elision

<HLE acquire prefix> lock();

do work;

<HLE release prefix> unlock()

first time around,

read lock and

execute speculatively

Page 53: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

54

Lock Elision

<HLE acquire prefix> lock();

do work;

<HLE release prefix> unlock()

if speculation fails,

no more Mr. Nice Guy,

acquire the lock

Page 54: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Conventional Locks

55

lock transfer latencies

serialized execution

locks

Page 55: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Lock Elision

56

locks lock elision

Page 56: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Lock Teleportation

a b c d

Page 57: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Lock Teleportation

a b c dread transaction

Page 58: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Lock Teleportation

a b c dread transaction

Page 59: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Lock Teleportation

a b c d

no locks acquired

Page 60: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

6161

Not all Skittles and Beer

• Limits to

– Transactional cache size

– Scheduling quantum

• Transaction cannot commit if it is

– Too big

– Too slow

– Actual limits platform-dependent

Page 61: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

HTM Strengths &

Weaknesses

• Ideal for lock-free data structures

Page 62: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

HTM Strengths &

Weaknesses

• Ideal for lock-free data structures

• Practical proposals have limits on

– Transaction size and length

– Bounded HW resources

– Guarantees vs best-effort

Page 63: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

HTM Strengths &

Weaknesses• Ideal for lock-free data structures

• Practical proposals have limits on

– Transaction size and length

– Bounded HW resources

– Guarantees vs best-effort

• On fail

– Diagnostics essential

– Retry in software?

Page 64: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Composition

Locks don’t compose, transactions do.

Composition necessary for Software Engineering.

But practical HTM doesn’t really support

composition!Why we need STM

Page 65: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Transactional Consistency

• Memory Transactions are collections of

reads and writes executed atomically

• They should maintain consistency

– External: with respect to the interleavings

of other transactions (linearizability).

– Internal: the transaction itself should

operate on a consistent state.

Page 66: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

68

A Simple Lock-Based STM

• STMs come in different forms

– Lock-based

– Lock-free

• Here : a simple lock-based STM

• Lets start by Guaranteeing External

Consistency

Page 67: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

69

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 68: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

7070

STM: Transactional Locking

Map

Application

Memory

V#

V#

V#

Array of

version #s &

locks

Page 69: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Encounter Order Locking (Undo Log)

V# 0 V# 0

V# 0

V# 0

V# 0

V# 0

V# 0

X V# 1

V# 0 Y V# 1

V# 0 V# 0

V#+1 0

V#+1 0

V# 0

V# 0

V# 0

V#+1 0

V# 0

V# 0

V# 0

V# 0

V#+1 0

V# 0

X

Y

Undo Mem LocksRead

Check unlocked,

add to read setWrite

lock,

add value to write set,

Validate

check read version #s

increment write version #s

unlock write set

Page 70: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Commit Time Locking (Write Buff)

V# 0 V# 0

V# 0

V# 0

V# 0

V# 0

V# 0

V# 0

V# 0 V# 0

V# 0 V# 0

Mem Locks

V#+1 0

V# 0

V# 0

V# 1

V# 1

V# 1X

Y

V#+1 0

V# 1V#+1 0

V# 0

V#+1 0

V# 0

V# 0

V# 0

V# 0

V#+1 0

V# 0

X

Y

Read

In write set? unlocked?

add value to read setWrite

add value to write set,

Validate

acquire locks

check version #s unchanged

install changes,

increment vesion #s, unlock

Page 71: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Commit Time Locking (Write Buff)

1. To Read: load lock + location

2. Location in write-set? (Bloom Filter)

3. Check unlocked add to Read-Set

4. To Write: add value to write set

5. Acquire Locks

6. Validate read/write v#’s unchanged

7. Release each lock with v#+1

V# 0 V# 0

V# 0

V# 0

V# 0

V# 0

V# 0

V# 0

V# 0 V# 0

V# 0 V# 0

Mem Locks

V#+1 0

V# 0

V# 0

Hold locks for very short duration

V# 1

V# 1

V# 1X

Y

V#+1 0

V# 1V#+1 0

V# 0

V#+1 0

V# 0

V# 0

V# 0

V# 0

V#+1 0

V# 0

X

Y

Page 72: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

COM vs. ENC High Load

ENC

Hand

MCS

COM

Red-Black Tree 20% Delete 20% Update 60% Lookup

Page 73: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

COM vs. ENC Low Load

COMENC

Hand

MCS

Red-Black Tree 5% Delete 5% Update 90% Lookup

Page 74: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Problem: Internal

Inconsistency

• A Zombie is an active transaction destined to

abort.

• If Zombies see inconsistent states bad things

can happen

Page 75: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

80

Internal Consistency

x

y

4

2

8

4

Invariant: x = 2y

Transaction A: reads x = 4

Transaction B: writes

8 to x, 16 to y, aborts A )

Transaction A: (zombie)

reads y = 4

computes 1/(x-y)

Divide by zero FAIL!

Page 76: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

81

Solution: The Global Clock

(The TL2 Algorithm)

• 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 77: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

100

Art of Multiprocessor

Programming

8282

Read-Only TransactionsMem

Locks

12

32

56

19

17

100

Shared Version

Clock

Private Read

Version (RV)

Copy version clock to local

read version clock

Page 78: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

100

Art of Multiprocessor

Programming

8383

Read-Only TransactionsMem

Locks

12

32

56

19

17

100

Shared Version

Clock

Private Read

Version (RV)

Copy version clock to local

read version clockRead lock, version #, and

memory

Page 79: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

8484

Read-Only TransactionsMem

Locks

12

32

56

19

17 Private Read

Version (RV)

Copy version clock to local

read version clockRead lock, version #, and

memory, check version # less

than read clockOn Commit:

check unlocked & version #s less

than local read clock

100

Shared Version

Clock

100

Page 80: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

8585

Read-Only TransactionsMem

Locks

12

32

56

19

17

100

Shared Version

Clock

Private Read

Version (RV)

Copy version clock to local

read version clockRead lock, version #, and

memoryOn Commit:

check unlocked &

version #s less than

local read clock100

We have taken a snapshot without

keeping an explicit read set!

Page 81: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Example Execution: Read Only

Trans

1. RV Shared Version Clock

2. On Read: read lock, read mem,

read lock: check unlocked,

unchanged, and v# <= RV

3. Commit.

87 0 87 0

34 0

88 0

V# 0

44 0

V# 0

34 0

99 0 99 0

50 0 50 0

Mem Locks

Reads form a snapshot of memory.

No read set!

100 Shared Version Clock

87 0

34 0

99 0

50 0

87 0

34 0

88 0

V# 0

44 0

V# 0

99 0

50 0

100 RV

Page 82: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

100

Art of Multiprocessor

Programming

8787

Regular (Writing) TransactionsMem

Locks

12

32

56

19

17

100

Shared Version

Clock

Private Read

Version (RV)

Copy version clock to local

read version clock

Page 83: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

100

Art of Multiprocessor

Programming

8888

Regular TransactionsMem

Locks

12

32

56

19

17

100

Shared Version

Clock

Private Read

Version (RV)

Copy version clock to local

read version clockOn read/write, check:

Unlocked & version # < RV

Add to R/W set

Page 84: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

8989

On CommitMem

Locks

100

Shared Version

Clock

100

12

32

56

19

17 Private Read

Version (RV)

Acquire write locks

Page 85: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

9090

On CommitMem

Locks

100

Shared Version

Clock

100101

12

32

56

19

17 Private Read

Version (RV)

Acquire write locks

Increment Version Clock

Page 86: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

9191

On CommitMem

Locks

100

Shared Version

Clock

100101

12

32

56

19

17 Private Read

Version (RV)

Acquire write locks

Increment Version Clock

Check version numbers ≤ RV

Page 87: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

9292

On CommitMem

Locks

100

Shared Version

Clock

100101

12

32

56

19

17 Private Read

Version (RV)

Acquire write locks

Increment Version Clock

Check version numbers ≤ RVUpdate memoryx

y

Page 88: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

9393

On CommitMem

Locks

100

Shared Version

Clock

100101

12

32

56

19

17 Private Read

Version (RV)

Acquire write locks

Increment Version Clock

Check version numbers ≤ RVUpdate memory

Update write version #s

x

y

101

101

Page 89: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Example: Writing Trans

1. RV Shared Version Clock

2. On Read/Write: check

unlocked and v# <= RV then

add to Read/Write-Set

3. Acquire Locks

4. WV = F&I(VClock)

5. Validate each v# <= RV

6. Release locks with v# WV

Reads+Inc+Writes

=serializable

100 Shared Version Clock

87 0 87 0

34 0

88 0

44 0

V# 0

34 0

99 0 99 0

50 0 50 0

Mem Locks

87 0

34 0

99 0

50 0

34 1

99 1

87 0

X

Y

Commit

121 0

121 0

50 0

87 0

121 0

88 0

V# 0

44 0

V# 0

121 0

50 0

100RV

100120121

X

Y

Page 90: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Version Clock Implementation

• Notice: Version clock rate is a progress concern, not a safety concern...

– (GV4) if failed clock CAS use Version Clock set by winner.

– (GV5) WV = Version Clock + 2; inc Version Clock on abort

– (GV6) composite of GV4 and GV5

Page 91: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

96

TM Design Issues

• Implementation

choices

• Language design

issues

• Semantic issues

Page 92: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

97

Granularity

• Object

– managed languages, Java, C#, …

– Easy to control interactions between

transactional & non-trans threads

• Word

– C, C++, …

– Hard to control interactions between

transactional & non-trans threads

Page 93: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

98

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 94: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

99

Conflict Detection

• Eager

– Detect before conflict arises

– “Contention manager” module resolves

• Lazy

– Detect on commit/abort

• Mixed

– Eager write/write, lazy read/write …

Page 95: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

100

Conflict Detection

• Eager detection may abort transactions

that could have committed.

• Lazy detection discards more

computation.

Page 96: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

101

Contention Management &

Scheduling

• How to resolve

conflicts?

• Who moves forward

and who rolls back?

• Lots of empirical

work but formal

work in infancy

Page 97: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

102

Contention Manager Strategies

• Exponential backoff

• Priority to

– Oldest?

– Most work?

– Non-waiting?

• None Dominates

• But needed anywayJudgment of Solomon

Page 98: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

103

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 99: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

104

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 100: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

105

Exceptions

int i = 0;

try {

atomic {

i++;

node = new Node();

}

} catch (Exception e) {

print(i);

}

Page 101: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

106

Exceptions

int i = 0;

try {

atomic {

i++;

node = new Node();

}

} catch (Exception e) {

print(i);

}

Throws OutOfMemoryException!

Page 102: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

107

Exceptions

int i = 0;

try {

atomic {

i++;

node = new Node();

}

} catch (Exception e) {

print(i);

}

Throws OutOfMemoryException!

What is

printed?

Page 103: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

108

Unhandled Exceptions

• Aborts transaction

– Preserves invariants

– Safer

• Commits transaction

– Like locking semantics

– What if exception object refers to values

modified in transaction?

Page 104: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

109

Nested Transactions

atomic void foo() {

bar();

}

atomic void bar() {

}

Page 105: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

110

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

Page 106: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Gartner Hype Cycle

Hat tip: Jeremy Kemp

You are here

Page 107: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

120

• Multicore forces us to rethink

almost everything

I, for one, Welcome our new

Multicore Overlords …

Page 108: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

121

• Multicore forces us to rethink

almost everything

• Standard approaches too

complex

I, for one, Welcome our new

Multicore Overlords …

Page 109: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

122

• Multicore forces us to rethink

almost everything

• Standard approaches won’t

scale

• Transactions might make life

simpler…

I, for one, Welcome our new

Multicore Overlords …

Page 110: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Art of Multiprocessor

Programming

123

• Multicore forces us to rethink almost

everything

• Standard approaches won’t scale

• Transactions might …

• Multicore programming

Plenty more to do…

Maybe you will save us…

I, for one, Welcome our new

Multicore Overlords …

Page 111: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Research

124

Page 112: Art of Multiprocessor Programming - Brown Universitycs.brown.edu/courses/cs176/lectures/chapter_18.pdf · 2 Our Vision for the Future In this course, we covered …. Best practices

Thanks ! תודה

Art of Multiprocessor

Programming

125