Haskell 中的并行映射 [英] Parallel map in haskell
问题描述
是否有一些 map
的替代品来并行评估列表?我不需要它偷懒.
Is there some substitute of map
which evaluates the list in parallel? I don't need it to be lazy.
类似:pmap :: (a -> b) ->[a] ->[b]
让我 pmap 昂贵的函数 big_list
并让我的所有内核都达到 100%.
Something like: pmap :: (a -> b) -> [a] -> [b]
letting me pmap expensive_function big_list
and have all my cores at 100%.
推荐答案
是的,参见 并行包:
ls `using` parList rdeepseq
将通过 rdeepseq
策略并行评估列表中的每个元素.请注意,如果您的元素太便宜而无法获得并行评估每个元素的好处(因为它节省了每个元素的火花),则使用具有良好块值的 parListChunk
可能会提供更好的性能.
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).
根据您的问题,我觉得我应该解释为什么这是一个答案.那是因为 Haskell 很懒!考虑语句
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
未评估任何内容.您刚刚创建了一个映射 expensiveFunction
的 thunk.那么我们如何并行评估它?
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
现在不要在以后的计算中使用 bs
列表,而是使用 cs
列表.IOW,您不需要并行地图,您可以使用常规(懒惰)地图和并行评估策略.
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.
如果你环顾四周,你会看到 parMap 函数执行我在此处展示的操作,但包装为一个辅助函数.
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屋!