I'm trying to learn how to use the Control.Parallel
module, but I think I didn't get it right.
I'm trying to run the following code (fibs.hs).
import Control.Parallel
fib :: Int -> Int
fib 0 = 0
fib 1 = 1
fib n = p `par` (q `pseq` (p + q))
where
p = fib (n-1)
q = fib (n-2)
main = print $ fib 30
I compiled this with:
ghc -O2 --make -threaded fibs.hs
And then I get the following results executing this program (output of a Python script that runs each program 100 times and returns the average and standard deviation of the execution time):
./fibs +RTS -N1 -> avg= 0.060203 s, deviation = 0.004112 s
./fibs +RTS -N2 -> avg= 0.052335 s, deviation = 0.006713 s
./fibs +RTS -N3 -> avg= 0.052935 s, deviation = 0.006183 s
./fibs +RTS -N4 -> avg= 0.053976 s, deviation = 0.007106 s
./fibs +RTS -N5 -> avg= 0.055227 s, deviation = 0.008598 s
./fibs +RTS -N6 -> avg= 0.055703 s, deviation = 0.006537 s
./fibs +RTS -N7 -> avg= 0.058327 s, deviation = 0.007526 s
My questions are:
What exactly is happening when I evaluate:
a `par` (b `pseq` (a + b)) ?
I understand that a
par
b is supposed to hint the compiler about calculating a in parallel with b and return b. OK. But what doespseq
do?Why do I see such a small performance increase? I'm running this in an Intel Core 2 Quad machine. I'd expect that running with -N5 or -N6 wouldn't make a real difference in performance or that the program would actually start to perform very badly. But why do I see no improvement from -N2 to -N3 and why is the initial improvement so small?
As Don explained, the problem is that you are creating too many sparks. Here's how you might rewrite it to get a good speedup.
demonstration:
You're creating an exponential number of sparks (think of how many recursive calls you're creating here). To actually get good parallelism you need to create less parallel work in this case, since your hardware can't handle that many threads (and so GHC doesn't make them).
The solution is to use a cutoff strategy, as described in this talk: http://donsbot.wordpress.com/2009/09/05/defun-2009-multicore-programming-in-haskell-now/
Basically, switch over to the straight line version once you reach a certain depth, and use +RTS -sstderr to see how many sparks are being converted, so you can determine if you're wasting work or not.
Since nobody gave a definitive answer about
pseq
, here's the official description:Re (1):
par
allowsa
to be computed in another thread. I am guessing here, but I thinkpseq
behaves much like aseq
: that it forces the first result to be computed first (well,seq
isn't guaranteed to do this, but in practice on GHC it does). So in this case, the computation ofa
is forked off as one thread, and the other thread computesb
and then sumsa
andb
.Re (2): This is a pretty trivial computation being forked off to other threads; it's probably just as fast for the cpu to just calculate it itself. I'm betting the overhead of threads is hurting almost as much as helping for this simple computation.