1

I am using the par.map expression to execute processes in parallel in Scala (SBT).

Consider list.par.map(function(_)) (I am preparing an MWE). This means that function(_) should be applied to all the elements of the list in a parallel fashion. In my example, list has 3 elements. But Scala executes only function(list(1)) and function(list(2)) in parallel, and only afterwards function(list(3)).

Is there a reason for this behaviour? Is there a relation with the fact that the programme is executed on a two-core processor? Or how could you impose to execute all three things in parallel?

Karlo
  • 1,630
  • 4
  • 33
  • 57
  • 1
    http://www.scala-lang.org/api/current/scala/collection/parallel/immutable/ParRange.html#tasksupport_=(ts:scala.collection.parallel.TaskSupport):Unit – 余杰水 Nov 11 '16 at 01:45
  • 2
    Possible duplicate of [scala parallel collections degree of parallelism](http://stackoverflow.com/questions/5424496/scala-parallel-collections-degree-of-parallelism) – Andrey Nov 11 '16 at 02:01

1 Answers1

5

This question has been asked before:

and is well documented:

what you want is something like:

var parallelList = list.par
parallelList.tasksupport = new ForkJoinTaskSupport(
     new scala.concurrent.forkjoin.ForkJoinPool(parlevel))
parallelList.map(function(_))

That said if your running on a 2 core processor you only have two threads (unless the cores are hyper threaded of course) meaning you can't have more than 2 parallel operations at once.

Community
  • 1
  • 1
Danny
  • 541
  • 2
  • 11