使用并行集合就地转换数组

发布于 2024-11-10 12:05:25 字数 334 浏览 3 评论 0原文

当拥有一组对象时,通常需要(例如出于性能原因)更新(替换)某些对象。例如,如果您有一个整数数组,您可能希望将负整数替换为正整数:

// Faster for primitives
var i = 0
while (i < a.length) {
  if (a(i) < 0) a(i) = -a(i)
  i += 1
}

// Fine for objects, often okay for primitives
for (i <- a.indices) if (a(i) < 0) a(i) = -a(i)

使用并行集合库执行此类修改的规范方法是什么?

When one has an array of objects it is often desirable (e.g. for performance reasons) to update (replace) some of the objects in place. For example, if you have an array of integers, you might want to replace the negative integers with positive ones:

// Faster for primitives
var i = 0
while (i < a.length) {
  if (a(i) < 0) a(i) = -a(i)
  i += 1
}

// Fine for objects, often okay for primitives
for (i <- a.indices) if (a(i) < 0) a(i) = -a(i)

What is the canonical way to perform a modification like this using the parallel collections library?

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

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

发布评论

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

评论(3

瞳孔里扚悲伤 2024-11-17 12:05:25

就并行阵列而言,这是一个疏忽。并行数组的并行转换可能会包含在下一个版本中。

但是,您可以使用并行范围来完成此操作:

for (i <- (0 until a.length).par) a(i) = computeSomething(i)

请注意,并非所有可变集合都可以通过这种方式进行修改。一般来说,如果您希望修改某些内容,则必须确保其正确同步。在这种情况下,这对于数组来说不是问题,因为不同的索引将修改不同的数组元素(并且最后调用者可以看到更改,因为并行操作的完成保证了所有写入都对调用者可见)。

As far as parallel arrays are considered - it's an oversight. A parallel transform for parallel arrays will probably be included in the next release.

You can, however, do it using a parallel range:

for (i <- (0 until a.length).par) a(i) = computeSomething(i)

Note that not all mutable collections are modifiable in place this way. In general, if you wish to modify something in place, you have to make sure it's properly synchronized. This is not a problem for arrays in this case, since different indices will modify different array elements (and the changes are visible to the caller at the end, since the completion of a parallel operation guarantees that all writes become visible to the caller).

月依秋水 2024-11-17 12:05:25

顺序可变集合具有诸如 transform 之类的就地工作的方法。

并行可变集合缺乏这些方法,但我不确定其背后是否有原因,或者是否只是一个疏忽。

我的回答是,你目前运气不好,但你当然可以自己写。

也许在对此进行更多讨论后提交罚单才有意义?

Sequential mutable collections have methods like transform which work in-place.

Parallel mutable collections lack these methods, but I'm not sure there is a reason behind it or if it is just an oversight.

My answer is that you're currently out of luck, but you could write it yourself of course.

Maybe it would make sense filing a ticket after this has been discussed a bit more?

青柠芒果 2024-11-17 12:05:25

如何创建一个并行集合,将索引保存到要转换的数组中,然后运行 ​​foreach 以在给定索引的情况下改变数组中的一个单元格。

这样你也有更多的控制权,并且可以让四个工人在阵列的四个季度工作。因为仅仅翻转一个整数符号可能不足以证明并行计算的合理性。

How about creating a parallel collection that holds the indices into the array to transform and then run foreach to mutate one cell in an array, given the index.

That way you also have more control and it is possible to make four workers, that work on the four quarters of an array. Because simply flipping one single integer sign is probably not enough work to justify a parallel computation.

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