Transactional Memory Lecturer: Danny Hendler. 2 2 From the New York Times…

Preview:

Citation preview

Transactional Memory

Lecturer: Danny Hendler

22

From the New York Times …

May 7th, 2004 – “Intel said on Friday that it was scrapping its development of a faster Pentium 4 to focus on “dual core” microprocessors, a move that is a shift in the company's business strategy….”

May 8th,2004 – “Intel … [has] decided to focus its development efforts on “dual core” processors …. with two engines instead of one, allowing for greater efficiency because the processor workload is essentially shared.”

From the New York Times…

3

Moore’s lawExponential growth in computing power

4

Speeding up uni-processors is harder and harder

Intel, Sun, AMD, IBM now focusing on “multi-core” architectures

Already, most computers are multiprocessors

How can we write correct and

efficient algorithms for

multiprocessors?

The Future of Computing

5

A fundamental problem of thread-level parallelism

. .Account[i] = Account[i]-X;Account[j] = Account[j]+X; .

. .

. .Account[i] = Account[i]-X;Account[j] = Account[j]+X; .

. .

Thread A Thread B

But what if execution is concurrent?

Must avoid race

conditions

6

Inter-thread synch. alternatives

7

8

9

1010

Transactional Memory:What is a transaction?

• A transaction is a sequence of memory reads and writes, executed by a single thread, that either commits or aborts

• If a transaction commits, all the reads and writes appear to have executed atomically

• If a transaction aborts, none of its stores take effect

• Transaction operations aren't visible until they commit (if they do)

1111

Transactions properties:

A transaction satisfies the following key property:

• Atomicity: Each transaction either commits (its changes seem to take effect atomically) or aborts (its changes have no effect).

12

Transactional Memory Goals

• A new multiprocessor architecture• The goal: Implementing lock-free synchronization

that is– efficient– easy to use compared with conventional techniques

based on mutual exclusion

• Implemented by hardware support (such as straightforward extensions to multiprocessor cache-coherence protocols) and / or by software mechanisms

13

A Usage Example

Locks:

Lock(L[i]); Lock(L[j]); Account[i] = Account[i] –

X; Account[j] = Account[j] +

X; Unlock(L[j]); Unlock(L[i]);

Transactional Memory:

atomic { Account[i] = Account[i] –

X; Account[j] = Account[j] +

X; };

Account[i] = Account[i]-X;Account[j] = Account[j]+X;

14

• Transactions execute in commit order

ld 0xdddd...st 0xbeef

Transaction ATime

ld 0xbeef

Transaction C

ld 0xbeef

Re-execute Re-execute with new datawith new data

Commit

ld 0xdddd...ld 0xbbbb

Transaction B

Commit Violation!Violation!

0xbeef0xbeef

Taken from a presentation by Royi Maimon & Merav Havuv, prepared for a seminar given by Prof. Yehuda Afek.

Transactions interaction

Two transactions conflict if the write-set of one intersects with

the data-set of the other

15

Software Transaction Memory(Nir Shavit and Dan Touitou, '95)

• No TM hardware support required

• Non-blocking– Memory locations acquired in increasing order– Help the transaction that fails you

• Shared memory operations required:– Read & Write– Load-link / store-conditional (LL / SC)

• Static transactions only (to permit 2-phase locking)

17

Load-linked / store-conditional (LL/SC)

Change a value only if a previously read variable was not written in the interim.

• Load-linked(w) - return the value of w

• Store-conditional(w,v) by p – writes v and returns success if no write/SC was applied to o since p’s last load-linked operation on o. Otherwise, it returns fail.

18

Shavit/Touitou STM:Data Structures

Memory

Ownerships

executingstatusversionSizeaddr[]oldValues[]

Rec1 Rec2 Recn

executingstatusversionSizeaddr[]oldValues[]

executingstatusversionSizeaddr[]oldValues[]

Each process owns a single transaction record

Basic ideas

• Changes are tentative until transaction commits

• LL/SC verifies ‘old’ processes do not overwrite completed transactions

• Helping: A failed transaction helps the transaction that failed it

19

Transaction record

executingstatusversionsizeaddr[]oldValues[]calc

Data-set size

Data-set addresses, in increasing order

Contains old values of dataset once transaction

succeeds

Incremented every new transaction

Only help transaction that are still executing

Function to apply to data-set

{NONE,SUCCESS, FAILURE}

A typical use of LL/SC and version field

LL(trans.field)…if (version != trans.version)

returnSC(trans.field, newval);

22

The startTransaction procedure

Status StartTransaction(TransactionBody Calc, MemAddr[] DataSet)

1. Initialize(Reci, DataSet)

2. Reci.calc=Calc

3. Reci.executing=true

4. Transaction(Reci, Reci.version, true) // true – called by tran. owner

5. Reci.executing=false // Disable helping

6. Reci.version++

7. if reci.status=SUCCESS

8. return (SUCCESS, Reci.oldValues)

9. Else10. return Failure

Code to execute in transaction

Addresses that may be accessed

The Transaction procedure

Transaction(rec, version, isInitiator)

1. AquireOwnerships(rec, version) // Try to capture memory ownership2. (status, failaddr) = LL(rec.status) // Status set by Aquire3. if (status == NONE) // did not fail4. if (version ≠ rec.version) // Transaction already completed5. return6. SC(rec.status(SUCCESS)7. (status, failaddr) = LL(rec.status) // failaddr is index of non-acquired

mem8. if (status == SUCCESS)9. AgreeOldValues(rec,version) // Copy old values to record10. NewValues=calc (rec.OldValues) // Calculate new values – apply

transact.11. UpdateMemory(rec, version, NewValues) // Write back to memory12. ReleaseOwnership(rec, version) // Release memory’s ownerships

13. else…

true – called by initiatorfalse – called by helper

24

The Transaction procedure

Transaction(rec, version, isInitiator)

…13. else // In case of failure14. ReleaseOwnerships(rec, version)15. if IsInitiator // Help only the transaction that failed mine16. failtran=ownerships[failaddr] // Find whom to help 17. if (failtran == Nobody) 18. return // That transaction already completed19. else20. failversion=failtran.version21. if (failtran.executing) // only help an executing transaction22. Transaction(failtran, failversion, false) // help once

25

The AcquireOwnerships procedureAcquireOwnerships(rec,version)

1. transize=rec.size // transaction size2. for j=1 to transize do3. while true do 4. location=rec.addr[j] // address of next memory cell5. if LL(rec.status ≠ NONE)6. return // Transaction already completed7. owner=LL(ownerships[rec.addr[j]) // find out who owns this cell8. if (rec.version ≠ version) return // Transaction already completed9. if (owner=rec) exit while loop // go to next cell10. if (owner=nobody) // memory cell free11. if SC(rec.status, NONE) // if status did not change12. if SC(ownerships[location], rec) // if managed to acquire cell13. exit while loop // go to next cell14. else // this transaction fails15. SC(rec.status, (Failure, j) )16. return

The AgreeOldValues procedureAgreeOldValues(rec,version)

1. transize=rec.size2. for (j=1 to transize)3. location=rec.addr[j]4. LL(rec.oldValues[j])5. if (rec.version ≠ version) return6. SC(rec.oldValues[j], Memory[location])

The ReleaseOwnership procedure

ReleaseOwnership(rec,version)

1. transize=rec.size2. for (j=1 to transize)3. location=rec.addr[j]4. if (LL(ownerships[location]) = rec)5. if (rec.version ≠ version) return // Transaction completed6. SC(ownerships[location], nobody)

Many new STM implementations

• DSTM Dynamic transactions Obstruction free Contention managers

• RSTM Object-based implementation for C++

• SXM STM package for C#

• Locking-based STM

• Scheduling-based STM…

Recommended