pekko/akka-tutorials/akka-tutorial-first/src/main/scala/Pi.scala

114 lines
3 KiB
Scala
Raw Normal View History

/**
* Copyright (C) 2009-2011 Typesafe Inc. <http://www.typesafe.com>
*/
package akka.tutorial.first.scala
import akka.actor.{ Actor, PoisonPill, ActorSystem }
import Actor._
import java.util.concurrent.CountDownLatch
2011-07-28 15:48:03 +03:00
import akka.routing.Routing.Broadcast
import akka.routing.{ RoutedProps, Routing }
object Pi extends App {
2011-10-12 11:34:35 +02:00
val app = ActorSystem()
calculate(nrOfWorkers = 4, nrOfElements = 10000, nrOfMessages = 10000)
// ====================
// ===== Messages =====
// ====================
sealed trait PiMessage
case object Calculate extends PiMessage
case class Work(start: Int, nrOfElements: Int) extends PiMessage
case class Result(value: Double) extends PiMessage
// ==================
// ===== Worker =====
// ==================
class Worker extends Actor {
// define the work
def calculatePiFor(start: Int, nrOfElements: Int): Double = {
var acc = 0.0
2011-07-26 18:33:59 +12:00
for (i start until (start + nrOfElements))
acc += 4.0 * (1 - (i % 2) * 2) / (2 * i + 1)
acc
}
def receive = {
case Work(start, nrOfElements) sender ! Result(calculatePiFor(start, nrOfElements)) // perform the work
}
}
// ==================
// ===== Master =====
// ==================
class Master(nrOfWorkers: Int, nrOfMessages: Int, nrOfElements: Int, latch: CountDownLatch)
extends Actor {
var pi: Double = _
2011-04-01 14:48:04 +02:00
var nrOfResults: Int = _
var start: Long = _
2011-04-01 14:48:04 +02:00
// create the workers
2011-10-18 17:56:23 +02:00
val workers = Vector.fill(nrOfWorkers)(app.actorOf[Worker])
2011-04-01 14:48:04 +02:00
// wrap them with a load-balancing router
2011-10-18 17:56:23 +02:00
val router = app.actorOf(RoutedProps().withRoundRobinRouter.withLocalConnections(workers), "pi")
2011-04-01 14:48:04 +02:00
// message handler
def receive = {
2011-07-26 18:33:59 +12:00
case Calculate
2011-04-01 14:48:04 +02:00
// schedule work
2011-07-26 18:33:59 +12:00
for (i 0 until nrOfMessages) router ! Work(i * nrOfElements, nrOfElements)
2011-04-01 14:48:04 +02:00
// send a PoisonPill to all workers telling them to shut down themselves
router ! Broadcast(PoisonPill)
2011-04-01 14:48:04 +02:00
// send a PoisonPill to the router, telling him to shut himself down
router ! PoisonPill
2011-07-26 18:33:59 +12:00
case Result(value)
// handle result from the worker
pi += value
2011-04-01 14:48:04 +02:00
nrOfResults += 1
if (nrOfResults == nrOfMessages) self.stop()
}
override def preStart() {
start = System.currentTimeMillis
}
override def postStop() {
// tell the world that the calculation is complete
println(
"\n\tPi estimate: \t\t%s\n\tCalculation time: \t%s millis"
2011-07-26 18:33:59 +12:00
.format(pi, (System.currentTimeMillis - start)))
latch.countDown()
}
}
// ==================
// ===== Run it =====
// ==================
def calculate(nrOfWorkers: Int, nrOfElements: Int, nrOfMessages: Int) {
// this latch is only plumbing to know when the calculation is completed
val latch = new CountDownLatch(1)
// create the master
2011-10-18 17:56:23 +02:00
val master = app.actorOf(new Master(nrOfWorkers, nrOfMessages, nrOfElements, latch))
2011-04-01 14:48:04 +02:00
// start the calculation
master ! Calculate
2011-04-01 14:48:04 +02:00
// wait for master to shut down
latch.await()
}
}