Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cleanup.
[simgrid.git] / src / surf / platf_generator.c
1 /* Copyright (c) 2012, 2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "simgrid/platf_generator.h"
8 #include "platf_generator_private.h"
9 #include "xbt.h"
10 #include "xbt/RngStream.h"
11 #include "surf/simgrid_dtd.h"
12 #include "surf_private.h"
13 #include <math.h>
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(platf_generator, surf, "Platform Generator");
16
17 static xbt_graph_t platform_graph = NULL;
18 static xbt_dynar_t promoter_dynar = NULL;
19 static xbt_dynar_t labeler_dynar = NULL;
20
21 static RngStream rng_stream = NULL;
22
23 static unsigned long last_link_id = 0;
24
25 xbt_graph_t platf_graph_get(void) {
26   // We need some debug, so let's add this function
27   // WARNING : should be removed when it becomes useless
28   return platform_graph;
29 }
30
31 /**
32  * \brief Set the seed of the platform generator RngStream
33  *
34  * This RngStream is used to generate all the random values needed to
35  * generate the platform
36  *
37  * \param seed A array of six integer; if NULL, the default seed will be used.
38  */
39 void platf_random_seed(unsigned long seed[6]) {
40
41   if(rng_stream == NULL) {
42     //stream not created yet, we do it now
43     rng_stream = RngStream_CreateStream(NULL);
44   }
45   if(seed != NULL) {
46     RngStream_SetSeed(rng_stream, seed);
47   }
48 }
49
50 /**
51  * \brief Initialize the platform generator
52  *
53  * This function create the graph and add node_count nodes to it
54  * \param node_count The number of nodes of the platform
55  */
56 void platf_graph_init(unsigned long node_count) {
57   unsigned long i;
58   platform_graph = xbt_graph_new_graph(FALSE, NULL);
59   if(rng_stream == NULL) {
60     rng_stream = RngStream_CreateStream(NULL);
61   }
62
63   for(i=0 ; i<node_count ; i++) {
64     context_node_t node_data = NULL;
65     node_data = xbt_new0(s_context_node_t, 1);
66     node_data->id = i+1;
67     node_data->x = 0;
68     node_data->y = 0;
69     node_data->degree = 0;
70     node_data->kind = ROUTER;
71     node_data->connect_checked = FALSE;
72     xbt_graph_new_node(platform_graph, (void*) node_data);
73   }
74
75   last_link_id = 0;
76
77 }
78
79 /**
80  * \brief Connect two nodes
81  * \param node1 The first node to connect
82  * \param node2 The second node to connect
83  */
84 void platf_node_connect(xbt_node_t node1, xbt_node_t node2) {
85   context_node_t node1_data;
86   context_node_t node2_data;
87   node1_data = (context_node_t) xbt_graph_node_get_data(node1);
88   node2_data = (context_node_t) xbt_graph_node_get_data(node2);
89   node1_data->degree++;
90   node2_data->degree++;
91
92   context_edge_t edge_data = NULL;
93   edge_data = xbt_new0(s_context_edge_t, 1);
94   edge_data->id = ++last_link_id;
95   edge_data->length = platf_node_distance(node1, node2);
96   edge_data->labeled = FALSE;
97   xbt_graph_new_edge(platform_graph, node1, node2, (void*)edge_data);
98 }
99
100 /**
101  * \brief Compute the distance between two nodes
102  * \param node1 The first node
103  * \param node2 The second node
104  * \return The distance between node1 and node2
105  */
106 double platf_node_distance(xbt_node_t node1, xbt_node_t node2) {
107   context_node_t node1_data;
108   context_node_t node2_data;
109   double delta_x;
110   double delta_y;
111   double distance;
112   node1_data = (context_node_t) xbt_graph_node_get_data(node1);
113   node2_data = (context_node_t) xbt_graph_node_get_data(node2);
114   delta_x = node1_data->x - node2_data->x;
115   delta_y = node1_data->y - node2_data->y;
116   distance = sqrt(delta_x*delta_x + delta_y*delta_y);
117   return distance;
118 }
119
120 /**
121  * \brief Initialize the platform, placing nodes uniformly on the unit square
122  * \param node_count The number of node
123  */
124 void platf_graph_uniform(unsigned long node_count) {
125   xbt_dynar_t dynar_nodes = NULL;
126   xbt_node_t graph_node = NULL;
127   context_node_t node_data = NULL;
128   unsigned int i;
129   platf_graph_init(node_count);
130   dynar_nodes = xbt_graph_get_nodes(platform_graph);
131   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
132     node_data = (context_node_t) xbt_graph_node_get_data(graph_node);
133     node_data->x = RngStream_RandU01(rng_stream);
134     node_data->y = RngStream_RandU01(rng_stream);
135   }
136 }
137
138 /**
139  * \brief Initialize the platform, placing nodes in little clusters on the unit square
140  * \param node_count The number of node
141  */
142 void platf_graph_heavytailed(unsigned long node_count) {
143   xbt_dynar_t dynar_nodes = NULL;
144   xbt_node_t graph_node = NULL;
145   context_node_t node_data = NULL;
146   unsigned int i;
147   platf_graph_init(node_count);
148   dynar_nodes = xbt_graph_get_nodes(platform_graph);
149   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
150     node_data = (context_node_t) xbt_graph_node_get_data(graph_node);
151     node_data->x = random_pareto(0, 1, 1.0/*K*/, 10e9/*P*/, 1.0/*alpha*/);
152     node_data->y = random_pareto(0, 1, 1.0/*K*/, 10e9/*P*/, 1.0/*alpha*/);
153   }
154 }
155
156 /**
157  * \brief Creates a simple topology where all nodes are connected to the first one in a star fashion
158  */
159 void platf_graph_interconnect_star(void) {
160   xbt_dynar_t dynar_nodes = NULL;
161   xbt_node_t graph_node = NULL;
162   xbt_node_t first_node = NULL;
163   unsigned int i;
164
165   dynar_nodes = xbt_graph_get_nodes(platform_graph);
166   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
167     if(i==0) {
168       //Ok, we get the first node, let's keep it somewhere...
169       first_node = graph_node;
170     } else {
171       //All the other nodes are connected to the first one
172       platf_node_connect(graph_node, first_node);
173     }
174   }
175 }
176
177 /**
178  * \brief Creates a simple topology where all nodes are connected in line
179  */
180 void platf_graph_interconnect_line(void) {
181   xbt_dynar_t dynar_nodes = NULL;
182   xbt_node_t graph_node = NULL;
183   xbt_node_t old_node = NULL;
184   unsigned int i;
185
186   dynar_nodes = xbt_graph_get_nodes(platform_graph);
187   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
188     if(old_node != NULL) {
189       platf_node_connect(graph_node, old_node);
190     }
191     old_node = graph_node;
192   }
193 }
194
195 /**
196  * \brief Create a simple topology where all nodes are connected along a ring
197  */
198 void platf_graph_interconnect_ring(void) {
199   xbt_dynar_t dynar_nodes = NULL;
200   xbt_node_t graph_node = NULL;
201   xbt_node_t old_node = NULL;
202   xbt_node_t first_node = NULL;
203   unsigned int i;
204
205   dynar_nodes = xbt_graph_get_nodes(platform_graph);
206   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
207     if(i == 0) {
208       // this is the first node, let's keep it somewhere
209       first_node = graph_node;
210     } else {
211       //connect each node to the previous one
212       platf_node_connect(graph_node, old_node);
213     }
214     old_node = graph_node;
215   }
216   //we still have to connect the first and the last node together
217   platf_node_connect(first_node, graph_node);
218 }
219
220 /**
221  * \brief Create a simple topology where all nodes are connected to each other, in a clique manner
222  */
223 void platf_graph_interconnect_clique(void) {
224   xbt_dynar_t dynar_nodes = NULL;
225   xbt_node_t first_node = NULL;
226   xbt_node_t second_node = NULL;
227   unsigned int i,j;
228
229   dynar_nodes = xbt_graph_get_nodes(platform_graph);
230   xbt_dynar_foreach(dynar_nodes, i, first_node) {
231     xbt_dynar_foreach(dynar_nodes, j, second_node) {
232       if(j>=i)
233         break;
234       platf_node_connect(first_node, second_node);
235     }
236   }
237 }
238
239 /**
240  * \brief Creates a topology where the probability to connect two nodes is uniform (unrealistic, but simple)
241  * \param alpha Probability for two nodes to get connected
242  */
243 void platf_graph_interconnect_uniform(double alpha) {
244   xbt_dynar_t dynar_nodes = NULL;
245   xbt_node_t first_node = NULL;
246   xbt_node_t second_node = NULL;
247   unsigned int i,j;
248
249   dynar_nodes = xbt_graph_get_nodes(platform_graph);
250   xbt_dynar_foreach(dynar_nodes, i, first_node) {
251     xbt_dynar_foreach(dynar_nodes, j, second_node) {
252       if(j>=i)
253         break;
254       if(RngStream_RandU01(rng_stream) < alpha) {
255         platf_node_connect(first_node, second_node);
256       }
257     }
258   }
259 }
260
261 /**
262  * \brief Create a topology where the probability follows an exponential law
263  * \param alpha Number of edges increases with alpha
264  */
265 void platf_graph_interconnect_exponential(double alpha) {
266   xbt_dynar_t dynar_nodes = NULL;
267   xbt_node_t first_node = NULL;
268   xbt_node_t second_node = NULL;
269   unsigned int i,j;
270   double L = sqrt(2.0); /*  L = c*sqrt(2); c=side of placement square */
271   dynar_nodes = xbt_graph_get_nodes(platform_graph);
272   xbt_dynar_foreach(dynar_nodes, i, first_node) {
273     xbt_dynar_foreach(dynar_nodes, j, second_node) {
274       if(j>=i)
275         break;
276       double d = platf_node_distance(first_node, second_node);
277       if(RngStream_RandU01(rng_stream) < alpha*exp(-d/(L-d))) {
278         platf_node_connect(first_node, second_node);
279       }
280     }
281   }
282 }
283
284 /**
285  * \brief Create a topology where the probability follows the model of Waxman
286  *
287  * see Waxman, Routing of Multipoint Connections, IEEE J. on Selected Areas in Comm., 1988
288  *
289  * \param alpha Number of edges increases with alpha
290  * \param beta Edge length heterogeneity increases with beta
291  */
292 void platf_graph_interconnect_waxman(double alpha, double beta) {
293   xbt_dynar_t dynar_nodes = NULL;
294   xbt_node_t first_node = NULL;
295   xbt_node_t second_node = NULL;
296   unsigned int i,j;
297   double L = sqrt(2.0); /*  L = c*sqrt(2); c=side of placement square */
298   dynar_nodes = xbt_graph_get_nodes(platform_graph);
299   xbt_dynar_foreach(dynar_nodes, i, first_node) {
300     xbt_dynar_foreach(dynar_nodes, j, second_node) {
301       if(j>=i)
302         break;
303       double d = platf_node_distance(first_node, second_node);
304       if(RngStream_RandU01(rng_stream) < alpha*exp(-d/(L*beta))) {
305         platf_node_connect(first_node, second_node);
306       }
307     }
308   }
309 }
310
311 /**
312  * \brief Create a topology where the probability follows the model of Zegura
313  * see Zegura, Calvert, Donahoo, A quantitative comparison of graph-based models
314  * for Internet topology, IEEE/ACM Transactions on Networking, 1997.
315  *
316  * \param alpha Probability of connexion for short edges
317  * \param beta Probability of connexion for long edges
318  * \param r Limit between long and short edges (between 0 and sqrt(2) since nodes are placed on the unit square)
319  */
320 void platf_graph_interconnect_zegura(double alpha, double beta, double r) {
321   xbt_dynar_t dynar_nodes = NULL;
322   xbt_node_t first_node = NULL;
323   xbt_node_t second_node = NULL;
324   unsigned int i,j;
325   dynar_nodes = xbt_graph_get_nodes(platform_graph);
326   xbt_dynar_foreach(dynar_nodes, i, first_node) {
327     xbt_dynar_foreach(dynar_nodes, j, second_node) {
328       if(j>=i)
329         break;
330       double d = platf_node_distance(first_node, second_node);
331       double proba = d < r ? alpha : beta;
332       if(RngStream_RandU01(rng_stream) < proba) {
333         platf_node_connect(first_node, second_node);
334       }
335     }
336   }
337 }
338
339 /**
340  * \brief Create a topology constructed according to the Barabasi-Albert algorithm (follows power laws)
341  * see Barabasi and Albert, Emergence of scaling in random networks, Science 1999, num 59, p509­-512.
342  */
343 void platf_graph_interconnect_barabasi(void) {
344   xbt_dynar_t dynar_nodes = NULL;
345   xbt_node_t first_node = NULL;
346   xbt_node_t second_node = NULL;
347   context_node_t node_data = NULL;
348   unsigned int i,j;
349   unsigned long sum = 0;
350   dynar_nodes = xbt_graph_get_nodes(platform_graph);
351   xbt_dynar_foreach(dynar_nodes, i, first_node) {
352     xbt_dynar_foreach(dynar_nodes, j, second_node) {
353       if(j>=i)
354         break;
355       node_data = xbt_graph_node_get_data(second_node);
356       if(sum==0 || RngStream_RandU01(rng_stream) < ((double)(node_data->degree)/ (double)sum)) {
357         platf_node_connect(first_node, second_node);
358         sum += 2;
359       }
360     }
361   }
362 }
363
364 /**
365  * \brief Check if the produced graph is connected
366  *
367  * You should check if the produced graph is connected before doing anything
368  * on it. You probably don't want any isolated node or group of nodes...
369  *
370  * \return TRUE if the graph is connected, FALSE otherwise
371  */
372 int platf_graph_is_connected(void) {
373   xbt_dynar_t dynar_nodes = NULL;
374   xbt_dynar_t connected_nodes = NULL;
375   xbt_dynar_t outgoing_edges = NULL;
376   xbt_node_t graph_node = NULL;
377   context_node_t node_data = NULL;
378   xbt_edge_t outedge = NULL;
379   unsigned long iterator;
380   unsigned int i;
381   dynar_nodes = xbt_graph_get_nodes(platform_graph);
382   connected_nodes = xbt_dynar_new(sizeof(xbt_node_t), NULL);
383
384   //Let's just check if every nodes are connected to something
385   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
386     node_data = xbt_graph_node_get_data(graph_node);
387     if(node_data->degree==0) {
388       return FALSE;
389     }
390   }
391
392   //We still need a real check
393   //Initialize the connected node array with the first node
394   xbt_dynar_get_cpy(dynar_nodes, 0, &graph_node);
395   node_data = xbt_graph_node_get_data(graph_node);
396   node_data->connect_checked = TRUE;
397   xbt_dynar_push(connected_nodes, &graph_node);
398   iterator = 0;
399   do {
400     //Get the next node
401     xbt_dynar_get_cpy(connected_nodes, iterator, &graph_node);
402     node_data = xbt_graph_node_get_data(graph_node);
403
404     //add all the linked nodes to the connected node array
405     outgoing_edges = xbt_graph_node_get_outedges(graph_node);
406     xbt_dynar_foreach(outgoing_edges, i, outedge) {
407       xbt_node_t src = xbt_graph_edge_get_source(outedge);
408       xbt_node_t dst = xbt_graph_edge_get_target(outedge);
409       node_data = xbt_graph_node_get_data(src);
410       if(!node_data->connect_checked) {
411         xbt_dynar_push(connected_nodes, &src);
412         node_data->connect_checked = TRUE;
413       }
414       node_data = xbt_graph_node_get_data(dst);
415       if(!node_data->connect_checked) {
416         xbt_dynar_push(connected_nodes, &dst);
417         node_data->connect_checked = TRUE;
418       }
419     }
420   } while(++iterator < xbt_dynar_length(connected_nodes));
421
422   // The graph is connected if the connected node array has the same length
423   // as the graph node array
424   return xbt_dynar_length(connected_nodes) == xbt_dynar_length(dynar_nodes);
425 }
426
427
428 /**
429  * \brief Remove the links in the created topology
430  *
431  * This is useful when the created topology is not connected, and you want
432  * to generate a new one.
433  */
434 void platf_graph_clear_links(void) {
435   xbt_dynar_t dynar_nodes = NULL;
436   xbt_dynar_t dynar_edges = NULL;
437   xbt_dynar_t dynar_edges_cpy = NULL;
438   xbt_node_t graph_node = NULL;
439   xbt_edge_t graph_edge = NULL;
440   context_node_t node_data = NULL;
441   unsigned int i;
442
443   //The graph edge dynar will be modified directly, so we work on a copy of it
444   dynar_edges = xbt_graph_get_edges(platform_graph);
445   dynar_edges_cpy = xbt_dynar_new(sizeof(xbt_edge_t), NULL);
446   xbt_dynar_foreach(dynar_edges, i, graph_edge) {
447     xbt_dynar_push_as(dynar_edges_cpy, xbt_edge_t, graph_edge);
448   }
449   //Delete edges from the graph
450   xbt_dynar_foreach(dynar_edges_cpy, i, graph_edge) {
451     xbt_graph_free_edge(platform_graph, graph_edge, xbt_free);
452   }
453   //remove the dynar copy
454   xbt_dynar_free(&dynar_edges_cpy);
455
456   //All the nodes will be of degree 0, unchecked from connectedness
457   dynar_nodes = xbt_graph_get_nodes(platform_graph);
458   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
459     node_data = xbt_graph_node_get_data(graph_node);
460     node_data->degree = 0;
461     node_data->connect_checked = FALSE;
462   }
463 }
464
465 /**
466  * \brief Promote a node to a host
467  *
468  * This function should be called in callbacks registered with the
469  * platf_graph_promoter function.
470  *
471  * \param node The node to promote
472  * \param parameters The parameters needed to build the host
473  */
474 void platf_graph_promote_to_host(context_node_t node, sg_platf_host_cbarg_t parameters) {
475   node->kind = HOST;
476   memcpy(&(node->host_parameters), parameters, sizeof(s_sg_platf_host_cbarg_t));
477 }
478
479 /**
480  * \brief Promote a node to a cluster
481  *
482  * This function should be called in callbacks registered with the
483  * platf_graph_promoter function.
484  *
485  * \param node The node to promote
486  * \param parameters The parameters needed to build the cluster
487  */
488 void platf_graph_promote_to_cluster(context_node_t node, sg_platf_cluster_cbarg_t parameters) {
489   node->kind = CLUSTER;
490   memcpy(&(node->cluster_parameters), parameters, sizeof(s_sg_platf_cluster_cbarg_t));
491 }
492
493 /**
494  * \brief Set the parameters of a network link.
495  *
496  * This function should be called in callbacks registered with the
497  * platf_graph_labeler function.
498  *
499  * \param edge The edge to modify
500  * \param parameters The parameters of the network link
501  */
502 void platf_graph_link_label(context_edge_t edge, sg_platf_link_cbarg_t parameters) {
503   memcpy(&(edge->link_parameters), parameters, sizeof(s_sg_platf_link_cbarg_t));
504   edge->labeled = TRUE;
505 }
506
507 /**
508  * \brief Register a callback to promote nodes
509  *
510  * The best way to promote nodes into host or cluster is to write a function
511  * which takes one parameter, a #context_node_t, make every needed test on
512  * it, and call platf_graph_promote_to_host or platf_graph_promote_to_cluster
513  * if needed. Then, register the function with this one.
514  * You can register several callbacks: the first registered function will be
515  * called first. If the node have not been promoted yet, the second function
516  * will be called, and so on...
517  *
518  * \param promoter_callback The callback function
519  */
520 void platf_graph_promoter(platf_promoter_cb_t promoter_callback) {
521   if(promoter_dynar == NULL) {
522     promoter_dynar = xbt_dynar_new(sizeof(platf_promoter_cb_t), NULL);
523   }
524   xbt_dynar_push(promoter_dynar, &promoter_callback);
525 }
526
527 /**
528  * \brief Register a callback to label links
529  *
530  * Like the node promotion, it is better, to set links, to write a function
531  * which take one parameter, a #context_edge_t, make every needed test on
532  * it, and call platf_graph_link_label if needed.
533  * You can register several callbacks: the first registered function will be
534  * called first. If the link have not been labeled yet, the second function
535  * will be called, and so on... All the links must have been labeled after
536  * all the calls.
537  *
538  * \param labeler_callback The callback function
539  */
540 void platf_graph_labeler(platf_labeler_cb_t labeler_callback) {
541   if(labeler_dynar == NULL) {
542     labeler_dynar = xbt_dynar_new(sizeof(void*), NULL);
543   }
544   xbt_dynar_push(labeler_dynar, &labeler_callback);
545 }
546
547 /**
548  * \brief Call the registered promoters on all nodes
549  *
550  * The promoters are called on all nodes, in the order of their registration
551  * If some nodes are not promoted, they will be routers
552  */
553 void platf_do_promote(void) {
554   platf_promoter_cb_t promoter_callback;
555   xbt_node_t graph_node = NULL;
556   xbt_dynar_t dynar_nodes = NULL;
557   context_node_t node = NULL;
558   unsigned int i, j;
559   dynar_nodes = xbt_graph_get_nodes(platform_graph);
560   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
561     node = (context_node_t) xbt_graph_node_get_data(graph_node);
562     xbt_dynar_foreach(promoter_dynar, j, promoter_callback) {
563       if(node->kind != ROUTER)
564         break;
565       promoter_callback(node);
566     }
567   }
568 }
569
570 /**
571  * \brief Call the registered labelers on all links
572  */
573 void platf_do_label(void) {
574   platf_labeler_cb_t labeler_callback;
575   xbt_edge_t graph_edge = NULL;
576   xbt_dynar_t dynar_edges = NULL;
577   context_edge_t edge = NULL;
578   unsigned int i, j;
579   dynar_edges = xbt_graph_get_edges(platform_graph);
580   xbt_dynar_foreach(dynar_edges, i, graph_edge) {
581     edge = (context_edge_t) xbt_graph_edge_get_data(graph_edge);
582     xbt_dynar_foreach(labeler_dynar, j, labeler_callback) {
583       if(edge->labeled)
584         break;
585       labeler_callback(edge);
586     }
587     if(!edge->labeled) {
588       XBT_ERROR("All links of the generated platform are not labeled.");
589       xbt_die("Please check your generation parameters.");
590     }
591   }
592 }
593
594 /**
595  * \brief putting into SURF the generated platform
596  *
597  * This function should be called when the generation is over and the platform
598  * is ready to be put in place in SURF. All the init function, like MSG_init,
599  * must have been called before, or this function will not do anything.
600  * After that function, it should be possible to list all the available hosts
601  * with the provided functions.
602  */
603 void platf_generate(void) {
604
605   xbt_dynar_t nodes = NULL;
606   xbt_node_t graph_node = NULL;
607   context_node_t node_data = NULL;
608   xbt_dynar_t edges = NULL;
609   xbt_edge_t graph_edge = NULL;
610   context_edge_t edge_data = NULL;
611   unsigned int i;
612
613   unsigned int last_host = 0;
614   unsigned int last_router = 0;
615   unsigned int last_cluster = 0;
616
617   sg_platf_host_cbarg_t host_parameters;
618   sg_platf_cluster_cbarg_t cluster_parameters;
619   sg_platf_link_cbarg_t link_parameters;
620   s_sg_platf_router_cbarg_t router_parameters; /* This one is not a pointer! */
621   s_sg_platf_route_cbarg_t route_parameters; /* neither this one! */
622
623   router_parameters.coord = NULL;
624   route_parameters.symmetrical = FALSE;
625   route_parameters.src = NULL;
626   route_parameters.dst = NULL;
627   route_parameters.gw_dst = NULL;
628   route_parameters.gw_src = NULL;
629   route_parameters.link_list = NULL;
630
631   nodes = xbt_graph_get_nodes(platform_graph);
632   edges = xbt_graph_get_edges(platform_graph);
633
634   sg_platf_begin();
635   surf_parse_init_callbacks();
636   routing_register_callbacks();
637
638   s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
639   AS.id = "random platform";
640   AS.routing = A_surfxml_AS_routing_Floyd;
641   sg_platf_new_AS_begin(&AS);
642
643   //Generate hosts, clusters and routers
644   xbt_dynar_foreach(nodes, i, graph_node) {
645     node_data = xbt_graph_node_get_data(graph_node);
646     switch(node_data->kind) {
647       case HOST:
648         host_parameters = &node_data->host_parameters;
649         last_host++;
650         if(host_parameters->id == NULL) {
651           host_parameters->id = bprintf("host-%d", last_host);
652         }
653         sg_platf_new_host(host_parameters);
654         break;
655       case CLUSTER:
656         cluster_parameters = &node_data->cluster_parameters;
657         last_cluster++;
658         if(cluster_parameters->prefix == NULL) {
659           cluster_parameters->prefix = "host-";
660         }
661         if(cluster_parameters->suffix == NULL) {
662           cluster_parameters->suffix = bprintf(".cluster-%d", last_cluster);
663         }
664         if(cluster_parameters->id == NULL) {
665           cluster_parameters->id = bprintf("cluster-%d", last_cluster);
666         }
667         sg_platf_new_cluster(cluster_parameters);
668         break;
669       case ROUTER:
670         node_data->router_id = bprintf("router-%d", ++last_router);
671         router_parameters.id = node_data->router_id;
672         sg_platf_new_router(&router_parameters);
673     }
674   }
675
676   //Generate links and routes
677   xbt_dynar_foreach(edges, i, graph_edge) {
678     xbt_node_t src = xbt_graph_edge_get_source(graph_edge);
679     xbt_node_t dst = xbt_graph_edge_get_target(graph_edge);
680     context_node_t src_data = xbt_graph_node_get_data(src);
681     context_node_t dst_data = xbt_graph_node_get_data(dst);
682     edge_data = xbt_graph_edge_get_data(graph_edge);
683     const char* temp = NULL;
684
685     //Add a link to the platform
686     link_parameters = &edge_data->link_parameters;
687     if(link_parameters->id == NULL) {
688       link_parameters->id = bprintf("link-%ld", edge_data->id);
689     }
690     sg_platf_new_link(link_parameters);
691
692     //Add a route matching this link
693     switch(src_data->kind) {
694       case ROUTER:
695         route_parameters.src = src_data->router_id;
696         break;
697       case CLUSTER:
698         route_parameters.src = src_data->cluster_parameters.id;
699         break;
700       case HOST:
701         route_parameters.src = src_data->host_parameters.id;
702     }
703     switch(dst_data->kind) {
704       case ROUTER:
705         route_parameters.dst = dst_data->router_id;
706         break;
707       case CLUSTER:
708         route_parameters.dst = dst_data->cluster_parameters.id;
709         break;
710       case HOST:
711         route_parameters.dst = dst_data->host_parameters.id;
712     }
713     sg_platf_route_begin(&route_parameters);
714     sg_platf_route_add_link(link_parameters->id, &route_parameters);
715     sg_platf_route_end(&route_parameters);
716
717     //Create the symmertical route
718     temp = route_parameters.dst;
719     route_parameters.dst = route_parameters.src;
720     route_parameters.src = temp;
721     sg_platf_route_begin(&route_parameters);
722     sg_platf_route_add_link(link_parameters->id, &route_parameters);
723     sg_platf_route_end(&route_parameters);
724   }
725
726   sg_platf_new_AS_end();
727   sg_platf_end();
728 }
729
730 /* Functions used to generate interesting random values */
731
732 double random_pareto(double min, double max, double K, double P, double ALPHA) {
733   double x = RngStream_RandU01(rng_stream);
734   double den = pow(1.0 - x + x*pow(K/P, ALPHA), 1.0/ALPHA);
735   double res = (1/den);
736   res += min - 1; // pareto is on [1, infinity) by default
737   if (res>max) {
738     return max;
739   }
740   return res;
741 }