Last active
January 8, 2021 16:29
-
-
Save bskim45/e6ab1ef46ed155535e8bdfed2e174ff2 to your computer and use it in GitHub Desktop.
Scala Parallel Collection with parallelism level
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Parallel Collection with parallelism level | |
* | |
* Changes the task support of a parallel collection to use a fork-join pool | |
* with desired parallelism level | |
* | |
* Usage: | |
* {{{ | |
* Seq(1, 2, 3, 4).par.withCores(Runtime.getRuntime.availableProcessors * 2).map(_ * 2).sum | |
* }}} | |
* | |
* @see [[scala.collection.parallel.TaskSupport]] | |
*/ | |
implicit class ParExtensions[T](iter: ParIterable[T]) { | |
def withCores(n: Int): ParIterable[T] = { | |
iter.tasksupport = new ForkJoinTaskSupport(new ForkJoinPool(n)) | |
iter | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment