scala implicit par makes no progress

Viewed 61

This is my first attempt with using Scala's parallelism. I have huge data (that can be stored as any collection) which I would like to parallelize on a multi-core system using a simple map operation (such as val out = data.par.map(foo(_)). The example I saw given at scala docs has the following snippet that gave weird output. The 'serial' versions seems to run with implicit parallelism and the parallel versions are not working. Any pointers towards a solution would be very much appreciated.

scala> val list = (1 to 1000000).toList
list: List[Int] = List(1, 2, 3, 4, 5,...   // used > 1000% cpu

scala> val out = list.map(_ + 42)          // again used > 1000% cpu
out: List[Int] = List(43, 44, 45, 46, 

scala> val out = list.par.map(_ + 42)      // process stalls, consumes no cpu!

scala> (1 to 10) map println               // initially used >400% cpu
1
2
3
4
5
6
7
8
9
10

scala> (1 to 10).par map println               // process stalls, consumes no cpu!

I am using Scala 2.12.10 (OpenJDK 64-Bit Server VM, Java 1.8.0_275)

Edit: The above code ran in a script but not in scala shell. Probably some limitation of the shell itself.

1 Answers

Try with -Yrepl-class-based

$ scala-runners/scala --scala-version 2.12.10 -Yrepl-class-based
Welcome to Scala 2.12.10 (Java HotSpot(TM) 64-Bit Server VM, Java 10.0.2).
Type in expressions for evaluation. Or try :help.

scala> (1 to 10).par map println
6
9
8
1
10
7
2
3
4
5
res0: scala.collection.parallel.immutable.ParSeq[Unit] = ParVector((), (), (), (), (), (), (), (), (), ())
Related