为什么没有“forall”?标准并行?
我一直在研究新的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您误解了 std.parallelism 对
foreach
的作用。如果您查看文档,它特别指出因此,除非抛出异常,否则您无法随时摆脱它 - 这正是
forall
的情况。当您将foreach
与parallel
结合使用时,您是在告诉它将该循环的迭代分配给单独的线程。它们几乎肯定是按顺序分发的,但它们是并行执行的,并且您并不真正关心顺序。如果你这样做了,你就不能并行地做它们。因此,添加forall
不会给您带来任何好处。D 本质上是一种顺序语言,就像大多数编程语言一样。它提供了一些与线程相关的强大功能(例如默认为线程本地存储),但我预计需要进行相当多的重新设计才能将诸如
forall
之类的内容直接放入语言中。事实证明,这是没有必要的。该语言足够强大,可以在其之上构建并行性。 std.parallelism 有效地为您提供了forall
。只是它是通过使用现有的语言功能foreach
来实现的,而不是必须更改语言来理解和包含forall
作为内置功能。而且,正如 CyberShadow 注释,一个新的模块 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 thatSo, 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 useforeach
withparallel
, 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 aforall
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 youforall
. It's just that it's doing it by using the existing language featureforeach
rather than having to have the language altered to understand and containforall
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.
这个怎么样?
我应该注意到,std.parallel_algorithm 正在开发中。
How about this?
I should note that
std.parallel_algorithm
is in the works.