Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8d4901aa5c65c4a14495edbc666b1e50b72f1591
[simgrid.git] / src / bindings / ruby / MasterSlaveData.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
15   def main(args) # args is an array containing arguments for function master
16    size = args.size
17    for i in 0..size-1
18      MSG::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    # Creates and sends the tasks
28     for i in 0..numberOfTask-1
29      task = MSG::Task.new("Task_"+ i.to_s, taskComputeSize , taskCommunicationSize);
30       task.setData("a messgae...");
31       mailbox = "slave " + (i%slaveCount).to_s
32       MSG::debug("Master Sending "+ task.name + " to " + mailbox + " with Comput Size " + 
33            task.compSize.to_s)
34       task.send(mailbox)
35       MSG::debug("Master Done Sending " + task.name + " to " + mailbox)
36     end
37   
38    # Sending Finalize MSG::Tasks
39    #MSG::info("Master: All "+numberOfTask+" tasks have been dispatched. Let's tell everybody the computation is over.")
40    MSG::info("Master: All tasks have been dispatched. Let's tell everybody the computation is over.")
41    for i in 0..slaveCount-1
42      mailbox = "slave " + i.to_s
43      finalize_task = Task.new("finalize",0,0)
44      finalize_task.send(mailbox)
45    end
46    MSG::info("Master : Everything's Done")
47   end    
48 end
49
50 #################################################
51 # Class Slave
52 #################################################
53 class Slave < MSG::Process
54
55   def main(args)
56     mailbox = "slave " + args[0]
57     for i in 0..args.size-1
58       MSG::debug("args["+String(i)+"]="+args[i])
59     end
60
61     while true
62        task = MSG::Task.receive(mailbox)
63        data = task.data if (task.hasData)
64        MSG::info("The data inside was :" + data)
65        if (task.name == "finalize")
66                break
67        end
68        task.execute
69        MSG::debug("Slave '" + mailbox + "' done executing task "+ task.name + ".")
70     end
71     MSG::info("I'm done, see you")
72   end    
73 end
74
75 #################################################
76 # main chunck
77 #################################################
78
79 if (ARGV.length == 2) 
80         MSG.createEnvironment(ARGV[0])
81         MSG.deployApplication(ARGV[1])
82 else 
83         MSG.createEnvironment("platform.xml")
84         MSG.deployApplication("deploy.xml")
85 end
86
87 MSG.run
88 puts "Simulation time : " + MSG.getClock .to_s
89 MSG.exit