(Nested) Open Memory Transactions in Haskell

Slides:



Advertisements
Similar presentations
1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Advertisements

1 Lecture 11: Transactions: Concurrency. 2 Overview Transactions Concurrency Control Locking Transactions in SQL.
Principles of Transaction Management. Outline Transaction concepts & protocols Performance impact of concurrency control Performance tuning.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Software Transactional Memory Steve Severance – Alpha Heavy Industries.
© 2009 Matthew J. Sottile, Timothy G. Mattson, and Craig E Rasmussen 1 Concurrency in Programming Languages Matthew J. Sottile Timothy G. Mattson Craig.
Simon Peyton Jones (Microsoft Research) Tokyo Haskell Users Group April 2010.
PARALLEL PROGRAMMING with TRANSACTIONAL MEMORY Pratibha Kona.
Parallelism and Concurrency Koen Lindström Claessen Chalmers University Gothenburg, Sweden Ulf Norell.
Transaction Processing Lecture ACID 2 phase commit.
Concurrency Control and Recovery In real life: users access the database concurrently, and systems crash. Concurrent access to the database also improves.
Distributed Systems 2006 Styles of Client/Server Computing.
Concurrency and Software Transactional Memories Satnam Singh, Microsoft Faculty Summit 2005.
08_Transactions_LECTURE2 DBMSs should guarantee ACID properties (Atomicity, Consistency, Isolation, Durability). This is typically done by guaranteeing.
Programming Paradigms for Concurrency Part 2: Transactional Memories Vasu Singh
1cs Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect.
Transactions and Concurrency Control Distribuerade Informationssystem, 1DT060, HT 2013 Adapted from, Copyright, Frederik Hermans.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness.
Wait-Free Multi-Word Compare- And-Swap using Greedy Helping and Grabbing Håkan Sundell PDPTA 2009.
QIAN XI COS597C 10/28/2010 Explicit Concurrent Programming in Haskell.
Chapter 15: Transactions Loc Hoang CS 157B. Definition n A transaction is a discrete unit of work that must be completely processed or not processed at.
CS533 – Spring Jeanie M. Schwenk Experiences and Processes and Monitors with Mesa What is Mesa? “Mesa is a strongly typed, block structured programming.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
CSE 153 Design of Operating Systems Winter 2015 Midterm Review.
CGS 3763 Operating Systems Concepts Spring 2013 Dan C. Marinescu Office: HEC 304 Office hours: M-Wd 11: :30 AM.
1 Synchronization via Transactions. 2 Concurrency Quiz If two threads execute this program concurrently, how many different final values of X are there?
1 Processes and Threads Part II Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
SHUJAZ IBRAHIM CHAYLASY GNOPHANXAY FIT, KMUTNB JANUARY 05, 2010 Distributed Database Systems | Dr.Nawaporn Wisitpongphan | KMUTNB Based on article by :
Jinze Liu. ACID Atomicity: TX’s are either completely done or not done at all Consistency: TX’s should leave the database in a consistent state Isolation:
Distributed Transactions What is a transaction? (A sequence of server operations that must be carried out atomically ) ACID properties - what are these.
R 1 Transactional abstractions: beyond atomic update Tim Harris r.
Parallelism and Concurrency Koen Lindström Claessen Chalmers University Gothenburg, Sweden Patrik Jansson.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Process Management Deadlocks.
Interprocess Communication Race Conditions
Parallelism and Concurrency
Transaction Management
Atomic Operations in Hardware
Atomic Operations in Hardware
C. Faloutsos Concurrency control - deadlocks
Deadlock B.Ramamurthy CSE421 9/17/2018 B.Ramamurthy.
Lecture 5: GPU Compute Architecture
Threads and Memory Models Hal Perkins Autumn 2011
Lecture 5: GPU Compute Architecture for the last time
Monitors Chapter 7.
Changing thread semantics
Transactional Memory Semaphores, monitors, and conditional critical regions all suffer from limitations based on lock semantics Naïve synchronization may.
On transactions, and Atomic Operations
Chapter 26 Concurrency and Thread
Transactional Events Kevin Donnelly, Boston University
COP 4600 Operating Systems Fall 2010
Threads and Memory Models Hal Perkins Autumn 2009
On transactions, and Atomic Operations
Database Security Transactions
Monitors Chapter 7.
Introduction of Week 13 Return assignment 11-1 and 3-1-5
Distributed Transactions
Concurrency: Mutual Exclusion and Process Synchronization
Design and Implementation Issues for Atomicity
Software Transactional Memory Should Not be Obstruction-Free
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
Deadlock B.Ramamurthy CSE421 4/26/2019 B.Ramamurthy.
CSE 542: Operating Systems
CSE 542: Operating Systems
Temple University – CIS Dept. CIS661 – Principles of Data Management
Presentation transcript:

(Nested) Open Memory Transactions in Haskell Marino Miculan Marco Peressotti Andrea Toneguzzo University of Udine CINA Final Meeting Civitanova Marche, 19-21 January 2015

Software Transactional Memory Software transactional memory is a coordination mechanism aiming to solve issues plaguing lock-based coordination mechanisms (semaphores, monitors, barriers…) Typical form: atomically { <code> } guarantees that <code> is executed atomically with respect to every other atomic block (as in ACID database transactions) Usually implemented by optimistic execution No deadlocks (no locks), no priority inversions, etc. STM is implemented in many languages (Java, Haskell, C, C++ …) close to modern hardware multi-core architectures (e.g., Haswell) Marino Miculan - UniUD Nested Open Memory Transactions

STM Haskell [Harris et al. PPOPP 2006] data STM a -- Sequencing, do notation ----------------------- (>>=) :: STM a -> (a -> STM b) -> STM b return :: a -> STM a -- Exceptions ------------------------------------ throw :: Exception e => e -> STM a catch :: Exception e => STM a->(e-> STM a)-> STM a -- Running isolated and atomic computations ------ atomically :: STM a -> IO a -- Transactional variables ------ data TVar a newTVar :: a -> STM (TVar a) readTVar :: TVar a -> STM a writeTVar :: TVar a -> a -> STM () Marino Miculan - UniUD Nested Open Memory Transactions

STM Haskell [Harris et al. PPOPP 2006] Example: an integer semaphore check :: Bool -> STM () check p = if p then return () else retry type Semaphore = TVar Int up :: Semaphore -> STM () up s = do v <- readTVar s writeTVar s (v + 1) down :: Semaphore -> STM () down s = do v <- readTVar s check v > 0 writeTVar s (v - 1) Operations are transactional (STM) and composable Typing prevents I/O actions inside transactions Marino Miculan - UniUD Nested Open Memory Transactions

Synchronizations and transactions Synchronization a la CCS can be implemented with a semaphore pair, and interleaving execution e.g.: a.P|ā.Q Typical interaction paradigm of RPC, RMI, XML/RPC… a.P = down a1; up a2; P ā.Q = up a1; down a2; Q Marino Miculan - UniUD Nested Open Memory Transactions

Synchronizations and transactions But what if synchronizations are part of transactions? atomically do{ down a1; up a2; P } atomically do{ up a1; down a2; Q } Both processes are stuck for none can commit Marino Miculan - UniUD Nested Open Memory Transactions

Atomicity and isolation in block execution Atomicity: “all or nothing” semantics; executions may overlap, with uncontrolled access to shared memory Isolation: “as if it were the only one” semantics; mutual exclusion, but no rollback on errors/exceptions Isolation No Yes Atomicity Normal blocks IO Lock (patterns) 2PL, synchronized ? Transactional Memory STM Haskell Open Transactions OTM Haskell Marino Miculan - UniUD Nested Open Memory Transactions

Sharing transactional variables x t2 start k1 start k2 write x read x abort Marino Miculan - UniUD Nested Open Memory Transactions

Sharing transactional variables x t2 start k1 start k2 write x k2 depends on data generated by k1 read x abort k2 cannot commit until k1 does Marino Miculan - UniUD Nested Open Memory Transactions

Sharing transactional variables x t2 start k1 start k2 write x k2 depends on data generated by k1 read x abort k2 cannot commit until k1 does Communication introduces dependencies Locking schemes avoid dependencies, but prevent communication Dependent transactions commit/abort as one Marino Miculan - UniUD Nested Open Memory Transactions

Nested Open Memory Transactions Transaction merges t1 x t2 start k1 start k2 write x read x Dependent transactions c./a. as one… merge them! Transactions “claim” locations Interaction is limited to “claimed” locations Transaction merge to share locations Marino Miculan - UniUD Nested Open Memory Transactions

Nested Open Memory Transactions Transaction merges k1 claims x, before writing t1 x t2 start k1 start k2 write x read x Dependent transactions c./a. as one… merge them! Transactions “claim” locations Interaction is limited to “claimed” locations Transaction merge to share locations Marino Miculan - UniUD Nested Open Memory Transactions

Transaction merges Dependent transactions c./a. as one… merge them! k1 claims x, before writing t1 x t2 start k1 start k2 k2 merges with k1 in order to access x write x k1+k2 read x Dependent transactions c./a. as one… merge them! Transactions “claim” locations Interaction is limited to “claimed” locations Transaction merge to share locations Marino Miculan - UniUD Nested Open Memory Transactions

Transaction merges Dependent transactions c./a. as one… merge them! k1 claims x, before writing t1 x t2 start k1 start k2 k2 merges with k1 in order to access x write x k1+k2 read x All participants (threads) agree on commit/abort/retry Dependent transactions c./a. as one… merge them! Transactions “claim” locations Interaction is limited to “claimed” locations Transaction merge to share locations Marino Miculan - UniUD Nested Open Memory Transactions

Nested Open Memory Transactions Formal model Without nesting: M. Miculan, M. Peressotti, A. Toneguzzo: Open Transactions on Shared Memory. COORDINATION 2015. With nesting: work in progress Formal operational semantics Marino Miculan - UniUD Nested Open Memory Transactions

Nested Open Memory Transactions Quite few rules… see the paper(s) Marino Miculan - UniUD Nested Open Memory Transactions

Nested Open Memory Transactions Formal model Without nesting: M. Miculan, M. Peressotti, A. Toneguzzo: Open Transactions on Shared Memory. COORDINATION 2015. With nesting: work in progress Formal operational semantics Complete w.r.t. STM Haskell semantics [Harris et al. 2006] Complete w.r.t. TCCS [Koutavas et al. 2014] Complete w.r.t. message-passing communicating transactions [Donnelly et al. 2008] Marino Miculan - UniUD Nested Open Memory Transactions

Nested Open Memory Transactions Opacity [GK07] Definition: A TM implementation is opaque if transactions always observe consistent states Subsumes serializability Important property for transactional memories Opacity corresponds to absence of cycles in the dependency graph Prop: OTM guarantees opacity Merges collapse cycles The dependency graph is always acyclic Works as it is for directed dependency graphs Marino Miculan - UniUD Nested Open Memory Transactions

Nested Open Memory Transactions Nested transactions t1 x t2 k1 k2 k3 Marino Miculan - UniUD Nested Open Memory Transactions

Nested open transactions x t2 k1 k2 k3 Marino Miculan - UniUD Nested Open Memory Transactions

Nesting open transactions x k1 k2 k3 k3 claims x Marino Miculan - UniUD Nested Open Memory Transactions

Nesting open transactions x k1 k2 k3 k3 claims x k3 commits its effects to k2 x is handed over to k2 Marino Miculan - UniUD Nested Open Memory Transactions

Nesting open transactions x k1 k2 k3 k3 claims x k3 commits its effects to k2 x is handed over to k2 k2 aborts, it effects are discarded Marino Miculan - UniUD Nested Open Memory Transactions

Nesting open transactions x t2 k1 h1 Create h3 to match the nesting of k3 and merge them. Merging h1 and k3 brakes nesting k2 k3 h3 h2 k3+h3 commits its effects to k2 and h2 Hence merge k2 and h2 k2+h2 aborts k1 and h1 do not see any effect Marino Miculan - UniUD Nested Open Memory Transactions

Atomically, split: OTM/ITM STM Isolation isolated OTM atomically Atomicity atomic Consistency, Durability IO IO OTM: a monad for atomic transactional actions ITM: a monad for atomic and isolated transactional actions (i.e. a drop-in replacement for STM) Marino Miculan - UniUD Nested Open Memory Transactions

Nested Open Memory Transactions OTM and ITM data ITM a :: * -> * data OTM a :: * -> * -- Sequencing, do notation ----------------------- (>>=) :: t a -> (a -> t b) -> t b return :: a -> t a -- Exceptions ------------------------------------ throw :: Exception e => e -> t a catch :: Exception e => t a -> (e -> t a) -> t a -- Running isolated and atomic computations ------ atomic :: OTM a -> IO a isolated :: ITM a -> OTM a atomically = atomic . isolated Marino Miculan - UniUD Nested Open Memory Transactions

Nested Open Memory Transactions OTM and ITM -- Blocking -------------------------------------- retry :: t a orElse :: t a -> t a -> t a -- Threading ------------------------------------- fork :: OTM () -> OTM ThreadId -- Transactional variables ----------------------- data OTVar a :: * -> * newOTVar :: a -> ITM (OTVar a) readOTVar :: OTVar a -> ITM a writeOTVar :: OTVar a -> a -> ITM () Marino Miculan - UniUD Nested Open Memory Transactions

Choosing informative types Consider the functions g, h: g = isolated (foldM f) h = foldM (isolated f) f :: a -> (OTVar b) -> ITM a foldM :: Monad m => (a->b->m a)-> a-> [b]-> m a Same type: a -> [b] -> OTM a h allows communication inside foldM (hence OTM) g happens in isolation (should be ITM) (isolated, atomic are not monad morphisms) Marino Miculan - UniUD Nested Open Memory Transactions

Nested Open Memory Transactions Example: semaphores type Semaphore = OTVar Int up :: Semaphore -> ITM () up s = do x <- readOTVar s writeOTVar s (x+1) down :: Semaphore -> ITM () down s = do x <- readOTVar s check (x > 0) writeOTVar s (x-1) Marino Miculan - UniUD Nested Open Memory Transactions

Example: synchronizations and transactions Two-party rendezvous inside open transactions: atomic $ do ... isolated (down a1) isolated (up a2) P atomic $ do ... isolated (up a1) isolated (down a2) Q Transactions merge to interact via a1 More than two participants but, they sync in pairs Implements TCCS Marino Miculan - UniUD Nested Open Memory Transactions

Example: Petri nets Multiple transitions can fire at the same time type Place = Semaphore put = up take = down transition :: [Place] -> [Place] -> IO () transition ins outs = fork (forever fire) where fire = atomic $ do mapM_ (isolated . take) ins mapM_ (isolated . put) outs Firing is atomic Single updates are isolated Marino Miculan - UniUD Nested Open Memory Transactions

Nested Open Memory Transactions Example: Petri nets Marino Miculan - UniUD Nested Open Memory Transactions

Example: Dining Philosophers Marino Miculan - UniUD Nested Open Memory Transactions

Conclusion and further work Open memory transactions in Haskell transactions may interact at runtime interaction are implicit by access to shared memory greater space/time decoupling atomicity and isolation have their own monads formal semantics (COORDINATION 2015) implementation in Haskell (no dep. on GHC) drop-in replacement for STM Future work Better integration with the Haskell runtime Transaction invariants (smarter scheduling) Axiomatization (effect algebras) Marino Miculan - UniUD Nested Open Memory Transactions

Thanks for your attention.