为什么以下内容将并行而不是顺序运行? [英] Why will the following run in parallel rather than sequentially?

查看:100
本文介绍了为什么以下内容将并行而不是顺序运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

分别赋予evalPairparPairdeepSeq以下功能,

Given the following functions for evalPair, parPair and deepSeq respectively,

evalPair :: Strategy a -> Strategy b -> Strategy (a,b)
evalPair sa sb (a,b) = do
      a' <- sa a
      b' <- sb b
      return (a',b')

parPair :: Strategy a -> Strategy b -> Strategy (a,b)
parPair sa sb = evalPair (rparWith sa) (rparWith sb)

rdeepseq :: NFData a => Strategy a
rdeepseq x = rseq (force x)

问题是,为什么下面的代码会像作者所说的那样被并行评估?

The question is why will the following code be evaluated in parallel as the author claims?

parPair rdeepseq rdeepseq :: (NFData a, NFData b) => Strategy (a,b)

自从西蒙·马洛(Simon Marlow)说以下话以来,我一直在糊涂

I am in dount since Simon Marlow says the following

要分解将此Strategy应用于一对时发生的情况: parPair分别调用evalPair,并且evalPair分别调用rparWith rdeepseq 对的组成部分.因此,效果是每个组件将 完全并行地评估为标准形式.

To break down what happens when this Strategy is applied to a pair: parPair calls evalPair, and evalPair calls rparWith rdeepseq on each component of the pair. So the effect is that each component will be fully evaluated to normal form in parallel.

根据我所见,rparWith rdeepseq是非正式的rpar . rseq . force,这意味着该参数将以正常形式求值,将依次使用rseq求值,因此最终rpar不会在并行上下文中使用.

According to what I see rparWith rdeepseq is informally rpar . rseq . force which means the argument will be evaluated to normal form, which it will be evaluated with rseq sequentially, and hence in the end the rpar is of no use in a parallel context.

一切取材于西蒙·马洛(Simon Marlow)的书.

Everything is taken from Simon Marlow's book.

先谢谢了.

推荐答案

让我们看一下parPair rdeepseq rdeepseq:

parPair rdeepseq rdeepseq (a,b)
 = evalPair (rparWith rdeepseq) (rparWith rdeepseq) (a,b)
 = do
    a' <- rparWith rdeepseq a
    b' <- rparWith rdeepseq b
    return (a', b')

由于rparWith会引发参数,因此rdeepseq a将并行(可能)求值.如果要使用,它将不会并行

Since rparWith sparks it arguments, rdeepseq a will be evaluated in parallel (possibly). It wouldn't be in parallel, if you were to use

evalPair rdeepseq rdeepseq

相反. rparWith是必不可少的,因为它激发了使用给定策略进行评估的论点.

instead. The rparWith is essential, as it sparks its argument for evaluation with the given strategy.

这篇关于为什么以下内容将并行而不是顺序运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆