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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于使用 JVM 1.6 或更高版本的最新主干,请使用:
不过,这可能会在将来发生变化。计划在下一个版本中采用更统一的方法来配置所有 Scala 任务并行 API。
但请注意,虽然这将确定查询使用的处理器数量,但这可能不是运行查询所涉及的实际线程数。由于并行集合支持嵌套并行性,因此实际线程池实现如果检测到有必要,可能会分配更多线程来运行查询。
编辑:
从 Scala 2.10 开始,设置并行级别的首选方法是将
tasksupport
字段设置为新的TaskSupport
对象,如下例所示:在实例化具有 fork join 池的
ForkJoinTaskSupport
对象,fork join 池的并行度必须设置为所需的值(示例中的2
)。With the newest trunk, using the JVM 1.6 or newer, use the:
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.
EDIT:
Starting from Scala 2.10, the preferred way to set the parallelism level is through setting the
tasksupport
field to a newTaskSupport
object, as in the following example: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).独立于 JVM 版本,在 Scala 2.9+(引入了并行集合)中,您还可以使用
grouped(Int)
和par
函数的组合来执行并行作业小块,如下所示:grouped(2)
创建长度为 2 或更小的块,seq
确保块的集合不是并行的(在本例中无用),然后_ * 2
函数在小并行块(使用par
创建)上执行,从而确保最多 2 个线程并行执行。然而,这可能比设置工作池参数的效率稍低,我对此不确定。
Independently of the JVM version, with Scala 2.9+ (introduced parallel collections), you can also use a combination of the
grouped(Int)
andpar
functions to execute parallel jobs on small chunks, like this:grouped(2)
creates chunks of length 2 or less,seq
makes sure the collection of chunks is not parallel (useless in this example), then the_ * 2
function is executed on the small parallel chunks (created withpar
), thus insuring that at most 2 threads is executed in parallel.This might be however slightly less efficient than setting the worker pool parameter, I'm not sure about that.