Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
efc3a9d7fd64626817e31055c6319c771458db2e
[simgrid.git] / examples / ruby / MasterSlave.rb
1 # Debug it with this command:
2 # make -C ../.. && valgrind ruby MasterSlave.rb --log=ruby.thres:debug 2>&1 | less
3 require 'simgrid'
4 include MSG
5 #################################################
6 # Class Master
7 #################################################
8
9
10 class Master < MSG::Process  
11   # main : that function that will be executed when running simulation
12
13   def main(args) # args is an array containing arguments for function master
14    size = args.size
15    for i in 0..size-1
16      MSG::info("args["+String(i)+"]="+args[i])
17    end
18   
19    raise "Master needs 3 arguments" if size < 3 
20    numberOfTask = Integer(args[0]) 
21    taskComputeSize = Float(args[1])
22    taskCommunicationSize = Float(args[2])
23    slaveCount = Integer(args[3]) 
24    
25    # Creates and sends the tasks
26     for i in 0..numberOfTask-1
27      task = RTask.new("Task_"+ i.to_s, taskComputeSize , taskCommunicationSize);
28      mailbox = "slave " + (i%slaveCount).to_s
29      MSG::info("Master Sending "+ task.name + " to " + mailbox + " with Comput Size " + 
30            task.compSize.to_s)
31      task.send(mailbox)
32      MSG::info("Master Done Sending " + task.name + " to " + mailbox)
33     end
34   
35    # Sending Finalize MSG::Tasks
36    #MSG::info("Master: All "+numberOfTask+" tasks have been dispatched. Let's tell everybody the computation is over.")
37    MSG::info("Master: All tasks have been dispatched. Let's tell everybody the computation is over.")
38    for i in 0..slaveCount-1
39      mailbox = "slave " + i.to_s
40      finalize_task = RTask.new("finalize",0,0)
41      finalize_task.send(mailbox)
42    end
43    MSG::info("Master : Everything's Done")
44   end    
45 end
46 #################################################
47 # Class Slave
48 #################################################
49 class Slave < MSG::Process
50
51   def main(args)
52     mailbox = "slave " + args[0]
53     for i in 0..args.size-1
54       MSG::debug("args["+String(i)+"]="+args[i])
55     end
56
57     while true
58        task = Task.receive(mailbox)
59        if (task.name == "finalize")
60                break
61        end
62        task.execute
63        MSG::debug("Slave '" + mailbox + "' done executing task "+ task.name + ".")
64     end
65     MSG::info("I'm done, see you")
66   end
67 end
68 #################################################
69 # main chunck
70 #################################################
71 if (ARGV.length == 2) 
72         MSG.createEnvironment(ARGV[0])
73         MSG.deployApplication(ARGV[1])
74 else 
75         MSG.createEnvironment("platform.xml")
76         MSG.deployApplication("deploy.xml")
77 end
78 MSG.run
79 puts "Simulation time : " + MSG.getClock .to_s
80 MSG.exit