Logo AND Algorithmique Numérique Distribuée

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