简体   繁体   English

我对并行Haskell感到困惑

[英]Am confused about parallel Haskell

How is this code: 这段代码怎么样:

parfib :: Int -> Int
parfib 0 = 1
parfib 1 = 1
parfib n = nf2 `par` (nf1 `par` (nf1+nf2+1))
           where nf1 = parfib (n-1)
                 nf2 = parfib (n-2)

Better than this: 比这更好:

parfib :: Int -> Int
parfib 0 = 1
parfib 1 = 1
parfib n = nf2 `par` (nf1 `seq` (nf1+nf2+1))
           where nf1 = parfib (n-1)
                 nf2 = parfib (n-2)

I don't get the explanations I've found online that say "In order to guarantee that the main expression is evaluated in the right order (ie without blocking the main task on the child task) the seq annotation is used". 我没有得到我在网上发现的解释说“为了保证主表达式以正确的顺序进行评估(即没有阻止子任务的主要任务),使用seq注释”。

Why is seq used? 为什么使用seq? I know it forces the interpreter to evaluate parfib (n-1) first but why is it necessary? 我知道它迫使翻译首先评估parfib(n-1),但为什么有必要呢?

When executing the second program, won't the interpeter spark a new process to evaluate nf2, while evaluating nf1 of the nf1+nf2+1 expression in parallel? 当执行第二个程序时,并不会引发一个新的过程来评估nf2,而并行评估nf1 + nf2 + 1表达式的nf1? What is the need to tell it to specify that it should start with nf1? 有什么需要告诉它指定它应该以nf1开头?

It doesn't make much sense to evaluate nf1 in parallel to nf1+... since the latter depends on nf1, so all it would do is block on the spark of nf1 . nf1nf1+...并行评估没有多大意义,因为后者取决于nf1,所以它所做的就是阻止nf1的火花。 Using seq it will only try to use nf1 once you know it has been evaluated. 使用seq一旦知道它已被评估,它将只尝试使用nf1

it could be because we want to minimize the number of sparks from my understanding the two vesrsion will produce the same result 这可能是因为我们想要最大限度地减少我理解的火花数量,这两个内容会产生相同的结果

but with the first option you will be sparking two additional process (nf1, nf2). 但是使用第一个选项,你将引发两个额外的过程(nf1,nf2)。 But when we use seq we just spark only one additionnal process ( nf1). 但是当我们使用seq时,我们只激发一个额外的过程(nf1)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM