Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1b0344fdb8ca5f2d222f47cdf4a60a67e31c8227
[simgrid.git] / src / bindings / ruby / MasterSlave.rb
1 # Debug it with this command:
2 # make -C ../.. && valgrind ruby MasterSlave.rb --log=ruby.thres:debug 2>&1 | less
3
4 require 'simgrid'
5
6 include MSG
7
8 #################################################
9 # Class Master
10 #################################################
11
12 class Master < MSG::Process  
13   # main : that function that will be executed when Running Simulation
14
15   def main(args) # args is an array containing arguments for function master
16    size = args.size
17    for i in 0..size-1
18      MSG::info("args["+String(i)+"]="+args[i])
19    end
20   
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]) 
26    
27    # Creates and sends the tasks
28    for i in 0..numberOfTask-1
29      task = MSG::Task.new("Task_"+ i.to_s, taskComputeSize , taskCommunicationSize);
30      mailbox = "slave " + (i%slaveCount).to_s
31      MSG::info("Master Sending "+ task.name + " to " + mailbox + " with Comput Size " + 
32           task.compSize.to_s)
33 #          task.compSize.to_s) # FIXME: This version creates a deadlock. Interesting
34      task.send(mailbox)
35      MSG::info("Master Done Sending " + task.name + " to " + mailbox)
36    end
37   
38    # Sending Finalize MSG::Tasks
39    MSG::info("Master: All tasks have been dispatched. Let's tell everybody the computation is over.")
40    for i in 0..slaveCount-1
41      mailbox = "slave " + i.to_s
42      MSG::info("Master Sending Finalize to " + mailbox)
43      finalize_task = Task.new("finalize",0,0)
44      finalize_task.send(mailbox)
45    end
46    MSG::info("Master : Everything's Done")
47    Thread.list.each {|t| p t}
48   end    
49 end
50
51 #################################################
52 # Class Slave
53 #################################################
54 class Slave < MSG::Process
55
56   def main(args)
57     mailbox = "slave " + args[0]
58     for i in 0..args.size-1
59       MSG::debug("args["+String(i)+"]="+args[i])
60     end
61
62     while true
63        info("Ready to Receive Task")
64        task = MSG::Task.receive(mailbox)
65        MSG::info("Task Received : " + task.name)
66        if (task.name == "finalize")
67                MSG::info("Slave " + mailbox + " got finalize msg")
68                break
69        end
70        MSG::info("Slave " + mailbox + " ...Processing" + task.name)
71        task.execute
72        MSG::info("task "+ task.name + " Executed !!")
73     end
74     MSG::info("Slave " + mailbox +  "I'm Done , See You !!")
75   end    
76 end
77
78 #################################################
79 # main chunck
80 #################################################
81
82 if (ARGV.length == 2) 
83         MSG.createEnvironment(ARGV[0])
84         MSG.deployApplication(ARGV[1])
85 else 
86         MSG.createEnvironment("platform.xml")
87         MSG.deployApplication("deploy.xml")
88   #Thread.list.each {|t| p t}
89 end
90
91 MSG.run
92 Thread.list.each {|t| p t}
93 puts "Simulation time : " + MSG.getClock .to_s
94
95 # exit()