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
12 # msg_main : that function that will be executed when Running Simulation
13 def msg_main(args) # args is an array containing arguments for function master
14 info("Hello From Master")
16 info ("Number of Args for Master = " + size.to_s)
18 info("args["+String(i)+"]="+args[i])
21 raise "Master needs 3 arguments" if size < 3
22 numberOfTask = Integer(args[0])
23 taskComputeSize = Float(args[1])
24 taskCommunicationSize = Float(args[2])
25 slaveCount = Integer(args[3])
27 #Creating & Sending Task
28 for i in 0..numberOfTask-1
31 task = Task.new("Task_"+ i.to_s, taskComputeSize , taskCommunicationSize );
32 s_alias = "slave>>" + (i%slaveCount).to_s
33 info("Master Sending "+ Task.name(task) + " to " + s_alias + " with Comput Size " + Task.compSize(task).to_s)
34 Task.send(task,s_alias)
35 info("Master Done Sending " +Task.name(task) + " to " + s_alias)
36 # sameTask = Task.receive(s_alias)
37 # puts "Master Receiving its Own Task"
40 # Sending Finalize Tasks
41 info ("Master: All tasks have been dispatched. Let's tell everybody the computation is over.")
42 for i in 0..slaveCount-1
43 s_alias = "slave " + i.to_s
44 info ("Master Sending Finalize to " + s_alias)
45 Task.send(Task.new("finalize",0,0),s_alias)
47 info("Master : Everything's Done")
51 #################################################
53 #################################################
54 class Slave < MsgProcess
55 # msg_main : that function that will be executed when Running Simulation
57 info("Hello From Slave")
58 s_mailbox = "slave>>" + args[0]
62 info("Ready to Receive Task")
63 task = Task.receive(s_mailbox)
64 task_name = Task.name(task)
65 info ("Task Received : " + task.name)
66 if (task_name == "finalize")
67 info("Slave" + s_mailbox + "got finalize msg")
70 info("Slave " + s_mailbox + " ...Processing" + Task.name(task))
73 info("Slave " + s_mailbox + "I'm Done , See You !!")
79 #################################################
81 #################################################
84 MSG.createEnvironment(ARGV[0])
85 MSG.deployApplication(ARGV[1])
87 MSG.createEnvironment("platform.xml")
88 MSG.deployApplication("deploy.xml")
89 Thread.list.each {|t| p t}
92 # Thread.list.each {|t| p t}