Logo AND Algorithmique Numérique Distribuée

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