scala 并行集合并行度 [英] scala parallel collections degree of parallelism

查看:53
本文介绍了scala 并行集合并行度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

scala 并行集合中是否有与 LINQ 的 withDegreeOfParallelism 等效的设置将运行查询的线程数?我想并行运行一个需要运行一定数量线程的操作.

Is there any equivalent in scala parallel collections to LINQ's withDegreeOfParallelism which sets the number of threads which will run a query? I want to run an operation in parallel which needs to have a set number of threads running.

推荐答案

使用最新的主干,使用 JVM 1.6 或更新版本,使用:

With the newest trunk, using the JVM 1.6 or newer, use the:

collection.parallel.ForkJoinTasks.defaultForkJoinPool.setParallelism(parlevel: Int)

不过,这可能会在未来发生变化.计划在下一个版本中采用更统一的方法来配置所有 Scala 任务并行 API.

This may be a subject to changes in the future, though. A more unified approach to configuring all Scala task parallel APIs is planned for the next releases.

但是请注意,虽然这将决定查询使用的处理器数量,但这可能不是运行查询所涉及的实际线程数.由于并行集合支持嵌套并行,因此实际线程池实现可能会在检测到有必要时分配更多线程来运行查询.

Note, however, that while this will determine the number of processors the query utilizes, this may not be the actual number of threads involved in running a query. Since parallel collections support nested parallelism, the actual thread pool implementation may allocate more threads to run the query if it detects this is necessary.

从 Scala 2.10 开始,设置并行级别的首选方法是将 tasksupport 字段设置为新的 TaskSupport 对象,如下例所示:

Starting from Scala 2.10, the preferred way to set the parallelism level is through setting the tasksupport field to a new TaskSupport object, as in the following example:

scala> import scala.collection.parallel._
import scala.collection.parallel._

scala> val pc = mutable.ParArray(1, 2, 3)
pc: scala.collection.parallel.mutable.ParArray[Int] = ParArray(1, 2, 3)

scala> pc.tasksupport = new ForkJoinTaskSupport(new scala.concurrent.forkjoin.ForkJoinPool(2))
pc.tasksupport: scala.collection.parallel.TaskSupport = scala.collection.parallel.ForkJoinTaskSupport@4a5d484a

scala> pc map { _ + 1 }
res0: scala.collection.parallel.mutable.ParArray[Int] = ParArray(2, 3, 4)

在使用 fork 连接池实例化 ForkJoinTaskSupport 对象时,必须将 fork 连接池的并行级别设置为所需的值(示例中为 2).

While instantiating the ForkJoinTaskSupport object with a fork join pool, the parallelism level of the fork join pool must be set to the desired value (2 in the example).

这篇关于scala 并行集合并行度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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