Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ruby examples
[simgrid.git] / examples / 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 #################################################
10 # Class Asker
11 #################################################
12
13 class Sender < MSG::Process  
14   # main : that function that will be executed when running simulation
15   def main(args) # args is an array containing arguments for function master
16   
17    hardworking_friend = MSG::Host.getByName(args[0]).name
18    taskComputeSize = Float(args[1])
19    taskCommunicationSize = Float(args[2])
20    send_mailbox =args[3]
21    
22    myTable = Array.new
23    myTable <<1<<-2<<45<<67<<87<<76<<89<<56<<78<<3<<-4<<99
24    MSG::info("Hello " + hardworking_friend + " !!, Please !! I need you to help me to sort my table , Here it is :")
25    p myTable
26    # Creates and send Task With the Table inside
27    task = MSG::Task.new("quicksort_task",taskComputeSize, taskCommunicationSize);
28    task.join(myTable);
29    MSG::debug("Sending "+ task.name + " to " + send_mailbox + " with Comput Size " + 
30            task.compSize.to_s)
31    task.send(send_mailbox)
32    MSG::debug("Done Sending " + task.name + " to " + send_mailbox)
33     
34    #waiting for results
35    
36    recv_mailbox = self.class
37    res_task = MSG::Task.receive(recv_mailbox.to_s)
38    result = res_task.data
39    MSG::info("Greate !! Thx Dude , you're my Best Friend !!")
40    MSG::info("Here is my table after a quicksort :)")
41    p result
42    MSG::info("Bye !! I finished My HomeWork !! Time to Sleep :)")
43    end
44
45 end
46
47
48 #################################################
49 # Class Clever
50 #################################################
51 class Receiver < MSG::Process
52
53   def main(args)
54     
55     lazy_friend = MSG::Host.getByName(args[0]).name
56     send_mailbox = args[1]
57     recv_mailbox = self.class
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.join(table)
63     MSG::info("Ok "+lazy_friend+ "I did it, next time try to do it yourself:)")
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