Logo AND Algorithmique Numérique Distribuée

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