在haskell中的并行地图 [英] Parallel map in haskell
问题描述
是否有替代并行计算列表的 map
?我不需要它是懒惰的。
类似于: pmap ::(a - > b) - > [a] - > [b]
让我 pmap expensive_function big_list
,并将所有内核都设置为100%。 h2_lin>解决方案
是的,请参阅 parallel package :
ls`using` parList rdeepseq
将通过 rdeepseq
策略并行评估列表中的每个元素。请注意,如果元素太便宜,无法并行评估每个元素(因为它可以节省每个元素的火花),所以使用 parListChunk
元素)。
编辑:根据你的问题,我觉得我应该解释为什么这是一个答案。这是因为Haskell很懒!考虑一下声明
let bs = map expensiveFunction as
没有任何评估。您刚刚创建了映射 expensiveFunction
的thunk。那么我们如何评估它呢?
let bs = map expensiveFunction as
cs = bs`using` parList rdeepseq
现在不要使用 bs
列出你未来的计算,而不是使用 cs
列表。 IOW,你不需要一个平行的地图,你可以使用常规(懒惰)地图和并行的evaulation策略。
回应你的评论,下面的代码不适合你吗?它适用于我。
import Control.Parallel.Strategies
func as =
让bs = map(+1)as
cs = bs`using` parList rdeepseq
in cs
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%.
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
这篇关于在haskell中的并行地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!