Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Transactional Memory Lecturer: Danny Hendler. 2 2 From the New York Times…"— Presentation transcript:

1 Transactional Memory Lecturer: Danny Hendler

2 2 2 From the New York Times…

3 3 Moore’s law Exponential growth in computing power

4 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 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 6 Inter-thread synch. alternatives

7 7

8 8

9 9

10 10 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)

11 11 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 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 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 14 Transactions execute in commit order ld 0xdddd... st 0xbeef Transaction A Time ld 0xbeef Transaction C ld 0xbeef Re-execute with new data Commit ld 0xdddd... ld 0xbbbb Transaction B Commit Violation! 0xbeef 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 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)

16 16 Usage example: kCAS kCAS(size, DataSet[], old[], new[]) { 1.BeginTransaction 2. for (i=0 to size) 3. if Read-transctional(DataSet[i] ≠ old[i]) 4. return-code=Failure 5. ExitTransaction 6. for (i=0 to size) 7. Write-transactional(DataSet[i], New[i]) 8. return-code=Success 9.EndTransaction }

17 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 18 Shavit/Touitou STM: Data Structures Memory Ownerships executing status version Size addr[] oldValues[] Rec 1 Rec 2 Rec n executing status version Size addr[] oldValues[] executing status version Size addr[] oldValues[] Each process owns a single transaction record

19 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

20 Transaction record executing status version size addr[] 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}

21 A typical use of LL/SC and version field LL(trans.field) … if (version != trans.version) return SC(trans.field, newval);

22 22 The startTransaction procedure Status StartTransaction(TransactionBody Calc, MemAddr[] DataSet) 1.Initialize(Rec i, DataSet) 2.Rec i.calc=Calc 3.Rec i.executing=true 4.Transaction(Rec i, Rec i.version, true) // true – called by tran. owner 5.Rec i.executing=false // Disable helping 6.Rec i.version++ 7.if rec i.status=SUCCESS 8. return (SUCCESS, Rec i.oldValues) 9.Else 10. return Failure Code to execute in transaction Addresses that may be accessed

23 The Transaction procedure Transaction(rec, version, isInitiator) 1.AquireOwnerships(rec, version) // Try to capture memory ownership 2.(status, failaddr) = LL(rec.status) // Status set by Aquire 3.if (status == NONE) // did not fail 4. if (version ≠ rec.version) // Transaction already completed 5. return 6. SC(rec.status(SUCCESS) 7.(status, failaddr) = LL(rec.status) // failaddr is index of non-acquired mem 8.if (status == SUCCESS) 9. AgreeOldValues(rec,version) // Copy old values to record 10. NewValues=calc (rec.OldValues) // Calculate new values – apply transact. 11. UpdateMemory(rec, version, NewValues) // Write back to memory 12. ReleaseOwnership(rec, version) // Release memory’s ownerships 13.else … true – called by initiator false – called by helper

24 24 The Transaction procedure Transaction(rec, version, isInitiator) … 13.else // In case of failure 14. ReleaseOwnerships(rec, version) 15. if IsInitiator // Help only the transaction that failed mine 16. failtran=ownerships[failaddr] // Find whom to help 17. if (failtran == Nobody) 18. return // That transaction already completed 19. else 20. failversion=failtran.version 21. if (failtran.executing) // only help an executing transaction 22. Transaction(failtran, failversion, false) // help once

25 25 The AcquireOwnerships procedure AcquireOwnerships(rec,version) 1.transize=rec.size // transaction size 2.for j=1 to transize do 3. while true do 4. location=rec.addr[j] // address of next memory cell 5. if LL(rec.status ≠ NONE) 6. return // Transaction already completed 7. owner=LL(ownerships[rec.addr[j]) // find out who owns this cell 8. if (rec.version ≠ version) return // Transaction already completed 9. if (owner=rec) exit while loop // go to next cell 10. if (owner=nobody) // memory cell free 11. if SC(rec.status, NONE) // if status did not change 12. if SC(ownerships[location], rec) // if managed to acquire cell 13. exit while loop // go to next cell 14. else // this transaction fails 15. SC(rec.status, (Failure, j) ) 16. return

26 The AgreeOldValues procedure AgreeOldValues(rec,version) 1.transize=rec.size 2.for (j=1 to transize) 3. location=rec.addr[j] 4. LL(rec.oldValues[j]) 5. if (rec.version ≠ version) return 6. SC(rec.oldValues[j], Memory[location])

27 The ReleaseOwnership procedure ReleaseOwnership(rec,version) 1.transize=rec.size 2.for (j=1 to transize) 3. location=rec.addr[j] 4. if (LL(ownerships[location]) = rec) 5. if (rec.version ≠ version) return // Transaction completed 6. SC(ownerships[location], nobody)

28 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 …


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

Similar presentations


Ads by Google