I know that you can set the number of threads to use for all .par operations like so:
collection.parallel.ForkJoinTasks.defaultForkJoinPool.setParallelism(parlevel: Int)
But is it possible to set the number of threads to use for just one .par call?
In Scala 2.11 you should use parallel collection task support, like this:
parallelCollection.tasksupport = new ForkJoinTaskSupport(
new java.util.concurrent.ForkJoinPool(parlevel))
parallelCollection.map( ... )
See task support documentation
You could create a block that sets the parallelism level and then execute specific methods within that block:
def withParallelism[A](n : Int)(block : => A) : A = {
import collection.parallel.ForkJoinTasks.defaultForkJoinPool._
val defaultParLevel = getParallelism
setParallelism(n)
val ret = block
setParallelism(defaultParLevel)
ret
}
And then call it as such:
withParallelism(2) {
(1 to 100).par.map(_ * 2)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With