Multicore Haskell Now!
Multicore Haskell Now!
• Language reasons:
– Purity, laziness and types mean you
can find more parallelism in your code
– No specified execution order
– Speculation and parallelism safe.
• Purity provides inherently more parallelism
• High level: shorter code.
main :: IO ()
main = print (take 1000 primes)
primes :: [Int]
primes = sieve [2..]
where
sieve :: [Int] -> [Int]
sieve (p:xs) =
p : sieve [ x | x <- xs, x `mod` p > 0]
$ ./A
[2,3,5,7,11,13,17,19,23, … 7883,7901,7907,7919]
getChar :: IO Char
putChar :: Char → IO ()
Such side-effecting code can only interact with other side effecting code.
It can't mess with pure code. Checked statically.
– Compile with
• -threaded -O2
– Run with
• +RTS -N2
• +RTS -N4
• ...
• +RTS -N64
• ...
© 2009 Galois, Inc. All rights reserved.
Warm up lap
• Check your machines are working
• Find out how many cores are available
• Fire up ghc
• Make sure you've got a threaded runtime
main = do
printf "Compiled with %s-%s on %s/%s\n"
compilerName
(showVersion compilerVersion)
os arch
f x y = (x * y) + (y ^ 2)
http://hackage.haskell.org/packages/parallel
import Control.Parallel
$ cabal unpack paralllel
Also ships with the Haskell Platform.
© 2009 Galois, Inc. All rights reserved.
The `par` combinator
a `par` b
pseq :: a → b → b
f `par` e `pseq` f + e
• Fine-grained parallelism
• Sparks need to be cheap
• Work-strealing thread pool in runtime, underneath
• Relies on purity: no side effects to get in the way
• Takes practice to learn where `par` is beneficial
• A good tool to have in the kit
forkIO :: IO () → IO ThreadId
import Control.Concurrent
import System.Directory
main = do
forkIO (writeFile "xyz" "thread was here")
v ← doesFileExist "xyz"
print v
worker ch = forever $ do
v ← readFile "/proc/loadavg"
writeChan ch v – – send msg back to receiver
threadDelay (10^5)
© 2009 Galois, Inc. All rights reserved.
Intel Haskell Concurrent Collections
• Intel Concurrent Collections for Haskell
– Scalable parallel programs over computation graphs
– LGPLd
– Pure parallel programming model built over forkIO
and Chans
– Try it out.
• http://software.intel.com/en-
us/blogs/2010/05/27/announcing-intel-
concurrent-collections-for-haskell-01/
• An optimisitic model
– Transactions run inside atomic blocks assuming no
conflicts
– System checks consistency at the end of the transaction
– Retry if conflicts
– Requires control of side effects (handled in the type system
)
© 2009 Galois, Inc. All rights reserved.
The stm package
• http://hackage.haskell.org/packages/stm
• import Control.Concurrent.STM
• $ cabal unpack stm
• In the Haskell Platform
© 2009 Galois, Inc. All rights reserved.
STM
data STM a
atomically :: STM a → IO a
retry :: STM a
orElse :: STM a → STM a → STM a
data TVar a
newTVar :: a → STM (TVar a)
readTVar :: TVar a → STM a
writeTVar :: TVar a → a → STM ()
• No deadlocks.
• Lock safe code when composed is still lock safe
plus
• Breakthrough:
Flattening: a compiler transformation to systematically
transform any nested data parallel program into a flat one
Requires -fvectorize