Logo AND Algorithmique Numérique Distribuée

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