PageRenderTime 16ms CodeModel.GetById 2ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/bench/scala/oBigPingPong.scala

http://github.com/kilim/kilim
Scala | 82 lines | 61 code | 12 blank | 9 comment | 6 complexity | 721185ebc0c11f48d8d98b76b79572a8 MD5 | raw file
 1package examples.actors
 2
 3/*
 4Adapted from PingPongEx.scala, itself an adaption of an example
 5from the scala distribution (added timing measurement).
 6
 7This example creates n tasks and has each send a message to the
 8others. It sends the same structure (and number of messages) as the
 9kilim/bench/BigPingPong example.
10*/
11import scala.actors._
12import scala.actors.Actor._
13
14object BigPingPong {
15  var beginTime: long = 0
16
17  def main(args : Array[String]): Unit = {
18    val nTasks = Integer.parseInt(args(0))
19    var i = nTasks
20    var tasks: Array[Ping] = new Array[Ping](nTasks)
21    Scheduler.impl = new SingleThreadedScheduler
22    val cTask = new Collector(nTasks);
23    cTask.start()
24
25    beginTime = System.currentTimeMillis()
26    for (val i <- 0 to nTasks-1) {
27      tasks(i) = new Ping(i)
28    }
29    for (val t <- tasks) {
30      t.setOthers(tasks, cTask)
31      t.start();
32    }
33  }
34}
35
36abstract class Msg
37case class PingMsg(from :int ) extends Msg
38
39class Ping(i: int) extends Actor {
40  var id : int = i
41  var tasks: Array[Ping] = Array()
42  var ctask : Collector = null
43
44  def setOthers(others: Array[Ping], ct : Collector) {
45    ctask = ct
46    tasks = others
47  }
48
49  def act(): unit = {
50    System.out.println("" + id + " n = " + tasks.length);
51    for (val t <- tasks) {
52      if (t != this) {
53	t ! new PingMsg(id)
54	receive {
55	  case PingMsg(from) => { 
56	    System.out.println ("" + from + " -> " + id);
57	  }
58	}
59      }
60    }
61    ctask ! new PingMsg(id)    
62  }
63}
64
65
66class Collector(n : int) extends Actor {
67  val nTasks = n
68
69  def act(): unit = {
70    for (val i <- 1 to nTasks) {
71      receive {
72	case PingMsg(from) => {
73	  //System.out.println("Completed: " + from);
74	}
75      }
76    }
77
78    val elapsed = System.currentTimeMillis() - BigPingPong.beginTime;
79    System.out.println("Elapsed  : " + elapsed)
80    System.exit(0);
81  }
82}