1 /* Copyright (c) 2012, 2014. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "simgrid/platf_generator.h"
8 #include "platf_generator_private.h"
10 #include "xbt/RngStream.h"
11 #include "surf/simgrid_dtd.h"
12 #include "surf_private.h"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(platf_generator, surf, "Platform Generator");
17 static xbt_graph_t platform_graph = NULL;
18 static xbt_dynar_t promoter_dynar = NULL;
19 static xbt_dynar_t labeler_dynar = NULL;
21 static RngStream rng_stream = NULL;
23 static unsigned long last_link_id = 0;
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;
32 * \brief Set the seed of the platform generator RngStream
34 * This RngStream is used to generate all the random values needed to
35 * generate the platform
37 * \param seed A array of six integer; if NULL, the default seed will be used.
39 void platf_random_seed(unsigned long seed[6]) {
41 if(rng_stream == NULL) {
42 //stream not created yet, we do it now
43 rng_stream = RngStream_CreateStream(NULL);
46 RngStream_SetSeed(rng_stream, seed);
51 * \brief Initialize the platform generator
53 * This function create the graph and add node_count nodes to it
54 * \param node_count The number of nodes of the platform
56 void platf_graph_init(unsigned long node_count) {
58 platform_graph = xbt_graph_new_graph(FALSE, NULL);
59 if(rng_stream == NULL) {
60 rng_stream = RngStream_CreateStream(NULL);
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);
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);
80 * \brief Connect two nodes
81 * \param node1 The first node to connect
82 * \param node2 The second node to connect
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);
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);
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
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;
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);
121 * \brief Initialize the platform, placing nodes uniformly on the unit square
122 * \param node_count The number of node
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;
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);
139 * \brief Initialize the platform, placing nodes in little clusters on the unit square
140 * \param node_count The number of node
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;
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*/);
157 * \brief Creates a simple topology where all nodes are connected to the first one in a star fashion
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;
165 dynar_nodes = xbt_graph_get_nodes(platform_graph);
166 xbt_dynar_foreach(dynar_nodes, i, graph_node) {
168 //Ok, we get the first node, let's keep it somewhere...
169 first_node = graph_node;
171 //All the other nodes are connected to the first one
172 platf_node_connect(graph_node, first_node);
178 * \brief Creates a simple topology where all nodes are connected in line
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;
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);
191 old_node = graph_node;
196 * \brief Create a simple topology where all nodes are connected along a ring
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;
205 dynar_nodes = xbt_graph_get_nodes(platform_graph);
206 xbt_dynar_foreach(dynar_nodes, i, graph_node) {
208 // this is the first node, let's keep it somewhere
209 first_node = graph_node;
211 //connect each node to the previous one
212 platf_node_connect(graph_node, old_node);
214 old_node = graph_node;
216 //we still have to connect the first and the last node together
217 platf_node_connect(first_node, graph_node);
221 * \brief Create a simple topology where all nodes are connected to each other, in a clique manner
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;
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) {
234 platf_node_connect(first_node, second_node);
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
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;
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) {
254 if(RngStream_RandU01(rng_stream) < alpha) {
255 platf_node_connect(first_node, second_node);
262 * \brief Create a topology where the probability follows an exponential law
263 * \param alpha Number of edges increases with alpha
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;
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) {
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);
285 * \brief Create a topology where the probability follows the model of Waxman
287 * see Waxman, Routing of Multipoint Connections, IEEE J. on Selected Areas in Comm., 1988
289 * \param alpha Number of edges increases with alpha
290 * \param beta Edge length heterogeneity increases with beta
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;
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) {
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);
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.
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)
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;
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) {
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);
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.
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;
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) {
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);
365 * \brief Check if the produced graph is connected
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...
370 * \return TRUE if the graph is connected, FALSE otherwise
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;
381 dynar_nodes = xbt_graph_get_nodes(platform_graph);
382 connected_nodes = xbt_dynar_new(sizeof(xbt_node_t), NULL);
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) {
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);
401 xbt_dynar_get_cpy(connected_nodes, iterator, &graph_node);
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;
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;
419 } while(++iterator < xbt_dynar_length(connected_nodes));
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);
428 * \brief Remove the links in the created topology
430 * This is useful when the created topology is not connected, and you want
431 * to generate a new one.
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;
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);
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);
452 //remove the dynar copy
453 xbt_dynar_free(&dynar_edges_cpy);
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;
465 * \brief Promote a node to a host
467 * This function should be called in callbacks registered with the
468 * platf_graph_promoter function.
470 * \param node The node to promote
471 * \param parameters The parameters needed to build the host
473 void platf_graph_promote_to_host(context_node_t node, sg_platf_host_cbarg_t parameters) {
475 memcpy(&(node->host_parameters), parameters, sizeof(s_sg_platf_host_cbarg_t));
479 * \brief Promote a node to a cluster
481 * This function should be called in callbacks registered with the
482 * platf_graph_promoter function.
484 * \param node The node to promote
485 * \param parameters The parameters needed to build the cluster
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));
493 * \brief Set the parameters of a network link.
495 * This function should be called in callbacks registered with the
496 * platf_graph_labeler function.
498 * \param edge The edge to modify
499 * \param parameters The parameters of the network link
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;
507 * \brief Register a callback to promote nodes
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...
517 * \param promoter_callback The callback function
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);
523 xbt_dynar_push(promoter_dynar, &promoter_callback);
527 * \brief Register a callback to label links
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
537 * \param labeler_callback The callback function
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);
543 xbt_dynar_push(labeler_dynar, &labeler_callback);
547 * \brief Call the registered promoters on all nodes
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
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;
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)
564 promoter_callback(node);
570 * \brief Call the registered labelers on all links
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;
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) {
584 labeler_callback(edge);
587 XBT_ERROR("All links of the generated platform are not labeled.");
588 xbt_die("Please check your generation parameters.");
594 * \brief putting into SURF the generated platform
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.
602 void platf_generate(void) {
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;
612 unsigned int last_host = 0;
613 unsigned int last_router = 0;
614 unsigned int last_cluster = 0;
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; /* This one is not a pointer! */
620 s_sg_platf_route_cbarg_t route_parameters; /* neither this one! */
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;
630 nodes = xbt_graph_get_nodes(platform_graph);
631 edges = xbt_graph_get_edges(platform_graph);
634 surf_parse_init_callbacks();
635 routing_register_callbacks();
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);
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) {
647 host_parameters = &node_data->host_parameters;
649 if(host_parameters->id == NULL) {
650 host_parameters->id = bprintf("host-%d", last_host);
652 sg_platf_new_host(host_parameters);
655 cluster_parameters = &node_data->cluster_parameters;
657 if(cluster_parameters->prefix == NULL) {
658 cluster_parameters->prefix = "host-";
660 if(cluster_parameters->suffix == NULL) {
661 cluster_parameters->suffix = bprintf(".cluster-%d", last_cluster);
663 if(cluster_parameters->id == NULL) {
664 cluster_parameters->id = bprintf("cluster-%d", last_cluster);
666 sg_platf_new_cluster(cluster_parameters);
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);
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;
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);
690 sg_platf_new_link(link_parameters);
692 //Add a route matching this link
693 switch(src_data->kind) {
695 route_parameters.src = src_data->router_id;
698 route_parameters.src = src_data->cluster_parameters.id;
701 route_parameters.src = src_data->host_parameters.id;
704 switch(dst_data->kind) {
706 route_parameters.dst = dst_data->router_id;
709 route_parameters.dst = dst_data->cluster_parameters.id;
712 route_parameters.dst = dst_data->host_parameters.id;
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);
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);
728 sg_platf_new_AS_end();
732 /* Functions used to generate interesting random values */
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