Parallelism and Concurrency

Slides:



Advertisements
Similar presentations
Parallel Haskell Tutorial: The Par Monad Simon Marlow Ryan Newton Simon Peyton Jones.
Advertisements

Concurrency unlocked transactional memory for composable concurrency Tim Harris Maurice Herlihy Simon Marlow Simon Peyton Jones.
Enabling Speculative Parallelization via Merge Semantics in STMs Kaushik Ravichandran Santosh Pande College.
Kathleen Fisher cs242 Reading: “A history of Haskell: Being lazy with class”,A history of Haskell: Being lazy with class Section 6.4 and Section 7 “Monads.
Kathleen Fisher cs242 Reading: “Beautiful Concurrency”,Beautiful Concurrency “The Transactional Memory / Garbage Collection Analogy”The Transactional Memory.
Software Transactional Memory Steve Severance – Alpha Heavy Industries.
Tim Harris Researcher Microsoft Corporation. Source: Table from
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.
Semi-Explicit Parallel Programming in Haskell Satnam Singh Microsoft Research Cambridge Leeds2009.
1 Lecture 7: Transactional Memory Intro Topics: introduction to transactional memory, “lazy” implementation.
Exceptions and side-effects in atomic blocks Tim Harris.
Language Support for Lightweight transactions Tim Harris & Keir Fraser Presented by Narayanan Sundaram 04/28/2008.
Concurrency and Software Transactional Memories Satnam Singh, Microsoft Faculty Summit 2005.
Department of Computer Science Presenters Dennis Gove Matthew Marzilli The ATOMO ∑ Transactional Programming Language.
Intel Concurrent Collections (for Haskell) Ryan Newton*, Chih-Ping Chen*, Simon Marlow+ *Intel +Microsoft Research Software and Services Group Jul 29,
Intel Concurrent Collections (for Haskell) Ryan Newton, Chih-Ping Chen, Simon Marlow Software and Services Group Jul 27, 2010.
Lightweight Concurrency in GHC KC Sivaramakrishnan Tim Harris Simon Marlow Simon Peyton Jones 1.
David Walker Optional Reading: “Beautiful Concurrency”, “The Transactional Memory / Garbage Collection Analogy” “A Tutorial on Parallel and Concurrent.
Programming Paradigms for Concurrency Part 2: Transactional Memories Vasu Singh
Kathleen Fisher cs242 Reading: “Beautiful Concurrency”,Beautiful Concurrency “The Transactional Memory / Garbage Collection Analogy”The Transactional Memory.
Reduced Hardware NOrec: A Safe and Scalable Hybrid Transactional Memory Alexander Matveev Nir Shavit MIT.
CSE 230 Concurrency: STM Slides due to: Kathleen Fisher, Simon Peyton Jones, Satnam Singh, Don Stewart.
Problems with Send and Receive Low level –programmer is engaged in I/O –server often not modular –takes 2 calls to get what you want (send, followed by.
Games Development 2 Concurrent Programming CO3301 Week 9.
Optimistic Design 1. Guarded Methods Do something based on the fact that one or more objects have particular states  Make a set of purchases assuming.
Internet Software Development Controlling Threads Paul J Krause.
Synchronized and Monitors. synchronized is a Java keyword to denote a block of code which must be executed atomically (uninterrupted). It can be applied.
Haskell - A Perspective Presented by Gábor Lipták April 2011.
CS162 Week 5 Kyle Dewey. Overview Announcements Reactive Imperative Programming Parallelism Software transactional memory.
QIAN XI COS597C 10/28/2010 Explicit Concurrent Programming in Haskell.
Software Transactional Memory Should Not Be Obstruction-Free Robert Ennals Presented by Abdulai Sei.
How D can make concurrent programming a piece of cake Bartosz Milewski D Programming Language.
Fall 2008Programming Development Techniques 1 Topic 20 Concurrency Section 3.4.
AtomCaml: First-class Atomicity via Rollback Michael F. Ringenburg and Dan Grossman University of Washington International Conference on Functional Programming.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
R 1 Transactional abstractions: beyond atomic update Tim Harris r.
Parallelism and Concurrency Koen Lindström Claessen Chalmers University Gothenburg, Sweden Patrik Jansson.
December 1, 2006©2006 Craig Zilles1 Threads & Atomic Operations in Hardware  Previously, we introduced multi-core parallelism & cache coherence —Today.
Functional Programming
Maurice Herlihy and J. Eliot B. Moss,  ISCA '93
Multithreading / Concurrency
Laziness and Infinite Datastructures
Parallelism and Concurrency
(Nested) Open Memory Transactions in Haskell
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
Atomic Operations in Hardware
Atomic Operations in Hardware
Software Transactional Memory
Maurice Herlihy, Victor Luchangco, Mark Moir, William N. Scherer III
Changing thread semantics
Transactional Memory Semaphores, monitors, and conditional critical regions all suffer from limitations based on lock semantics Naïve synchronization may.
Control Flow Chapter 6.
Lecture 6: Transactions
Yiannis Nikolakopoulos
Design and Implementation Issues for Atomicity
Software Transactional Memory Should Not be Obstruction-Free
Concurrency and Immutability
Locking Protocols & Software Transactional Memory
- When you approach operating system concepts there might be several confusing terms that may look similar but in fact refer to different concepts:  multiprogramming, multiprocessing, multitasking,
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Programming Languages
Threads and Multithreading
CSC 143 Java Errors and Exceptions.
CSE 332: Concurrency and Locks
Software Engineering and Architecture
Concurrency in GO CS240 23/8/2017.
Presentation transcript:

Parallelism and Concurrency Patrik Jansson Koen Lindström Claessen Chalmers University Gothenburg, Sweden 1

Expressing Parallelism In a pure, lazy language Evaluation is done when needed Evaluation order does not affect meaning Many sub-expr. could be eval. in parallel But how can we express that? 2

pseq x y: Evaluate first x, and then y Two primitives pseq x y: Evaluate first x, and then y pseq :: a -> b -> b -- denotational semantics: pseq _|_ y = _|_ pseq _ y = y par thread main: Evaluate thread in parallel, and immediately return main par :: a -> b -> b -- denotational semantics: par thread main = main 3

Example normal, paraNormal :: X -> Y -> N paraNormal x y = x `par` y `par` normal x y Idea: Write ”normal” program first, then add parallelism to speed it up 4

Example: QuickSort qsort :: (Ord a) => [a] -> [a] qsort [] = [] qsort [x] = [x] qsort (x:xs) = losort `par` hisort `par` losort ++ (x:hisort) where losort = qsort [y | y <- xs, y < x ] hisort = qsort [y | y <- xs, y >= x ] 5

QuickSort (II) qsort :: (Ord a) => [a] -> [a] qsort [] = [] qsort [x] = [x] qsort (x:xs) = force losort `par` force hisort `par` losort ++ (x:hisort) where losort = qsort [y | y <- xs, y < x ] hisort = qsort [y | y <- xs, y >= x ] force :: [a] -> () force [] = () force (x:xs) = x `pseq` force xs 6

Example: Parallel Map pmap :: (a -> b) -> [a] -> [b] pmap f [] = [] pmap f (x:xs) = fx `par` fxs `par` fx:fxs where fx = f x fxs = pmap f xs 7

Evaluation Strategies -- From module Control.Parallel.Strategies (v1) type Done = () type Strategy a = a -> Done using :: a -> Strategy a -> a a `using` strat = strat a `pseq` a 8

Evaluation Strategies (II) rwhnf :: Strategy a -- Called rseq in later versions class NFData a where rnf :: Strategy a -- Evaluate to normal form parList :: Strategy a -> Strategy [a] parList strat [] = () parList strat (x:xs) = strat x `par` parList strat xs 9

Parallel Evaluation Strategies pmap :: Strategy b -> (a -> b) -> [a] -> [b] pmap strat f xs = map f xs `using` parList strat 10

More ... Implemented in GHC -- hackage parallel Control.Parallel (par, pseq) Control.Parallel.Strategies Also look at: Control.Concurrent (ghc -threaded) Control.Monad.STM RWH: Ch. 24 and Ch. 28 11

Concurrent Programming Processes Concurrency Parallelism Shared resources Communication Locks Blocking 12

Concurrent Haskell Control.Concurrent starting/killing processes fork :: IO a -> IO Pid kill :: Pid -> IO () a shared resource type MVar a newEmptyMVar :: IO (MVar a) takeMVar :: MVar a -> IO a putMVar :: MVar a -> a -> IO () blocking actions 13

Concurrent Haskell Control.Concurrent.Chan an unbounded channel type Chan a newChan :: IO (Chan a) readChan :: Chan a -> IO a writeChan :: Chan a -> a -> IO () write returns immediately 14

Typical Concurrent Programming Today Use MVars (or similar concepts) to implement ”locks” Grab the lock Block if someone else has it Do your thing Release the lock

Problems With Locking Races Forgotten lock Deadlock Grabbing/releasing locks in wrong order Error recovery Invariants Locks 16

The Biggest Problem Locks are not compositional! Compositional = build a working system from working pieces action1 = withdraw a 100 action2 = deposit b 100 action3 = do withdraw a 100 deposit b 100 Inconsistent state 17

Solution (?) Expose the locks Danger of deadlock! action3 = do lock a lock b withdraw a 100 deposit b 100 release a release b – better but error-prone if a < b then do lock a; lock b else do lock b; lock a 18

Need to keep track of all locks of an action, and compose these More Problems action4 = do ... action5 = do ... action6 = action4 AND action5 Impossible! Need to keep track of all locks of an action, and compose these

Conclusion Programming with explicit locks is Not compositional Not scalable (to many cores / threads) Gives you a headache Leads to code with errors ... A new concurrent programming paradigm is sorely needed

Idea behind STM Borrow ideas from database people Transactions Add ideas from functional programming Computations are first-class values What side-effects can happen where is controlled Et voila!

Software Transactional Memory (STM) First ideas in 1993 New developments in 2005 Simon Peyton Jones Simon Marlow Tim Harris Maurice Herlihy

”write sequential code, and wrap atomically around it” Atomic Transactions action3 = atomically $ do withdraw a 100 deposit b 100 ”write sequential code, and wrap atomically around it”

How Does It Work? Execute body without locks action3 = atomically $ do withdraw a 100 deposit b 100 Execute body without locks Each memory access is logged No actual update is performed At the end, we try to commit the log to memory Commit may fail, then we retry the whole atomic block

Transactional Memory No locks, so no race conditions No locks, so no deadlocks Error recovery is easy; an exception aborts the whole block and retries Simple code, and scalable

Caveats Absolutely forbidden: To read a transaction variable outside an atomic block To write to a transaction variable outside an atomic block Side-effects inside an atomic block...

Simon’s Missile Program action3 = atomically $ do withdraw a 100 launchNuclearMissiles deposit b 100 launchNuclearMissiles :: IO () No side effects allowed! (type error)

STM Haskell Control.Concurrent.STM First fully-fledged implementation of STM Impl.s for C++, Java, C# available But it is difficult to solve the problems In Haskell, it is easy! Controlled side-effects

STM Haskell Control.Concurrent.STM type STM a instance Monad STM type TVar a newTVar :: a -> STM (TVar a) readTVar :: TVar a -> STM a writeTVar :: TVar a -> a -> STM () atomically :: STM a -> IO a -- run function

Example type Account = TVar Int deposit :: Account -> Int -> STM () deposit r i = do v <- readTVar r writeTVar r (v+i) main = do ... atomically (deposit r 13) ...

Example retry :: STM a withdraw :: Account -> Int -> STM () withdraw r i = do v <- readTVar r if v < i then retry else writeTVar r (v-i) main = do ... atomically (do withdraw r1 4 deposit r2 4 ) ...

Referential transparency! Retrying An atomic block is retried when the programmer says so, or the commit at the end fails. Before retrying, the STM implementation waits until one of the variables used in the atomic block is changed Why? Referential transparency!

Compositional Choice orElse :: STM a -> STM a -> STM a main = do ... atomically ( withdraw r1 4 `orElse` withdraw r2 4) ... instance MonadPlus STM where mzero = retry mplus = orElse -- Laws m1 `orElse` (m2 `orElse` m3) = (m1 `orElse` m2) `orElse` m3 retry `orElse` m = m m `orElse` retry = m

Blocking or not? nonBlockWithdraw :: Account -> Int -> STM Bool nonBlockWithdraw r i = do withdraw r i return True `orElse` return False Choice of blocking / non-blocking is up to the caller, not the method (here ”withdraw”) itself

Example: MVars MVars can be implemented using TVars type MVar a = TVar (Maybe a) (Demo if time permits.)

STM in Haskell summary Safe transactions through type safety Degenerate ”IO-like” monad STM We can only access TVars TVars can only be accessed in STM monad Referential transparency Explicit retry -- expressiveness Compositional choice -- expressiveness

Problems in C++ / Java / C# Retry semantics IO in atomic blocks Access of transaction variables outside of atomic blocks Access to regular variables inside of atomic blocks

STM Haskell Control.Concurrent.STM type STM a instance Monad STM type TVar a newTVar :: a -> STM (TVar a) readTVar :: TVar a -> STM a writeTVar :: TVar a -> a -> STM () atomically :: STM a -> IO a retry :: STM a orElse :: STM a -> STM a -> STM a