并行映射(生成器)运算符

发布于 2024-08-09 03:08:24 字数 496 浏览 10 评论 0原文

我有兴趣为我的语言定义并行映射运算符。它将一个列表转换为给定表达式的新列表。它的语法类似于生成器。与 C# 和 Python 中的生成器不同,如果编译器需要(例如,有一个空闲核心,并且列表非常大),它可能会并行评估。它将被称为witheach,以区别于顺序执行的foreach

例如,考虑一下:

var f = function(int x) : int { return x * 2; }
var my_list = 0..1000000;
var my_mapped_list = witheach (i in mylist) yield f(i);

我的问题是,对于可能在 f 中添加副作用的程序员来说,这是否太不直观?当然,我会说不要在文档中这样做,但大多数程序员不阅读语言文档。 :-)

我想更大的问题是,现代程序员能否适应他们语言中的隐式并行列表处理语义,或者他们是否需要更明确的东西?

I am interested in defining a parallel map operator for my language. It transforms a list into a new list given an expression. It would have a syntax similar to a generator. Unlike generators in C# and Python, it would potentially be evaluated in parallel, if the compiler desires (e.g. there is an idle core, and the list is really big). It would be called witheach to distinguish it from foreach which is executed sequentially.

For example consider:

var f = function(int x) : int { return x * 2; }
var my_list = 0..1000000;
var my_mapped_list = witheach (i in mylist) yield f(i);

My question is, is this going to be too unintuitive for programmers who may put side-effects in f? Of course, I would say not to do this in the documentation, but most programmers don't read language documentation. :-)

I guess the bigger question, is can modern day programmers adapt to implicit parallel list processing semantics in their language, or do they need things to be more explicit?

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

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

发布评论

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

评论(2

梦境 2024-08-16 03:08:24

你是对的,大多数用户不会阅读文档,因此可能会将非线程安全代码放入 witheach 块中(特别是名称和语法与 非常相似) foreach)。您要么必须信任您的用户,要么对该块的并发安全性执行一些静态分析。如果您选择后者,那么您可能不必费心使用 witheach - 只要可能就自动并行化 foreach

至于用户是否准备好承担选择潜在危险的责任,我敢打赌“是”,只要他们事先知道什么是安全的。许多语言强迫您每天做出这种选择(指针、手动内存管理、共享内存并发)。您可能希望使语法不那么模糊(即称之为parallelforeach),以便人们知道他们正在注册的目的。

You're right in that most users won't read the documentation, and as such will probably put non-thread safe code in a witheach block (especially with a name and syntax so similar to foreach). You'll either have to trust your users, or perform some static analysis on the concurrent safety of that block. If you chose the later, then you may as well not bother with witheach - just automatically parallelise foreach when possible.

As to whether users are ready for the responsibility of choosing to be potentially dangerous, I would wager "yes", as long as they know up front what is safe. Many languages force you to make this choice each day (pointers, manual memory management, shared memory concurrency). You might want to make your syntax less ambiguous (i.e. call it parallelforeach), so that people know what they're signing up for.

月亮坠入山谷 2024-08-16 03:08:24

你的意思是像 pmap 之类的东西?

You mean something like pmap?

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