The Case for Hardware Transactional Memory

The Case for Hardware Transactional Memory

Lecture 20: Transactional Memory CMU 15-418: Parallel Computer Architecture and Programming (Spring 2012) Giving credit ▪ Many of the slides in today’s talk are the work of Professor Christos Kozyrakis (Stanford University) (CMU 15-418, Spring 2012) Raising level of abstraction for synchronization ▪ Machine-level synchronization prims: - fetch-and-op, test-and-set, compare-and-swap ▪ We used these primitives to construct higher level, but still quite basic, synchronization prims: - lock, unlock, barrier ▪ Today: - transactional memory: higher level synchronization (CMU 15-418, Spring 2012) What you should know ▪ What a transaction is ▪ The difference between the atomic construct and locks ▪ Design space of transactional memory implementations - data versioning policy - con"ict detection policy - granularity of detection ▪ Understand HW implementation of transaction memory (consider how it relates to coherence protocol implementations we’ve discussed in the past) (CMU 15-418, Spring 2012) Example void deposit(account, amount) { lock(account); int t = bank.get(account); t = t + amount; bank.put(account, t); unlock(account); } ▪ Deposit is a read-modify-write operation: want “deposit” to be atomic with respect to other bank operations on this account. ▪ Lock/unlock pair is one mechanism to ensure atomicity (ensures mutual exclusion on the account) (CMU 15-418, Spring 2012) Programming with transactional memory void deposit(account, amount){ void deposit(account, amount){ lock(account); atomic { int t = bank.get(account); int t = bank.get(account); t = t + amount; t = t + amount; bank.put(account, t); bank.put(account, t); unlock(account); } } } nDeclarative synchronization nProgrammers says what but not how nNo explicit declaration or management of locks nSystem implements synchronization nTypically with optimistic concurrency nSlow down only on true con"icts (R-W or W-W) (CMU 15-418, Spring 2012) Declarative vs. imperative abstractions ▪ Declarative: programmer de#nes what should be done - Process all these 1000 tasks - Perform this set of operations atomically ▪ Imperative: programmer states how it should be done - Spawn N worker threads. Pull work from shared task queue - Acquire a lock, perform operations, release the lock (CMU 15-418, Spring 2012) Transactional Memory (TM) nMemory transaction nAn atomic & isolated sequence of memory accesses nInspired by database transactions nAtomicity (all or nothing) nAt commit, all memory writes take effect at once nOn abort, none of the writes appear to take effect nIsolation nNo other code can observe writes before commit nSerializability nTransactions seem to commit in a single serial order nThe exact order is not guaranteed though (CMU 15-418, Spring 2012) Advantages of transactional memory (CMU 15-418, Spring 2012) Another example: Java 1.4 HashMap nMap: Key → Value public Object get(Object key) { int idx = hash(key); // Compute hash HashEntry e = buckets[idx]; // to find bucket while (e != null) { // Find element in bucket if (equals(key, e.key)) return e.value; e = e.next; } return null; } nNot thread safe nBut no lock overhead when not needed (CMU 15-418, Spring 2012) Synchronized HashMap nJava 1.4 solution: synchronized layer nConvert any map to thread-safe variant nUses explicit, coarse-grain locking specified by programmer public Object get(Object key) { synchronized (mutex) { // mutex guards all accesses to hashMap return myHashMap.get(key); } } nCoarse-grain synchronized HashMap nPros: thread-safe, easy to program nCons: limits concurrency, poor scalability n Only one thread can operate on map at any time (CMU 15-418, Spring 2012) Better solution? public Object get(Object key) { int idx = hash(key); // Compute hash HashEntry e = buckets[idx]; // to find bucket while (e != null) { // Find element in bucket if (equals(key, e.key)) return e.value; e = e.next; } return null; } ▪ Fined-grained synchronization: e.g., lock per bucket ▪ Now thread safe: but lock overhead even if not needed (CMU 15-418, Spring 2012) Performance: Locks coarse locks fine locks 1.0000 0.7500 0.5000 Execution Time Execution 0.2500 Hash-Table 0 1 2 4 8 16 Processors coarse locks fine locks 5.0000 3.7500 2.5000 Execution Time Execution 1.2500 Balanced Tree 0 1 2 4 8 16 Processors (CMU 15-418, Spring 2012) Transactional HashMap nSimply enclose all operation in atomic block nSystem ensures atomicity public Object get(Object key) { atomic { // System guarantees atomicity return m.get(key); } } nTransactional HashMap nPros: thread-safe, easy to program nQ: good performance & scalability? n Depends on the implementation, but typically yes (CMU 15-418, Spring 2012) Another example: tree update 1 2 3 4 Goal: Modify node 3 in a thread-safe way. Slide credit: Austen McDonald Synchronization Example 1 2 3 4 Slide credit: Austen McDonald Synchronization Example 1 2 3 4 Slide credit: Austen McDonald Synchronization Example 1 2 3 4 Slide credit: Austen McDonald Synchronization Example 1 2 3 4 Slide credit: Austen McDonald Synchronization Example 1 2 3 4 Slide credit: Austen McDonald Synchronization Example 1 2 3 4 Goals: Modify nodes 3 and 4 in a thread-safe way. Locking prevents concurrency Slide credit: Austen McDonald TM Example 1 2 3 4 Transacon A READ: 1, 2, 3 WRITE: Slide credit: Austen McDonald TM Example 1 2 3 4 Transacon A READ: 1, 2, 3 WRITE: 3 Slide credit: Austen McDonald TM Example: no con"icts 1 2 3 4 Transacon A Transacon B READ: 1, 2, 3 READ: 1, 2, 4 WRITE: 3 WRITE: 4 NO READ-WRITE, or WRITE-WRITE conflicts! Slide credit: Austen McDonald TM Example: with con"icts 1 2 3 4 Transacon A Transacon B READ: 1, 2, 3 READ: 1, 2, 3 WRITE: 3 WRITE: 3 Conflicts exist! Must be serialized! Slide credit: Austen McDonald Performance: locks vs. transactions coarse locks fine locks TCC 1.0000 0.7500 0.5000 HashMap Execution Time Execution 0.2500 0 1 2 4 8 16 Processors coarse locks fine locks TCC 4.0000 3.0000 2.0000 Execution Time Execution 1.0000 Balanced Tree 0 1 2 4 8 16 Processors TCC: a HW-based TM system (CMU 15-418, Spring 2012) Failure atomicity: locks void transfer(A, B, amount) synchronized(bank) { try { withdraw(A, amount); deposit(B, amount); } catch(exception1) { /* undo code 1*/ } catch(exception2) { /* undo code 2*/ } … } nManually catch exceptions nProgrammer provides undo code on a case by case basis n Complexity: what to undo and how… nSome side-effects may become visible to other threads n E.g., an uncaught case can deadlock the system… (CMU 15-418, Spring 2012) Failure atomicity: transactions void transfer(A, B, amount) atomic { withdraw(A, amount); deposit(B, amount); } nSystem processes exceptions n All but those explicitly managed by the programmer n Transaction is aborted and updates are undone n No partial updates are visible to other threads n E.g., No locks held by a failing threads… (CMU 15-418, Spring 2012) Composability: locks void transfer(A, B, amount) void transfer(B, A, amount) synchronized(A) { DEADLOCK! synchronized(B) { synchronized(B) { synchronized(A) { withdraw(A, amount); withdraw(B, amount); deposit(B, amount); deposit(A, amount); } } } } nComposing lock-based code can be tricky nRequires system-wide policies to get correct nBreaks software modularity nBetween an extra lock & a hard place nFine-grain locking: good for performance, but can lead to deadlock (CMU 15-418, Spring 2012) Composability: transactions void transfer(A, B, amount) void transfer(B, A, amount) atomic { atomic { withdraw(A, amount); withdraw(B, amount); deposit(B, amount); deposit(A, amount); } } nTransactions compose gracefully n Programmer declares global intent (atomic transfer) n No need to know of global implementation strategy n Transaction in transfer subsumes those in withdraw & deposit n Outermost transaction defines atomicity boundary nSystem manages concurrency as well as possible n Serialization for transfer(A, B, $100) & transfer(B, A, $200) n Concurrency for transfer(A, B, $100) & transfer(C, D, $200) 25 (CMU 15-418, Spring 2012) Advantages of transactional memory nEasy to use synchronization construct n As easy to use as coarse-grain locks n Programmer declares, system implements nOften performs as well as fine-grain locks n Automatic read-read concurrency & fine-grain concurrency nFailure atomicity & recovery n No lost locks when a thread fails n Failure recovery = transaction abort + restart nComposability n Safe & scalable composition of software modules (CMU 15-418, Spring 2012) Example integration with OpenMP n Example: OpenTM = OpenMP + TM n OpenMP: master-slave parallel model n Easy to specify parallel loops & tasks n TM: atomic & isolation execution n Easy to specify synchronization and speculation n OpenTM features n Transactions, transactional loops & sections n Data directives for TM (e.g., thread private data) n Runtime system hints for TM n Code example #pragma omp transfor schedule (static, chunk=50) for (int i=0; i<N; i++) { bin[A[i]] = bin[A[i]]+1; } (CMU 15-418, Spring 2012) Atomic() ≠ lock()+unlock() n The difference n Atomic: high-level declaration of atomicity n Does not specify implementation/blocking behavior n Does not provide a consistency model n Lock: low-level blocking primitive n Does not provide atomicity or isolation on its own n Keep in mind n Locks can be used to implement atomic(), but… n Locks can be used for purposes beyond atomicity n Cannot replace all lock regions with atomic regions n Atomic eliminates many data races, but.. n Programming with atomic blocks can still suffer from atomicity violations. e.g., atomic sequence incorrectly split into two atomic blocks (CMU 15-418, Spring 2012) Example: lock-based code that does not work with atomic // Thread 1 // Thread 2 synchronized(lock1) { synchronized(lock2) { … … flagB = true; flagA = true; while (flagA==0); while (flagB==0); … … } } n What is the problem with replacing synchronized with atomic? (CMU 15-418, Spring 2012) Example: atomicity violation // Thread 1 // Thread 2 atomic() { atomic { … … ptr = A; ptr = NULL; … } } atomic() { B = ptr->field; } n Programmer mistake: logically atomic code sequence separated into two atomic() blocks.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    61 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us