Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright notices
[simgrid.git] / src / surf / platf_generator.c
1 /* Copyright (c) 2012, 2014-2015. 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
403     //add all the linked nodes to the connected node array
404     outgoing_edges = xbt_graph_node_get_outedges(graph_node);
405     xbt_dynar_foreach(outgoing_edges, i, outedge) {
406       xbt_node_t src = xbt_graph_edge_get_source(outedge);
407       xbt_node_t dst = xbt_graph_edge_get_target(outedge);
408       node_data = xbt_graph_node_get_data(src);
409       if(!node_data->connect_checked) {
410         xbt_dynar_push(connected_nodes, &src);
411         node_data->connect_checked = TRUE;
412       }
413       node_data = xbt_graph_node_get_data(dst);
414       if(!node_data->connect_checked) {
415         xbt_dynar_push(connected_nodes, &dst);
416         node_data->connect_checked = TRUE;
417       }
418     }
419   } while(++iterator < xbt_dynar_length(connected_nodes));
420
421   // The graph is connected if the connected node array has the same length
422   // as the graph node array
423   return xbt_dynar_length(connected_nodes) == xbt_dynar_length(dynar_nodes);
424 }
425
426
427 /**
428  * \brief Remove the links in the created topology
429  *
430  * This is useful when the created topology is not connected, and you want
431  * to generate a new one.
432  */
433 void platf_graph_clear_links(void) {
434   xbt_dynar_t dynar_nodes = NULL;
435   xbt_dynar_t dynar_edges = NULL;
436   xbt_dynar_t dynar_edges_cpy = NULL;
437   xbt_node_t graph_node = NULL;
438   xbt_edge_t graph_edge = NULL;
439   context_node_t node_data = NULL;
440   unsigned int i;
441
442   //The graph edge dynar will be modified directly, so we work on a copy of it
443   dynar_edges = xbt_graph_get_edges(platform_graph);
444   dynar_edges_cpy = xbt_dynar_new(sizeof(xbt_edge_t), NULL);
445   xbt_dynar_foreach(dynar_edges, i, graph_edge) {
446     xbt_dynar_push_as(dynar_edges_cpy, xbt_edge_t, graph_edge);
447   }
448   //Delete edges from the graph
449   xbt_dynar_foreach(dynar_edges_cpy, i, graph_edge) {
450     xbt_graph_free_edge(platform_graph, graph_edge, xbt_free_f);
451   }
452   //remove the dynar copy
453   xbt_dynar_free(&dynar_edges_cpy);
454
455   //All the nodes will be of degree 0, unchecked from connectedness
456   dynar_nodes = xbt_graph_get_nodes(platform_graph);
457   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
458     node_data = xbt_graph_node_get_data(graph_node);
459     node_data->degree = 0;
460     node_data->connect_checked = FALSE;
461   }
462 }
463
464 /**
465  * \brief Promote a node to a host
466  *
467  * This function should be called in callbacks registered with the
468  * platf_graph_promoter function.
469  *
470  * \param node The node to promote
471  * \param parameters The parameters needed to build the host
472  */
473 void platf_graph_promote_to_host(context_node_t node, sg_platf_host_cbarg_t parameters) {
474   node->kind = HOST;
475   memcpy(&(node->host_parameters), parameters, sizeof(s_sg_platf_host_cbarg_t));
476 }
477
478 /**
479  * \brief Promote a node to a cluster
480  *
481  * This function should be called in callbacks registered with the
482  * platf_graph_promoter function.
483  *
484  * \param node The node to promote
485  * \param parameters The parameters needed to build the cluster
486  */
487 void platf_graph_promote_to_cluster(context_node_t node, sg_platf_cluster_cbarg_t parameters) {
488   node->kind = CLUSTER;
489   memcpy(&(node->cluster_parameters), parameters, sizeof(s_sg_platf_cluster_cbarg_t));
490 }
491
492 /**
493  * \brief Set the parameters of a network link.
494  *
495  * This function should be called in callbacks registered with the
496  * platf_graph_labeler function.
497  *
498  * \param edge The edge to modify
499  * \param parameters The parameters of the network link
500  */
501 void platf_graph_link_label(context_edge_t edge, sg_platf_link_cbarg_t parameters) {
502   memcpy(&(edge->link_parameters), parameters, sizeof(s_sg_platf_link_cbarg_t));
503   edge->labeled = TRUE;
504 }
505
506 /**
507  * \brief Register a callback to promote nodes
508  *
509  * The best way to promote nodes into host or cluster is to write a function
510  * which takes one parameter, a #context_node_t, make every needed test on
511  * it, and call platf_graph_promote_to_host or platf_graph_promote_to_cluster
512  * if needed. Then, register the function with this one.
513  * You can register several callbacks: the first registered function will be
514  * called first. If the node have not been promoted yet, the second function
515  * will be called, and so on...
516  *
517  * \param promoter_callback The callback function
518  */
519 void platf_graph_promoter(platf_promoter_cb_t promoter_callback) {
520   if(promoter_dynar == NULL) {
521     promoter_dynar = xbt_dynar_new(sizeof(platf_promoter_cb_t), NULL);
522   }
523   xbt_dynar_push(promoter_dynar, &promoter_callback);
524 }
525
526 /**
527  * \brief Register a callback to label links
528  *
529  * Like the node promotion, it is better, to set links, to write a function
530  * which take one parameter, a #context_edge_t, make every needed test on
531  * it, and call platf_graph_link_label if needed.
532  * You can register several callbacks: the first registered function will be
533  * called first. If the link have not been labeled yet, the second function
534  * will be called, and so on... All the links must have been labeled after
535  * all the calls.
536  *
537  * \param labeler_callback The callback function
538  */
539 void platf_graph_labeler(platf_labeler_cb_t labeler_callback) {
540   if(labeler_dynar == NULL) {
541     labeler_dynar = xbt_dynar_new(sizeof(void*), NULL);
542   }
543   xbt_dynar_push(labeler_dynar, &labeler_callback);
544 }
545
546 /**
547  * \brief Call the registered promoters on all nodes
548  *
549  * The promoters are called on all nodes, in the order of their registration
550  * If some nodes are not promoted, they will be routers
551  */
552 void platf_do_promote(void) {
553   platf_promoter_cb_t promoter_callback;
554   xbt_node_t graph_node = NULL;
555   xbt_dynar_t dynar_nodes = NULL;
556   context_node_t node = NULL;
557   unsigned int i, j;
558   dynar_nodes = xbt_graph_get_nodes(platform_graph);
559   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
560     node = (context_node_t) xbt_graph_node_get_data(graph_node);
561     xbt_dynar_foreach(promoter_dynar, j, promoter_callback) {
562       if(node->kind != ROUTER)
563         break;
564       promoter_callback(node);
565     }
566   }
567 }
568
569 /**
570  * \brief Call the registered labelers on all links
571  */
572 void platf_do_label(void) {
573   platf_labeler_cb_t labeler_callback;
574   xbt_edge_t graph_edge = NULL;
575   xbt_dynar_t dynar_edges = NULL;
576   context_edge_t edge = NULL;
577   unsigned int i, j;
578   dynar_edges = xbt_graph_get_edges(platform_graph);
579   xbt_dynar_foreach(dynar_edges, i, graph_edge) {
580     edge = (context_edge_t) xbt_graph_edge_get_data(graph_edge);
581     xbt_dynar_foreach(labeler_dynar, j, labeler_callback) {
582       if(edge->labeled)
583         break;
584       labeler_callback(edge);
585     }
586     if(!edge->labeled) {
587       XBT_ERROR("All links of the generated platform are not labeled.");
588       xbt_die("Please check your generation parameters.");
589     }
590   }
591 }
592
593 /**
594  * \brief putting into SURF the generated platform
595  *
596  * This function should be called when the generation is over and the platform
597  * is ready to be put in place in SURF. All the init function, like MSG_init,
598  * must have been called before, or this function will not do anything.
599  * After that function, it should be possible to list all the available hosts
600  * with the provided functions.
601  */
602 void platf_generate(void) {
603
604   xbt_dynar_t nodes = NULL;
605   xbt_node_t graph_node = NULL;
606   context_node_t node_data = NULL;
607   xbt_dynar_t edges = NULL;
608   xbt_edge_t graph_edge = NULL;
609   context_edge_t edge_data = NULL;
610   unsigned int i;
611
612   unsigned int last_host = 0;
613   unsigned int last_router = 0;
614   unsigned int last_cluster = 0;
615
616   sg_platf_host_cbarg_t host_parameters;
617   sg_platf_cluster_cbarg_t cluster_parameters;
618   sg_platf_link_cbarg_t link_parameters;
619   s_sg_platf_router_cbarg_t router_parameters = SG_PLATF_ROUTER_INITIALIZER; /* This one is not a pointer! */
620   s_sg_platf_route_cbarg_t route_parameters   = SG_PLATF_ROUTE_INITIALIZER; /* neither this one! */
621
622   router_parameters.coord = NULL;
623   route_parameters.symmetrical = FALSE;
624   route_parameters.src = NULL;
625   route_parameters.dst = NULL;
626   route_parameters.gw_dst = NULL;
627   route_parameters.gw_src = NULL;
628   route_parameters.link_list = NULL;
629
630   nodes = xbt_graph_get_nodes(platform_graph);
631   edges = xbt_graph_get_edges(platform_graph);
632
633   sg_platf_begin();
634   surf_parse_init_callbacks();
635   routing_register_callbacks();
636
637   s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
638   AS.id = "random platform";
639   AS.routing = A_surfxml_AS_routing_Floyd;
640   sg_platf_new_AS_begin(&AS);
641
642   //Generate hosts, clusters and routers
643   xbt_dynar_foreach(nodes, i, graph_node) {
644     node_data = xbt_graph_node_get_data(graph_node);
645     switch(node_data->kind) {
646       case HOST:
647         host_parameters = &node_data->host_parameters;
648         last_host++;
649         if(host_parameters->id == NULL) {
650           host_parameters->id = bprintf("host-%d", last_host);
651         }
652         sg_platf_new_host(host_parameters);
653         break;
654       case CLUSTER:
655         cluster_parameters = &node_data->cluster_parameters;
656         last_cluster++;
657         if(cluster_parameters->prefix == NULL) {
658           cluster_parameters->prefix = "host-";
659         }
660         if(cluster_parameters->suffix == NULL) {
661           cluster_parameters->suffix = bprintf(".cluster-%d", last_cluster);
662         }
663         if(cluster_parameters->id == NULL) {
664           cluster_parameters->id = bprintf("cluster-%d", last_cluster);
665         }
666         sg_platf_new_cluster(cluster_parameters);
667         break;
668       case ROUTER:
669         node_data->router_id = bprintf("router-%d", ++last_router);
670         router_parameters.id = node_data->router_id;
671         sg_platf_new_router(&router_parameters);
672         break;
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         break;
703     }
704     switch(dst_data->kind) {
705       case ROUTER:
706         route_parameters.dst = dst_data->router_id;
707         break;
708       case CLUSTER:
709         route_parameters.dst = dst_data->cluster_parameters.id;
710         break;
711       case HOST:
712         route_parameters.dst = dst_data->host_parameters.id;
713         break;
714     }
715     sg_platf_route_begin(&route_parameters);
716     sg_platf_route_add_link(link_parameters->id, &route_parameters);
717     sg_platf_route_end(&route_parameters);
718
719     //Create the symmertical route
720     temp = route_parameters.dst;
721     route_parameters.dst = route_parameters.src;
722     route_parameters.src = temp;
723     sg_platf_route_begin(&route_parameters);
724     sg_platf_route_add_link(link_parameters->id, &route_parameters);
725     sg_platf_route_end(&route_parameters);
726   }
727
728   sg_platf_new_AS_end();
729   sg_platf_end();
730 }
731
732 /* Functions used to generate interesting random values */
733
734 double random_pareto(double min, double max, double K, double P, double ALPHA) {
735   double x = RngStream_RandU01(rng_stream);
736   double den = pow(1.0 - x + x*pow(K/P, ALPHA), 1.0/ALPHA);
737   double res = (1/den);
738   res += min - 1; // pareto is on [1, infinity) by default
739   if (res>max) {
740     return max;
741   }
742   return res;
743 }