Logo AND Algorithmique Numérique Distribuée

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