Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
removing ApplicationHandler ruby Class, keeping only C Handler to create Process
[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
6 include MSG
7
8 #################################################
9 # Class Master
10 #################################################
11
12 class Master < MSG::Process  
13   # main : that function that will be executed when Running Simulation
14   def main(args) # args is an array containing arguments for function master
15    size = args.size
16    for i in 0..size-1
17      MSG::info("args["+String(i)+"]="+args[i])
18    end
19   
20    raise "Master needs 3 arguments" if size < 3 
21    numberOfTask = Integer(args[0]) 
22    taskComputeSize = Float(args[1])
23    taskCommunicationSize = Float(args[2])
24    slaveCount = Integer(args[3]) 
25    
26    # Creates and sends the tasks
27    for i in 0..numberOfTask-1
28      task = MSG::Task.new("Task_"+ i.to_s, taskComputeSize , taskCommunicationSize);
29      mailbox = "slave " + (i%slaveCount).to_s
30      MSG::info("Master Sending "+ MSG::Task.name(task) + " to " + mailbox + " with Comput Size " + 
31           MSG::Task.compSize(task).to_s)
32 #          task.compSize.to_s) # FIXME: This version creates a deadlock. Interesting
33      MSG::Task.send(task,mailbox)
34      MSG::info("Master Done Sending " +MSG::Task.name(task) + " to " + mailbox)
35    end
36   
37    # Sending Finalize MSG::Tasks
38    MSG::info("Master: All tasks have been dispatched. Let's tell everybody the computation is over.")
39    for i in 0..slaveCount-1
40      mailbox = "slave " + i.to_s
41      MSG::info("Master Sending Finalize to " + mailbox)
42      MSG::Task.send(Task.new("finalize",0,0),mailbox)
43    end
44    MSG::info("Master : Everything's Done")
45   end    
46 end
47
48 #################################################
49 # Class Slave
50 #################################################
51 class Slave < MSG::Process
52   def main(args)
53     mailbox = "slave " + args[0]
54
55     while true
56        info("Ready to Receive Task")
57        task = MSG::Task.receive(mailbox)
58        task_name = MSG::Task.name(task)
59        MSG::info("Task Received : " + task.name)
60        if (task_name == "finalize")
61                MSG::info("Slave" + s_mailbox + "got finalize msg")
62                break
63        end
64        MSG::info("Slave " + s_mailbox + " ...Processing" + MSG::Task.name(task))
65        MSG::Task.execute(task)
66     end
67     MSG::info("Slave " + s_mailbox +  "I'm Done , See You !!")
68   end    
69 end
70
71
72 #################################################
73 # main chunck
74 #################################################
75
76 if (ARGV.length == 2) 
77         MSG.createEnvironment(ARGV[0])
78         MSG.deployApplication(ARGV[1])
79 else 
80         MSG.createEnvironment("platform.xml")
81         MSG.deployApplication("deploy2.xml")
82   #Thread.list.each {|t| p t}
83 end
84
85 # Thread.list.each {|t| p t}
86 MSG.run
87 MSG.getClock #FIXME: write "puts MSG.getClock" instead
88 # exit()