Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parallel computation in Haskell

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
like image 836
Cartesius00 Avatar asked Sep 03 '12 14:09

Cartesius00


People also ask

Is Haskell is good at parallel programming?

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);

Is Haskell parallel?

Haskell supports both pure parallelism and explicit concurrency.

Is Haskell single threaded?

Without -threaded , the Haskell process uses a single OS thread only, and multithreaded foreign calls are not supported.


1 Answers

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

like image 60
Vic Smith Avatar answered Oct 06 '22 20:10

Vic Smith