3
votes

I'm taking a Distributed Systems class this fall and since Scala actors render themselves so useful for this area, I've just started out with learning Scala. From what I have read so far about Actors from the text 'Actors in Scala' I have a question which doesn't seem to have been addresses very clearly in the text.

I'm presently modeling my program on a single machine with multi-core processor and using event-based actors in a Master-Workers model. Now which among the following two design choices would lead to the all the processor cores being used optimally?

1) Limit the number of actors to a small fixed number, irrespective of the number of jobs that the master Actor has, to hand out to the worker actors(which by the way may sometimes be considerable large)

2) Spawn as many actors as the number of jobs that the Master Actor needs to be finished

What factors would govern my decision for the above two choices?

UPDATE: I printed out the thread ids of the 100 worker actors I'm running and here's the output -

No. of Processors-2
MainActor is on thread:Thread[ForkJoinPool-1-worker-0,5,main]
AccumulatorActor is on thread:Thread[ForkJoinPool-1-worker-1,5,main]

Worker Actor-1 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-4 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-5 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-6 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-7 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-8 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-9 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-10 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-3 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-12 running on thread:Thread[ForkJoinPool-1-worker-0,5,main] 
Worker Actor-13 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-14 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-15 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-2 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-16 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-11 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-17 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-18 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-19 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-20 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-21 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-22 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-23 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-24 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-25 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-26 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-28 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-29 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-27 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-30 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-31 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-33 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-32 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-34 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-35 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-36 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-38 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-39 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-40 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-41 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-42 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-43 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-44 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-45 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-46 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-47 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-48 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-51 running on thread:Thread[ForkJoinPool-1-worker-1,5,main]
Worker Actor-50 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-54 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-49 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-56 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-57 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-58 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-55 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-59 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-52 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-61 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-60 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-63 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-53 running on thread:Thread[ForkJoinPool-1-worker-1,5,main]
Worker Actor-65 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-66 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-68 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-64 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-62 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-70 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-69 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-67 running on thread:Thread[ForkJoinPool-1-worker-1,5,main]
Worker Actor-72 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-73 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-71 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-75 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-78 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-79 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-80 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-81 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-82 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-83 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-74 running on thread:Thread[ForkJoinPool-1-worker-1,5,main]
Worker Actor-84 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-85 running on thread:Thread[ForkJoinPool-1-worker-1,5,main]
Worker Actor-77 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-76 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-86 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-87 running on thread:Thread[ForkJoinPool-1-worker-1,5,main]
Worker Actor-88 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-89 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-91 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-92 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-94 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-95 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]
Worker Actor-96 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-90 running on thread:Thread[ForkJoinPool-1-worker-1,5,main]
Worker Actor-98 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-99 running on thread:Thread[ForkJoinPool-1-worker-1,5,main]
Worker Actor-100 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-93 running on thread:Thread[ForkJoinPool-1-worker-2,5,main]
Worker Actor-37 running on thread:Thread[ForkJoinPool-1-worker-0,5,main]
Worker Actor-97 running on thread:Thread[ForkJoinPool-1-worker-3,5,main]

As can be seen above there are four worker threads running in the ForkJoinPool, while I have 2 cpu cores on my machine. I'm not sure why this is though because a quick look at the source of ResizableThreadPoolScheduler leads me to believe that initially the number of worker threads in the pool are equal to number of cpu cores and these are increased dynamically in case any of the actors are blocked on one of the worker threads in the pool. All the actors I use are event based(react) and do not perform any blocking operations. An ideal scenario would be to have just 2 threads in the ForkJoinPool and have them run in parallel on the two cores performing the work of each actor. Is my understanding right about this or am I missing something here?

1
Rememeber that many processors have hyperthreading so the optimal number of threads is likely higher than the number of cores.Luigi Plinge

1 Answers

4
votes

You sound like you're thinking about actors as if they were threads. Part of the purpose of having an actor system is to decouple the question you have from the actors themselves. Actors are cheap to create, so it's fine to have a couple thousand actors. Having thousands of actors doesn't mean they'll each try to run simultaneously.

The Scheduler is what manages how to execute actor requests. By default, you'll get a ForkJoinScheduler or (on exceptional JVMs) ResizableThreadPoolScheduler

The number of parallel tasks is governed by ThreadPoolConfig in both cases and defaults to max(2*cores,4). In this class, you can also see how to override these defaults through System properties:

  • actors.enableForkJoin allows you to control the choice of scheduler
  • actors.corePoolSize sets the initial "thread" pool size
  • actors.maxPoolSize sets the maximum size

Note that it is probably a better idea to study Akka actors, since they are likely to replace the original Scala actor implementation at some point, provide more features and enforce cleaner adherence to the actor model (because you only get an ActorRef and can not directly invoke methods on your actor any more). In Akka, the equivalent of schedulers are dispatchers.