[Haskell-cafe] Parallel compilation and execution?

Daniel Fischer daniel.is.fischer at googlemail.com
Thu May 26 14:16:48 CEST 2011


On Thursday 26 May 2011 13:24:09, michael rice wrote:
> How do I compile and run this parallel program?
> Michael
> ===========================
> import Control.Parallel
> nfib :: Int -> Int
> nfib n | n <= 1 = 1       
>        | otherwise = par n1 (seq n2 (n1 + n2 + 1))

The 'seq' here should be a 'pseq' to ensure that n2 is (started to be) 
calculated before (n1 + n2 +1) is evaluated (iirc, currently, both work 
with ghc, but it's not guaranteed for seq).
 
>          where 
>            n1 = nfib (n-1)               
>            n2 = nfib (n-2)
>  {-nfib :: Int -> Intnfib n | n <= 1 = 1
>           | otherwise = nfib (n-1) + nfib (n-2)-}
> 
> main = do putStrLn $ show $ nfib 39
> ===========================
> [michael at hostname ~]$ ghc --make -threaded nfib.hs
> [michael at hostname ~]$ ./nfib +RTS -N3

In principle, that is the correct way to compile and run it (note that with 
ghc-7, you don't need the --make).

> nfib: the flag -N3 requires the program to be built with
> -threaded

Have you previously compiled the programme without -threaded?
Unless you snipped the compilation messages, their absence strongly 
indicates so.

If that is the case, you fell victim to ghc's recompilation-avoidance which 
doesn't keep track of the flags a programme/module was compiled with, so it 
saw that nothing [as far as it knows] changed, hence didn't recompile.

Then removing .o and .hi or passing -fforce-recomp will make it recompile 
and link with the threaded runtime.



More information about the Haskell-Cafe mailing list