Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Initialize arrays at declaration, and remove use of deprecated bzero().
[simgrid.git] / contrib / deployment_generation / generate_random_deployment.pl
1 #!/usr/bin/perl -w
2 use strict;
3
4 sub melange {
5     my $tableau=shift;
6     my($i,$j);
7     
8     for($i = @$tableau ; --$i; ) {
9         $j = int rand ($i+1);
10         next if $i==$j;
11         @$tableau[$i,$j] = @$tableau[$j,$i];
12     }
13 }
14
15 sub read_file {
16     my($filename)=shift;
17     my($line);
18     my(@host_list);
19     open(INPUT,"$filename");
20     while(defined($line=<INPUT>)) {
21         chomp $line;
22         if($line=~/host id/) {
23             $line=~ s/.*host id="//;
24             $line=~ s/\".*$//;
25             push @host_list,$line;
26         }
27     }
28     close(INPUT);
29     return \@host_list;
30 }
31
32 sub generate_random_deployment{
33     my($host_list,$nflows,$filename)=@_;
34     my(%pairs);
35     my($nhost) = scalar(@$host_list);
36     my(%taken);
37
38     $nflows< $nhost*$nhost-$nhost or die "Too much flows! I can't do it\n";
39     
40     open(OUTPUT,"> $filename");
41     while(scalar(keys(%pairs))<$nflows) {
42         my($src)=int(rand(scalar(@$host_list)));
43         my($dst)=int(rand(scalar(@$host_list)));
44
45         if($src!=$dst && !defined($pairs{"$$host_list[$src] $$host_list[$dst]"})) {
46             $pairs{"$$host_list[$src] $$host_list[$dst]"}=1;
47             $taken{"$$host_list[$src]"}=1;
48             $taken{"$$host_list[$dst]"}=1;
49 #          && !$taken{$$host_list[$src]} && !$taken{$$host_list[$dst]}
50         }
51     }
52     my($p);
53
54     my($count)=0;
55
56     print OUTPUT <<EOF;
57 <?xml version='1.0'?>
58 <!DOCTYPE platform SYSTEM "http://simgrid.gforge.inria.fr/simgrid.dtd">
59 <platform version="2">
60 EOF
61
62     foreach $p (keys %pairs) {
63         my($src,$dst)=split(/ /,$p);
64         print OUTPUT "  <process host='$src' function='master'>\n";
65         print OUTPUT "    <argument value='10000000'/>\n";
66         print OUTPUT "    <argument value='$dst'/>\n";
67         print OUTPUT "    <argument value='$count'/>\n";
68         print OUTPUT "  </process>\n";
69         print OUTPUT "  <process host='$dst' function='slave'>\n";
70         print OUTPUT "    <argument value='$count'/>\n";
71         print OUTPUT "  </process>\n";
72         $count++;
73     }
74
75     print OUTPUT <<EOF;
76 </platform>
77 EOF
78     close(OUTPUT);
79 }
80
81
82 sub generate_random_deployment2{
83     my($host_list,$nflows,$filename)=@_;
84     my(%pairs);
85     my($nhost) = scalar(@$host_list);
86     my(%taken);
87
88     melange($host_list);
89     $nflows< $nhost/2 or die "Too much flows! I can't do it\n";
90     
91     open(OUTPUT,"> $filename");
92     foreach (0..$nflows-1) {
93         my($src)=shift(@$host_list);
94         my($dst)=shift(@$host_list);
95
96         $pairs{"$src $dst"}=1;
97     }
98     my($p);
99
100     my($count)=0;
101
102     print OUTPUT <<EOF;
103 <?xml version='1.0'?>
104 <!DOCTYPE platform SYSTEM "http://simgrid.gforge.inria.fr/simgrid.dtd">
105 <platform version="2">
106 EOF
107
108     foreach $p (keys %pairs) {
109         my($src,$dst)=split(/ /,$p);
110         print OUTPUT "  <process host='$src' function='master'>\n";
111         print OUTPUT "    <argument value='10000000'/>\n";
112         print OUTPUT "    <argument value='$dst'/>\n";
113         print OUTPUT "    <argument value='$count'/>\n";
114         print OUTPUT "  </process>\n";
115         print OUTPUT "  <process host='$dst' function='slave'>\n";
116         print OUTPUT "    <argument value='$count'/>\n";
117         print OUTPUT "  </process>\n";
118         $count++;
119     }
120
121     print OUTPUT <<EOF;
122 </platform>
123 EOF
124     close(OUTPUT);
125 }
126
127 sub main {
128     my($nodes,$edges,$interferences,$host_list,$count_interferences);
129
130     $#ARGV>=1 or die "Need a xml platform file and a number of flows!";
131     my($filename)=$ARGV[0];
132     my($nflows)=$ARGV[1];
133     $filename =~ s/\.xml$//g;
134     $filename =~ s/-p$//g;
135     
136     $host_list = read_file $ARGV[0];
137     generate_random_deployment2($host_list,$nflows,"$filename-d.xml");
138 }
139
140 main;