Mixing Parallel Collections with Akka

akka, parallel-processing, scala, scala-collections

Solution

At present this is not supported / handled well.

Prior to Scala 2.11-M7, attempting to use the dispatcher as the ContextExecutor throws an exception.

That is, the following code in an actor's receive will throw a `NotImplementedError`:

val par = List(1,2,3).par
par.tasksupport = new ExecutionContextTaskSupport(context.dispatcher)

par foreach println

Incidentally, this has been fixed in 2.11-M7, though it was not done to correct the above issue. In reading through the notes on the fix it sounds like the implementation provided by `ExecutionContextTaskSupport` in the above case could have some overhead over directly using one of the other TaskSupport implementations; however, I have done nothing to test that interpretation or evaluate the magnitude of any impact.

A Note on Parallel Collections: By default Parallel Collections will use the global ExecutorContext (`ExecutionContext.Implicits.global`) just as you might use for Futures. While this is well behaved, if you want to be constrained by the dispatcher (using `context.dispatcher`)—as you are likely to do with Futures in Akka—you need to set a different `TaskSupport` as shown in the code sample above.

Problem

How well to scala parallel collection operations get along with the concurrency/parallelism used by Akka Actors (and Futures) with respect to efficient scheduling on the system? Actors' and Futures' execution is handled by an `ExecutionContext` generally provided by the `Dispatcher`. What I find on parallel collections indicates they use a `TaskSupport` object. I found a `ExecutionContextTaskSupport` object that may connect the two but am not sure. What is the proper way to mix the two concurrency solutions, or is it advised not to?

Original source