为什么 Haskell 中没有隐式并行性? [英] Why is there no implicit parallelism in Haskell?

查看:25
本文介绍了为什么 Haskell 中没有隐式并行性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Haskell 是纯函数式的,因此基本上它具有编译器能够处理隐式并行.

Haskell is functional and pure, so basically it has all the properties needed for a compiler to be able to tackle implicit parallelism.

考虑这个简单的例子:

f = do
  a <- Just 1
  b <- Just $ Just 2
  -- ^ The above line does not utilize an `a` variable, so it can be safely
  -- executed in parallel with the preceding line
  c <- b
  -- ^ The above line references a `b` variable, so it can only be executed
  -- sequentially after it
  return (a, c)
  -- On the exit from a monad scope we wait for all computations to finish and 
  -- gather the results

从原理上讲,执行计划可以描述为:

Schematically the execution plan can be described as:

               do
                |
      +---------+---------+
      |                   |
  a <- Just 1      b <- Just $ Just 2
      |                   |
      |                 c <- b
      |                   |
      +---------+---------+
                |
           return (a, c)

为什么在编译器中还没有使用标志或编译指示实现此类功能?实际原因是什么?

Why is there no such functionality implemented in the compiler with a flag or a pragma yet? What are the practical reasons?

推荐答案

这是一个长期研究的话题.虽然您可以在 Haskell 代码中隐式地推导出并行性,但问题是对于当前的硬件,并行性太多,粒度太细.

This is a long studied topic. While you can implicitly derive parallelism in Haskell code, the problem is that there is too much parallelism, at too fine a grain, for current hardware.

因此,您最终将精力花在记账上,而不是更快地运行.

So you end up spending effort on book keeping, not running things faster.

因为我们没有无限的并行硬件,所以选择正确的粒度也很重要——粗,会有空闲的处理器,太细和开销将是不可接受的.

Since we don't have infinite parallel hardware, it is all about picking the right granularity -- too coarse and there will be idle processors, too fine and the overheads will be unacceptable.

我们拥有的是更粗粒度的并行性(火花),适用于生成数千或数百万个并行任务(因此不是在指令级别),它们映射到我们今天通常可用的少数核心上.

What we have is more coarse grained parallelism (sparks) suitable for generating thousands or millions of parallel tasks (so not at the instruction level), which map down onto the mere handful of cores we typically have available today.

请注意,对于某些子集(例如数组处理),存在具有紧成本模型的全自动并行化库.

Note that for some subsets (e.g. array processing) there are fully automatic parallelization libraries with tight cost models.

有关此方面的背景信息,请参阅 Feedback Directed Implicit Parallelism,其中介绍了一种自动化方法在任意 Haskell 程序中插入 par.

For background on this see Feedback Directed Implicit Parallelism, where they introduce an automated approach to the insertion of par in arbitrary Haskell programs.

这篇关于为什么 Haskell 中没有隐式并行性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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