Skip to content

`par` `pseq` does not work as expected

The following Wombat program is from "A Tutorial on Parallel and Concurrent Programming in Haskell". It does not work as expected on my computer (Core(TM)2 Quad CPU). Only one core is used when I watch by "mpstat -P ALL 1 200". I compile and run by:

ghc --make -threaded wombat.hs
./wombat +RTS -N4

and the result is:

par sum: 119201850
par time: 20.932636 seconds.
seq sum: 119201850
seq time: 20.926783 seconds.

Please check if is is a bug.

---- wombat.hs ----
import System.Time
import Control.Parallel

fib :: Int -> Int
fib 0 = 0
fib 1 = 1
fib n = fib (n-1) + fib (n-2)

secDiff :: ClockTime -> ClockTime -> Float
secDiff (TOD secs1 psecs1) (TOD secs2 psecs2) = 
	fromInteger (psecs2 - psecs1) / 1e12 + fromInteger (secs2 - secs1)

mkList :: Int -> [Int]
mkList n = [1..n-1]

relprime :: Int -> Int -> Bool
relprime x y = gcd x y == 1

euler :: Int -> Int
euler n = length (filter (relprime n) (mkList n))

sumEuler :: Int -> Int
sumEuler = sum . (map euler) . mkList

seqSumFibEuler:: Int -> Int -> Int
seqSumFibEuler a b = fib a + sumEuler b

parSumFibEuler a b = f `par` (e `pseq` (e+ f)) 
	where  
		f = fib a 
		e = sumEuler b

r1 = seqSumFibEuler 40 7450
r2 = parSumFibEuler 40 7450

main :: IO ()
main = 
	do 
		t0 <- getClockTime
		pseq r1 (return())
		t1 <- getClockTime
		putStrLn ("seq sum: " ++ show r1)
		putStrLn ("seq time: " ++ show (secDiff t0 t1) ++ " seconds.")
		t0 <- getClockTime
		pseq r2 (return())
		t1 <- getClockTime
		putStrLn ("par sum: " ++ show r2)
		putStrLn ("par time: " ++ show (secDiff t0 t1) ++ " seconds.")
Edited by Simon Marlow
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information