When I run this Haskell snippet it gets only 1 CPU loaded. Both f
and g
are non-sense, but shouldn't it load two CPUs when available? Compiled as ghc -O2 snippet.hs
.
f x = 1 + (f $! x)
g x = 5 + (g $! x)
z = a `par` b `seq` a+b
where
a = f 3
b = g 5
main = do
print z
Parallel and concurrent programming is much easier in Haskell: it's pure, which means that there are no mutations to observe and all data can be shared between threads freely; it supports modern techniques like STM (among many other options for parallel and concurrent programming);
Haskell supports both pure parallelism and explicit concurrency.
Without -threaded , the Haskell process uses a single OS thread only, and multithreaded foreign calls are not supported.
You need to compile with the threaded option, ie ghc -O2 -threaded snippet.hs
, and then pass the executable the number of cores on the command line as follows for four cores:
./snippet +RTS -N4
Or you can have the machine choose the number of cores using just -N
.
See http://www.haskell.org/haskellwiki/Haskell_for_multicores
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With