1 # Debug it with this command:
2 # make -C ../.. && valgrind ruby MasterSlave.rb --log=ruby.thres:debug 2>&1 | less
7 #################################################
9 #################################################
11 class Master < MsgProcess
17 # msg_main : that function that will be executed when Running Simulation
18 def msg_main(args) # args is an array containing arguments for function master
19 info("Hello From Master")
21 info ("Number of Args for Master = " + size.to_s)
23 info("args["+String(i)+"]="+args[i])
26 raise "Master needs 3 arguments" if size < 3
27 numberOfTask = Integer(args[0])
28 taskComputeSize = Float(args[1])
29 taskCommunicationSize = Float(args[2])
30 slaveCount = Integer(args[3])
32 #Creating & Sending Task
33 for i in 0..numberOfTask-1
36 task = Task.new("Task_"+ i.to_s, taskComputeSize , taskCommunicationSize );
37 s_alias = "slave>>" + (i%slaveCount).to_s
38 info("Master Sending "+ Task.name(task) + " to " + s_alias + " with Comput Size " + Task.compSize(task).to_s)
39 Task.send(task,s_alias)
40 info("Master Done Sending " +Task.name(task) + " to " + s_alias)
41 # sameTask = Task.receive(s_alias)
42 # puts "Master Receiving its Own Task"
45 # Sending Finalize Tasks
46 info ("Master: All tasks have been dispatched. Let's tell everybody the computation is over.")
47 for i in 0..slaveCount-1
48 s_alias = "slave " + i.to_s
49 info ("Master Sending Finalize to " + s_alias)
50 Task.send(Task.new("finalize",0,0),s_alias)
52 info("Master : Everything's Done")
56 #################################################
58 #################################################
59 class Slave < MsgProcess
65 # msg_main : that function that will be executed when Running Simulation
67 info("Hello From Slave")
68 s_mailbox = "slave>>" + args[0]
72 info("Ready to Receive Task")
73 task = Task.receive(s_mailbox)
74 task_name = Task.name(task)
75 info ("Task Received : " + task.name)
76 if (task_name == "finalize")
77 info("Slave" + s_mailbox + "got finalize msg")
80 info("Slave " + s_mailbox + " ...Processing" + Task.name(task))
83 info("Slave " + s_mailbox + "I'm Done , See You !!")
89 #################################################
91 #################################################
94 MSG.createEnvironment(ARGV[0])
95 MSG.deployApplication(ARGV[1])
97 MSG.createEnvironment("platform.xml")
98 MSG.deployApplication("deploy.xml")
99 Thread.list.each {|t| p t}
102 # Thread.list.each {|t| p t}