Hi Simon,<div><br></div><div>I tried with forkIO and added another dowork functions but the result is the same: only one core is used, three other cores are idle. Do you have any other suggestions? Is there anything I should take care when installing GHC?</div>
<div><br></div><div>I also did try the Wombat.hs from the tutorial, but only one core is used and the times are almost the same.</div><div><br></div><div><div>seq sum: 119201850</div><div>seq time: 20.959932 seconds.</div>
<div>par sum: 119201850</div><div>par time: 20.959547 seconds.</div><div><br></div><div>--------<br></div></div><div>import System.Time</div><div>import Control.Parallel</div><div><br></div><div>fib :: Int -> Int</div>
<div>fib 0 = 0</div><div>fib 1 = 1</div><div>fib n = fib (n-1) + fib (n-2)</div><div><br></div><div>secDiff :: ClockTime -> ClockTime -> Float</div><div>secDiff (TOD secs1 psecs1) (TOD secs2 psecs2) = </div><div><span class="Apple-tab-span" style="white-space:pre">        </span>fromInteger (psecs2 - psecs1) / 1e12 + fromInteger (secs2 - secs1)</div>
<div><br></div><div>mkList :: Int -> [Int]</div><div>mkList n = [1..n-1]</div><div><br></div><div>relprime :: Int -> Int -> Bool</div><div>relprime x y = gcd x y == 1</div><div><br></div><div>euler :: Int -> Int</div>
<div>euler n = length (filter (relprime n) (mkList n))</div><div><br></div><div>sumEuler :: Int -> Int</div><div>sumEuler = sum . (map euler) . mkList</div><div><br></div><div>sumFibEuler:: Int -> Int -> Int</div>
<div>sumFibEuler a b = fib a + sumEuler b</div><div><br></div><div>parSumFibEuler a b = f `par` (e `pseq` (e+ f)) </div><div><span class="Apple-tab-span" style="white-space:pre">        </span>where </div><div><span class="Apple-tab-span" style="white-space:pre">                </span>f = fib a </div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>e = sumEuler b</div><div><br></div><div>r1 :: Int</div><div>r1 = sumFibEuler 40 7450</div><div><br></div><div>r2 :: Int</div><div>r2 = parSumFibEuler 40 7450</div>
<div><br></div><div><br></div><div>main :: IO ()</div><div>main = </div><div><span class="Apple-tab-span" style="white-space:pre">        </span>do </div><div><span class="Apple-tab-span" style="white-space:pre">                </span>t0 <- getClockTime</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>pseq r1 (return())</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>t1 <- getClockTime</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>putStrLn ("seq sum: " ++ show r1)</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>putStrLn ("seq time: " ++ show (secDiff t0 t1) ++ " seconds.")</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>t0 <- getClockTime</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>pseq r2 (return())</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>t1 <- getClockTime</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>putStrLn ("par sum: " ++ show r2)</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>putStrLn ("par time: " ++ show (secDiff t0 t1) ++ " seconds.")</div><div>----- <br></div><div><br></div><div><div>Many thanks,</div>
<div><br></div><div>Hoang<br></div><div><br></div></div><div><br><div class="gmail_quote">On Tue, Dec 9, 2008 at 7:26 PM, Simon Marlow <span dir="ltr"><<a href="mailto:marlowsd@gmail.com" target="_blank">marlowsd@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>Hoang Truong wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hello everybody,<br>
<br>
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). <br>
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. <br>
Do any of you know why or has any suggestions? Below is my program:<br>
</blockquote>
<br></div>
Why do people still insist on using forkOS? You don't need forkOS unless you need to call C libraries that use thread-local state. Otherwise, it will just reduce your performance compared to forkIO. Admittedly the documentation for forkOS has been misleading in the past, but I think the current version is pretty clear:<br>
<br>
<a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Control-Concurrent.html#v%3AforkOS" target="_blank">http://www.haskell.org/ghc/docs/latest/html/libraries/base/Control-Concurrent.html#v%3AforkOS</a><div>
<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
import Control.Concurrent<br>
import Control.Concurrent.MVar<br>
<br>
fib :: Int -> Int<br>
fib 0 = 0<br>
fib 1 = 1<br>
fib n = fib (n-1) + fib (n-2)<br>
<br>
dowork =<br>
putStrLn ("fib 35 = " ++ (show (fib 35)))<br>
</blockquote>
<br></div>
Perhaps you were expecting "fib 35" to be repeatedly executed each time you call dowork? Laziness means it only gets evaluated once.<br>
<br>
Cheers,<br><font color="#888888">
Simon<br>
</font></blockquote></div><br></div>