3 #include "simgrid/platf_generator.h"
4 #include "platf_generator_private.h"
6 #include "xbt/RngStream.h"
7 #include "surf/simgrid_dtd.h"
8 #include "surf_private.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(platf_generator, surf, "Platform Generator");
13 static xbt_graph_t platform_graph = NULL;
14 static xbt_dynar_t promoter_dynar = NULL;
15 static xbt_dynar_t labeler_dynar = NULL;
17 static RngStream rng_stream = NULL;
19 static unsigned long last_link_id = 0;
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;
28 * \brief Set the seed of the platform generator RngStream
30 * This RngStream is used to generate all the random values needed to
31 * generate the platform
33 * \param seed A array of six integer; if NULL, the default seed will be used.
35 void platf_random_seed(unsigned long seed[6]) {
37 if(rng_stream == NULL) {
38 //stream not created yet, we do it now
39 rng_stream = RngStream_CreateStream(NULL);
42 RngStream_SetSeed(rng_stream, seed);
47 * \brief Initialize the platform generator
49 * This function create the graph and add node_count nodes to it
50 * \param node_count The number of nodes of the platform
52 void platf_graph_init(unsigned long node_count) {
54 platform_graph = xbt_graph_new_graph(FALSE, NULL);
55 if(rng_stream == NULL) {
56 rng_stream = RngStream_CreateStream(NULL);
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);
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);
76 * \brief Connect two nodes
77 * \param node1 The first node to connect
78 * \param node2 The second node to connect
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);
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);
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
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;
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);
117 * \brief Initialize the platform, placing nodes uniformly on the unit square
118 * \param node_count The number of node
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;
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);
135 * \brief Initialize the platform, placing nodes in little clusters on the unit square
136 * \param node_count The number of node
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;
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*/);
153 * \brief Creates a simple topology where all nodes are connected to the first one in a star fashion
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;
161 dynar_nodes = xbt_graph_get_nodes(platform_graph);
162 xbt_dynar_foreach(dynar_nodes, i, graph_node) {
164 //Ok, we get the first node, let's keep it somewhere...
165 first_node = graph_node;
167 //All the other nodes are connected to the first one
168 platf_node_connect(graph_node, first_node);
174 * \brief Creates a simple topology where all nodes are connected in line
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;
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);
187 old_node = graph_node;
192 * \brief Create a simple topology where all nodes are connected along a ring
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;
201 dynar_nodes = xbt_graph_get_nodes(platform_graph);
202 xbt_dynar_foreach(dynar_nodes, i, graph_node) {
204 // this is the first node, let's keep it somewhere
205 first_node = graph_node;
207 //connect each node to the previous one
208 platf_node_connect(graph_node, old_node);
210 old_node = graph_node;
212 //we still have to connect the first and the last node together
213 platf_node_connect(first_node, graph_node);
217 * \brief Create a simple topology where all nodes are connected to each other, in a clique manner
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;
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) {
230 platf_node_connect(first_node, second_node);
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
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;
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) {
250 if(RngStream_RandU01(rng_stream) < alpha) {
251 platf_node_connect(first_node, second_node);
258 * \brief Create a topology where the probability follows an exponential law
259 * \param alpha Number of edges increases with alpha
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;
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) {
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);
281 * \brief Create a topology where the probability follows the model of Waxman
283 * see Waxman, Routing of Multipoint Connections, IEEE J. on Selected Areas in Comm., 1988
285 * \param alpha Number of edges increases with alpha
286 * \param beta Edge length heterogeneity increases with beta
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;
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) {
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);
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.
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)
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;
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) {
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);
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.
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;
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) {
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);
361 * \brief Check if the produced graph is connected
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...
366 * \return TRUE if the graph is connected, FALSE otherwise
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;
377 dynar_nodes = xbt_graph_get_nodes(platform_graph);
378 connected_nodes = xbt_dynar_new(sizeof(xbt_node_t), NULL);
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) {
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);
397 xbt_dynar_get_cpy(connected_nodes, iterator, &graph_node);
398 node_data = xbt_graph_node_get_data(graph_node);
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;
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;
416 } while(++iterator < xbt_dynar_length(connected_nodes));
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);
425 * \brief Remove the links in the created topology
427 * This is useful when the created topology is not connected, and you want
428 * to generate a new one.
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;
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);
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);
449 //remove the dynar copy
450 xbt_dynar_free(&dynar_edges_cpy);
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;
462 * \brief Promote a node to a host
464 * This function should be called in callbacks registered with the
465 * platf_graph_promoter function.
467 * \param node The node to promote
468 * \param parameters The parameters needed to build the host
470 void platf_graph_promote_to_host(context_node_t node, sg_platf_host_cbarg_t parameters) {
472 memcpy(&(node->host_parameters), parameters, sizeof(s_sg_platf_host_cbarg_t));
476 * \brief Promote a node to a cluster
478 * This function should be called in callbacks registered with the
479 * platf_graph_promoter function.
481 * \param node The node to promote
482 * \param parameters The parameters needed to build the cluster
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));
490 * \brief Set the parameters of a network link.
492 * This function should be called in callbacks registered with the
493 * platf_graph_labeler function.
495 * \param edge The edge to modify
496 * \param parameters The parameters of the network link
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;
504 * \brief Register a callback to promote nodes
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...
514 * \param promoter_callback The callback function
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);
520 xbt_dynar_push(promoter_dynar, &promoter_callback);
524 * \brief Register a callback to label links
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
534 * \param labeler_callback The callback function
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);
540 xbt_dynar_push(labeler_dynar, &labeler_callback);
544 * \brief Call the registered promoters on all nodes
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
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;
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)
561 promoter_callback(node);
567 * \brief Call the registered labelers on all links
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;
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) {
581 labeler_callback(edge);
584 XBT_ERROR("All links of the generated platform are not labeled.");
585 xbt_die("Please check your generation parameters.");
591 * \brief putting into SURF the generated platform
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.
599 void platf_generate(void) {
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;
609 unsigned int last_host = 0;
610 unsigned int last_router = 0;
611 unsigned int last_cluster = 0;
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! */
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;
627 nodes = xbt_graph_get_nodes(platform_graph);
628 edges = xbt_graph_get_edges(platform_graph);
631 surf_parse_init_callbacks();
632 routing_register_callbacks();
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);
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) {
644 host_parameters = &node_data->host_parameters;
646 if(host_parameters->id == NULL) {
647 host_parameters->id = bprintf("host-%d", last_host);
649 sg_platf_new_host(host_parameters);
652 cluster_parameters = &node_data->cluster_parameters;
654 if(cluster_parameters->prefix == NULL) {
655 cluster_parameters->prefix = "host-";
657 if(cluster_parameters->suffix == NULL) {
658 cluster_parameters->suffix = bprintf(".cluster-%d", last_cluster);
660 if(cluster_parameters->id == NULL) {
661 cluster_parameters->id = bprintf("cluster-%d", last_cluster);
663 sg_platf_new_cluster(cluster_parameters);
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);
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;
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);
686 sg_platf_new_link(link_parameters);
688 //Add a route matching this link
689 switch(src_data->kind) {
691 route_parameters.src = src_data->router_id;
694 route_parameters.src = src_data->cluster_parameters.id;
697 route_parameters.src = src_data->host_parameters.id;
699 switch(dst_data->kind) {
701 route_parameters.dst = dst_data->router_id;
704 route_parameters.dst = dst_data->cluster_parameters.id;
707 route_parameters.dst = dst_data->host_parameters.id;
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);
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);
722 sg_platf_new_AS_end();
726 /* Functions used to generate interesting random values */
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