Parallel forkOS does not work on ubuntu

Hoang Truong hoangta at comp.nus.edu.sg
Mon Dec 8 21:27:27 EST 2008


Hello everybody,
I am following "A Tutorial on Parallel and Concurrent Programming in
Haskell" and I have a problem with making Haskell to use my multi-cores
(Core 2 Quad  CPU).

The Haskel version I used is GHC 6.10.1, for Haskell 98. I compile my below
program with command: ghc --make -threaded -debug thread0.hs, and run
with: thread0 +RTS -N4 while watching the cpu usage on another terminal (by:
mpstat -P ALL 1 100), but the program uses only one core of my Ubuntu
Linux.

Do any of you know why or has any suggestions? Below is my program:

import Control.Concurrent
import Control.Concurrent.MVar

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

dowork =
putStrLn ("fib 35 = " ++ (show (fib 35)))
threadA :: MVar Int -> MVar Int -> MVar Int -> IO ()
threadA valueToSendMVar valueToReadMVar valueToQuit
= do
-- some work
dowork
-- perform rendezvous
putMVar valueToSendMVar 30 -- send value
v <- takeMVar valueToReadMVar
putStrLn ("result, fib 30 = " ++ (show v))
dowork
-- notify done
putMVar valueToQuit 0 -- send value

threadB :: MVar Int -> MVar Int -> MVar Int -> IO ()
threadB valueToReceiveMVar valueToSendMVar valueToQuit
= do
-- some work
dowork
-- perform rendezvous by waiting
z <- takeMVar valueToReceiveMVar
putMVar valueToSendMVar (fib z)
-- continue with other work
dowork
-- notify done
putMVar valueToQuit 0 -- send value

main :: IO ()
main
= do
aQuitA <- newEmptyMVar
aQuitB <- newEmptyMVar
aMVar <- newEmptyMVar
bMVar <- newEmptyMVar
forkOS (threadA aMVar bMVar aQuitA )
forkOS (threadB aMVar bMVar aQuitB )
-- wait for threadA and threadB
takeMVar aQuitA
takeMVar aQuitB
return ()


Thanks a lot,
Hoang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/glasgow-haskell-users/attachments/20081209/380acfa2/attachment.htm


More information about the Glasgow-haskell-users mailing list