It's a doubt about parallel strategies and parMap (Control.Parallel.Strategies)
It's about parMap rseq equivalence with parMap rpar.
Since parMap uses parList it evaluates in parallel so using either rseq or rpar will evaluate in parallel to WHNF. Isn't it ?
Update:
Since
parMap strat f = (`using` parList strat) . map f
parList = parTraversable
parTraversable strat = evalTraversable (rpar `dot` strat)
evalTraversable = traverse
strat2 `dot` strat1 = strat2 . runEval . strat1
parMap rseq uses the strategy
rpar `dot` rseq
which gives:
rpar . runEval . rseq
which gives:
(\x -> x `par` return x) . runEval . (\x -> x `pseq` return x)
It is hard to think about the result.
Update:
I see, the lazy evaluation takes the first function of the composition first, and
(\x -> x `par` return x)
grants that every element in the traversable container will be sparked for a parallel computation when possible.
So we could add that (rpar dot rseq) is equivalent to (rseq dot rpar), isn't it?
And that parMap rpar is redundant in that it generates two sparks for every traversable element. !!
A quick smoke test reveals that yes parMap rseq and parMap rpar both get evaluated in parallel.
import Control.Parallel.Strategies
fib 0 = 1
fib 1 = 1
fib n = fib (n-1) + fib (n-2)
main = print resultPlain where
resultPlain = [fib 34, fib 34, fib 34, fib 34]
resultPar = parMap rpar id [fib 34, fib 34, fib 34, fib 34]
resultSeq = parMap rseq id [fib 34, fib 34, fib 34, fib 34]
and then, using each kind of result_____ I timed the compiled binaries
ghc -threaded --make rpar
time ./rpar +RTS -N4
and saw that resultPlain was much longer than resultPar or resultSeq (about 2x longer) and resultPar and resultSeq were comparatively identically timed.
More details into GHC's actual interpretation of the Eval monad are lacking, but given that parMap strat f = withStrategy (parList strat) . map f along with the results of this experiment I feel confident saying each element in the list is sparked for evaluation to WHNF.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With