Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Do not request status if not requested by caller.
[simgrid.git] / src / bindings / ruby / Quicksort.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 Asker
10 #################################################
11
12 class Sender < 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   
16    hardworking_friend = MSG::Host.getByName(args[0]).name
17    taskComputeSize = Float(args[1])
18    taskCommunicationSize = Float(args[2])
19    send_mailbox =args[3]
20    
21    myTable = Array.new
22    myTable <<1<<-2<<45<<67<<87<<76<<89<<56<<78<<3<<-4<<99
23    MSG::info("Hello " + hardworking_friend + " !!, Please !! I need you to help me to sort my table , Here it is :")
24    p myTable
25    # Creates and send Task With the Table inside
26    task = MSG::Task.new("quicksort_task",taskComputeSize, taskCommunicationSize);
27    task.setData(myTable);
28    MSG::debug("Sending "+ task.name + " to " + send_mailbox + " with Comput Size " + 
29            task.compSize.to_s)
30    task.send(send_mailbox)
31    MSG::debug("Done Sending " + task.name + " to " + send_mailbox)
32     
33    #waiting for results
34    
35    recv_mailbox = self.class
36    res_task = MSG::Task.receive(recv_mailbox.to_s)
37    result = res_task.data
38    MSG::info("Greate !! Thx Dude , you're my Best Friend !!")
39    MSG::info("Here is my table after a quicksort :)")
40    p result
41    MSG::info("Bye !! I finished My HomeWork !! Time to Sleep :)")
42    end
43
44 end
45
46
47 #################################################
48 # Class Clever
49 #################################################
50 class Receiver < MSG::Process
51
52   def main(args)
53     
54     lazy_friend = MSG::Host.getByName(args[0]).name
55     send_mailbox = args[1]
56     recv_mailbox = self.class
57     
58     MSG::info("Oh Not Again !! Grrr")
59     task = MSG::Task.receive(recv_mailbox.to_s)
60     table = task.data
61     quicksort(table,0,table.size-1)
62     task.setData(table)
63     MSG::info("Ok "+lazy_friend+ "I did it, next time try to do it on your own :)")
64     task.send(send_mailbox)
65     MSG::info("Bye lazy Friend !!")
66     
67   end    
68   
69   def quicksort(list, p, r)
70     if p < r then
71         q = partition(list, p, r)
72         quicksort(list, p, q-1)
73         quicksort(list, q+1, r)
74     end
75 end
76
77 def partition(list, p, r)
78     pivot = list[r]
79     i = p - 1
80     p.upto(r-1) do |j|
81         if list[j] <= pivot
82             i = i+1
83             list[i], list[j] = list[j],list[i]
84         end
85     end
86     list[i+1],list[r] = list[r],list[i+1]
87     return i + 1
88 end
89   
90  
91 end
92
93 #################################################
94 # main chunck
95 #################################################
96
97 if (ARGV.length == 2) 
98         MSG.createEnvironment(ARGV[0])
99         MSG.deployApplication(ARGV[1])
100 else 
101         MSG.createEnvironment("quicksort_platform.xml")
102         MSG.deployApplication("quicksort_deployment.xml")
103 end
104
105 MSG.run
106 puts "Simulation time : " + MSG.getClock .to_s
107 MSG.exit