Logo AND Algorithmique Numérique Distribuée

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