Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New platform files (just add AS tag)
[simgrid.git] / examples / ruby / MasterSlave.rb
1 require 'simgrid'
2 include MSG
3 #################################################
4 # Class Master
5 #################################################
6
7 class Master < MSG::Process  
8   # main : that function that will be executed when running simulation
9
10   def main(args) # args is an array containing arguments for function master
11    size = args.size
12    for i in 0..size-1
13      MSG::info("args["+String(i)+"]="+args[i])
14    end
15   
16    raise "Master needs 3 arguments" if size < 3 
17    numberOfTask = Integer(args[0]) 
18    taskComputeSize = Float(args[1])
19    taskCommunicationSize = Float(args[2])
20    slaveCount = Integer(args[3]) 
21    
22    # Creates and sends the tasks
23     for i in 0..numberOfTask-1
24      task = Task.new("Task_"+ i.to_s, taskComputeSize , taskCommunicationSize);
25      mailbox = "slave " + (i%slaveCount).to_s
26      MSG::info("Master Sending "+ task.name + " to " + mailbox + " with Comput Size " + 
27            task.compSize.to_s)
28      task.send(mailbox)
29      MSG::info("Master Done Sending " + task.name + " to " + mailbox)
30     end
31   
32    # Sending Finalize MSG::Tasks
33    #MSG::info("Master: All "+numberOfTask+" tasks have been dispatched. Let's tell everybody the computation is over.")
34    MSG::info("Master: All tasks have been dispatched. Let's tell everybody the computation is over.")
35    for i in 0..slaveCount-1
36      mailbox = "slave " + i.to_s
37      finalize_task = Task.new("finalize",0,0)
38      finalize_task.send(mailbox)
39    end
40    MSG::info("Master : Everything's Done")
41   end    
42 end
43
44 # end_of_master
45
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 # end_of_slave
70
71 #################################################
72 # main chunck
73 #################################################
74 if (ARGV.length == 2) 
75         MSG.createEnvironment(ARGV[0])
76         MSG.deployApplication(ARGV[1])
77 else 
78         MSG.createEnvironment("platform.xml")
79         MSG.deployApplication("deploy.xml")
80 end
81 MSG.run
82 puts "Simulation time : " + MSG.getClock .to_s
83 MSG.exit