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 : shold be removed when it becomes useless
22 return platform_graph;
25 void platf_random_seed(unsigned long seed[6]) {
27 if(rng_stream == NULL) {
28 //stream not created yet, we do it now
29 rng_stream = RngStream_CreateStream(NULL);
32 RngStream_SetSeed(rng_stream, seed);
36 void platf_graph_init(unsigned long node_count) {
38 platform_graph = xbt_graph_new_graph(FALSE, NULL);
39 if(rng_stream == NULL) {
40 rng_stream = RngStream_CreateStream(NULL);
43 for(i=0 ; i<node_count ; i++) {
44 context_node_t node_data = NULL;
45 node_data = xbt_new0(s_context_node_t, 1);
49 node_data->degree = 0;
50 node_data->kind = ROUTER;
51 xbt_graph_new_node(platform_graph, (void*) node_data);
58 void platf_node_connect(xbt_node_t node1, xbt_node_t node2) {
59 context_node_t node1_data;
60 context_node_t node2_data;
61 node1_data = (context_node_t) xbt_graph_node_get_data(node1);
62 node2_data = (context_node_t) xbt_graph_node_get_data(node2);
66 context_edge_t edge_data = NULL;
67 edge_data = xbt_new0(s_context_edge_t, 1);
68 edge_data->id = ++last_link_id;
69 edge_data->length = platf_node_distance(node1, node2);
70 edge_data->labeled = FALSE;
71 xbt_graph_new_edge(platform_graph, node1, node2, (void*)edge_data);
74 double platf_node_distance(xbt_node_t node1, xbt_node_t node2) {
75 context_node_t node1_data;
76 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);
82 delta_x = node1_data->x - node2_data->x;
83 delta_y = node1_data->y - node2_data->y;
84 distance = sqrt(delta_x*delta_x + delta_y*delta_y);
88 void platf_graph_uniform(unsigned long node_count) {
89 xbt_dynar_t dynar_nodes = NULL;
90 xbt_node_t graph_node = NULL;
91 context_node_t node_data = NULL;
93 platf_graph_init(node_count);
94 dynar_nodes = xbt_graph_get_nodes(platform_graph);
95 xbt_dynar_foreach(dynar_nodes, i, graph_node) {
96 node_data = (context_node_t) xbt_graph_node_get_data(graph_node);
97 node_data->x = RngStream_RandU01(rng_stream);
98 node_data->y = RngStream_RandU01(rng_stream);
102 void platf_graph_heavytailed(unsigned long node_count) {
103 xbt_dynar_t dynar_nodes = NULL;
104 xbt_node_t graph_node = NULL;
105 context_node_t node_data = NULL;
107 platf_graph_init(node_count);
108 dynar_nodes = xbt_graph_get_nodes(platform_graph);
109 xbt_dynar_foreach(dynar_nodes, i, graph_node) {
110 node_data = (context_node_t) xbt_graph_node_get_data(graph_node);
111 node_data->x = random_pareto(0, 1, 1.0/*K*/, 10e9/*P*/, 1.0/*alpha*/);
112 node_data->y = random_pareto(0, 1, 1.0/*K*/, 10e9/*P*/, 1.0/*alpha*/);
116 void platf_graph_interconnect_star(void) {
117 /* All the nodes are connected to the first one */
118 xbt_dynar_t dynar_nodes = NULL;
119 xbt_node_t graph_node = NULL;
120 xbt_node_t first_node = NULL;
123 dynar_nodes = xbt_graph_get_nodes(platform_graph);
124 xbt_dynar_foreach(dynar_nodes, i, graph_node) {
126 //Ok, we get the first node, let's keep it somewhere...
127 first_node = graph_node;
129 //All the other nodes are connected to the first one
130 platf_node_connect(graph_node, first_node);
135 void platf_graph_interconnect_line(void) {
136 /* Node are connected to the previous and the next node, in a line */
137 xbt_dynar_t dynar_nodes = NULL;
138 xbt_node_t graph_node = NULL;
139 xbt_node_t old_node = NULL;
142 dynar_nodes = xbt_graph_get_nodes(platform_graph);
143 xbt_dynar_foreach(dynar_nodes, i, graph_node) {
144 if(old_node != NULL) {
145 platf_node_connect(graph_node, old_node);
147 old_node = graph_node;
151 void platf_graph_interconnect_ring(void) {
152 /* Create a simple topology where all nodes are connected along a ring */
153 xbt_dynar_t dynar_nodes = NULL;
154 xbt_node_t graph_node = NULL;
155 xbt_node_t old_node = NULL;
156 xbt_node_t first_node = NULL;
159 dynar_nodes = xbt_graph_get_nodes(platform_graph);
160 xbt_dynar_foreach(dynar_nodes, i, graph_node) {
162 // this is the first node, let's keep it somewhere
163 first_node = graph_node;
165 //connect each node to the previous one
166 platf_node_connect(graph_node, old_node);
168 old_node = graph_node;
170 //we still have to connect the first and the last node together
171 platf_node_connect(first_node, graph_node);
174 void platf_graph_interconnect_clique(void) {
175 /* Create a simple topology where all nodes are connected to each other, in a clique manner */
176 xbt_dynar_t dynar_nodes = NULL;
177 xbt_node_t first_node = NULL;
178 xbt_node_t second_node = NULL;
181 dynar_nodes = xbt_graph_get_nodes(platform_graph);
182 xbt_dynar_foreach(dynar_nodes, i, first_node) {
183 xbt_dynar_foreach(dynar_nodes, j, second_node) {
184 platf_node_connect(first_node, second_node);
189 void platf_graph_interconnect_uniform(double alpha) {
190 /* Creates a topology where the probability to connect two nodes is uniform (unrealistic, but simple)
191 alpha : Probability for two nodes to get connected */
192 xbt_dynar_t dynar_nodes = NULL;
193 xbt_node_t first_node = NULL;
194 xbt_node_t second_node = NULL;
197 dynar_nodes = xbt_graph_get_nodes(platform_graph);
198 xbt_dynar_foreach(dynar_nodes, i, first_node) {
199 xbt_dynar_foreach(dynar_nodes, j, second_node) {
202 if(RngStream_RandU01(rng_stream) < alpha) {
203 platf_node_connect(first_node, second_node);
209 void platf_graph_interconnect_exponential(double alpha) {
210 /* Create a topology where the probability follows an exponential law
211 Number of edges increases with alpha */
212 xbt_dynar_t dynar_nodes = NULL;
213 xbt_node_t first_node = NULL;
214 xbt_node_t second_node = NULL;
216 double L = sqrt(2.0); /* L = c*sqrt(2); c=side of placement square */
217 dynar_nodes = xbt_graph_get_nodes(platform_graph);
218 xbt_dynar_foreach(dynar_nodes, i, first_node) {
219 xbt_dynar_foreach(dynar_nodes, j, second_node) {
222 double d = platf_node_distance(first_node, second_node);
223 if(RngStream_RandU01(rng_stream) < alpha*exp(-d/(L-d))) {
224 platf_node_connect(first_node, second_node);
230 void platf_graph_interconnect_waxman(double alpha, double beta) {
231 /* Create a topology where the probability follows the model of Waxman
232 * (see Waxman, Routing of Multipoint Connections, IEEE J. on Selected Areas in Comm., 1988)
234 * Number of edges increases with alpha
235 * edge length heterogeneity increases with beta
237 xbt_dynar_t dynar_nodes = NULL;
238 xbt_node_t first_node = NULL;
239 xbt_node_t second_node = NULL;
241 double L = sqrt(2.0); /* L = c*sqrt(2); c=side of placement square */
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 double d = platf_node_distance(first_node, second_node);
248 if(RngStream_RandU01(rng_stream) < alpha*exp(-d/(L*beta))) {
249 platf_node_connect(first_node, second_node);
255 void platf_graph_interconnect_zegura(double alpha, double beta, double r) {
256 /* Create a topology where the probability follows the model of Zegura
257 * (see Zegura, Calvert, Donahoo, A quantitative comparison of graph-based models
258 * for Internet topology, IEEE/ACM Transactions on Networking, 1997.)
260 * alpha : Probability of connexion for short edges
261 * beta : Probability of connexion for long edges
262 * r : Limit between long and short edges (between 0 and sqrt(2) since nodes are placed on the unit square)
264 xbt_dynar_t dynar_nodes = NULL;
265 xbt_node_t first_node = NULL;
266 xbt_node_t second_node = NULL;
268 dynar_nodes = xbt_graph_get_nodes(platform_graph);
269 xbt_dynar_foreach(dynar_nodes, i, first_node) {
270 xbt_dynar_foreach(dynar_nodes, j, second_node) {
273 double d = platf_node_distance(first_node, second_node);
274 double proba = d < r ? alpha : beta;
275 if(RngStream_RandU01(rng_stream) < proba) {
276 platf_node_connect(first_node, second_node);
282 void platf_graph_interconnect_barabasi(void) {
283 /* Create a topology constructed according to the Barabasi-Albert algorithm (follows power laws)
284 (see Barabasi and Albert, Emergence of scaling in random networks, Science 1999, num 59, p509-512.) */
285 xbt_dynar_t dynar_nodes = NULL;
286 xbt_node_t first_node = NULL;
287 xbt_node_t second_node = NULL;
288 context_node_t node_data = NULL;
290 unsigned long sum = 0;
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 node_data = xbt_graph_node_get_data(second_node);
297 if(sum==0 || RngStream_RandU01(rng_stream) < ((double)(node_data->degree)/ (double)sum)) {
298 platf_node_connect(first_node, second_node);
305 int platf_graph_is_connected(void) {
306 xbt_dynar_t dynar_nodes = NULL;
307 xbt_dynar_t connected_nodes = NULL;
308 xbt_dynar_t outgoing_edges = NULL;
309 xbt_node_t graph_node = NULL;
310 xbt_edge_t outedge = NULL;
311 unsigned long iterator;
313 dynar_nodes = xbt_graph_get_nodes(platform_graph);
314 connected_nodes = xbt_dynar_new(sizeof(xbt_node_t), NULL);
316 //Initialize the connected node array with the first node
317 xbt_dynar_get_cpy(dynar_nodes, 0, &graph_node);
318 xbt_dynar_push(connected_nodes, &graph_node);
322 xbt_dynar_get_cpy(connected_nodes, iterator, &graph_node);
324 //add all the linked nodes to the connected node array
325 outgoing_edges = xbt_graph_node_get_outedges(graph_node);
326 xbt_dynar_foreach(outgoing_edges, i, outedge) {
327 xbt_node_t src = xbt_graph_edge_get_source(outedge);
328 xbt_node_t dst = xbt_graph_edge_get_target(outedge);
329 if(!xbt_dynar_member(connected_nodes, &src)) {
330 xbt_dynar_push(connected_nodes, &src);
332 if(!xbt_dynar_member(connected_nodes, &dst)) {
333 xbt_dynar_push(connected_nodes, &dst);
336 } while(++iterator < xbt_dynar_length(connected_nodes));
338 // The graph is connected if the connected node array has the same length
339 // as the graph node array
340 return xbt_dynar_length(connected_nodes) == xbt_dynar_length(dynar_nodes);
343 void platf_graph_clear_links(void) {
344 xbt_dynar_t dynar_nodes = NULL;
345 xbt_dynar_t dynar_edges = NULL;
346 xbt_node_t graph_node = NULL;
347 xbt_edge_t graph_edge = NULL;
348 context_node_t node_data = NULL;
351 //Delete edges from the graph
352 dynar_edges = xbt_graph_get_edges(platform_graph);
353 xbt_dynar_foreach(dynar_edges, i, graph_edge) {
354 xbt_graph_free_edge(platform_graph, graph_edge, xbt_free);
357 //All the nodes will be of degree 0
358 dynar_nodes = xbt_graph_get_nodes(platform_graph);
359 xbt_dynar_foreach(dynar_nodes, i, graph_node) {
360 node_data = xbt_graph_node_get_data(graph_node);
361 node_data->degree = 0;
365 void platf_graph_promote_to_host(context_node_t node, sg_platf_host_cbarg_t parameters) {
367 memcpy(&(node->host_parameters), parameters, sizeof(s_sg_platf_host_cbarg_t));
370 void platf_graph_promote_to_cluster(context_node_t node, sg_platf_cluster_cbarg_t parameters) {
371 node->kind = CLUSTER;
372 memcpy(&(node->cluster_parameters), parameters, sizeof(s_sg_platf_cluster_cbarg_t));
375 void platf_graph_link_label(context_edge_t edge, sg_platf_link_cbarg_t parameters) {
376 memcpy(&(edge->link_parameters), parameters, sizeof(s_sg_platf_link_cbarg_t));
379 void platf_graph_promoter(platf_promoter_cb_t promoter_callback) {
380 if(promoter_dynar == NULL) {
381 promoter_dynar = xbt_dynar_new(sizeof(platf_promoter_cb_t), NULL);
383 xbt_dynar_push(promoter_dynar, &promoter_callback);
386 void platf_graph_labeler(platf_labeler_cb_t labeler_callback) {
387 if(labeler_dynar == NULL) {
388 labeler_dynar = xbt_dynar_new(sizeof(void*), NULL);
390 xbt_dynar_push(labeler_dynar, &labeler_callback);
393 void platf_do_promote(void) {
394 platf_promoter_cb_t promoter_callback;
395 xbt_node_t graph_node = NULL;
396 xbt_dynar_t dynar_nodes = NULL;
397 context_node_t node = NULL;
399 dynar_nodes = xbt_graph_get_nodes(platform_graph);
400 xbt_dynar_foreach(dynar_nodes, i, graph_node) {
401 node = (context_node_t) xbt_graph_node_get_data(graph_node);
402 xbt_dynar_foreach(promoter_dynar, j, promoter_callback) {
403 if(node->kind != ROUTER)
405 promoter_callback(node);
410 void platf_do_label(void) {
411 platf_labeler_cb_t labeler_callback;
412 xbt_edge_t graph_edge = NULL;
413 xbt_dynar_t dynar_edges = NULL;
414 context_edge_t edge = NULL;
416 dynar_edges = xbt_graph_get_edges(platform_graph);
417 xbt_dynar_foreach(dynar_edges, i, graph_edge) {
418 edge = (context_edge_t) xbt_graph_edge_get_data(graph_edge);
419 xbt_dynar_foreach(promoter_dynar, j, labeler_callback) {
420 if(edge->labeled == TRUE)
422 labeler_callback(edge);
427 void platf_generate(void) {
429 xbt_dynar_t nodes = NULL;
430 xbt_node_t graph_node = NULL;
431 context_node_t node_data = NULL;
434 unsigned int last_host = 0;
435 unsigned int last_router = 0;
436 unsigned int last_cluster = 0;
438 sg_platf_host_cbarg_t host_parameters;
439 s_sg_platf_router_cbarg_t router_parameters; /* This one is not a pointer! */
440 sg_platf_cluster_cbarg_t cluster_parameters;
442 router_parameters.coord = NULL;
444 nodes = xbt_graph_get_nodes(platform_graph);
447 surf_parse_init_callbacks();
448 routing_register_callbacks();
451 sg_platf_new_AS_begin("random platform", A_surfxml_AS_routing_Floyd);
453 xbt_dynar_foreach(nodes, i, graph_node) {
454 node_data = xbt_graph_node_get_data(graph_node);
455 switch(node_data->kind) {
457 host_parameters = &node_data->host_parameters;
459 if(host_parameters->id == NULL) {
460 host_parameters->id = bprintf("host-%d", last_host);
462 sg_platf_new_host(host_parameters);
465 cluster_parameters = &node_data->cluster_parameters;
467 if(cluster_parameters->prefix == NULL) {
468 cluster_parameters->prefix = "host-";
470 if(cluster_parameters->suffix == NULL) {
471 cluster_parameters->suffix = bprintf(".cluster-%d", last_cluster);
473 if(cluster_parameters->id == NULL) {
474 cluster_parameters->id = bprintf("cluster-%d", last_cluster);
476 sg_platf_new_cluster(cluster_parameters);
479 router_parameters.id = bprintf("router-%d", ++last_router);
480 sg_platf_new_router(&router_parameters);
485 sg_platf_new_AS_end();
489 /* Functions used to generate interesting random values */
491 double random_pareto(double min, double max, double K, double P, double ALPHA) {
492 double x = RngStream_RandU01(rng_stream);
493 double den = pow(1.0 - x + x*pow(K/P, ALPHA), 1.0/ALPHA);
494 double res = (1/den);
495 res += min - 1; // pareto is on [1, infinity) by default