Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Flavien's platform generator. It can produce lines, rings, tores, meshes, hypercubes...
[simgrid.git] / tools / platform_generation / simple_generator.c
1 /* $Id$ */
2
3 /* Simple Generator                                                         */
4 /* generates SimGrid platforms being either lines, rings, mesh, tore or     */
5 /*  hypercubes.                                                             */
6
7 /* Copyright (C) 2006 Flavien Vernier (original code)                       */
8 /* Copyright (C) 2006 Martin Quinson  (cleanups & maintainance)             */
9 /* All right reserved.                                                      */
10
11 /* This program is free software; you can redistribute it and/or modify it
12  * under the terms of the license (GNU LGPL) which comes with this package. */
13
14
15 #include<stdio.h>
16 #include<math.h>
17 #include <stdlib.h>
18
19 int MFLOPS=1000;
20 int MBYTES=100;
21 double LAT_S=0.00015;
22
23 int size=0;
24 int x=0,y=0;
25 int dimension=0;
26
27 int rand_mflops_min=0, rand_mflops_max=0;
28 int rand_mbytes_min=0, rand_mbytes_max=0;
29 double rand_lat_s_min=0.0, rand_lat_s_max=0.0;
30
31 void randParam(){
32   if(rand_mflops_min!=rand_mflops_max){
33     MFLOPS=rand_mflops_min+((double)(rand_mflops_max-rand_mflops_min)*((double)rand()/RAND_MAX));
34   }
35   if(rand_mbytes_min!=rand_mbytes_max){
36     MBYTES=rand_mbytes_min+((double)(rand_mbytes_max-rand_mbytes_min)*((double)rand()/RAND_MAX));
37   }
38   if(rand_lat_s_min!=rand_lat_s_max){
39     LAT_S=rand_lat_s_min+((rand_lat_s_max-rand_lat_s_min)*((double)rand()/RAND_MAX));
40   }
41 }
42
43 void generateLine(){
44   int i;
45   fprintf(stdout,"<?xml version='1.0'?>\n<!DOCTYPE platform_description SYSTEM \"surfxml.dtd\">\n<platform_description>\n");
46   
47   
48   for(i=0;i<size;i++){
49     fprintf(stdout,"<cpu name=\"node%i\" power=\"%i\"/>\n",i,MFLOPS);
50     randParam();
51   }
52   
53   for(i=0;i<2*(size-1);i++){
54     fprintf(stdout,"<network_link name=\"link%i\" bandwidth=\"%i\" latency=\"%f\"/>\n",i,MBYTES,LAT_S);
55     randParam();
56   }
57
58   for(i=0;i<size-1;i++){
59     fprintf(stdout,"<route src=\"node%i\" dst=\"node%i\"> <route_element name=\"link%i\"/></route>\n",i,(i+1),2*i);
60     fprintf(stdout,"<route src=\"node%i\" dst=\"node%i\"> <route_element name=\"link%i\"/></route>\n",(i+1),i,2*i+1);
61   }
62   fprintf(stdout,"</platform_description>\n");
63
64 }
65
66 void generateRing(){
67   int i;
68   fprintf(stdout,"<?xml version='1.0'?>\n<!DOCTYPE platform_description SYSTEM \"surfxml.dtd\">\n<platform_description>\n");
69   
70   
71   for(i=0;i<size;i++){
72     fprintf(stdout,"<cpu name=\"node%i\" power=\"%i\"/>\n",i,MFLOPS);
73     randParam();
74   }
75   
76   for(i=0;i<2*size;i++){
77     fprintf(stdout,"<network_link name=\"link%i\" bandwidth=\"%i\" latency=\"%f\"/>\n",i,MBYTES,LAT_S);
78     randParam();
79   }
80
81   for(i=0;i<size;i++){
82     fprintf(stdout,"<route src=\"node%i\" dst=\"node%i\"> <route_element name=\"link%i\"/></route>\n",i,(i+1)%size,2*i);
83     fprintf(stdout,"<route src=\"node%i\" dst=\"node%i\"> <route_element name=\"link%i\"/></route>\n",(i+1)%size,i,2*i+1);
84   }
85   fprintf(stdout,"</platform_description>\n");
86 }
87
88 void generateMesh(){
89   int i,j,l;
90   fprintf(stdout,"<?xml version='1.0'?>\n<!DOCTYPE platform_description SYSTEM \"surfxml.dtd\">\n<platform_description>\n");
91   
92   for(i=0;i<x*y;i++){
93     fprintf(stdout,"<cpu name=\"node%i\" power=\"%i\"/>\n",i,MFLOPS);
94     randParam();
95   }
96   
97   for(i=0;i<2*((x-1)*y+(y-1)*x);i++){
98     fprintf(stdout,"<network_link name=\"link%i\" bandwidth=\"%i\" latency=\"%f\"/>\n",i,MBYTES,LAT_S);
99     randParam();
100   }
101
102   l=0;
103   for(j=0;j<y;j++){
104     for(i=0;i<x;i++){
105       if((i+1)%x!=0){
106         fprintf(stdout,"<route src=\"node%i\" dst=\"node%i\"> <route_element name=\"link%i\"/></route>\n",j*x+i,j*x+(i+1),l++);
107         fprintf(stdout,"<route src=\"node%i\" dst=\"node%i\"> <route_element name=\"link%i\"/></route>\n",j*x+(i+1),j*x+i,l++);
108       }
109       if(j<y-1){
110         fprintf(stdout,"<route src=\"node%i\" dst=\"node%i\"> <route_element name=\"link%i\"/></route>\n",j*x+i,j*x+(i+x),l++);
111         fprintf(stdout,"<route src=\"node%i\" dst=\"node%i\"> <route_element name=\"link%i\"/></route>\n",j*x+(i+x),j*x+i,l++);
112       }
113     }
114   }
115   fprintf(stdout,"</platform_description>\n");
116 }
117
118 void generateTore(){
119   int i,j,l;
120   fprintf(stdout,"<?xml version='1.0'?>\n<!DOCTYPE platform_description SYSTEM \"surfxml.dtd\">\n<platform_description>\n");
121   
122   for(i=0;i<x*y;i++){
123     fprintf(stdout,"<cpu name=\"node%i\" power=\"%i\"/>\n",i,MFLOPS);
124     randParam();
125   }
126   
127   for(i=0;i<4*x*y;i++){
128     fprintf(stdout,"<network_link name=\"link%i\" bandwidth=\"%i\" latency=\"%f\"/>\n",i,MBYTES,LAT_S);
129     randParam();
130   }
131
132   l=0;
133   for(j=0;j<y;j++){
134     for(i=0;i<x;i++){
135       if((i+1)%x!=0){
136         fprintf(stdout,"<route src=\"node%i\" dst=\"node%i\"> <route_element name=\"link%i\"/></route>\n",j*x+i,j*x+(i+1),l++);
137         fprintf(stdout,"<route src=\"node%i\" dst=\"node%i\"> <route_element name=\"link%i\"/></route>\n",j*x+(i+1),j*x+i,l++);
138       }else{
139         fprintf(stdout,"<route src=\"node%i\" dst=\"node%i\"> <route_element name=\"link%i\"/></route>\n",j*x+i,j*x+i-x+1,l++);
140         fprintf(stdout,"<route src=\"node%i\" dst=\"node%i\"> <route_element name=\"link%i\"/></route>\n",j*x+i-x+1,j*x+i,l++);
141       }
142       if(j<y-1){
143         fprintf(stdout,"<route src=\"node%i\" dst=\"node%i\"> <route_element name=\"link%i\"/></route>\n",j*x+i,j*x+(i+x),l++);
144         fprintf(stdout,"<route src=\"node%i\" dst=\"node%i\"> <route_element name=\"link%i\"/></route>\n",j*x+(i+x),j*x+i,l++);
145       }else{
146         fprintf(stdout,"<route src=\"node%i\" dst=\"node%i\"> <route_element name=\"link%i\"/></route>\n",j*x+i,i,l++);
147         fprintf(stdout,"<route src=\"node%i\" dst=\"node%i\"> <route_element name=\"link%i\"/></route>\n",i,j*x+i,l++);
148       }
149     }
150   }
151   fprintf(stdout,"</platform_description>\n");
152 }
153
154
155 void generateHypercube(){
156   int i,d,l;
157
158   fprintf(stdout,"<?xml version='1.0'?>\n<!DOCTYPE platform_description SYSTEM \"surfxml.dtd\">\n<platform_description>\n");
159   
160   for(i=0;i<(int)pow(2.0,(double)dimension);i++){
161     fprintf(stdout,"<cpu name=\"node%i\" power=\"%i\"/>\n",i,MFLOPS);
162     randParam();
163   }
164
165   for(i=0;i<dimension*(int)pow(2.0,(double)dimension);i++){
166     fprintf(stdout,"<network_link name=\"link%i\" bandwidth=\"%i\" latency=\"%f\"/>\n",i,MBYTES,LAT_S);
167     randParam();
168   }
169
170   fprintf(stdout,"ROUTES\n");
171   l=0;
172   for(i=0;i<(int)pow(2.0,(double)dimension);i++){
173     for(d=1;d<(int)pow(2.0,(double)dimension);d=2*d)
174       fprintf(stdout,"<route src=\"node%i\" dst=\"node%i\"> <route_element name=\"link%i\"/></route>\n",i,i^d,l++);
175   }
176   fprintf(stdout,"</platform_description>\n");
177 }
178
179 void usage(int argc, char** argv){
180   fprintf(stderr,"%s topo [options] > platform_file.xml\n",argv[0]);
181   fprintf(stderr,"\ntopo:\n");
182   fprintf(stderr,"\t LINE n   \t generates a line of n nodes.\n");
183   fprintf(stderr,"\t RING n   \t generates a ring of n nodes.\n");
184   fprintf(stderr,"\t MESH x y \t generates a mesh of x line(s) y column(s).\n");
185   fprintf(stderr,"\t TORE x y \t generates a tore of x line(s) y column(s).\n");
186   fprintf(stderr,"\t HYPE d   \t generates a hypercubee of dimension d\n");
187   fprintf(stderr,"\noptions:\n");
188   fprintf(stderr,"\t --mflops n \t defines the power of each node (default: 1000), n must be integer\n");
189   fprintf(stderr,"\t --mbytes n \t defines the bandwidth of each edge (default: 100), n must be integer\n");
190   fprintf(stderr,"\t --lat_s  n \t defines the latency of each edge (default: 0.00015), n can be float\n");
191   fprintf(stderr,"\t --rand_mflops min max \t defines a random power between min and max for each node\n");
192   fprintf(stderr,"\t --rand_mbytes min max \t defines a random bandwidth between min and max for each edge\n");
193   fprintf(stderr,"\t --rand_lat_s  min max \t defines a random latency between min and max for each edge\n");
194   fprintf(stderr,"\t --help\n");
195 }
196
197
198 int main(int argc, char** argv){
199   int i=1;
200
201   void (*generate)(); 
202   generate=NULL;
203
204   srand(time(NULL));
205
206   while(i<argc){
207     if((strcmp(argv[i],"LINE")==0)&&(i+1<argc)){
208       generate=generateLine;
209       size=atoi(argv[i+1]);
210       i+=2;
211     }else if((strcmp(argv[i],"RING")==0)&&(i+1<argc)){
212       generate=generateRing;
213       size=atoi(argv[i+1]);
214       i+=2;
215     }else if((strcmp(argv[i],"MESH")==0)&&(i+2<argc)){
216       generate=generateMesh;
217       x=atoi(argv[i+1]);
218       y=atoi(argv[i+2]);
219       i+=3;
220     }else if((strcmp(argv[i],"TORE")==0)&&(i+2<argc)){
221       generate=generateTore;
222       x=atoi(argv[i+1]);
223       y=atoi(argv[i+2]);
224       i+=3;
225     }else if((strcmp(argv[i],"HYPE")==0)&&(i+1<argc)){
226       generate=generateHypercube;
227       dimension=atoi(argv[i+1]);
228       i+=2;
229     }else if((strcmp(argv[i],"--mflops")==0)&&(i+1<argc)){
230       MFLOPS=atoi(argv[i+1]);
231       i+=2;
232     }else if((strcmp(argv[i],"--mbytes")==0)&&(i+1<argc)){
233       MBYTES=atoi(argv[i+1]);
234       i+=2;
235     }else if((strcmp(argv[i],"--lat_s")==0)&&(i+1<argc)){
236       LAT_S=atof(argv[i+1]);
237       i+=2;
238     }else if((strcmp(argv[i],"--rand_mflops")==0)&&(i+2<argc)){
239       rand_mflops_min=atoi(argv[i+1]);
240       rand_mflops_max=atoi(argv[i+2]);
241       randParam();
242       i+=3;
243     }else if((strcmp(argv[i],"--rand_mbytes")==0)&&(i+2<argc)){
244       rand_mbytes_min=atoi(argv[i+1]);
245       rand_mbytes_max=atoi(argv[i+2]);
246       randParam();
247       i+=3;
248     }else if((strcmp(argv[i],"--rand_lat_s")==0)&&(i+2<argc)){
249       rand_lat_s_min=atof(argv[i+1]);
250       rand_lat_s_max=atof(argv[i+2]);
251       randParam();
252       i+=3;
253     }else{
254       usage(argc,argv);
255       return 1;
256     }
257   }
258   if(generate!=NULL){
259     (*generate)();
260     return 0;
261   }else{
262       usage(argc,argv);
263       return 1;    
264   }
265   return 0;
266 }
267