Transactional Memory Lecturer Danny Hendler From the New




![A fundamental problem of thread-level parallelism Thread A. . Account[i] = Account[i]-X; Account[j] = A fundamental problem of thread-level parallelism Thread A. . Account[i] = Account[i]-X; Account[j] =](https://slidetodoc.com/presentation_image/2bc27fa93d35bd6243e3a349d7705004/image-5.jpg)







![A Usage Example Account[i] = Account[i]-X; Account[j] = Account[j]+X; Locks: Lock(L[i]); Lock(L[j]); Account[i] = A Usage Example Account[i] = Account[i]-X; Account[j] = Account[j]+X; Locks: Lock(L[i]); Lock(L[j]); Account[i] =](https://slidetodoc.com/presentation_image/2bc27fa93d35bd6243e3a349d7705004/image-13.jpg)



![Shavit/Touitou STM: Data Structures Memory Ownerships executing status version Size addr[] old. Values[] Rec Shavit/Touitou STM: Data Structures Memory Ownerships executing status version Size addr[] old. Values[] Rec](https://slidetodoc.com/presentation_image/2bc27fa93d35bd6243e3a349d7705004/image-17.jpg)










- Slides: 27

Transactional Memory Lecturer: Danny Hendler

From the New York Times… 2 2

Moore’s law Exponential growth in computing power 3

The Future of Computing Speeding up uni-processors is harder and harder Intel, Sun, AMD, IBM now focusing on “multicore” architectures Already, most computers are multiprocessors How can we write correct and efficient algorithms for multiprocessors? 4
![A fundamental problem of threadlevel parallelism Thread A Accounti AccountiX Accountj A fundamental problem of thread-level parallelism Thread A. . Account[i] = Account[i]-X; Account[j] =](https://slidetodoc.com/presentation_image/2bc27fa93d35bd6243e3a349d7705004/image-5.jpg)
A fundamental problem of thread-level parallelism Thread A. . Account[i] = Account[i]-X; Account[j] = Account[j]+X; . . . Thread B. . Account[i] = Account[i]-X; Account[j] = Account[j]+X; . . . But what if execution is concurrent? Must avoid race conditions 5

Inter-thread synch. alternatives 6

7

8

9

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

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). 11 11

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 cachecoherence protocols) and / or by software mechanisms 12
![A Usage Example Accounti AccountiX Accountj AccountjX Locks LockLi LockLj Accounti A Usage Example Account[i] = Account[i]-X; Account[j] = Account[j]+X; Locks: Lock(L[i]); Lock(L[j]); Account[i] =](https://slidetodoc.com/presentation_image/2bc27fa93d35bd6243e3a349d7705004/image-13.jpg)
A Usage Example Account[i] = Account[i]-X; Account[j] = Account[j]+X; 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; }; 13

Transactions interaction • Transactions execute in commit order Time Transaction A Transaction C Transaction B ld 0 xdddd. . . st 0 xbeef Commit 0 xbeef ld 0 xdddd. . . ld 0 xbbbb 0 xbeef Commit Two transactions conflict if the write-set of one intersects with the data-set of the other ld 0 xbeef Violation! ld 0 xbeef Re-execute with new data 14 Taken from a presentation by Royi Maimon & Merav Havuv, prepared for a seminar given by Prof. Yehuda Afek.

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

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. 17
![ShavitTouitou STM Data Structures Memory Ownerships executing status version Size addr old Values Rec Shavit/Touitou STM: Data Structures Memory Ownerships executing status version Size addr[] old. Values[] Rec](https://slidetodoc.com/presentation_image/2bc27fa93d35bd6243e3a349d7705004/image-17.jpg)
Shavit/Touitou STM: Data Structures Memory Ownerships executing status version Size addr[] old. Values[] Rec 1 executing status version Size addr[] old. Values[] Rec 2 Recn Each process owns a single transaction record 18

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 {NONE, SUCCESS, FAILURE} Only help transaction that are still executing status version size addr[] old. Values[] calc Function to apply to data-set Incremented every new transaction Data-set size Data-set addresses, in increasing order Contains old values of dataset once transaction succeeds

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

The start. Transaction that may procedure Addresses Code to execute be accessed in transaction Status Start. Transaction(Transaction. Body Calc, Mem. Addr[] Data. Set) 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. Initialize(Reci, Data. Set) Reci. calc=Calc Reci. executing=true Transaction(Reci, Reci. version, true) // true – called by tran. owner Reci. executing=false // Disable helping Reci. version++ if reci. status=SUCCESS return (SUCCESS, Reci. old. Values) Else return Failure 22

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

The Transaction procedure Transaction(rec, version, is. Initiator) … 13. else // In case of failure 14. Release. Ownerships(rec, version) 15. if Is. Initiator // 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 24

The Acquire. Ownerships procedure Acquire. Ownerships(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) ) 25 16. return

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

The Release. Ownership procedure Release. Ownership(rec, version) 1. 2. 3. 4. 5. 6. transize=rec. size for (j=1 to transize) location=rec. addr[j] if (LL(ownerships[location]) = rec) if (rec. version ≠ version) return // Transaction completed 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 …