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 static xbt_graph_t platform_graph = NULL;
12 static xbt_dynar_t promoter_dynar = NULL;
13 static xbt_dynar_t labeler_dynar = NULL;
15 static RngStream rng_stream = NULL;
17 static unsigned long last_link_id = 0;
19 xbt_graph_t platf_graph_get(void) {
20 // We need some debug, so let's add this function
21 // WARNING : should be removed when it becomes useless
22 return platform_graph;
26 * \brief Set the seed of the platform generator RngStream
28 * This RngStream is used to generate all the random values needed to
29 * generate the platform
31 * \param seed A array of six integer; if NULL, the default seed will be used.
33 void platf_random_seed(unsigned long seed[6]) {
35 if(rng_stream == NULL) {
36 //stream not created yet, we do it now
37 rng_stream = RngStream_CreateStream(NULL);
40 RngStream_SetSeed(rng_stream, seed);
45 * \brief Initialize the platform generator
47 * This function create the graph and add node_count nodes to it
48 * \param node_count The number of nodes of the platform
50 void platf_graph_init(unsigned long node_count) {
52 platform_graph = xbt_graph_new_graph(FALSE, NULL);
53 if(rng_stream == NULL) {
54 rng_stream = RngStream_CreateStream(NULL);
57 for(i=0 ; i<node_count ; i++) {
58 context_node_t node_data = NULL;
59 node_data = xbt_new0(s_context_node_t, 1);
63 node_data->degree = 0;
64 node_data->kind = ROUTER;
65 xbt_graph_new_node(platform_graph, (void*) node_data);
73 * \brief Connect two nodes
74 * \param node1 The first node to connect
75 * \param node2 The second node to connect
77 void platf_node_connect(xbt_node_t node1, xbt_node_t node2) {
78 context_node_t node1_data;
79 context_node_t node2_data;
80 node1_data = (context_node_t) xbt_graph_node_get_data(node1);
81 node2_data = (context_node_t) xbt_graph_node_get_data(node2);
85 context_edge_t edge_data = NULL;
86 edge_data = xbt_new0(s_context_edge_t, 1);
87 edge_data->id = ++last_link_id;
88 edge_data->length = platf_node_distance(node1, node2);
89 edge_data->labeled = FALSE;
90 xbt_graph_new_edge(platform_graph, node1, node2, (void*)edge_data);
94 * \brief Compute the distance between two nodes
95 * \param node1 The first node
96 * \param node2 The second node
97 * \return The distance between node1 and node2
99 double platf_node_distance(xbt_node_t node1, xbt_node_t node2) {
100 context_node_t node1_data;
101 context_node_t node2_data;
105 node1_data = (context_node_t) xbt_graph_node_get_data(node1);
106 node2_data = (context_node_t) xbt_graph_node_get_data(node2);
107 delta_x = node1_data->x - node2_data->x;
108 delta_y = node1_data->y - node2_data->y;
109 distance = sqrt(delta_x*delta_x + delta_y*delta_y);
114 * \brief Initialize the platform, placing nodes uniformly on the unit square
115 * \param node_count The number of node
117 void platf_graph_uniform(unsigned long node_count) {
118 xbt_dynar_t dynar_nodes = NULL;
119 xbt_node_t graph_node = NULL;
120 context_node_t node_data = NULL;
122 platf_graph_init(node_count);
123 dynar_nodes = xbt_graph_get_nodes(platform_graph);
124 xbt_dynar_foreach(dynar_nodes, i, graph_node) {
125 node_data = (context_node_t) xbt_graph_node_get_data(graph_node);
126 node_data->x = RngStream_RandU01(rng_stream);
127 node_data->y = RngStream_RandU01(rng_stream);
132 * \brief Initialize the platform, placing nodes in little clusters on the unit square
133 * \param node_count The number of node
135 void platf_graph_heavytailed(unsigned long node_count) {
136 xbt_dynar_t dynar_nodes = NULL;
137 xbt_node_t graph_node = NULL;
138 context_node_t node_data = NULL;
140 platf_graph_init(node_count);
141 dynar_nodes = xbt_graph_get_nodes(platform_graph);
142 xbt_dynar_foreach(dynar_nodes, i, graph_node) {
143 node_data = (context_node_t) xbt_graph_node_get_data(graph_node);
144 node_data->x = random_pareto(0, 1, 1.0/*K*/, 10e9/*P*/, 1.0/*alpha*/);
145 node_data->y = random_pareto(0, 1, 1.0/*K*/, 10e9/*P*/, 1.0/*alpha*/);
150 * \brief Creates a simple topology where all nodes are connected to the first one in a star fashion
152 void platf_graph_interconnect_star(void) {
153 xbt_dynar_t dynar_nodes = NULL;
154 xbt_node_t graph_node = NULL;
155 xbt_node_t first_node = NULL;
158 dynar_nodes = xbt_graph_get_nodes(platform_graph);
159 xbt_dynar_foreach(dynar_nodes, i, graph_node) {
161 //Ok, we get the first node, let's keep it somewhere...
162 first_node = graph_node;
164 //All the other nodes are connected to the first one
165 platf_node_connect(graph_node, first_node);
171 * \brief Creates a simple topology where all nodes are connected in line
173 void platf_graph_interconnect_line(void) {
174 xbt_dynar_t dynar_nodes = NULL;
175 xbt_node_t graph_node = NULL;
176 xbt_node_t old_node = NULL;
179 dynar_nodes = xbt_graph_get_nodes(platform_graph);
180 xbt_dynar_foreach(dynar_nodes, i, graph_node) {
181 if(old_node != NULL) {
182 platf_node_connect(graph_node, old_node);
184 old_node = graph_node;
189 * \brief Create a simple topology where all nodes are connected along a ring
191 void platf_graph_interconnect_ring(void) {
192 xbt_dynar_t dynar_nodes = NULL;
193 xbt_node_t graph_node = NULL;
194 xbt_node_t old_node = NULL;
195 xbt_node_t first_node = NULL;
198 dynar_nodes = xbt_graph_get_nodes(platform_graph);
199 xbt_dynar_foreach(dynar_nodes, i, graph_node) {
201 // this is the first node, let's keep it somewhere
202 first_node = graph_node;
204 //connect each node to the previous one
205 platf_node_connect(graph_node, old_node);
207 old_node = graph_node;
209 //we still have to connect the first and the last node together
210 platf_node_connect(first_node, graph_node);
214 * \brief Create a simple topology where all nodes are connected to each other, in a clique manner
216 void platf_graph_interconnect_clique(void) {
217 xbt_dynar_t dynar_nodes = NULL;
218 xbt_node_t first_node = NULL;
219 xbt_node_t second_node = NULL;
222 dynar_nodes = xbt_graph_get_nodes(platform_graph);
223 xbt_dynar_foreach(dynar_nodes, i, first_node) {
224 xbt_dynar_foreach(dynar_nodes, j, second_node) {
225 platf_node_connect(first_node, second_node);
231 * \brief Creates a topology where the probability to connect two nodes is uniform (unrealistic, but simple)
232 * \param alpha Probability for two nodes to get connected
234 void platf_graph_interconnect_uniform(double alpha) {
235 xbt_dynar_t dynar_nodes = NULL;
236 xbt_node_t first_node = NULL;
237 xbt_node_t second_node = NULL;
240 dynar_nodes = xbt_graph_get_nodes(platform_graph);
241 xbt_dynar_foreach(dynar_nodes, i, first_node) {
242 xbt_dynar_foreach(dynar_nodes, j, second_node) {
245 if(RngStream_RandU01(rng_stream) < alpha) {
246 platf_node_connect(first_node, second_node);
253 * \brief Create a topology where the probability follows an exponential law
254 * \param alpha Number of edges increases with alpha
256 void platf_graph_interconnect_exponential(double alpha) {
257 xbt_dynar_t dynar_nodes = NULL;
258 xbt_node_t first_node = NULL;
259 xbt_node_t second_node = NULL;
261 double L = sqrt(2.0); /* L = c*sqrt(2); c=side of placement square */
262 dynar_nodes = xbt_graph_get_nodes(platform_graph);
263 xbt_dynar_foreach(dynar_nodes, i, first_node) {
264 xbt_dynar_foreach(dynar_nodes, j, second_node) {
267 double d = platf_node_distance(first_node, second_node);
268 if(RngStream_RandU01(rng_stream) < alpha*exp(-d/(L-d))) {
269 platf_node_connect(first_node, second_node);
276 * \brief Create a topology where the probability follows the model of Waxman
278 * see Waxman, Routing of Multipoint Connections, IEEE J. on Selected Areas in Comm., 1988
280 * \param alpha Number of edges increases with alpha
281 * \param beta Edge length heterogeneity increases with beta
283 void platf_graph_interconnect_waxman(double alpha, double beta) {
284 xbt_dynar_t dynar_nodes = NULL;
285 xbt_node_t first_node = NULL;
286 xbt_node_t second_node = NULL;
288 double L = sqrt(2.0); /* L = c*sqrt(2); c=side of placement square */
289 dynar_nodes = xbt_graph_get_nodes(platform_graph);
290 xbt_dynar_foreach(dynar_nodes, i, first_node) {
291 xbt_dynar_foreach(dynar_nodes, j, second_node) {
294 double d = platf_node_distance(first_node, second_node);
295 if(RngStream_RandU01(rng_stream) < alpha*exp(-d/(L*beta))) {
296 platf_node_connect(first_node, second_node);
303 * \brief Create a topology where the probability follows the model of Zegura
304 * see Zegura, Calvert, Donahoo, A quantitative comparison of graph-based models
305 * for Internet topology, IEEE/ACM Transactions on Networking, 1997.
307 * \param alpha Probability of connexion for short edges
308 * \param beta Probability of connexion for long edges
309 * \param r Limit between long and short edges (between 0 and sqrt(2) since nodes are placed on the unit square)
311 void platf_graph_interconnect_zegura(double alpha, double beta, double r) {
312 xbt_dynar_t dynar_nodes = NULL;
313 xbt_node_t first_node = NULL;
314 xbt_node_t second_node = NULL;
316 dynar_nodes = xbt_graph_get_nodes(platform_graph);
317 xbt_dynar_foreach(dynar_nodes, i, first_node) {
318 xbt_dynar_foreach(dynar_nodes, j, second_node) {
321 double d = platf_node_distance(first_node, second_node);
322 double proba = d < r ? alpha : beta;
323 if(RngStream_RandU01(rng_stream) < proba) {
324 platf_node_connect(first_node, second_node);
331 * \brief Create a topology constructed according to the Barabasi-Albert algorithm (follows power laws)
332 * see Barabasi and Albert, Emergence of scaling in random networks, Science 1999, num 59, p509Â-512.
334 void platf_graph_interconnect_barabasi(void) {
335 xbt_dynar_t dynar_nodes = NULL;
336 xbt_node_t first_node = NULL;
337 xbt_node_t second_node = NULL;
338 context_node_t node_data = NULL;
340 unsigned long sum = 0;
341 dynar_nodes = xbt_graph_get_nodes(platform_graph);
342 xbt_dynar_foreach(dynar_nodes, i, first_node) {
343 xbt_dynar_foreach(dynar_nodes, j, second_node) {
346 node_data = xbt_graph_node_get_data(second_node);
347 if(sum==0 || RngStream_RandU01(rng_stream) < ((double)(node_data->degree)/ (double)sum)) {
348 platf_node_connect(first_node, second_node);
356 * \brief Check if the produced graph is connected
358 * You should check if the produced graph is connected before doing anything
359 * on it. You probably don't want any isolated node or group of nodes...
361 * \return TRUE if the graph is connected, FALSE otherwise
363 int platf_graph_is_connected(void) {
364 xbt_dynar_t dynar_nodes = NULL;
365 xbt_dynar_t connected_nodes = NULL;
366 xbt_dynar_t outgoing_edges = NULL;
367 xbt_node_t graph_node = NULL;
368 xbt_edge_t outedge = NULL;
369 unsigned long iterator;
371 dynar_nodes = xbt_graph_get_nodes(platform_graph);
372 connected_nodes = xbt_dynar_new(sizeof(xbt_node_t), NULL);
374 //Initialize the connected node array with the first node
375 xbt_dynar_get_cpy(dynar_nodes, 0, &graph_node);
376 xbt_dynar_push(connected_nodes, &graph_node);
380 xbt_dynar_get_cpy(connected_nodes, iterator, &graph_node);
382 //add all the linked nodes to the connected node array
383 outgoing_edges = xbt_graph_node_get_outedges(graph_node);
384 xbt_dynar_foreach(outgoing_edges, i, outedge) {
385 xbt_node_t src = xbt_graph_edge_get_source(outedge);
386 xbt_node_t dst = xbt_graph_edge_get_target(outedge);
387 if(!xbt_dynar_member(connected_nodes, &src)) {
388 xbt_dynar_push(connected_nodes, &src);
390 if(!xbt_dynar_member(connected_nodes, &dst)) {
391 xbt_dynar_push(connected_nodes, &dst);
394 } while(++iterator < xbt_dynar_length(connected_nodes));
396 // The graph is connected if the connected node array has the same length
397 // as the graph node array
398 return xbt_dynar_length(connected_nodes) == xbt_dynar_length(dynar_nodes);
403 * \brief Remove the links in the created topology
405 * This is useful when the created topology is not connected, and you want
406 * to generate a new one.
408 void platf_graph_clear_links(void) {
409 xbt_dynar_t dynar_nodes = NULL;
410 xbt_dynar_t dynar_edges = NULL;
411 xbt_node_t graph_node = NULL;
412 xbt_edge_t graph_edge = NULL;
413 context_node_t node_data = NULL;
416 //Delete edges from the graph
417 dynar_edges = xbt_graph_get_edges(platform_graph);
418 xbt_dynar_foreach(dynar_edges, i, graph_edge) {
419 xbt_graph_free_edge(platform_graph, graph_edge, xbt_free);
422 //All the nodes will be of degree 0
423 dynar_nodes = xbt_graph_get_nodes(platform_graph);
424 xbt_dynar_foreach(dynar_nodes, i, graph_node) {
425 node_data = xbt_graph_node_get_data(graph_node);
426 node_data->degree = 0;
431 * \brief Promote a node to a host
433 * This function should be called in callbacks registered with the
434 * platf_graph_promoter function.
436 * \param node The node to promote
437 * \param parameters The parameters needed to build the host
439 void platf_graph_promote_to_host(context_node_t node, sg_platf_host_cbarg_t parameters) {
441 memcpy(&(node->host_parameters), parameters, sizeof(s_sg_platf_host_cbarg_t));
445 * \brief Promote a node to a cluster
447 * This function should be called in callbacks registered with the
448 * platf_graph_promoter function.
450 * \param node The node to promote
451 * \param parameters The parameters needed to build the cluster
453 void platf_graph_promote_to_cluster(context_node_t node, sg_platf_cluster_cbarg_t parameters) {
454 node->kind = CLUSTER;
455 memcpy(&(node->cluster_parameters), parameters, sizeof(s_sg_platf_cluster_cbarg_t));
459 * \brief Set the parameters of a network link.
461 * This function should be called in callbacks registered with the
462 * platf_graph_labeler function.
464 * \param edge The edge to modify
465 * \param parameters The parameters of the network link
467 void platf_graph_link_label(context_edge_t edge, sg_platf_link_cbarg_t parameters) {
468 memcpy(&(edge->link_parameters), parameters, sizeof(s_sg_platf_link_cbarg_t));
472 * \brief Register a callback to promote nodes
474 * The best way to promote nodes into host or cluster is to write a function
475 * which takes one parameter, a #context_node_t, make every needed test on
476 * it, and call platf_graph_promote_to_host or platf_graph_promote_to_cluster
477 * if needed. Then, register the function with this one.
478 * You can register several callbacks: the first registered function will be
479 * called first. If the node have not been promoted yet, the second function
480 * will be called, and so on...
482 * \param promoter_callback The callback function
484 void platf_graph_promoter(platf_promoter_cb_t promoter_callback) {
485 if(promoter_dynar == NULL) {
486 promoter_dynar = xbt_dynar_new(sizeof(platf_promoter_cb_t), NULL);
488 xbt_dynar_push(promoter_dynar, &promoter_callback);
492 * \brief Register a callback to label links
494 * Like the node promotion, it is better, to set links, to write a function
495 * which take one parameter, a #context_edge_t, make every needed test on
496 * it, and call platf_graph_link_label if needed.
497 * You can register several callbacks: the first registered function will be
498 * called first. If the link have not been labeled yet, the second function
499 * will be called, and so on...
501 * \param labeler_callback The callback function
503 void platf_graph_labeler(platf_labeler_cb_t labeler_callback) {
504 if(labeler_dynar == NULL) {
505 labeler_dynar = xbt_dynar_new(sizeof(void*), NULL);
507 xbt_dynar_push(labeler_dynar, &labeler_callback);
511 * \brief Call the registered promoters on all nodes
513 * The promoters are called on all nodes, in the order of their registration
514 * If some nodes are not promoted, they will be routers
516 void platf_do_promote(void) {
517 platf_promoter_cb_t promoter_callback;
518 xbt_node_t graph_node = NULL;
519 xbt_dynar_t dynar_nodes = NULL;
520 context_node_t node = NULL;
522 dynar_nodes = xbt_graph_get_nodes(platform_graph);
523 xbt_dynar_foreach(dynar_nodes, i, graph_node) {
524 node = (context_node_t) xbt_graph_node_get_data(graph_node);
525 xbt_dynar_foreach(promoter_dynar, j, promoter_callback) {
526 if(node->kind != ROUTER)
528 promoter_callback(node);
534 * \brief Call the registered labelers on all links
536 void platf_do_label(void) {
537 platf_labeler_cb_t labeler_callback;
538 xbt_edge_t graph_edge = NULL;
539 xbt_dynar_t dynar_edges = NULL;
540 context_edge_t edge = NULL;
542 dynar_edges = xbt_graph_get_edges(platform_graph);
543 xbt_dynar_foreach(dynar_edges, i, graph_edge) {
544 edge = (context_edge_t) xbt_graph_edge_get_data(graph_edge);
545 xbt_dynar_foreach(promoter_dynar, j, labeler_callback) {
546 if(edge->labeled == TRUE)
548 labeler_callback(edge);
554 * \brief putting into SURF the generated platform
556 * This function should be called when the generation is over and the platform
557 * is ready to be put in place in SURF. All the init function, like MSG_init,
558 * must have been called before, or this function will not do anything.
559 * After that function, it should be possible to list all the available hosts
560 * with the provided functions.
562 void platf_generate(void) {
564 xbt_dynar_t nodes = NULL;
565 xbt_node_t graph_node = NULL;
566 context_node_t node_data = NULL;
569 unsigned int last_host = 0;
570 unsigned int last_router = 0;
571 unsigned int last_cluster = 0;
573 sg_platf_host_cbarg_t host_parameters;
574 s_sg_platf_router_cbarg_t router_parameters; /* This one is not a pointer! */
575 sg_platf_cluster_cbarg_t cluster_parameters;
577 router_parameters.coord = NULL;
579 nodes = xbt_graph_get_nodes(platform_graph);
582 surf_parse_init_callbacks();
583 routing_register_callbacks();
586 sg_platf_new_AS_begin("random platform", A_surfxml_AS_routing_Floyd);
588 xbt_dynar_foreach(nodes, i, graph_node) {
589 node_data = xbt_graph_node_get_data(graph_node);
590 switch(node_data->kind) {
592 host_parameters = &node_data->host_parameters;
594 if(host_parameters->id == NULL) {
595 host_parameters->id = bprintf("host-%d", last_host);
597 sg_platf_new_host(host_parameters);
600 cluster_parameters = &node_data->cluster_parameters;
602 if(cluster_parameters->prefix == NULL) {
603 cluster_parameters->prefix = "host-";
605 if(cluster_parameters->suffix == NULL) {
606 cluster_parameters->suffix = bprintf(".cluster-%d", last_cluster);
608 if(cluster_parameters->id == NULL) {
609 cluster_parameters->id = bprintf("cluster-%d", last_cluster);
611 sg_platf_new_cluster(cluster_parameters);
614 router_parameters.id = bprintf("router-%d", ++last_router);
615 sg_platf_new_router(&router_parameters);
620 sg_platf_new_AS_end();
624 /* Functions used to generate interesting random values */
626 double random_pareto(double min, double max, double K, double P, double ALPHA) {
627 double x = RngStream_RandU01(rng_stream);
628 double den = pow(1.0 - x + x*pow(K/P, ALPHA), 1.0/ALPHA);
629 double res = (1/den);
630 res += min - 1; // pareto is on [1, infinity) by default