[Haskell-cafe] Go parallel

Jonathan Cast jonathanccast at fastmail.fm
Mon Nov 5 15:38:12 EST 2007


On Mon, 2007-11-05 at 20:12 +0000, Andrew Coppin wrote:
> Hi folks.
> 
> Take a look at this:
> 
>   render :: IOArray Point Colour -> (Point -> Colour) -> IO ()
>   render framebuffer fn = mapM_ (\p -> writeArray framebuffer p (fn p)) 
> all_points
> 
> How do I alter this to compute fn in multiple threads in parallel? (As 
> you can see from the type signature, the calculation is independent for 
> each pixel.)

You can spark a thread for each computation of fn, like such:

writeArray framebuffer p `parApp` fn p
where
  parApp f x = x `par` f x

Or, alternatively, since I believe IOArray is lazy, you could grab a
list and hand it to parListChunk (from Parallel.Strategies) or there
abouts:

xn <- getElems framebuffer
evaluate $ parListChunk 100 rwhnf

or something (evaluate is from Control.Exception; rwhnf is also from
Parallel.Strategies).

jcc




More information about the Haskell-Cafe mailing list