自动并行化 [英] Automatic parallelization

查看:164
本文介绍了自动并行化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于试图获取代码并自动将其拆分为线程的项目(可能是编译时,可能是在运行时),您有什么看法。

What is your opinion regarding a project that will try to take a code and split it to threads automatically(maybe compile time, probably in runtime).

看下面的代码:

for(int i=0;i<100;i++)
   sum1 += rand(100)
for(int j=0;j<100;j++)
   sum2 += rand(100)/2

这种代码可以自动拆分为2个并行运行的不同线程。
你认为它甚至可能吗?
我有一种感觉,理论上它是不可能的(它让我想起停止问题),但我无法证明这种想法。

This kind of code can automatically get split to 2 different threads that run in parallel. Do you think it's even possible? I have a feeling that theoretically it's impossible (it reminds me the halting problem) but I can't justify this thought.

你觉得它有用吗?项目?是否有类似的东西?

Do you think it's a useful project? is there anything like it?

推荐答案

在一般情况下是否可以知道一段代码是否可以并行化真的很重要,因为即使你的算法无法检测到所有可以并行化的情况,也许它可以检测到其中的一些。

Whether it's possible in the general case to know whether a piece of code can be parallelized does not really matter, because even if your algorithm cannot detect all cases that can be parallelized, maybe it can detect some of them.

这并不意味着它会有用。请考虑以下事项:

That does not mean it would be useful. Consider the following:


  1. 首先,要在编译时执行此操作,您必须检查内部可能到达的所有代码路径要并行化的构造。除了简单的计算之外,这可能很棘手。

  2. 其次,你必须以某种方式决定哪些是可并行化的,哪些不是。例如,您不能轻易地将一个将相同状态修改为多个线程的循环分解。这可能是一项非常困难的任务,在很多情况下你最终会不确定 - 实际上两个变量可能会引用同一个对象。

  3. 即使你能做到这一点,也会最终让用户感到困惑。很难解释为什么他的代码不可并行化以及如何更改它。

我想如果你想要在Java中实现这一点,您需要将其更多地作为库编写,并让用户决定要并行化的内容(库函数与注释一起?只是大声思考)。函数式语言更适合这种情况。

I think that if you want to achieve this in Java, you need to write it more as a library, and let the user decide what to parallelize (library functions together with annotations? just thinking aloud). Functional languages are much more suited for this.

作为一个琐事:在并行编程课程中,我们必须检查代码并确定它是否可并行化。我不记得具体内容(关于最多一次属性的内容?有人填写我的内容吗?),但故事的寓意是即使对于看似微不足道的案件也是如此。

As a piece of trivia: during a parallel programming course, we had to inspect code and decide whether it was parallelizable or not. I cannot remember the specifics (something about the "at-most-once" property? Someone fill me in?), but the moral of the story is that it was extremely difficult even for what appeared to be trivial cases.

这篇关于自动并行化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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