2011-04-01 14:28:08 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009-2011 Scalable Solutions AB <http://scalablesolutions.se>
|
|
|
|
|
*/
|
|
|
|
|
|
2011-04-05 17:28:16 +02:00
|
|
|
package akka.tutorial.scala.first
|
2011-04-01 14:28:08 +02:00
|
|
|
|
2011-04-01 15:33:46 +02:00
|
|
|
import akka.actor.{Actor, ActorRef, PoisonPill}
|
2011-04-01 14:28:08 +02:00
|
|
|
import Actor._
|
|
|
|
|
import akka.routing.{Routing, CyclicIterator}
|
2011-04-01 15:33:46 +02:00
|
|
|
import Routing._
|
2011-04-01 14:28:08 +02:00
|
|
|
import akka.dispatch.Dispatchers
|
|
|
|
|
|
|
|
|
|
import System.{currentTimeMillis => now}
|
|
|
|
|
import java.util.concurrent.CountDownLatch
|
|
|
|
|
|
2011-04-06 10:45:48 +02:00
|
|
|
import scala.annotation.tailrec
|
|
|
|
|
|
2011-04-01 16:02:55 +02:00
|
|
|
/**
|
2011-04-05 17:28:16 +02:00
|
|
|
* First part in Akka tutorial.
|
2011-04-01 16:02:55 +02:00
|
|
|
* <p/>
|
|
|
|
|
* Calculates Pi.
|
|
|
|
|
* <p/>
|
2011-04-05 17:28:16 +02:00
|
|
|
* Run on command line:
|
|
|
|
|
* <pre>
|
|
|
|
|
* $ cd akka-1.1
|
|
|
|
|
* $ export AKKA_HOME=`pwd`
|
|
|
|
|
* $ scalac -cp dist/akka-actor-1.1-SNAPSHOT.jar Pi.scala
|
|
|
|
|
* $ java -cp dist/akka-actor-1.1-SNAPSHOT.jar:scala-library.jar:. akka.tutorial.scala.first.Pi
|
|
|
|
|
* $ ...
|
|
|
|
|
* </pre>
|
|
|
|
|
* <p/>
|
2011-04-01 16:02:55 +02:00
|
|
|
* Run it in SBT:
|
|
|
|
|
* <pre>
|
|
|
|
|
* $ sbt
|
|
|
|
|
* > update
|
|
|
|
|
* > console
|
2011-04-05 17:28:16 +02:00
|
|
|
* > akka.tutorial.scala.first.Pi.calculate(nrOfWorkers = 4, nrOfElements = 10000, nrOfMessages = 10000)
|
2011-04-01 16:02:55 +02:00
|
|
|
* > ...
|
|
|
|
|
* > :quit
|
|
|
|
|
* </pre>
|
|
|
|
|
*
|
|
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
|
|
|
|
*/
|
2011-04-05 17:28:16 +02:00
|
|
|
object Pi extends App {
|
|
|
|
|
|
|
|
|
|
calculate(nrOfWorkers = 4, nrOfElements = 10000, nrOfMessages = 10000)
|
2011-04-01 14:28:08 +02:00
|
|
|
|
2011-04-01 16:02:55 +02:00
|
|
|
// ====================
|
2011-04-01 14:28:08 +02:00
|
|
|
// ===== Messages =====
|
2011-04-01 16:02:55 +02:00
|
|
|
// ====================
|
2011-04-01 14:28:08 +02:00
|
|
|
sealed trait PiMessage
|
2011-04-02 14:01:36 +02:00
|
|
|
case object Calculate extends PiMessage
|
|
|
|
|
case class Work(arg: Int, nrOfElements: Int) extends PiMessage
|
2011-04-01 14:28:08 +02:00
|
|
|
case class Result(value: Double) extends PiMessage
|
|
|
|
|
|
2011-04-01 16:02:55 +02:00
|
|
|
// ==================
|
2011-04-01 14:28:08 +02:00
|
|
|
// ===== Worker =====
|
2011-04-01 16:02:55 +02:00
|
|
|
// ==================
|
2011-04-06 13:39:18 +02:00
|
|
|
class Worker extends Actor {
|
2011-04-02 14:01:36 +02:00
|
|
|
// define the work
|
2011-04-06 09:53:30 +02:00
|
|
|
|
2011-04-06 10:45:48 +02:00
|
|
|
/*
|
2011-04-06 09:53:30 +02:00
|
|
|
// FIXME tail-recursive fun instead
|
2011-04-02 14:01:36 +02:00
|
|
|
val calculatePiFor = (arg: Int, nrOfElements: Int) => {
|
2011-04-08 16:46:29 +12:00
|
|
|
val range = (arg * nrOfElements) until ((arg + 1) * nrOfElements)
|
2011-04-05 17:28:16 +02:00
|
|
|
var acc = 0.0D
|
|
|
|
|
range foreach (i => acc += 4 * math.pow(-1, i) / (2 * i + 1))
|
|
|
|
|
acc
|
2011-04-06 09:53:30 +02:00
|
|
|
// Use this for more functional style but is twice as slow
|
|
|
|
|
// range.foldLeft(0.0D)( (acc, i) => acc + 4 * math.pow(-1, i) / (2 * i + 1) )
|
2011-04-02 14:01:36 +02:00
|
|
|
}
|
2011-04-06 10:45:48 +02:00
|
|
|
*/
|
|
|
|
|
def calculatePiFor(arg: Int, nrOfElements: Int): Double = {
|
2011-04-08 16:46:29 +12:00
|
|
|
val end = (arg + 1) * nrOfElements
|
2011-04-06 10:45:48 +02:00
|
|
|
@tailrec def doCalculatePiFor(cursor: Int, acc: Double): Double = {
|
2011-04-08 16:46:29 +12:00
|
|
|
if (cursor == end) acc
|
2011-04-06 10:45:48 +02:00
|
|
|
else doCalculatePiFor(cursor + 1, acc + (4 * math.pow(-1, cursor) / (2 * cursor + 1)))
|
|
|
|
|
}
|
|
|
|
|
doCalculatePiFor(arg * nrOfElements, 0.0D)
|
|
|
|
|
}
|
2011-04-02 14:01:36 +02:00
|
|
|
|
2011-04-01 14:28:08 +02:00
|
|
|
def receive = {
|
2011-04-02 14:01:36 +02:00
|
|
|
case Work(arg, nrOfElements) =>
|
|
|
|
|
self reply Result(calculatePiFor(arg, nrOfElements)) // perform the work
|
2011-04-01 14:28:08 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-01 16:02:55 +02:00
|
|
|
// ==================
|
2011-04-01 14:28:08 +02:00
|
|
|
// ===== Master =====
|
2011-04-01 16:02:55 +02:00
|
|
|
// ==================
|
2011-04-02 14:01:36 +02:00
|
|
|
class Master(nrOfWorkers: Int, nrOfMessages: Int, nrOfElements: Int, latch: CountDownLatch) extends Actor {
|
2011-04-01 14:28:08 +02:00
|
|
|
var pi: Double = _
|
2011-04-01 14:48:04 +02:00
|
|
|
var nrOfResults: Int = _
|
2011-04-01 14:28:08 +02:00
|
|
|
var start: Long = _
|
|
|
|
|
|
2011-04-01 14:48:04 +02:00
|
|
|
// create the workers
|
2011-04-02 15:22:38 +02:00
|
|
|
val workers = Vector.fill(nrOfWorkers)(actorOf[Worker].start)
|
2011-04-01 14:48:04 +02:00
|
|
|
|
|
|
|
|
// wrap them with a load-balancing router
|
|
|
|
|
val router = Routing.loadBalancerActor(CyclicIterator(workers)).start
|
|
|
|
|
|
2011-04-01 16:02:55 +02:00
|
|
|
// message handler
|
2011-04-01 14:28:08 +02:00
|
|
|
def receive = {
|
2011-04-02 14:01:36 +02:00
|
|
|
case Calculate =>
|
2011-04-01 14:48:04 +02:00
|
|
|
// schedule work
|
2011-04-02 14:01:36 +02:00
|
|
|
for (arg <- 0 until nrOfMessages) router ! Work(arg, nrOfElements)
|
2011-04-01 14:48:04 +02:00
|
|
|
|
|
|
|
|
// send a PoisonPill to all workers telling them to shut down themselves
|
2011-04-01 15:33:46 +02:00
|
|
|
router ! Broadcast(PoisonPill)
|
2011-04-01 14:48:04 +02:00
|
|
|
|
2011-04-02 15:22:38 +02:00
|
|
|
// send a PoisonPill to the router, telling him to shut himself down
|
|
|
|
|
router ! PoisonPill
|
|
|
|
|
|
2011-04-01 14:28:08 +02:00
|
|
|
case Result(value) =>
|
2011-04-01 16:02:55 +02:00
|
|
|
// handle result from the worker
|
2011-04-01 14:28:08 +02:00
|
|
|
pi += value
|
2011-04-01 14:48:04 +02:00
|
|
|
nrOfResults += 1
|
|
|
|
|
if (nrOfResults == nrOfMessages) self.stop
|
2011-04-01 14:28:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def preStart = start = now
|
|
|
|
|
|
|
|
|
|
override def postStop = {
|
2011-04-01 16:02:55 +02:00
|
|
|
// tell the world that the calculation is complete
|
2011-04-05 11:08:44 +02:00
|
|
|
println("\n\tPi estimate: \t\t%s\n\tCalculation time: \t%s millis".format(pi, (now - start)))
|
2011-04-01 15:33:46 +02:00
|
|
|
latch.countDown
|
2011-04-01 14:28:08 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-01 16:02:55 +02:00
|
|
|
// ==================
|
|
|
|
|
// ===== Run it =====
|
|
|
|
|
// ==================
|
2011-04-05 17:28:16 +02:00
|
|
|
def calculate(nrOfWorkers: Int, nrOfElements: Int, nrOfMessages: Int) {
|
2011-04-02 14:01:36 +02:00
|
|
|
|
2011-04-01 16:02:55 +02:00
|
|
|
// this latch is only plumbing to know when the calculation is completed
|
2011-04-01 15:33:46 +02:00
|
|
|
val latch = new CountDownLatch(1)
|
2011-04-01 14:28:08 +02:00
|
|
|
|
|
|
|
|
// create the master
|
2011-04-02 14:01:36 +02:00
|
|
|
val master = actorOf(new Master(nrOfWorkers, nrOfMessages, nrOfElements, latch)).start
|
2011-04-01 14:28:08 +02:00
|
|
|
|
2011-04-01 14:48:04 +02:00
|
|
|
// start the calculation
|
2011-04-02 14:01:36 +02:00
|
|
|
master ! Calculate
|
2011-04-01 14:28:08 +02:00
|
|
|
|
2011-04-01 14:48:04 +02:00
|
|
|
// wait for master to shut down
|
2011-04-01 14:28:08 +02:00
|
|
|
latch.await
|
|
|
|
|
}
|
|
|
|
|
}
|