1 # Debug it with this command:
2 # make -C ../.. && valgrind ruby MasterSlave.rb --log=ruby.thres:debug 2>&1 | less
8 #################################################
10 #################################################
12 class Master < MSG::Process
13 # main : that function that will be executed when Running Simulation
14 def main(args) # args is an array containing arguments for function master
17 MSG::info("args["+String(i)+"]="+args[i])
20 raise "Master needs 3 arguments" if size < 3
21 numberOfTask = Integer(args[0])
22 taskComputeSize = Float(args[1])
23 taskCommunicationSize = Float(args[2])
24 slaveCount = Integer(args[3])
26 # Creates and sends the tasks
27 for i in 0..numberOfTask-1
28 task = MSG::Task.new("Task_"+ i.to_s, taskComputeSize , taskCommunicationSize);
29 mailbox = "slave " + (i%slaveCount).to_s
30 MSG::info("Master Sending "+ task.name + " to " + mailbox + " with Comput Size " +
32 # task.compSize.to_s) # FIXME: This version creates a deadlock. Interesting
34 MSG::info("Master Done Sending " + task.name + " to " + mailbox)
37 # Sending Finalize MSG::Tasks
38 MSG::info("Master: All tasks have been dispatched. Let's tell everybody the computation is over.")
39 for i in 0..slaveCount-1
40 mailbox = "slave " + i.to_s
41 MSG::info("Master Sending Finalize to " + mailbox)
42 finalize_task = Task.new("finalize",0,0)
43 finalize_task.send(mailbox)
45 MSG::info("Master : Everything's Done")
49 #################################################
51 #################################################
52 class Slave < MSG::Process
54 mailbox = "slave " + args[0]
55 for i in 0..args.size-1
56 MSG::debug("args["+String(i)+"]="+args[i])
60 info("Ready to Receive Task")
61 task = MSG::Task.receive(mailbox)
62 MSG::info("Task Received : " + task.name)
63 if (task.name == "finalize")
64 MSG::info("Slave " + mailbox + " got finalize msg")
67 MSG::info("Slave " + mailbox + " ...Processing" + task.name)
70 MSG::info("Slave " + mailbox + "I'm Done , See You !!")
75 #################################################
77 #################################################
80 MSG.createEnvironment(ARGV[0])
81 MSG.deployApplication(ARGV[1])
83 MSG.createEnvironment("platform.xml")
84 MSG.deployApplication("deploy.xml")
85 #Thread.list.each {|t| p t}
88 # Thread.list.each {|t| p t}
90 puts "Simulation time : " + MSG.getClock .to_s