[Haskell-cafe] parMap doesn't work fine

Maciej Marcin Piechotka uzytkownik2 at gmail.com
Sat May 14 22:11:42 CEST 2011

On Thu, 2011-05-12 at 15:29 +0400, Grigory Sarnitskiy wrote:
> Hello!
> I've just started using parallel computations in Haskell. parMap works fine, it is so easy to use. However, parMap fails with functions returning lazy structures, e.g. tuples.
> This code works as expected:
> (parMap rpar) bm tvalues
> bm :: Double -> Double is some heavy function. But if I want to return list of pairs (t, bm t) it doesn't use cpu cores fine (only one is in use):
> (parMap rpar) (\t -> (t, bm t)) tvalues
> The same is valid for functions returning lists. How do I use multiple cores with functions returning tuples?

You probably want deepseq:

parMap rpar ((\x -> deepseq x x) . bm) tvalues

where bm returns tuples. For examples

parMap rpar ((\x -> deepseq x x) . (\t -> (t, bm t)) tvalues

If you want to get only head:

parMap rpar (\t -> let z = bm t in z  `seq` t `seq` (t, z)) tvalues


-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110514/59df6de4/attachment.pgp>

More information about the Haskell-Cafe mailing list