Cartesius00
Cartesius00

Reputation: 24364

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

Upvotes: 8

Views: 337

Answers (1)

Vic Smith
Vic Smith

Reputation: 3497

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

Upvotes: 10

Related Questions