Parallel map in haskell

爷,独闯天下 提交于 2019-12-17 22:13:49

问题


Is there some substitute of map which evaluates the list in parallel? I don't need it to be lazy.

Something like: pmap :: (a -> b) -> [a] -> [b] letting me pmap expensive_function big_list and have all my cores at 100%.


回答1:


Yes, see the parallel package:

ls `using` parList rdeepseq

will evaluate each element of the list in parallel via the rdeepseq strategy. Note the use of parListChunk with a good chunk value might give better performance if your elements are too cheap to get a benefit evaluating each one in parallel (because it saves on sparking for each element).

EDIT: Based on your question I feel I should explain why this is an answer. It's because Haskell is lazy! Consider the statement

let bs = map expensiveFunction as

Nothing has been evaluated. You've just created a thunk that maps expensiveFunction. So how do we evaluate it in parallel?

let bs = map expensiveFunction as
    cs = bs `using` parList rdeepseq

Now don't use the bs list in your future computations, instead use the cs list. IOW, you don't need a parallel map, you can use the regular (lazy) maps and a parallel evaulation strategy.

EDIT: And if you look around enough you'll see the parMap function that does what I showed here but wrapped into one helper function.

In response to your comment, does the below code not work for you? it works for me.

import Control.Parallel.Strategies

func as =
        let bs = map (+1) as
            cs = bs `using` parList rdeepseq
        in cs



回答2:


Besides using explicit strategies yourself as Tom has described, the parallel package also exports parMap:

 parMap :: Strategy b -> (a -> b) -> [a] -> [b]

where the strategy argument is something like rdeepseq.

And there's also parMap in the par-monad package (you step out of pure Haskell, and into a parallel monad):

 parMap :: NFData b => (a -> b) -> [a] -> Par [b]

The par-monad package is documented here.



来源:https://stackoverflow.com/questions/5606165/parallel-map-in-haskell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!