[Haskell-cafe] Go parallel

Andrew Coppin andrewcoppin at btinternet.com
Mon Nov 5 15:46:45 EST 2007

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.


More information about the Haskell-Cafe mailing list