QtConcurrent 的 Scala 类似物 [英] Scala analogues of QtConcurrent

查看:25
本文介绍了QtConcurrent 的 Scala 类似物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Scala(或 Java)的 QtConcurrent 的类似物是什么?即MapReduce、并行map 和foldl 的简化实现.谢谢

What are the analogues of QtConcurrent for Scala (or Java)? Ie simplified implementation of MapReduce, the parallel map and foldl. Thank you

推荐答案

你可以使用 Scala Parallel Collections.它们目前是 Scala 夜间版本的一部分,并将在 Scala 2.9 中发布.这个想法是常规集合中可用的大多数操作都是并行化的,因此可以以相同的方式使用并行集合.

You can use Scala Parallel Collections. They are currently a part of Scala nightly releases, and will be released in Scala 2.9. The idea is that most operations available in regular collections are parallelized, so that parallel collections can be used in the same way.

目前,有几种可用的集合类型 - 并行范围、并行数组和并行哈希尝试.例如,您可以像这样在并行数组上调用并行 mapfold 操作:

Currently, there are a few collection types available - parallel ranges, parallel arrays and parallel hash tries. For instance, you can invoke a parallel map and fold operations on a parallel array like this:

scala> val pa = (0 until 10000).toArray.par
pa: scala.collection.parallel.mutable.ParArray[Int] = ParArray(0, 1, 2, 3, 4, 5, 6,...

scala> pa.map(_ + 1)
res0: scala.collection.parallel.mutable.ParArray[Int] = ParArray(1, 2, 3, 4, 5, 6, 7,...

scala> pa map { v => if (v % 2 == 0) v else -v }
res1: scala.collection.parallel.mutable.ParArray[Int] = ParArray(0, -1, 2, -3, 4, -5,...

scala> pa.fold(0) { _ + _ }
res2: Int = 49995000

还有其他可用的并行收集操作.请注意 fold 必须采用关联运算符 - 在上面的示例中,加法是关联的 ((A + B) + C == A + (B + C)),即您可以添加数字的子序列以任何顺序,您将始终获得相同的金额(reduce 有类似的合约).

There are other parallel collection operations available as well. Note that fold must take an associative operator - in the example above, addition is associative ((A + B) + C == A + (B + C)), i.e. you can add subsequences of numbers in any order and you will always obtain the same sum (reduce has a similar contract).

要注意的另一件事是,传递给并行集合的闭包是同时调用的.如果它们有副作用,例如修改环境中的局部变量,则这些访问必须同步.例如,您可以这样做:

One other thing to be aware of is that the closures passed to parallel collections are invoked simultaneously. If they have side-effects, such as modifying a local variable in the environment, these accesses have to be synchronized. For instance, you could do something like this:

scala> var a = 0                                                                                                                                                                 
a: Int = 0                                                                                                                                                                       

scala> pa foreach { a += _ }                                                                                                                                                     

scala> a                                                                                                                                                                         
res1: Int = 49995000             

scala> a = 0
a: Int = 0

scala> pa foreach { a += _ }

scala> a
res7: Int = 49990086

并且每次都有不同的结果,因为 foreach 并行调用 { a += _ }.在上面的例子中,a 应该是同步的,用锁或原子保护.

and have different results every time, because the foreach invokes { a += _ } in parallel. In the example above, a should be made synchronized, protected with a lock or atomic.

但想法是使用内置组合器来完成任务并倾向于函数式编程,避免如上例中的局部副作用.

But the idea is to use built-in combinators to accomplish a task and lean towards functional programming, avoiding local side-effects as in the example above.

您可能想在其他答案中提供的链接中阅读更多有关其内部机制的信息.

You might want to read a little bit more about their internal mechanisms in the links provided in the other answer.

这篇关于QtConcurrent 的 Scala 类似物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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