Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
further simplifications to ruby: kill useless ProcessFactory (should to the same...
[simgrid.git] / src / bindings / ruby / MasterSlave.rb
index 419f513..0156cc9 100644 (file)
@@ -1,79 +1,95 @@
-require 'msg'
-require 'RubyProcess'
+# Debug it with this command:
+# make -C ../.. && valgrind ruby MasterSlave.rb --log=ruby.thres:debug 2>&1 | less
 
-def Master(*args)
-  
-   puts "Hey ..from Master"#info("Hey")
-    
-   slaves = Array.new()
-   
-   size = args.size
-   
-   puts "Args = " + size
-   
+require 'simgrid'
+include MSG
+
+#################################################
+# Class Master
+#################################################
+
+class Master < MsgProcess  
+  # msg_main : that function that will be executed when Running Simulation
+  def msg_main(args) # args is an array containing arguments for function master
+    info("Hello From Master")
+    size = args.size
+    info ("Number of Args for Master = " + size.to_s)
    for i in 0..size-1
-     puts "argv :" + args[1]
+      info("args["+String(i)+"]="+args[i])
    end
    
-   raise "Master needs 3 arguments" if size < 3
-   
-   numberOfTask = args[0] #convert to int
-   taskComputeSize = args[1] #convert to double
-   taskCommunicationSize = args[2] #convert to double
-   slaveCount = args[3] #convert to int
-   
-   
-#    todo = Array.new(numberOfTask)
+   raise "Master needs 3 arguments" if size < 3 
+   numberOfTask = Integer(args[0]) 
+   taskComputeSize = Float(args[1])
+   taskCommunicationSize = Float(args[2])
+   slaveCount = Integer(args[3]) 
    
    #Creating & Sending Task
-   for i in 0..numberOfTask 
-     
-     task = RbTask.new("Task_" + i.to_s, taskComputeSize , taskCommunicationSize );
-     s_alias = "slave " + (i%slaveCount).to_s
-     puts "Master Sending "+ RbTask.name(task) + " to " + s_alias
-     RbTask.send(task,s_alias)
-     puts "Master Done Sending " +RbTask.name(task) + " to " + s_alias
+   for i in 0..numberOfTask-1
+  
      
+     task = Task.new("Task_"+ i.to_s, taskComputeSize , taskCommunicationSize );
+     s_alias = "slave>>" + (i%slaveCount).to_s
+     info("Master Sending "+ Task.name(task) + " to " + s_alias + " with Comput Size " + Task.compSize(task).to_s)
+     Task.send(task,s_alias)
+     info("Master Done Sending " +Task.name(task) + " to " + s_alias)
+#       sameTask = Task.receive(s_alias)
+#      puts "Master Receiving its Own Task"
    end
   
    # Sending Finalize Tasks
-   puts "Master: All tasks have been dispatched. Let's tell everybody the computation is over."
+   info ("Master: All tasks have been dispatched. Let's tell everybody the computation is over.")
    for i in 0..slaveCount-1
      s_alias = "slave " + i.to_s
-     puts "Master Sending Finalize to " + s_alias
-     RbTask.send(RbTask.new("finalize",0,0),s_alias)
+     info ("Master Sending Finalize to " + s_alias)
+     Task.send(Task.new("finalize",0,0),s_alias)
    end
-     
-   puts "Master : Everything's Done"
-  
-  
-  
+   info("Master : Everything's Done")
+  end    
 end
 
+#################################################
+# Class Slave
+#################################################
+class Slave < MsgProcess
+ # msg_main : that function that will be executed when Running Simulation
+  def msg_main(args)
+    info("Hello From Slave")
+    s_mailbox = "slave>>" + args[0]
 
-def Slave(*args)
-  
-   puts "Hello From Slave"
-    s_mailbox = "slave" + args[0]
-    
-    
     while true
-      
-      task = RbTask.recieve(s_mailbox)
-      
-      task_name = RbTask.name(task)
-      
-      if ( task_name == "finalize" )
-       puts "Slave" + s_mailbox + "got finalize msg"
+        
+       info("Ready to Receive Task")
+       task = Task.receive(s_mailbox)
+       task_name = Task.name(task)
+       info ("Task Received : " + task.name)
+      if (task_name == "finalize")
+       info("Slave" + s_mailbox + "got finalize msg")
        break
       end
-      
-      puts "Slave " + s_mailbox + "Processing" + RbTask.name(task)
-      RbTask.execute(task)
-      
+      info("Slave " + s_mailbox + " ...Processing" + Task.name(task))
+       Task.execute(task)
     end
-       
-    puts "Slave " + s_mailbox + "I'm Done , See You !!"
-  
-  
-end
\ No newline at end of file
+    info("Slave " + s_mailbox +  "I'm Done , See You !!")
+    end
+    
+  end
+
+
+#################################################
+# main chunck
+#################################################
+
+if (ARGV.length == 2) 
+       MSG.createEnvironment(ARGV[0])
+       MSG.deployApplication(ARGV[1])
+else 
+       MSG.createEnvironment("platform.xml")
+       MSG.deployApplication("deploy.xml")
+  Thread.list.each {|t| p t}
+end
+
+# Thread.list.each {|t| p t}
+MSG.run()
+MSG.getClock()
+# exit()