为什么没有“forall”?标准并行?

发布于 2024-11-17 04:24:36 字数 614 浏览 3 评论 0原文

我一直在研究新的 std.parallel 库。我不是语言或库设计者,所以请原谅我的无知,但是如果语言中有一个 forall 语句,或者至少在 std.parallel 中,不是有好处吗?

例如,

auto logs = new double[1_000_000];
foreach(i, ref elem; taskPool.parallel(logs)){
        elem = log(i + 1.0);
}

我们可以这样写:

auto logs = new double[1_000_000];
forall!((x){ return log(x + 1.0); })(logs);

foreach 本质上是顺序的,我们可以随时打破它,而 forall 保证所有元素都将被已处理。这是正确的说法吗? forall 的实现只是时间问题,还是有充分的理由不实现它?

I've been going over the new std.parallel library. I'm not a language or library designer, so forgive my ignorance, but would it not be beneficial if there was a forall statement in the language, or at least in std.parallel?

For example, instead of this:

auto logs = new double[1_000_000];
foreach(i, ref elem; taskPool.parallel(logs)){
        elem = log(i + 1.0);
}

we could write this:

auto logs = new double[1_000_000];
forall!((x){ return log(x + 1.0); })(logs);

foreach is sequential by nature and we can break out of it anytime, whereas forall is a guarantee that all elements will be processed. Is that a correct statement? Is it only a matter of time before forall is implemented, or is there a good reason for not having it?

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

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

发布评论

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

评论(2

疑心病 2024-11-24 04:24:36

我认为您误解了 std.parallelism 对 foreach 的作用。如果您查看文档,它特别指出

打破并行 foreach
通过中断循环,标记为中断,
标记为继续、返回或转到
声明抛出一个
ParallelForeachError。

因此,除非抛出异常,否则您无法随时摆脱它 - 这正是 forall 的情况。当您将 foreachparallel 结合使用时,您是在告诉它将该循环的迭代分配给单独的线程。它们几乎肯定是按顺序分发的,但它们是并行执行的,并且您并不真正关心顺序。如果你这样做了,你就不能并行地做它们。因此,添加 forall 不会给您带来任何好处。

D 本质上是一种顺序语言,就像大多数编程语言一样。它提供了一些与线程相关的强大功能(例如默认为线程本地存储),但我预计需要进行相当多的重新设计才能将诸如 forall 之类的内容直接放入语言中。事实证明,这是没有必要的。该语言足够强大,可以在其之上构建并行性。 std.parallelism 有效地为您提供了forall。只是它是通过使用现有的语言功能 foreach 来实现的,而不是必须更改语言来理解和包含 forall 作为内置功能。

而且,正如 Cyber​​Shadow 注释,一个新的模块 std.parallel_algorithm 正在开发中,它将具有 std.algorithm 中许多函数的并行版本,以便您获得并行性免费。总体而言,std.parallelism 似乎在为 D 提供易于使用但强大的并行功能方面做得很好。

I think that you're misunderstanding what std.parallelism is doing with foreach. If you look at the documentation, it specifically states that

Break­ing from a par­al­lel fore­ach
loop via a break, la­beled break,
la­beled con­tinue, re­turn or goto
state­ment throws a
Par­al­lelFore­achEr­ror.

So, you can't break out of it at any time unless you throw an exception - which is exactly what the case would be with forall. When you use foreach with parallel, you're telling it to dole out the iterations of that loop to separate threads. They're almost certainly doled out in sequential order, but they're executed in parallel, and you don't really care about the order. If you did, you couldn't do them in parallel. So, adding a forall wouldn't buy you anything here.

D is by its very nature a sequential language just like most programming languages. It provides some powerful features which relate to threading (such as defaulting to thread-local storage), but I expect that it would require a fair bit of redesign to put something like forall directly in the language. And as it turns out, it's not necessary. The language is powerful enough to allow for the parallelism to be built on top of it. std.parallelism is effectively giving you forall. It's just that it's doing it by using the existing language feature foreach rather than having to have the language altered to understand and contain forall as a built-in feature.

And, as CyberShadow notes, a new module, std.parallel_algorithm, is in the works which will have parallel versions of many of the functions in std.algorithm so that you get that parallelism for free. Overall, std.parallelism seems to be doing a good job of giving easy to use but powerful parallelism features for D.

耳根太软 2024-11-24 04:24:36

这个怎么样?

auto logs = array(taskPool.amap!`log(a + 1.0)`(iota(0, 1_000_000)));

我应该注意到,std.parallel_algorithm 正在开发中。

How about this?

auto logs = array(taskPool.amap!`log(a + 1.0)`(iota(0, 1_000_000)));

I should note that std.parallel_algorithm is in the works.

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