Uses forkOS all CPUS on-board?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Uses forkOS all CPUS on-board?

Kolář Dušan
Hello haskellers,

  in past few days, a lot of stuff on concurrency went through
the conference. I'm trying to use posted things and my own.
They work, even in a context switching regime, but I can't
exploit all the CPU's on my computer. Always is "active"
just one thread and, thus, the computation is even slower
than having a sequential version. Below, you can find
my code - it computes nothing useful, it's been simplified
to test parallelism, nothing else. Where's my error?

  Regards

    Dusan


import Control.Concurrent

-- computes nothing too much useful, but takes a long time ;-)
sumAllSums [] = 0
sumAllSums l@(_:xs) = sumlist 0 l + sumAllSums xs
    where sumlist res [] = res
          sumlist sr  (v:vs) = sumlist (sr+v) vs


main = do
  putStrLn "Starting..."
  mv1 <- newEmptyMVar
  mv2 <- newEmptyMVar
  t1 <- forkOS $ mkSum1 mv1
  t2 <- forkOS $ mkSum2 mv2
  tt mv1 mv2
  forkOS $ do killThread t1
              killThread t2
  putStrLn "Done!"
  where
    mkSum1 mv = do
      let res = sumAllSums [1..10000]
      let ms1 = "Sum1: " ++ show res
      seq (length ms1) (putMVar mv ms1)
    mkSum2 mv = do
      let res = sumAllSums [1..10001]
      let ms2 = "Sum2: " ++ show res
      seq (length ms2) (putMVar mv ms2)
    tt mv1 mv2 = do
      yield
      mr1 <- tryTakeMVar mv1
      case mr1 of
        Just r1 -> do
          yield
          putStrLn r1
          yield
          r2 <- takeMVar mv2
          putStrLn r2
        Nothing -> do
          mr2 <- tryTakeMVar mv2
          case mr2 of
            Just r2 -> do
              yield
              putStrLn r2
              yield
              r1 <- takeMVar mv1
              putStrLn r1
            Nothing -> tt mv1 mv2
 

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Uses forkOS all CPUS on-board?

Sebastian Sylvan
On 11/23/05, Dusan Kolar <[hidden email]> wrote:

> Hello haskellers,
>
>   in past few days, a lot of stuff on concurrency went through
> the conference. I'm trying to use posted things and my own.
> They work, even in a context switching regime, but I can't
> exploit all the CPU's on my computer. Always is "active"
> just one thread and, thus, the computation is even slower
> than having a sequential version. Below, you can find
> my code - it computes nothing useful, it's been simplified
> to test parallelism, nothing else. Where's my error?
>

Did you compile it with "-threaded"?

--
Sebastian Sylvan
+46(0)736-818655
UIN: 44640862
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re[2]: Uses forkOS all CPUS on-board?

Bulat Ziganshin
Hello Sebastian,

Wednesday, November 23, 2005, 6:46:54 PM, you wrote:

>> They work, even in a context switching regime, but I can't
>> exploit all the CPU's on my computer. Always is "active"
>> just one thread

SS> Did you compile it with "-threaded"?

this will not help. multi-processing support will be added in GHC 6.6,
which must roll out due 6 months, as HCAR says

--
Best regards,
 Bulat                            mailto:[hidden email]



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Re[2]: Uses forkOS all CPUS on-board?

Shae Matijs Erisson
Bulat Ziganshin <[hidden email]> writes:

> >> They work, even in a context switching regime, but I can't
> >> exploit all the CPU's on my computer. Always is "active"
> >> just one thread
> this will not help. multi-processing support will be added in GHC 6.6,
> which must roll out due 6 months, as HCAR says

In the meantime, you can try it yourself by grabbing ghc 6.5 from cvs and
reading http://www.haskell.org/~simonmar/papers/multiproc.pdf
--
Shae Matijs Erisson - http://www.ScannedInAvian.com/ - Sockmonster once said:
You could switch out the unicycles for badgers, and the game would be the same.

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Re[2]: Uses forkOS all CPUS on-board?

Sebastian Sylvan
In reply to this post by Bulat Ziganshin
On 11/23/05, Bulat Ziganshin <[hidden email]> wrote:

> Hello Sebastian,
>
> Wednesday, November 23, 2005, 6:46:54 PM, you wrote:
>
> >> They work, even in a context switching regime, but I can't
> >> exploit all the CPU's on my computer. Always is "active"
> >> just one thread
>
> SS> Did you compile it with "-threaded"?
>
> this will not help. multi-processing support will be added in GHC 6.6,
> which must roll out due 6 months, as HCAR says
>

I seem to remember getting it to work months ago. Using the
CVS-version of GHC, though!

/S

--
Sebastian Sylvan
+46(0)736-818655
UIN: 44640862
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

RE: Re: Re[2]: Uses forkOS all CPUS on-board?

Simon Marlow
In reply to this post by Kolář Dušan
On 24 November 2005 01:35, Shae Matijs Erisson wrote:

> Bulat Ziganshin <[hidden email]> writes:
>
>>>> They work, even in a context switching regime, but I can't
>>>> exploit all the CPU's on my computer. Always is "active"
>>>> just one thread
>> this will not help. multi-processing support will be added in GHC
>> 6.6, which must roll out due 6 months, as HCAR says
>
> In the meantime, you can try it yourself by grabbing ghc 6.5 from cvs
> and reading http://www.haskell.org/~simonmar/papers/multiproc.pdf

The code is still a bit green, but it's improving.  I'm tracking down
various crashes at the moment.  By all means try it out though, and I'm
interested in any test programs and benchmarks that folk have.

To compile a program for SMP:

  - Add "GhcLibWays += s" to mk/build.mk when building GHC
  - Add -smp when compiling your code
  - +RTS -N2 -RTS to use 2 CPUs (for example)

BTW you don't need to use forkOS to get parallelism, forkIO will do.
Also we have now implemented par (as in Control.Parallel.par).

Cheers,
        Simon
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe