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 xbt_graph_new_node(platform_graph, (void*) node_data);
75 * \brief Connect two nodes
76 * \param node1 The first node to connect
77 * \param node2 The second node to connect
79 void platf_node_connect(xbt_node_t node1, xbt_node_t node2) {
80 context_node_t node1_data;
81 context_node_t node2_data;
82 node1_data = (context_node_t) xbt_graph_node_get_data(node1);
83 node2_data = (context_node_t) xbt_graph_node_get_data(node2);
87 context_edge_t edge_data = NULL;
88 edge_data = xbt_new0(s_context_edge_t, 1);
89 edge_data->id = ++last_link_id;
90 edge_data->length = platf_node_distance(node1, node2);
91 edge_data->labeled = FALSE;
92 xbt_graph_new_edge(platform_graph, node1, node2, (void*)edge_data);
96 * \brief Compute the distance between two nodes
97 * \param node1 The first node
98 * \param node2 The second node
99 * \return The distance between node1 and node2
101 double platf_node_distance(xbt_node_t node1, xbt_node_t node2) {
102 context_node_t node1_data;
103 context_node_t node2_data;
107 node1_data = (context_node_t) xbt_graph_node_get_data(node1);
108 node2_data = (context_node_t) xbt_graph_node_get_data(node2);
109 delta_x = node1_data->x - node2_data->x;
110 delta_y = node1_data->y - node2_data->y;
111 distance = sqrt(delta_x*delta_x + delta_y*delta_y);
116 * \brief Initialize the platform, placing nodes uniformly on the unit square
117 * \param node_count The number of node
119 void platf_graph_uniform(unsigned long node_count) {
120 xbt_dynar_t dynar_nodes = NULL;
121 xbt_node_t graph_node = NULL;
122 context_node_t node_data = NULL;
124 platf_graph_init(node_count);
125 dynar_nodes = xbt_graph_get_nodes(platform_graph);
126 xbt_dynar_foreach(dynar_nodes, i, graph_node) {
127 node_data = (context_node_t) xbt_graph_node_get_data(graph_node);
128 node_data->x = RngStream_RandU01(rng_stream);
129 node_data->y = RngStream_RandU01(rng_stream);
134 * \brief Initialize the platform, placing nodes in little clusters on the unit square
135 * \param node_count The number of node
137 void platf_graph_heavytailed(unsigned long node_count) {
138 xbt_dynar_t dynar_nodes = NULL;
139 xbt_node_t graph_node = NULL;
140 context_node_t node_data = NULL;
142 platf_graph_init(node_count);
143 dynar_nodes = xbt_graph_get_nodes(platform_graph);
144 xbt_dynar_foreach(dynar_nodes, i, graph_node) {
145 node_data = (context_node_t) xbt_graph_node_get_data(graph_node);
146 node_data->x = random_pareto(0, 1, 1.0/*K*/, 10e9/*P*/, 1.0/*alpha*/);
147 node_data->y = random_pareto(0, 1, 1.0/*K*/, 10e9/*P*/, 1.0/*alpha*/);
152 * \brief Creates a simple topology where all nodes are connected to the first one in a star fashion
154 void platf_graph_interconnect_star(void) {
155 xbt_dynar_t dynar_nodes = NULL;
156 xbt_node_t graph_node = NULL;
157 xbt_node_t first_node = NULL;
160 dynar_nodes = xbt_graph_get_nodes(platform_graph);
161 xbt_dynar_foreach(dynar_nodes, i, graph_node) {
163 //Ok, we get the first node, let's keep it somewhere...
164 first_node = graph_node;
166 //All the other nodes are connected to the first one
167 platf_node_connect(graph_node, first_node);
173 * \brief Creates a simple topology where all nodes are connected in line
175 void platf_graph_interconnect_line(void) {
176 xbt_dynar_t dynar_nodes = NULL;
177 xbt_node_t graph_node = NULL;
178 xbt_node_t old_node = NULL;
181 dynar_nodes = xbt_graph_get_nodes(platform_graph);
182 xbt_dynar_foreach(dynar_nodes, i, graph_node) {
183 if(old_node != NULL) {
184 platf_node_connect(graph_node, old_node);
186 old_node = graph_node;
191 * \brief Create a simple topology where all nodes are connected along a ring
193 void platf_graph_interconnect_ring(void) {
194 xbt_dynar_t dynar_nodes = NULL;
195 xbt_node_t graph_node = NULL;
196 xbt_node_t old_node = NULL;
197 xbt_node_t first_node = NULL;
200 dynar_nodes = xbt_graph_get_nodes(platform_graph);
201 xbt_dynar_foreach(dynar_nodes, i, graph_node) {
203 // this is the first node, let's keep it somewhere
204 first_node = graph_node;
206 //connect each node to the previous one
207 platf_node_connect(graph_node, old_node);
209 old_node = graph_node;
211 //we still have to connect the first and the last node together
212 platf_node_connect(first_node, graph_node);
216 * \brief Create a simple topology where all nodes are connected to each other, in a clique manner
218 void platf_graph_interconnect_clique(void) {
219 xbt_dynar_t dynar_nodes = NULL;
220 xbt_node_t first_node = NULL;
221 xbt_node_t second_node = NULL;
224 dynar_nodes = xbt_graph_get_nodes(platform_graph);
225 xbt_dynar_foreach(dynar_nodes, i, first_node) {
226 xbt_dynar_foreach(dynar_nodes, j, second_node) {
227 platf_node_connect(first_node, second_node);
233 * \brief Creates a topology where the probability to connect two nodes is uniform (unrealistic, but simple)
234 * \param alpha Probability for two nodes to get connected
236 void platf_graph_interconnect_uniform(double alpha) {
237 xbt_dynar_t dynar_nodes = NULL;
238 xbt_node_t first_node = NULL;
239 xbt_node_t second_node = NULL;
242 dynar_nodes = xbt_graph_get_nodes(platform_graph);
243 xbt_dynar_foreach(dynar_nodes, i, first_node) {
244 xbt_dynar_foreach(dynar_nodes, j, second_node) {
247 if(RngStream_RandU01(rng_stream) < alpha) {
248 platf_node_connect(first_node, second_node);
255 * \brief Create a topology where the probability follows an exponential law
256 * \param alpha Number of edges increases with alpha
258 void platf_graph_interconnect_exponential(double alpha) {
259 xbt_dynar_t dynar_nodes = NULL;
260 xbt_node_t first_node = NULL;
261 xbt_node_t second_node = NULL;
263 double L = sqrt(2.0); /* L = c*sqrt(2); c=side of placement square */
264 dynar_nodes = xbt_graph_get_nodes(platform_graph);
265 xbt_dynar_foreach(dynar_nodes, i, first_node) {
266 xbt_dynar_foreach(dynar_nodes, j, second_node) {
269 double d = platf_node_distance(first_node, second_node);
270 if(RngStream_RandU01(rng_stream) < alpha*exp(-d/(L-d))) {
271 platf_node_connect(first_node, second_node);
278 * \brief Create a topology where the probability follows the model of Waxman
280 * see Waxman, Routing of Multipoint Connections, IEEE J. on Selected Areas in Comm., 1988
282 * \param alpha Number of edges increases with alpha
283 * \param beta Edge length heterogeneity increases with beta
285 void platf_graph_interconnect_waxman(double alpha, double beta) {
286 xbt_dynar_t dynar_nodes = NULL;
287 xbt_node_t first_node = NULL;
288 xbt_node_t second_node = NULL;
290 double L = sqrt(2.0); /* L = c*sqrt(2); c=side of placement square */
291 dynar_nodes = xbt_graph_get_nodes(platform_graph);
292 xbt_dynar_foreach(dynar_nodes, i, first_node) {
293 xbt_dynar_foreach(dynar_nodes, j, second_node) {
296 double d = platf_node_distance(first_node, second_node);
297 if(RngStream_RandU01(rng_stream) < alpha*exp(-d/(L*beta))) {
298 platf_node_connect(first_node, second_node);
305 * \brief Create a topology where the probability follows the model of Zegura
306 * see Zegura, Calvert, Donahoo, A quantitative comparison of graph-based models
307 * for Internet topology, IEEE/ACM Transactions on Networking, 1997.
309 * \param alpha Probability of connexion for short edges
310 * \param beta Probability of connexion for long edges
311 * \param r Limit between long and short edges (between 0 and sqrt(2) since nodes are placed on the unit square)
313 void platf_graph_interconnect_zegura(double alpha, double beta, double r) {
314 xbt_dynar_t dynar_nodes = NULL;
315 xbt_node_t first_node = NULL;
316 xbt_node_t second_node = NULL;
318 dynar_nodes = xbt_graph_get_nodes(platform_graph);
319 xbt_dynar_foreach(dynar_nodes, i, first_node) {
320 xbt_dynar_foreach(dynar_nodes, j, second_node) {
323 double d = platf_node_distance(first_node, second_node);
324 double proba = d < r ? alpha : beta;
325 if(RngStream_RandU01(rng_stream) < proba) {
326 platf_node_connect(first_node, second_node);
333 * \brief Create a topology constructed according to the Barabasi-Albert algorithm (follows power laws)
334 * see Barabasi and Albert, Emergence of scaling in random networks, Science 1999, num 59, p509Â-512.
336 void platf_graph_interconnect_barabasi(void) {
337 xbt_dynar_t dynar_nodes = NULL;
338 xbt_node_t first_node = NULL;
339 xbt_node_t second_node = NULL;
340 context_node_t node_data = NULL;
342 unsigned long sum = 0;
343 dynar_nodes = xbt_graph_get_nodes(platform_graph);
344 xbt_dynar_foreach(dynar_nodes, i, first_node) {
345 xbt_dynar_foreach(dynar_nodes, j, second_node) {
348 node_data = xbt_graph_node_get_data(second_node);
349 if(sum==0 || RngStream_RandU01(rng_stream) < ((double)(node_data->degree)/ (double)sum)) {
350 platf_node_connect(first_node, second_node);
358 * \brief Check if the produced graph is connected
360 * You should check if the produced graph is connected before doing anything
361 * on it. You probably don't want any isolated node or group of nodes...
363 * \return TRUE if the graph is connected, FALSE otherwise
365 int platf_graph_is_connected(void) {
366 xbt_dynar_t dynar_nodes = NULL;
367 xbt_dynar_t connected_nodes = NULL;
368 xbt_dynar_t outgoing_edges = NULL;
369 xbt_node_t graph_node = NULL;
370 xbt_edge_t outedge = NULL;
371 unsigned long iterator;
373 dynar_nodes = xbt_graph_get_nodes(platform_graph);
374 connected_nodes = xbt_dynar_new(sizeof(xbt_node_t), NULL);
376 //Initialize the connected node array with the first node
377 xbt_dynar_get_cpy(dynar_nodes, 0, &graph_node);
378 xbt_dynar_push(connected_nodes, &graph_node);
382 xbt_dynar_get_cpy(connected_nodes, iterator, &graph_node);
384 //add all the linked nodes to the connected node array
385 outgoing_edges = xbt_graph_node_get_outedges(graph_node);
386 xbt_dynar_foreach(outgoing_edges, i, outedge) {
387 xbt_node_t src = xbt_graph_edge_get_source(outedge);
388 xbt_node_t dst = xbt_graph_edge_get_target(outedge);
389 if(!xbt_dynar_member(connected_nodes, &src)) {
390 xbt_dynar_push(connected_nodes, &src);
392 if(!xbt_dynar_member(connected_nodes, &dst)) {
393 xbt_dynar_push(connected_nodes, &dst);
396 } while(++iterator < xbt_dynar_length(connected_nodes));
398 // The graph is connected if the connected node array has the same length
399 // as the graph node array
400 return xbt_dynar_length(connected_nodes) == xbt_dynar_length(dynar_nodes);
405 * \brief Remove the links in the created topology
407 * This is useful when the created topology is not connected, and you want
408 * to generate a new one.
410 void platf_graph_clear_links(void) {
411 xbt_dynar_t dynar_nodes = NULL;
412 xbt_dynar_t dynar_edges = NULL;
413 xbt_node_t graph_node = NULL;
414 xbt_edge_t graph_edge = NULL;
415 context_node_t node_data = NULL;
418 //Delete edges from the graph
419 dynar_edges = xbt_graph_get_edges(platform_graph);
420 xbt_dynar_foreach(dynar_edges, i, graph_edge) {
421 xbt_graph_free_edge(platform_graph, graph_edge, xbt_free);
424 //All the nodes will be of degree 0
425 dynar_nodes = xbt_graph_get_nodes(platform_graph);
426 xbt_dynar_foreach(dynar_nodes, i, graph_node) {
427 node_data = xbt_graph_node_get_data(graph_node);
428 node_data->degree = 0;
433 * \brief Promote a node to a host
435 * This function should be called in callbacks registered with the
436 * platf_graph_promoter function.
438 * \param node The node to promote
439 * \param parameters The parameters needed to build the host
441 void platf_graph_promote_to_host(context_node_t node, sg_platf_host_cbarg_t parameters) {
443 memcpy(&(node->host_parameters), parameters, sizeof(s_sg_platf_host_cbarg_t));
447 * \brief Promote a node to a cluster
449 * This function should be called in callbacks registered with the
450 * platf_graph_promoter function.
452 * \param node The node to promote
453 * \param parameters The parameters needed to build the cluster
455 void platf_graph_promote_to_cluster(context_node_t node, sg_platf_cluster_cbarg_t parameters) {
456 node->kind = CLUSTER;
457 memcpy(&(node->cluster_parameters), parameters, sizeof(s_sg_platf_cluster_cbarg_t));
461 * \brief Set the parameters of a network link.
463 * This function should be called in callbacks registered with the
464 * platf_graph_labeler function.
466 * \param edge The edge to modify
467 * \param parameters The parameters of the network link
469 void platf_graph_link_label(context_edge_t edge, sg_platf_link_cbarg_t parameters) {
470 memcpy(&(edge->link_parameters), parameters, sizeof(s_sg_platf_link_cbarg_t));
471 edge->labeled = TRUE;
475 * \brief Register a callback to promote nodes
477 * The best way to promote nodes into host or cluster is to write a function
478 * which takes one parameter, a #context_node_t, make every needed test on
479 * it, and call platf_graph_promote_to_host or platf_graph_promote_to_cluster
480 * if needed. Then, register the function with this one.
481 * You can register several callbacks: the first registered function will be
482 * called first. If the node have not been promoted yet, the second function
483 * will be called, and so on...
485 * \param promoter_callback The callback function
487 void platf_graph_promoter(platf_promoter_cb_t promoter_callback) {
488 if(promoter_dynar == NULL) {
489 promoter_dynar = xbt_dynar_new(sizeof(platf_promoter_cb_t), NULL);
491 xbt_dynar_push(promoter_dynar, &promoter_callback);
495 * \brief Register a callback to label links
497 * Like the node promotion, it is better, to set links, to write a function
498 * which take one parameter, a #context_edge_t, make every needed test on
499 * it, and call platf_graph_link_label if needed.
500 * You can register several callbacks: the first registered function will be
501 * called first. If the link have not been labeled yet, the second function
502 * will be called, and so on... All the links must have been labeled after
505 * \param labeler_callback The callback function
507 void platf_graph_labeler(platf_labeler_cb_t labeler_callback) {
508 if(labeler_dynar == NULL) {
509 labeler_dynar = xbt_dynar_new(sizeof(void*), NULL);
511 xbt_dynar_push(labeler_dynar, &labeler_callback);
515 * \brief Call the registered promoters on all nodes
517 * The promoters are called on all nodes, in the order of their registration
518 * If some nodes are not promoted, they will be routers
520 void platf_do_promote(void) {
521 platf_promoter_cb_t promoter_callback;
522 xbt_node_t graph_node = NULL;
523 xbt_dynar_t dynar_nodes = NULL;
524 context_node_t node = NULL;
526 dynar_nodes = xbt_graph_get_nodes(platform_graph);
527 xbt_dynar_foreach(dynar_nodes, i, graph_node) {
528 node = (context_node_t) xbt_graph_node_get_data(graph_node);
529 xbt_dynar_foreach(promoter_dynar, j, promoter_callback) {
530 if(node->kind != ROUTER)
532 promoter_callback(node);
538 * \brief Call the registered labelers on all links
540 void platf_do_label(void) {
541 platf_labeler_cb_t labeler_callback;
542 xbt_edge_t graph_edge = NULL;
543 xbt_dynar_t dynar_edges = NULL;
544 context_edge_t edge = NULL;
546 dynar_edges = xbt_graph_get_edges(platform_graph);
547 xbt_dynar_foreach(dynar_edges, i, graph_edge) {
548 edge = (context_edge_t) xbt_graph_edge_get_data(graph_edge);
549 xbt_dynar_foreach(labeler_dynar, j, labeler_callback) {
552 labeler_callback(edge);
555 XBT_ERROR("All links of the generated platform are not labeled.");
556 xbt_die("Please check your generation parameters.");
562 * \brief putting into SURF the generated platform
564 * This function should be called when the generation is over and the platform
565 * is ready to be put in place in SURF. All the init function, like MSG_init,
566 * must have been called before, or this function will not do anything.
567 * After that function, it should be possible to list all the available hosts
568 * with the provided functions.
570 void platf_generate(void) {
572 xbt_dynar_t nodes = NULL;
573 xbt_node_t graph_node = NULL;
574 context_node_t node_data = NULL;
577 unsigned int last_host = 0;
578 unsigned int last_router = 0;
579 unsigned int last_cluster = 0;
581 sg_platf_host_cbarg_t host_parameters;
582 s_sg_platf_router_cbarg_t router_parameters; /* This one is not a pointer! */
583 sg_platf_cluster_cbarg_t cluster_parameters;
585 router_parameters.coord = NULL;
587 nodes = xbt_graph_get_nodes(platform_graph);
590 surf_parse_init_callbacks();
591 routing_register_callbacks();
594 sg_platf_new_AS_begin("random platform", A_surfxml_AS_routing_Floyd);
596 xbt_dynar_foreach(nodes, i, graph_node) {
597 node_data = xbt_graph_node_get_data(graph_node);
598 switch(node_data->kind) {
600 host_parameters = &node_data->host_parameters;
602 if(host_parameters->id == NULL) {
603 host_parameters->id = bprintf("host-%d", last_host);
605 sg_platf_new_host(host_parameters);
608 cluster_parameters = &node_data->cluster_parameters;
610 if(cluster_parameters->prefix == NULL) {
611 cluster_parameters->prefix = "host-";
613 if(cluster_parameters->suffix == NULL) {
614 cluster_parameters->suffix = bprintf(".cluster-%d", last_cluster);
616 if(cluster_parameters->id == NULL) {
617 cluster_parameters->id = bprintf("cluster-%d", last_cluster);
619 sg_platf_new_cluster(cluster_parameters);
622 router_parameters.id = bprintf("router-%d", ++last_router);
623 sg_platf_new_router(&router_parameters);
628 sg_platf_new_AS_end();
632 /* Functions used to generate interesting random values */
634 double random_pareto(double min, double max, double K, double P, double ALPHA) {
635 double x = RngStream_RandU01(rng_stream);
636 double den = pow(1.0 - x + x*pow(K/P, ALPHA), 1.0/ALPHA);
637 double res = (1/den);
638 res += min - 1; // pareto is on [1, infinity) by default