[Haskell-cafe] Re: Can't seem to get `par` working appropriately with lists

Don Stewart dons at galois.com
Thu Feb 21 19:46:01 EST 2008


ben.franksen:
> Luke Andrew wrote:
> >     main = do print $ zipWith (+) (fiblist2 37 `par` fiblist1 37)
> > (fiblist2 37)
> > 
> > compilation & testing:
> > 
> >     luke at Sonata:~/mcls$ ghc -O2 -threaded --make test2
> > 
> >     luke at Sonata:~/mcls$ time ./test2 +RTS -N1
> >     [2,2,4,6,10,16,26,42...    ...405774,18454930,29860704,48315634]
> > 
> >     real    0m15.294s
> >     user    0m15.196s
> >     sys     0m0.013s
> > 
> >     luke at Sonata:~/mcls$ time ./test2 +RTS -N2
> >     [2,2,4,6,10,16,26,42...    ...405774,18454930,29860704,48315634]
> > 
> >     real    0m15.268s
> >     user    0m15.169s
> >     sys     0m0.013s
> 
> This is due to lazyness: 'fiblist2 37' does not evaluate the whole resulting
> list, so the thread spawned by 'par' will almost immediately return.
> 
> Take a look at Control.Parallel.Strategies, the 'parList' combinator is
> probably what you need here. To get a feel what this 'strategies' stuff is
> all about, it is a good idea to read (or at least skim over) the
> accompanying paper, just google for "Algorithm + Strategy = Parallelism".

or 'rnf' on the result.

    rnf x `par` y

is the basic "fully strict" strategy.


More information about the Haskell-Cafe mailing list