Title: Transactional Memory
1Transactional Memory
- Hao Tian
- Instructor Dr. Barbara Chapman
2Agenda
- 1.Motivation
- 2. What is Transactional Memory
- 3. Software Transactional Memory (STM)
- 3.1RSTM
- 4. Summary
- 5. References
3Motivation
- Drawbacks of lock-based synchronization
- Conservative blocking approach
- Acquiring locks whenever there is a possibility
of conflict - Hard to find the right granularity
- Coarse-grained locks not scale
- Fine-grained locks overhead
- Vulnerable to failure and fault
- Blocking other threads if one thread dies,
deadlock - Priority inversion
- High priority threads cannot proceed if a low
priority thread holds the lock
4Motivation
- Critical Section a piece of code that accesses a
shared resource (data structure or device) that
must not be concurrently accessed by more than
one thread of execution. - A deadlock is a situation wherein two or more
competing actions are waiting for the other to
finish, and thus neither ever does.
5Motivation
- Priority inversion a low priority task holds a
shared resource that is required by a high
priority task, usually the lower task is called
by a higher task. - these types of problems are time-sensitive, they
are often very hard to find during normal testing
and in some cases go undetected until after the
application is deployed.
6What is Transactional Memory
- An idea from DBMS
- A unit of interaction with a DBMS that is treated
in a coherent and reliable way - A transaction several queries, each of them
reading and/or writing information in the
database. - It must be either entirely completed or aborted
to ensure the integrity of a database. - 3 Conventional states ACTIVE, ABORTED, and
COMMITTED
7What is Transactional Memory
- Transactional memory is a construct for
concurrency control that enables access to data
shared by threads. - Only one thread can contain given data item.
- A transaction is a high-level construct that
executes reads and writes to data as an
indivisible operation.
8What is Transactional Memory
- Two trivial code examples
- locking
- Lock(L) x unlock(L)
- atomic block construct
- atomicx
9What is Transactional Memory
- A finite sequence of memory reads and writes
executed by a single thread - Transactional Memory is optimal
- Executing threads as if there are no other
threads - Using log to record memory modifications
- Checking the records for consistency
- Aborted and re-executing transaction
10Agenda
- 1.Motivation
- 2. What is Transactional Memory
- 3. Software Transactional Memory (STM)
- 3.1RSTM
- 4. Summary
- 5. References
11What is Software Transactional Memory
- Software Transactional Memory is a concurrent
programming API in which conventional critical
sections are replaced by transactions. - A transaction is a sequence of steps executed by
a single thread.
12What is Software Transactional Memory
- Transaction properties ACID
- Atomicity
- Either all of the operations in a transaction are
performed or none of them are. - Consistency
- A legal database state before and after the
transaction - Isolation
- Changes made by operations in a transaction are
not visible to other outside operations until the
transaction is committed. - Durability
- Once committed, the transaction will persist
13Software Transactional Memory
- Transaction Descriptor each thread reuses a
single statically allocated Transaction
Descriptor across all of its transactions, it
contains some information we need during
transaction.
14Software Transactional Memory
- Dynamic STM
- ACTIVE/ABORTED the old version
- COMMITTED the new version
15Software Transactional Memory
- Conflict detection
- Eager Detection writer acquires objects at open
time. - Lazy Detection writer delays acquiring objects
until just before commit time.
16Software Transactional Memory
- Eager Detection It allows conflicts between
transactions to be detected early, some useless
work can be aborted early, but some transaction
will fail to commit itself. - Lazy Detection It allows doomed transactions to
continue, but it can not see the potential
conflicts
17RSTM
- The Rochester Software Transactional Memory
Runtime - RSTM is a C library for multithreaded,
non-blocking transaction-based code. - Why non-blocking?
- Non-blocking data structures avoid many of the
principal problems with locks, including
deadlock, convoying, priority inversion, and
performance anomalies due to preemption and page
faults.
18RSTM
- 9 instructions in the RSTM API
- stm_init() - must be called in each thread before
any transactions are issued - stm_dest() - call once a thread shuts down
- open_RO() - open an object for reading
- open_RW() - open an object for writing
- shared() - get a transactional wrapper for an
open object - release() - "close" an object opened with
open_RO() - tx_alloc() - get memory from the transaction heap
- tx_free() - free memory allocated with tx_alloc
- BEGIN_TRANSACTION - begin a transaction
- END_TRANSACTION - end a transaction (must be at
same nesting level as BEGIN_TRANSACTION)
19RSTM
20RSTM
21RSTM
Begin_Transaction()
Judges which thread starts the transaction
Yes, continue
No, goes into slowpath(), Enters a backoff
section
b64 128, 2564096
22RSTM
23What is Software Transactional Memory
- STM is optimistic
- Every thread completes its modifications to
shared memory without regard for what other do. - It is placed on the reader to make sure other
threads didnt make changes to the memory it
accessed.( using log to record) - A transaction may abort at any time, causing all
of its prior changes to be rolled back or undone. - If a transaction can not be finished due to
conflicting changes, it is aborted and
re-executed from the beginning. - Commit if validation is successful
24Performance
- The benefits of this approach increases
concurrency - No thread needs to wait to access to a resource.
- Theoretically, when there are n concurrent
transactions running in the same time, there
could be need of O(n) memory and processor time
consumption.
25Advantages
- STM simplifies conceptual understanding of
multithreaded programs, but lock-based
programming still need to do following - They require thinking about overlapping
operations and partial operations in separated
and unrelated sections of code. - They require programmers to adopt a locking
policy to prevent deadlock and other failures to
make progress. - They can lead to priority inversion.
26Proposed language support
- // Insert a node into a doubly-linked list
atomically atomic - newNode-gtprev node
- newNode-gtnext node-gtnext
- node-gtnext-gtprev newNode
- node-gtnext newNode
-
- When the end of the program is reached, the
transaction is committed.
27Proposed language support
- atomic (queueSize gt 0)
- remove item from queue and use it
-
28Proposed language support
- atomic
- if (queueSize gt 0)
- remove item from queue and use it
- else
- retry
-
-
- This ability to retry dynamically late in the
transaction simplifies the programming model and
opens up new possibilities
29Summary
- How can the programmer write parallel code more
effectively, that is, write robust code that
doesnt have bugs, but still scales and benefits
from additional cores that each successive
generation provides Asked by Ali-Reza, Intel
Principal Engineer. - Maybe TM can provide us a easy way to implement
our codes.
30References
- Lowering the Overhead of Nonblocking Software
Transactional Memory, by V. J. Marathe, M. F.
Spear, Heriot,A. Acharya, - http//www.cs.rochester.edu/research/synchronizati
on/rstm/ - http//en.wikipedia.org/wiki/Transactional_memory
- http//www.hpcwire.com/hpc/1196095.html
- http//softwareblogs.intel.com/2007/01/17/transact
ional-memory-whats-all-the-hubbub-bub/