How Good can a Transactional Memory be? R. Guerraoui, EPFL.

86
How Good can a Transactional Memory be? R. Guerraoui , EPFL

Transcript of How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Page 1: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

How Good can a Transactional Memory be?

R. Guerraoui , EPFL

Page 2: How Good can a Transactional Memory be? R. Guerraoui, EPFL.
Page 3: How Good can a Transactional Memory be? R. Guerraoui, EPFL.
Page 4: How Good can a Transactional Memory be? R. Guerraoui, EPFL.
Page 5: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

How Good can a Transactional Memory

be?

Page 6: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

From the New York TimesSan Francisco, May 7, 2004Intel announces a drastic change in its business strategy:« Multicore is THE way to boost performance »

Page 7: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Multicores Multicores areare almost almost everywhereeverywhere

Dual-core commonplace in laptopsQuad-core in desktopsDual quad-core in serversAll major chip manufacturers produce multicore CPUs

SUN Niagara (8 cores, 64 concurrent threads)Intel Xeon (6 cores)AMD Opteron (4 cores)…

Page 8: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

The free ride is over

Page 9: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

The free ride is over

Every one will need to fork threads

Page 10: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Forking threads is easy

Handling their conflicts is hard

Page 11: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Traditional scalingTraditional scaling

1x2x

4x

Time: Moore’s Law

Speedup

User code

Traditional CPU

Page 12: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Speedup

1x2x

4x

User code

Multicore CPU

Time: Moore’s Law

Ideal multicore scalingIdeal multicore scaling

Page 13: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Real multicore scalingReal multicore scaling

Speedup

1x1.4x

2.2x

User code

Multicore CPU

Time: Moore’s Law

Parallelization & synchronization require

great care!

Parallelization & synchronization require

great care!

Page 14: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Coarse grained locks => slow

Fine grained locks => errors

Page 15: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Double-ended queue

Enqueue Dequeue

Page 16: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

A synchronization abstraction that is:

Simple to use Efficient to implement

Wanted

Page 17: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Transactions

Page 18: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

accessing object 1; accessing object 2;

Back to the undergraduate level

atomic {

}

Page 19: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Historical perspective

Eswaran et al (CACM’76) Database Papadimitriou (JACM’79) Theory Liskov/Sheifler (TOPLAS’82) Language Knight (ICFP’86) Architecture Herlihy/Moss (ISCA’93) Hardware Shavit/Touitou (PODC’95) Software

Page 20: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Simple example

(consistency invariant)

0 < x < y

Page 21: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

T: x := x+1 ; y:= y+1

Simple example

(transaction)

Page 22: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Two-phase locking

To write O, T requires a write-lock on O;

To read O, T requires a read-lock on O;

Before committing, T releases all its locks

T waits if some T’ acquired a lock on O

T waits if some T’ acquired a write-lock on O

Page 23: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Why two phases?

To write O, T requires a write-lock on O; T waits if some T’ acquired a lock on O

To read O, T requires a read-lock on O; T waits if some T’ acquired a write-lock on O

T releases the lock on O when done with O

Page 24: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Why two phases?

T1

T2

read(0) write(1)

O1 O2

read(0) write(1)

O2 O1

Page 25: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Two-phase locking - better dead than wait

-

To write O, T requires a write-lock on O;

To read O, T requires a read-lock on O;

Before committing, T releases all its locks

T aborts if some T’ acquired a lock on O

A transaction that aborts restarts again

T aborts if some T’ acquired a lock on O

Page 26: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Two-phase locking - better kill than wait -

To write O, T requires a write-lock on O;

To read O, T requires a read-lock on O;

Before committing, T releases all its lock

T aborts T’ if T’ acquired a lock on O

T aborts T’ if T’ acquired a write-lock on O

A transaction that aborts restarts again

Page 27: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Two-phase locking- invisible reads -

To write O, T requires a write-lock on O; T aborts T’ if some T’ acquired a write-lock

on OTo read O, T checks if all objects read remain valid - else T aborts

Before committing, T checks if all objects read remain valid and releases all its locks

Page 28: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

“It is better for Intel to get involved in this [Transactional Memory] now so when we get to the point of having …tons… of cores we will have the answers”

Justin Rattner, Intel Chief Technology Officer

Page 29: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

“…we need to explore new techniques like transactional memory that will allow us to get the full benefit of all those transistors and map that into higher and higher performance.”

Bill Gates, President of a transparently operated private foundation

Page 30: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

“…manual synchronization is intractable…transactions are the only plausible solution….”

Tim Sweeney, Epic Games

Page 31: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Speedup

1x2x

4x

User code

Multicore CPU

Time: Moore’s Law

Multicore scalingMulticore scaling

Page 32: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Micro-benchmarks …

Linked-lists; red-black trees, etc.Consider specific loads: typically read-only transactions

Page 33: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Challenging TMsSTMBench7 (Eurosys’07)

Large data structure: challenge memory overhead

Long operations: kills non-linear algorithms

Complex access patterns: stretches flexibility

Page 34: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

STMBench7:(Transact’08) Transaction Terminator

All TMs collapsed because of internal bugs or memory usage (except X)

Certain urban legends about performance revealed inaccurate; e.g., OF is inherently slower than lock-based TMs

Page 35: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Real-world scalingReal-world scaling

Speedup

1x1.4x

2.2x

User code

Multicore CPU

Time: Moore’s Law

Parallelization & synchronization require

great care!

Parallelization & synchronization require

great care!

Page 36: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Software Transactional Memory:Why is it only a Research Toy?

C. Cascaval, C. Blundell, M. Michael,H. Cain, P. Wu, S. Chiras, S. Chatterjee

Page 37: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Why STM can be more thana Research Toy?

A. Dragojević, P. Felber, V. Gramoli, R. Guerraoui

Page 38: How Good can a Transactional Memory be? R. Guerraoui, EPFL.
Page 39: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

How Good can a Transactional Memory

be?

Page 40: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

How much commit can a TM perform?

Page 41: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

A closer look at TM

What really prevents commitment? (Safety)

How much commitment is practically possible? (Progress)

Page 42: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

To write an object O, a transaction acquires O and aborts “the” transaction that owns O

To read an object, a transaction T takes a snapshot to see if the system hasn’t changed since T’s last reads; else T is aborted

Two-phase locking- invisible reads -

Page 43: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Killer write (ownership)

Careful read (validation)

Two-phase locking- invisible reads -

Page 44: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

More efficient algorithm

Apologizing versus asking permission

Killer writeLazy read: validity check at commit time

Page 45: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

A history is atomic if itsrestriction to committed transactions is serializable

The old safety (Pap79)

Page 46: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

A history H of committed transactions is serializable if there is a history S(H) that is (1) equivalent to H(2) sequential (3) legal

Page 47: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Back to the example

Invariant: 0 < x < yInitially: x := 1; y := 2

Page 48: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Division by zero

T1: x := x+1 ; y:= y+1

T2: z := 1 / (y - x)

Page 49: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

T1: x := 3; y:= 6

Infinite loop

T2: a := y; b:= x; repeat b:= b + 1 until a = b

Page 50: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

We need to restrict ALL transactions (as with critical sections)

The old safety property restricts only committed transactions

Page 51: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

A history H is opaque if for every transaction T in H, at least one history in committed(T,H) is serializable

Candidate property: Opacity(PPoPP’08)

Page 52: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Careful read (validation)

Killer write (ownership)

Two-phase locking- invisible reads -

Page 53: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Visible vs Invisible Read (SXM; RSTM)

Write is mega killer: to write an object, a transaction aborts any live one which has read or written the object

Visible but not so careful read: when a transaction reads an object, it says so

Page 54: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

TheoremReads are eithervisible or careful

NB. Assuming minimal progress and single versions

Page 55: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Intuition of the proof

T1

T2

read()

write()commit

I1,I2,..,Im

O1,O2,..,Onread()

Ik

Page 56: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

The theorem does not hold for classical atomicity

i.e., the theorem does not hold for database transactions

Page 57: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

A closer look at TM

What really prevents commitment? (Opacity)

Page 58: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

How to prove opacity?

Model checking

Page 59: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Check that the conflict graph is acyclic

Number of nodes is unbounded (STM) NP-Complete problem

Model checking opacity?

Page 60: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Hint: reduce the verification space

Uniform system All transactions are treated equallyTransaction and variable names do not

matter

Atomic system Read, write and commit operations are

atomic

Page 61: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

TM verification theorem (PLDI’08)

A TM either violates opacity with 2 transactions and 3 variables or satisfies it with any number of variables and transactions

Page 62: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Examples

It takes 15mn to check the correctness of TL2 and DSTM

Reverse two lines in TL2: bug found in 10mn

Page 63: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

A closer look at TM

What really prevents commitment? Opacity

How much commitment is possible (given opacity)?

Page 64: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Ideal progress

No correct transaction aborts

Page 65: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

T1

T2

read()

write()

commit

O1

O1write()

O2

Aborting is a fatality

read()

O2

abort

Page 66: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Eventual progress

Every correct transaction eventually commits

NB. We allow the possibility for a transaction to abort a finite number of times as long as it eventually commits

Page 67: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Eventual progress

T1

T2

read()

write()

commit

O1

O1write()

O2

read()

O2

abort

Page 68: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Eventual progress is impossible

NB. This impossibility is fundamentally different from FLP: It holds no matter what underlying object is used

Page 69: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

A closer look at TM

What really prevents commitment? Opacity

How much commitment is possible (given opacity)?

Page 70: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Conditional progress - obstruction-freedom -

A correct transaction that does not encounter contention eventually commits

Page 71: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

DSTM

To write O, T requires a write-lock on O (use C&S);

T aborts T’ if some T’ acquired a write-lock on O (use C&S)

To read O, T checks if all objects read remain valid - else abort (use C&S)

Before committing, T releases all its locks (use C&S)

Page 72: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

DSTM uses C&S

C&S is the strongest synchronization primitive

Is OF-TM possible with less than C&S?

e.g., R/W objects

Page 73: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

The consensus number of OF-TM is 2 (SPAA’08)

OF-TM cannot be implemented with R/W objects only

OF-TM does not need C&S

Page 74: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

A closer look at TM

What really prevents commitment? Opacity

How much commitment is practically possible? conditional

More?

Page 75: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Permissiveness (DISC’08)

A TM is permissive if it never aborts when it should not

Page 76: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

More precisely

Let P be any safety property and H any P-safe history prefix of a deterministic TM

We say that a TM is permissive w.r.t P if – Whenever <H;commit> satisfies P– <H;commit> can be generated by the TM

Page 77: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Permissive TM

• But….

• Very expensive (maintain global conflict graphs)

Page 78: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

• Let P be any safety property and H any history H generated by a TM

• The TM is probabilistic permissive with respect to P if – Whenever <H;commit> satisfies P:– <H;commit> can be generated by

TM with a positive probability

Probabilistic permissiveness

Page 79: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

• AVSTM: a probabilistically permissive TM w.r.t opacity

• Idea: at commit, a transaction guesses a serialization time in the future

AVSTM

Page 80: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

• Non-blocking

• Good performance under high-contention

• Can be combined with an OF-TM

AVSTM

Page 81: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

A closer look at TM

• What really prevents commitment? Opacity

• How much commitment is practically possible? conditional; permissiveness

• How fast?

Page 82: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Off-line scheduler (PODC’95)

• Compare the TM protocol with an off-line scheduler that knows:

– The starting time of transactions– Which objects are accessed (i.e., conflicts)

Page 83: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Greedy contention manager

• State• Priority (based on start time)• Waiting flag (set while waiting)

• Wait if other has• Higher priority AND not waiting

• Abort other if• Lower priority OR waiting

Page 84: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Example of competitive ratio

• Let s be the number of objects accessed by all transactions

• Compare time to commit all transactions

• Greedy is O(s)-competitive with the off-line scheduler– GHP’05 O(s2)– AEST’06 O(s)

Page 85: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Joint work with

• A. Dragojevic• T. Henzinger• M. Herlihy • M. Kapalka• V. Sing

• See Transactions@epfl: lpdwww.epfl.ch

Page 86: How Good can a Transactional Memory be? R. Guerraoui, EPFL.

Transactions are conquering the parallel programming world

They look simple and familiar and might make the programmer

happy

They are in fact very tricky and should make US happy

The one slide to remember