Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add a scala masterslave example
[simgrid.git] / examples / scala / masterslave / Master.scala
diff --git a/examples/scala/masterslave/Master.scala b/examples/scala/masterslave/Master.scala
new file mode 100644 (file)
index 0000000..aba2c71
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * Master of a basic master/slave example in Java
+ *
+ * Copyright 2006-2012 The SimGrid Team. All rights reserved. 
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the license (GNU LGPL) which comes with this package. 
+ */
+package masterslave;
+
+import org.simgrid.msg.Host;
+import org.simgrid.msg.Msg;
+import org.simgrid.msg.MsgException;
+import org.simgrid.msg.Task;
+import org.simgrid.msg.Process;;
+
+class Master(host:Host, name:String, args:Array[String]) extends Process(host,name,args) {
+  def main(args:Array[String]) {
+    if (args.length < 4) {
+      Msg.info("Master needs 4 arguments")
+      System.exit(1)
+    }
+
+    val tasksCount = args(0).toInt             
+    val taskComputeSize = args(1).toDouble             
+    val taskCommunicateSize = args(2).toDouble
+    val slavesCount = args(3).toInt
+
+    Msg.info("Hello! Got " +  slavesCount + " slaves and " + tasksCount + " tasks to process")
+
+    for (i <- 0 until tasksCount) {
+      val task = new Task("Task_" + i, taskComputeSize, taskCommunicateSize)
+      task.send("slave_"+(i%slavesCount))
+    }
+
+    Msg.info("All tasks have been dispatched. Let's tell everybody the computation is over.")
+
+    for (i <- 0 until slavesCount) {
+      val task = new FinalizeTask()
+      task.send("slave_"+(i%slavesCount))
+    }
+
+    Msg.info("Goodbye now!")
+  }
+}