Jonathan Cast wrote:
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

Hmm, that may be a little *too* fine-grained. (But then, just because I spark 175,862 threads doesn't mean it will actually *run* that many at once, right?) I guess I'll try it and see...

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).

Yes, IOArray is lazy. The *real* array type I'm using is unboxed and therefore strict - however, the order in which pixels are written is of no interest to me. I will sit and have a think about this one too.

Thanks.

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to