Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3adb0d76cd0de4ee5594037c62037b251a4767fd
[simgrid.git] / examples / msg / chainsend / generate_deployment_file.rb
1 #!/usr/bin/env ruby
2
3 require 'rexml/document'
4
5 class HostsExtractor
6   @@doc = nil
7   @@hosts = []
8
9   def initialize(xml)
10     @@doc = REXML::Document.new(xml)
11     @@doc.elements.each('platform') do |platform|
12       extract_hosts(platform)
13     end
14   end
15
16   def extract_hosts(doc)
17     doc.elements.each('AS') do |as|
18       extract_hosts_from_AS(as)
19       extract_hosts(as)
20     end
21   end
22
23   def extract_hosts_from_AS(doc)
24     doc.elements.each('host') do |h|
25       @@hosts << h.attributes['id']
26       puts "hosts %s" % h.attributes['id']
27     end
28
29     doc.elements.each('cluster') do |c|
30       prefix = c.attributes['prefix']
31       suffix = c.attributes['suffix']
32       puts "%s %s %s" % [prefix, c.attributes['radical'], suffix]
33       expand_radical(c.attributes['radical']).each do |num|
34         @@hosts << "%s%s%s" % [prefix, num, suffix]
35       end
36     end
37   end
38
39   def hosts
40     return @@hosts
41   end
42
43   def expand_radical(radical)
44    l = []
45    puts radical
46    radical.split(',').each do |range|
47      range.scan(/^\d+$/) { |x| l << x }
48      range.scan(/^(\d+)-(\d+)$/) { |x, y| (x..y).each do |i| l << i end }
49    end
50    return l
51   end
52 end
53
54 class DeploymentGenerator
55   @@outfile = nil
56
57   def initialize(fname)
58     @@outfile = File.new(fname, "w")
59   end
60
61   def write_header
62     @@outfile.puts "<?xml version='1.0'?>"
63     @@outfile.puts "<!DOCTYPE platform SYSTEM \"http://simgrid.gforge.inria.fr/simgrid.dtd\">"
64     @@outfile.puts "<platform version=\"3\">"
65   end
66
67   def write_process(name, function, hosts, args)
68     @@outfile.puts "  <!-- %s -->" % name
69     hosts.zip(args).each do |h, a|
70       @@outfile.puts "  <process host=\"%s\" function=\"%s\">" % [h, function]
71       @@outfile.puts "    <argument value=\"%s\"/>" % [a]
72       @@outfile.puts "  </process>"
73     end
74   end
75
76   def write_footer
77     @@outfile.puts "</platform>"
78   end
79 end
80
81 xml = File.read(ARGV.shift)
82 he = HostsExtractor.new(xml)
83
84 raise "Cannot run with less than 2 hosts" unless he.hosts.size > 1
85
86 output = ARGV.shift
87 n = ARGV.shift
88 if n == nil or n.to_i < 2
89   n = he.hosts.size - 1
90 else
91   n = n.to_i - 1
92 end
93 puts n
94
95 dg = DeploymentGenerator.new(output)
96 dg.write_header
97
98 puts he.hosts
99 broadcaster = he.hosts.shift
100 peers = he.hosts
101
102 dg.write_process("Broadcaster", "broadcaster", [broadcaster], [n])
103 dg.write_process("Peers", "peer", peers[0..n-1], (1..n))
104
105 dg.write_footer