为什么我们需要“seq”或“pseq”与“标准杆”在哈斯克尔?

发布于 2024-10-09 23:29:26 字数 390 浏览 4 评论 0原文

我试图理解为什么我们需要标准示例代码的所有部分:

a `par` b `pseq` a+b

为什么以下内容还不够?

a `par` b `par` a+b

上面的表达式看起来非常具有描述性:尝试并行计算 ab,并返回结果 a+b。仅仅是因为效率的原因吗:第二个版本会触发两次而不是一次?

下面更简洁的版本怎么样?

a `par` a+b

为什么我们需要确保 ba+b 之前计算,就像在原始标准代码中一样?

I'm trying to understand why we need all parts of the standard sample code:

a `par` b `pseq` a+b

Why won't the following be sufficient?

a `par` b `par` a+b

The above expression seems very descriptive: Try to evaluate both a and b in parallel, and return the result a+b. Is the reason only that of efficiency: the second version would spark off twice instead of once?

How about the following, more succinct version?

a `par` a+b

Why would we need to make sure b is evaluated before a+b as in the original, standard code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓦然回首 2024-10-16 23:29:26

好的。我认为以下论文回答了我的问题: http://community.haskell.org/~ simonmar/papers/threadscope.pdf

总之,

a `par` b `par` a+b 

和 的

a `par` a+b

问题在于缺乏评估顺序。在这两个版本中,主线程都会立即在 a(有时是 b)上工作,导致火花立即“消失”,因为不再需要启动一个线程来评估主线程已经开始评估的内容。

原始版本

a `par` b `pseq` a+b

确保主线程在 b before a+b 上工作(否则将开始评估 a相反),从而使 Spark a 有机会具体化为线程以进行并行评估。

Ok. I think the following paper answers my question: http://community.haskell.org/~simonmar/papers/threadscope.pdf

In summary, the problem with

a `par` b `par` a+b 

and

a `par` a+b

is the lack of ordering of evaluation. In both versions, the main thread gets to work on a (or sometimes b) immediately, causing the sparks to "fizzle" away immediately since there is no more need to start a thread to evaluate what the main thread has already started evaluating.

The original version

a `par` b `pseq` a+b

ensures the main thread works on b before a+b (or else would have started evaluating a instead), thus giving a chance for the spark a to materialize into a thread for parallel evaluation.

未蓝澄海的烟 2024-10-16 23:29:26
a `par` b `par` a+b 

将并行评估 a 和 b 并返回a+b,是的。

然而,其中的pseq确保a+b之前对a和b进行求值。

有关该主题的更多详细信息,请参阅此链接

a `par` b `par` a+b 

will evaluate a and b in parallel and returns a+b, yes.

However, the pseq there ensures both a and b are evaluated before a+b is.

See this link for more details on that topic.

驱逐舰岛风号 2024-10-16 23:29:26

a `par` b `par` a+b 会为 ab 创建火花,但是 a+b > 立即到达,因此其中一个火花将失败(即,它在主线程中进行评估)。这样做的问题是效率,因为我们产生了不必要的火花。如果您使用它来实现并行除法 &征服那么开销将限制你的加速。

a `par` a+b 看起来更好,因为它只产生一个火花。但是,尝试在 b 之前计算 a 将会导致 a 的火花失败,因为 b 没有Spark 这将导致对 a+b 进行顺序评估。将顺序切换为 b+a 可以解决这个问题,但作为代码,这不会强制排序,Haskell 仍然可以将其计算为 a+b

因此,在尝试计算 a+b< 之前,我们会执行 a `par` b `pseq` a+b 来强制在主线程中计算 b。 /代码>。这使得在我们尝试评估 a+b 之前,a 火花有机会实现,并且我们没有创建任何不必要的火花。

a `par` b `par` a+b creates sparks for both a and b, but a+b is reached immediately so one of the sparks will fizzle (i.e., it is evaluated in the main thread). The problem with this is efficiency, as we created an unnecessary spark. If you're using this to implement parallel divide & conquer then the overhead will limit your speedup.

a `par` a+b seems better because it only creates a single spark. However, attempting to evaluate a before b will fizzle the spark for a, and as b does not have a spark this will result in sequential evaluation of a+b. Switching the order to b+a would solve this problem, but as code this doesn't enforce ordering and Haskell could still evaluate that as a+b.

So, we do a `par` b `pseq` a+b to force evaluation of b in the main thread before we attempt to evaluate a+b. This gives the a spark chance to materialise before we try evaluating a+b, and we haven't created any unnecessary sparks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文