Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ceb98aa3c3c9f89b5019224b8349d56b73aea66d
[simgrid.git] / examples / msg / gtnets / generate_random_deployment.pl
1 #!/usr/bin/perl -w
2 use strict;
3
4 sub read_file {
5     my($filename)=shift;
6     my($line);
7     my(@host_list);
8     open(INPUT,"$filename");
9     while(defined($line=<INPUT>)) {
10         chomp $line;
11         if($line=~/host id/) {
12             $line=~ s/.*host id="//;
13             $line=~ s/\".*$//;
14             push @host_list,$line;
15         }
16     }
17     close(INPUT);
18     return \@host_list;
19 }
20
21 sub generate_random_deployment{
22     my($host_list,$nflows,$filename)=@_;
23     my(%pairs);
24     my($nhost) = scalar(@$host_list);
25
26     $nflows< $nhost*$nhost-$nhost or die "Too much flows! I can't do it\n";
27     
28     open(OUTPUT,"> $filename");
29     while(scalar(keys(%pairs))<$nflows) {
30         my($src)=int(rand(scalar(@$host_list)));
31         my($dst)=int(rand(scalar(@$host_list)));
32
33         if($src!=$dst && !defined($pairs{"$$host_list[$src] $$host_list[$dst]"})) {
34             $pairs{"$$host_list[$src] $$host_list[$dst]"}=1;
35         }
36     }
37     my($p);
38     print OUTPUT <<EOF;
39 <?xml version='1.0'?>
40 <!DOCTYPE platform SYSTEM "simgrid.dtd">
41 <platform version="2">
42 EOF
43
44     foreach $p (keys %pairs) {
45         my($src,$dst)=split(/ /,$p);
46         print OUTPUT "  <process host='$src' function='master'>\n";
47         print OUTPUT "    <argument value='1000000'/>\n";
48         print OUTPUT "    <argument value='$dst'/>\n";
49         print OUTPUT "  </process>\n";
50         print OUTPUT "  <process host='$dst' function='slave'/>\n";
51     }
52     print OUTPUT <<EOF;
53 </platform>
54 EOF
55     close(OUTPUT);
56 }
57
58 sub main {
59     my($nodes,$edges,$interferences,$host_list,$count_interferences);
60
61     $#ARGV>=1 or die "Need a xml platform file and a number of flows!";
62     my($filename)=$ARGV[0];
63     my($nflows)=$ARGV[1];
64     $filename =~ s/\.xml$//g;
65     $filename =~ s/-p$//g;
66     
67     $host_list = read_file $ARGV[0];
68     generate_random_deployment($host_list,$nflows,"$filename-d.xml");
69 }
70
71 main;