Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Platform generation: Cluster generation should work
[simgrid.git] / src / surf / platf_generator.c
1
2
3 #include "simgrid/platf_generator.h"
4 #include "platf_generator_private.h"
5 #include "xbt.h"
6 #include "xbt/RngStream.h"
7 #include "surf/simgrid_dtd.h"
8 #include "surf_private.h"
9 #include <math.h>
10
11 static xbt_graph_t platform_graph = NULL;
12 static xbt_dynar_t promoter_dynar = NULL;
13 static xbt_dynar_t labeler_dynar = NULL;
14
15 static RngStream rng_stream = NULL;
16
17 static unsigned long last_link_id = 0;
18
19 xbt_graph_t platf_graph_get(void) {
20   // We need some debug, so let's add this function
21   // WARNING : shold be removed when it becomes useless
22   return platform_graph;
23 }
24
25 void platf_random_seed(unsigned long seed[6]) {
26
27   if(rng_stream == NULL) {
28     //stream not created yet, we do it now
29     rng_stream = RngStream_CreateStream(NULL);
30   }
31   if(seed != NULL) {
32     RngStream_SetSeed(rng_stream, seed);
33   }
34 }
35
36 void platf_graph_init(unsigned long node_count) {
37   unsigned long i;
38   platform_graph = xbt_graph_new_graph(FALSE, NULL);
39   if(rng_stream == NULL) {
40     rng_stream = RngStream_CreateStream(NULL);
41   }
42
43   for(i=0 ; i<node_count ; i++) {
44     context_node_t node_data = NULL;
45     node_data = xbt_new0(s_context_node_t, 1);
46     node_data->id = i+1;
47     node_data->x = 0;
48     node_data->y = 0;
49     node_data->degree = 0;
50     node_data->kind = ROUTER;
51     xbt_graph_new_node(platform_graph, (void*) node_data);
52   }
53
54   last_link_id = 0;
55
56 }
57
58 void platf_node_connect(xbt_node_t node1, xbt_node_t node2) {
59   context_node_t node1_data;
60   context_node_t node2_data;
61   node1_data = (context_node_t) xbt_graph_node_get_data(node1);
62   node2_data = (context_node_t) xbt_graph_node_get_data(node2);
63   node1_data->degree++;
64   node2_data->degree++;
65
66   context_edge_t edge_data = NULL;
67   edge_data = xbt_new0(s_context_edge_t, 1);
68   edge_data->id = ++last_link_id;
69   edge_data->length = platf_node_distance(node1, node2);
70   edge_data->labeled = FALSE;
71   xbt_graph_new_edge(platform_graph, node1, node2, (void*)edge_data);
72 }
73
74 double platf_node_distance(xbt_node_t node1, xbt_node_t node2) {
75   context_node_t node1_data;
76   context_node_t node2_data;
77   double delta_x;
78   double delta_y;
79   double distance;
80   node1_data = (context_node_t) xbt_graph_node_get_data(node1);
81   node2_data = (context_node_t) xbt_graph_node_get_data(node2);
82   delta_x = node1_data->x - node2_data->x;
83   delta_y = node1_data->y - node2_data->y;
84   distance = sqrt(delta_x*delta_x + delta_y*delta_y);
85   return distance;
86 }
87
88 void platf_graph_uniform(unsigned long node_count) {
89   xbt_dynar_t dynar_nodes = NULL;
90   xbt_node_t graph_node = NULL;
91   context_node_t node_data = NULL;
92   unsigned int i;
93   platf_graph_init(node_count);
94   dynar_nodes = xbt_graph_get_nodes(platform_graph);
95   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
96     node_data = (context_node_t) xbt_graph_node_get_data(graph_node);
97     node_data->x = RngStream_RandU01(rng_stream);
98     node_data->y = RngStream_RandU01(rng_stream);
99   }
100 }
101
102 void platf_graph_heavytailed(unsigned long node_count) {
103   xbt_dynar_t dynar_nodes = NULL;
104   xbt_node_t graph_node = NULL;
105   context_node_t node_data = NULL;
106   unsigned int i;
107   platf_graph_init(node_count);
108   dynar_nodes = xbt_graph_get_nodes(platform_graph);
109   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
110     node_data = (context_node_t) xbt_graph_node_get_data(graph_node);
111     node_data->x = random_pareto(0, 1, 1.0/*K*/, 10e9/*P*/, 1.0/*alpha*/);
112     node_data->y = random_pareto(0, 1, 1.0/*K*/, 10e9/*P*/, 1.0/*alpha*/);
113   }
114 }
115
116 void platf_graph_interconnect_star(void) {
117   /* All the nodes are connected to the first one */
118   xbt_dynar_t dynar_nodes = NULL;
119   xbt_node_t graph_node = NULL;
120   xbt_node_t first_node = NULL;
121   unsigned int i;
122
123   dynar_nodes = xbt_graph_get_nodes(platform_graph);
124   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
125     if(i==0) {
126       //Ok, we get the first node, let's keep it somewhere...
127       first_node = graph_node;
128     } else {
129       //All the other nodes are connected to the first one
130       platf_node_connect(graph_node, first_node);
131     }
132   }
133 }
134
135 void platf_graph_interconnect_line(void) {
136   /* Node are connected to the previous and the next node, in a line */
137   xbt_dynar_t dynar_nodes = NULL;
138   xbt_node_t graph_node = NULL;
139   xbt_node_t old_node = NULL;
140   unsigned int i;
141
142   dynar_nodes = xbt_graph_get_nodes(platform_graph);
143   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
144     if(old_node != NULL) {
145       platf_node_connect(graph_node, old_node);
146     }
147     old_node = graph_node;
148   }
149 }
150
151 void platf_graph_interconnect_ring(void) {
152   /* Create a simple topology where all nodes are connected along a ring */
153   xbt_dynar_t dynar_nodes = NULL;
154   xbt_node_t graph_node = NULL;
155   xbt_node_t old_node = NULL;
156   xbt_node_t first_node = NULL;
157   unsigned int i;
158
159   dynar_nodes = xbt_graph_get_nodes(platform_graph);
160   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
161     if(i == 0) {
162       // this is the first node, let's keep it somewhere
163       first_node = graph_node;
164     } else {
165       //connect each node to the previous one
166       platf_node_connect(graph_node, old_node);
167     }
168     old_node = graph_node;
169   }
170   //we still have to connect the first and the last node together
171   platf_node_connect(first_node, graph_node);
172 }
173
174 void platf_graph_interconnect_clique(void) {
175   /* Create a simple topology where all nodes are connected to each other, in a clique manner */
176   xbt_dynar_t dynar_nodes = NULL;
177   xbt_node_t first_node = NULL;
178   xbt_node_t second_node = NULL;
179   unsigned int i,j;
180
181   dynar_nodes = xbt_graph_get_nodes(platform_graph);
182   xbt_dynar_foreach(dynar_nodes, i, first_node) {
183     xbt_dynar_foreach(dynar_nodes, j, second_node) {
184       platf_node_connect(first_node, second_node);
185     }
186   }
187 }
188
189 void platf_graph_interconnect_uniform(double alpha) {
190   /* Creates a topology where the probability to connect two nodes is uniform (unrealistic, but simple)
191      alpha : Probability for two nodes to get connected */
192   xbt_dynar_t dynar_nodes = NULL;
193   xbt_node_t first_node = NULL;
194   xbt_node_t second_node = NULL;
195   unsigned int i,j;
196
197   dynar_nodes = xbt_graph_get_nodes(platform_graph);
198   xbt_dynar_foreach(dynar_nodes, i, first_node) {
199     xbt_dynar_foreach(dynar_nodes, j, second_node) {
200       if(j>=i)
201         break;
202       if(RngStream_RandU01(rng_stream) < alpha) {
203         platf_node_connect(first_node, second_node);
204       }
205     }
206   }
207 }
208
209 void platf_graph_interconnect_exponential(double alpha) {
210   /* Create a topology where the probability follows an exponential law
211      Number of edges increases with alpha */
212   xbt_dynar_t dynar_nodes = NULL;
213   xbt_node_t first_node = NULL;
214   xbt_node_t second_node = NULL;
215   unsigned int i,j;
216   double L = sqrt(2.0); /*  L = c*sqrt(2); c=side of placement square */
217   dynar_nodes = xbt_graph_get_nodes(platform_graph);
218   xbt_dynar_foreach(dynar_nodes, i, first_node) {
219     xbt_dynar_foreach(dynar_nodes, j, second_node) {
220       if(j>=i)
221         break;
222       double d = platf_node_distance(first_node, second_node);
223       if(RngStream_RandU01(rng_stream) < alpha*exp(-d/(L-d))) {
224         platf_node_connect(first_node, second_node);
225       }
226     }
227   }
228 }
229
230 void platf_graph_interconnect_waxman(double alpha, double beta) {
231   /* Create a topology where the probability follows the model of Waxman
232    * (see Waxman, Routing of Multipoint Connections, IEEE J. on Selected Areas in Comm., 1988)
233    *
234    * Number of edges increases with alpha
235    * edge length heterogeneity increases with beta
236    */
237   xbt_dynar_t dynar_nodes = NULL;
238   xbt_node_t first_node = NULL;
239   xbt_node_t second_node = NULL;
240   unsigned int i,j;
241   double L = sqrt(2.0); /*  L = c*sqrt(2); c=side of placement square */
242   dynar_nodes = xbt_graph_get_nodes(platform_graph);
243   xbt_dynar_foreach(dynar_nodes, i, first_node) {
244     xbt_dynar_foreach(dynar_nodes, j, second_node) {
245       if(j>=i)
246         break;
247       double d = platf_node_distance(first_node, second_node);
248       if(RngStream_RandU01(rng_stream) < alpha*exp(-d/(L*beta))) {
249         platf_node_connect(first_node, second_node);
250       }
251     }
252   }
253 }
254
255 void platf_graph_interconnect_zegura(double alpha, double beta, double r) {
256   /* Create a topology where the probability follows the model of Zegura
257    * (see Zegura, Calvert, Donahoo, A quantitative comparison of graph-based models
258    * for Internet topology, IEEE/ACM Transactions on Networking, 1997.)
259    *
260    * alpha : Probability of connexion for short edges
261    * beta : Probability of connexion for long edges
262    * r : Limit between long and short edges (between 0 and sqrt(2) since nodes are placed on the unit square)
263    */
264   xbt_dynar_t dynar_nodes = NULL;
265   xbt_node_t first_node = NULL;
266   xbt_node_t second_node = NULL;
267   unsigned int i,j;
268   dynar_nodes = xbt_graph_get_nodes(platform_graph);
269   xbt_dynar_foreach(dynar_nodes, i, first_node) {
270     xbt_dynar_foreach(dynar_nodes, j, second_node) {
271       if(j>=i)
272         break;
273       double d = platf_node_distance(first_node, second_node);
274       double proba = d < r ? alpha : beta;
275       if(RngStream_RandU01(rng_stream) < proba) {
276         platf_node_connect(first_node, second_node);
277       }
278     }
279   }
280 }
281
282 void platf_graph_interconnect_barabasi(void) {
283   /* Create a topology constructed according to the Barabasi-Albert algorithm (follows power laws)
284      (see Barabasi and Albert, Emergence of scaling in random networks, Science 1999, num 59, p509­-512.) */
285   xbt_dynar_t dynar_nodes = NULL;
286   xbt_node_t first_node = NULL;
287   xbt_node_t second_node = NULL;
288   context_node_t node_data = NULL;
289   unsigned int i,j;
290   unsigned long sum = 0;
291   dynar_nodes = xbt_graph_get_nodes(platform_graph);
292   xbt_dynar_foreach(dynar_nodes, i, first_node) {
293     xbt_dynar_foreach(dynar_nodes, j, second_node) {
294       if(j>=i)
295         break;
296       node_data = xbt_graph_node_get_data(second_node);
297       if(sum==0 || RngStream_RandU01(rng_stream) < ((double)(node_data->degree)/ (double)sum)) {
298         platf_node_connect(first_node, second_node);
299         sum += 2;
300       }
301     }
302   }
303 }
304
305 void platf_graph_promote_to_host(context_node_t node, sg_platf_host_cbarg_t parameters) {
306   node->kind = HOST;
307   memcpy(&(node->host_parameters), parameters, sizeof(s_sg_platf_host_cbarg_t));
308 }
309
310 void platf_graph_promote_to_cluster(context_node_t node, sg_platf_cluster_cbarg_t parameters) {
311   node->kind = CLUSTER;
312   memcpy(&(node->cluster_parameters), parameters, sizeof(s_sg_platf_cluster_cbarg_t));
313 }
314
315 void platf_graph_link_label(context_edge_t edge, sg_platf_link_cbarg_t parameters) {
316   memcpy(&(edge->link_parameters), parameters, sizeof(s_sg_platf_link_cbarg_t));
317 }
318
319 void platf_graph_promoter(platf_promoter_cb_t promoter_callback) {
320   if(promoter_dynar == NULL) {
321     promoter_dynar = xbt_dynar_new(sizeof(platf_promoter_cb_t), NULL);
322   }
323   xbt_dynar_push(promoter_dynar, &promoter_callback);
324 }
325
326 void platf_graph_labeler(platf_labeler_cb_t labeler_callback) {
327   if(labeler_dynar == NULL) {
328     labeler_dynar = xbt_dynar_new(sizeof(void*), NULL);
329   }
330   xbt_dynar_push(labeler_dynar, &labeler_callback);
331 }
332
333 void platf_do_promote(void) {
334   platf_promoter_cb_t promoter_callback;
335   xbt_node_t graph_node = NULL;
336   xbt_dynar_t dynar_nodes = NULL;
337   context_node_t node = NULL;
338   unsigned int i, j;
339   dynar_nodes = xbt_graph_get_nodes(platform_graph);
340   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
341     node = (context_node_t) xbt_graph_node_get_data(graph_node);
342     xbt_dynar_foreach(promoter_dynar, j, promoter_callback) {
343       if(node->kind != ROUTER)
344         break;
345       promoter_callback(node);
346     }
347   }
348 }
349
350 void platf_do_label(void) {
351   platf_labeler_cb_t labeler_callback;
352   xbt_edge_t graph_edge = NULL;
353   xbt_dynar_t dynar_edges = NULL;
354   context_edge_t edge = NULL;
355   unsigned int i, j;
356   dynar_edges = xbt_graph_get_edges(platform_graph);
357   xbt_dynar_foreach(dynar_edges, i, graph_edge) {
358     edge = (context_edge_t) xbt_graph_edge_get_data(graph_edge);
359     xbt_dynar_foreach(promoter_dynar, j, labeler_callback) {
360       if(edge->labeled == TRUE)
361         break;
362       labeler_callback(edge);
363     }
364   }
365 }
366
367 void platf_generate(void) {
368
369   xbt_dynar_t nodes = NULL;
370   xbt_node_t graph_node = NULL;
371   context_node_t node_data = NULL;
372   unsigned int i;
373
374   unsigned int last_host = 0;
375   unsigned int last_router = 0;
376   unsigned int last_cluster = 0;
377
378   sg_platf_host_cbarg_t host_parameters;
379   s_sg_platf_router_cbarg_t router_parameters; /* This one is not a pointer! */
380   sg_platf_cluster_cbarg_t cluster_parameters;
381
382   router_parameters.coord = NULL;
383
384   nodes = xbt_graph_get_nodes(platform_graph);
385
386   sg_platf_begin();
387   surf_parse_init_callbacks();
388   routing_register_callbacks();
389
390
391   sg_platf_new_AS_begin("random platform", A_surfxml_AS_routing_Floyd);
392
393   xbt_dynar_foreach(nodes, i, graph_node) {
394     node_data = xbt_graph_node_get_data(graph_node);
395     switch(node_data->kind) {
396       case HOST:
397         host_parameters = &node_data->host_parameters;
398         last_host++;
399         if(host_parameters->id == NULL) {
400           host_parameters->id = bprintf("host-%d", last_host);
401         }
402         sg_platf_new_host(host_parameters);
403         break;
404       case CLUSTER:
405         cluster_parameters = &node_data->cluster_parameters;
406         last_cluster++;
407         if(cluster_parameters->prefix == NULL) {
408           cluster_parameters->prefix = "host-";
409         }
410         if(cluster_parameters->suffix == NULL) {
411           cluster_parameters->suffix = bprintf(".cluster-%d", last_cluster);
412         }
413         if(cluster_parameters->id == NULL) {
414           cluster_parameters->id = bprintf("cluster-%d", last_cluster);
415         }
416         sg_platf_new_cluster(cluster_parameters);
417         break;
418       case ROUTER:
419         router_parameters.id = bprintf("router-%d", ++last_router);
420         sg_platf_new_router(&router_parameters);
421     }
422   }
423
424
425   sg_platf_new_AS_end();
426   sg_platf_end();
427 }
428
429 /* Functions used to generate interesting random values */
430
431 double random_pareto(double min, double max, double K, double P, double ALPHA) {
432   double x = RngStream_RandU01(rng_stream);
433   double den = pow(1.0 - x + x*pow(K/P, ALPHA), 1.0/ALPHA);
434   double res = (1/den);
435   res += min - 1; // pareto is on [1, infinity) by default
436   if (res>max) {
437     return max;
438   }
439   return res;
440 }