Logo AND Algorithmique Numérique Distribuée

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