Thank, Daniel

Multiple threads are in evidence in my system monitor, but I wonder why I'm getting two different answers, one twice the other. The first is the parallel solution and the second is the non.

Michael

===========

{-
import Control.Parallel

nfib :: Int -> Int
nfib n | n <= 1 = 1
       | otherwise = par n1 (pseq n2 (n1 + n2 + 1))
                     where n1 = nfib (n-1)
                           n2 = nfib (n-2)
-}

nfib :: Int -> Int
nfib n | n <= 1 = 1
       | otherwise = nfib (n-1) + nfib (n-2)

main = do putStrLn $ show $ nfib 39

=============

[michael@hostname ~]$ ghc --make -threaded nfib.hs
[1 of 1] Compiling Main             ( nfib.hs, nfib.o )
Linking nfib ...
[michael@hostname ~]$ ./nfib +RTS -N3
204668309
[michael@hostname ~]$ ghc --make nfib.hs
[1 of 1] Compiling Main             ( nfib.hs, nfib.o )
Linking nfib ...
[michael@hostname ~]$ ./nfib
102334155
[michael@hostname ~]$ 


--- On Thu, 5/26/11, Daniel Fischer <daniel.is.fischer@googlemail.com> wrote:

From: Daniel Fischer <daniel.is.fischer@googlemail.com>
Subject: Re: [Haskell-cafe] Parallel compilation and execution?
To: haskell-cafe@haskell.org
Cc: "michael rice" <nowgate@yahoo.com>
Date: Thursday, May 26, 2011, 8:16 AM

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@hostname ~]$ ghc --make -threaded nfib.hs
> [michael@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.