Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
6f809f290f4f338982ff9b6b63708ab8a89b764b
[simgrid.git] / src / bindings / ruby / MasterSlave.rb
1 require 'simgrid'
2 include MSG
3
4 #################################################
5 # Class Master
6 #################################################
7
8 class Master < MsgProcess  
9   def initialize2()
10     super()
11   end
12  
13   
14   # msg_main : that function that will be executed when Running Simulation
15   def msg_main(args) # args is an array containing arguments for function master
16     info("Hello From Master")
17     size = args.size
18     info ("Number of Args for Master = " + size.to_s)
19    for i in 0..size-1
20       info("args["+String(i)+"]="+args[i])
21    end
22    
23    raise "Master needs 3 arguments" if size < 3 
24    numberOfTask = Integer(args[0]) 
25    taskComputeSize = Float(args[1])
26    taskCommunicationSize = Float(args[2])
27    slaveCount = Integer(args[3]) 
28    
29    #Creating & Sending Task
30    for i in 0..numberOfTask-1
31   
32      
33      task = Task.new("Task_"+ i.to_s, taskComputeSize , taskCommunicationSize );
34      s_alias = "slave>>" + (i%slaveCount).to_s
35      info("Master Sending "+ Task.name(task) + " to " + s_alias + " with Comput Size " + Task.compSize(task).to_s)
36      Task.send(task,s_alias)
37      info("Master Done Sending " +Task.name(task) + " to " + s_alias)
38 #       sameTask = Task.receive(s_alias)
39 #      puts "Master Receiving its Own Task"
40    end
41   
42    # Sending Finalize Tasks
43    info ("Master: All tasks have been dispatched. Let's tell everybody the computation is over.")
44    for i in 0..slaveCount-1
45      s_alias = "slave " + i.to_s
46      info ("Master Sending Finalize to " + s_alias)
47      Task.send(Task.new("finalize",0,0),s_alias)
48    end
49    info("Master : Everything's Done")
50   end    
51 end
52
53 #################################################
54 # Class Slave
55 #################################################
56 class Slave < MsgProcess
57   
58   def initialize()
59     super()
60   end
61   
62  # msg_main : that function that will be executed when Running Simulation
63   def msg_main(args)
64     info("Hello From Slave")
65     s_mailbox = "slave>>" + args[0]
66
67     while true
68         
69        info("Ready to Receive Task")
70        task = Task.receive(s_mailbox)
71        task_name = Task.name(task)
72        info ("Task Received : " + task.name)
73       if (task_name == "finalize")
74         info("Slave" + s_mailbox + "got finalize msg")
75         break
76       end
77       info("Slave " + s_mailbox + " ...Processing" + Task.name(task))
78        Task.execute(task)
79     end
80     info("Slave " + s_mailbox +  "I'm Done , See You !!")
81     end
82     
83   end
84
85
86 #################################################
87 # main chunck
88 #################################################
89
90 if (ARGV.length == 2) 
91         MSG.createEnvironment(ARGV[0])
92         MSG.deployApplication(ARGV[1])
93 else 
94         MSG.createEnvironment("platform.xml")
95         MSG.deployApplication("deploy.xml")
96   Thread.list.each {|t| p t}
97 end
98
99 # Thread.list.each {|t| p t}
100 MSG.run()
101 MSG.getClock()
102 # exit()