Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add SMPI bugged example to test the MC.
[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 !!")
40    MSG::info("Here is my table after a quicksort :)")
41    p result
42    MSG::info("Bye Now :)")
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("Receiving Table from "+lazy_friend)
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("Sort Done ... Sending Back the new table")
64     task.send(send_mailbox)
65     MSG::info("Bye lazy Boy!!")
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 end
91
92 #################################################
93 # main chunck
94 #################################################
95
96 if (ARGV.length == 2) 
97         MSG.createEnvironment(ARGV[0])
98         MSG.deployApplication(ARGV[1])
99 else 
100         MSG.createEnvironment("quicksort_platform.xml")
101         MSG.deployApplication("quicksort_deployment.xml")
102 end
103
104 MSG.run
105 puts "Simulation time : " + MSG.getClock .to_s
106 MSG.exit