Hi,<br>I've been trying to use Control.Concurrent.mergeIO to parallelize computation, and can't make it work. In the sample program below, I expect the function 'parallelTest' to be almost twice as fast as 'sequentialTest', and to compute its results in two threads, as implied by the documentation for mergeIO. This is not what happens. If I link my program with the option '-threaded', the running process does have three threads. If I run with the option "+RTS -N2", the process will have 5 threads. In no case does the process appear to be using more than one CPU, and in fact it is slower with the threading options turned on.<br>
<br>I'm sure I am doing something obviously (to someone else) wrong. Any ideas?<br><br>I am running the latest version of Mac OSX on a core2 duo machine with 2 cores, using ghc version 6.10.4.<br><br>Cheers, Brock<br>
<br>My test program follows:<br><br>{-# OPTIONS_GHC -fglasgow-exts #-}<br>module Main where<br><br>import Control.Concurrent<br>import Random<br><br>doSum :: RandomGen g => g -> Int -> Integer<br>doSum g count<br>
= let runner curG sum numDone<br> | numDone == count = sum<br> | otherwise<br> = let (newNum :: Integer, newG) = random curG<br> newSum = sum + newNum<br> newNumDone = numDone + 1<br>
in ((runner $! newG) $! newSum) $! newNumDone<br> in runner g 0 0<br><br>sequentialTest<br> = do let gen = mkStdGen 0<br> (g0,g1) = split gen<br> count = 10000000<br> sum0 = doSum g0 count<br>
sum1 = doSum g1 count<br> total = sum0 + sum1<br> putStrLn $ "total: " ++ show total<br><br>parallelTest<br> = do let gen = mkStdGen 0<br> (g0,g1) = split gen<br> count = 10000000<br>
sum0 = doSum g0 count<br> sum1 = doSum g1 count<br> [res0, res1] <- mergeIO [sum0] [sum1]<br> let total = res0 + res1<br> putStrLn $ "total: " ++ show total<br>main<br> = parallelTest<br>