Logo AND Algorithmique Numérique Distribuée

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