Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Platform generation: record the router id given while generation
[simgrid.git] / src / surf / platf_generator.c
1
2
3 #include "simgrid/platf_generator.h"
4 #include "platf_generator_private.h"
5 #include "xbt.h"
6 #include "xbt/RngStream.h"
7 #include "surf/simgrid_dtd.h"
8 #include "surf_private.h"
9 #include <math.h>
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(platf_generator, surf, "Platform Generator");
12
13 static xbt_graph_t platform_graph = NULL;
14 static xbt_dynar_t promoter_dynar = NULL;
15 static xbt_dynar_t labeler_dynar = NULL;
16
17 static RngStream rng_stream = NULL;
18
19 static unsigned long last_link_id = 0;
20
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;
25 }
26
27 /**
28  * \brief Set the seed of the platform generator RngStream
29  *
30  * This RngStream is used to generate all the random values needed to
31  * generate the platform
32  *
33  * \param seed A array of six integer; if NULL, the default seed will be used.
34  */
35 void platf_random_seed(unsigned long seed[6]) {
36
37   if(rng_stream == NULL) {
38     //stream not created yet, we do it now
39     rng_stream = RngStream_CreateStream(NULL);
40   }
41   if(seed != NULL) {
42     RngStream_SetSeed(rng_stream, seed);
43   }
44 }
45
46 /**
47  * \brief Initialize the platform generator
48  *
49  * This function create the graph and add node_count nodes to it
50  * \param node_count The number of nodes of the platform
51  */
52 void platf_graph_init(unsigned long node_count) {
53   unsigned long i;
54   platform_graph = xbt_graph_new_graph(FALSE, NULL);
55   if(rng_stream == NULL) {
56     rng_stream = RngStream_CreateStream(NULL);
57   }
58
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);
62     node_data->id = i+1;
63     node_data->x = 0;
64     node_data->y = 0;
65     node_data->degree = 0;
66     node_data->kind = ROUTER;
67     xbt_graph_new_node(platform_graph, (void*) node_data);
68   }
69
70   last_link_id = 0;
71
72 }
73
74 /**
75  * \brief Connect two nodes
76  * \param node1 The first node to connect
77  * \param node2 The second node to connect
78  */
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);
84   node1_data->degree++;
85   node2_data->degree++;
86
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);
93 }
94
95 /**
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
100  */
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;
104   double delta_x;
105   double delta_y;
106   double distance;
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);
112   return distance;
113 }
114
115 /**
116  * \brief Initialize the platform, placing nodes uniformly on the unit square
117  * \param node_count The number of node
118  */
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;
123   unsigned int i;
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);
130   }
131 }
132
133 /**
134  * \brief Initialize the platform, placing nodes in little clusters on the unit square
135  * \param node_count The number of node
136  */
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;
141   unsigned int i;
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*/);
148   }
149 }
150
151 /**
152  * \brief Creates a simple topology where all nodes are connected to the first one in a star fashion
153  */
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;
158   unsigned int i;
159
160   dynar_nodes = xbt_graph_get_nodes(platform_graph);
161   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
162     if(i==0) {
163       //Ok, we get the first node, let's keep it somewhere...
164       first_node = graph_node;
165     } else {
166       //All the other nodes are connected to the first one
167       platf_node_connect(graph_node, first_node);
168     }
169   }
170 }
171
172 /**
173  * \brief Creates a simple topology where all nodes are connected in line
174  */
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;
179   unsigned int i;
180
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);
185     }
186     old_node = graph_node;
187   }
188 }
189
190 /**
191  * \brief Create a simple topology where all nodes are connected along a ring
192  */
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;
198   unsigned int i;
199
200   dynar_nodes = xbt_graph_get_nodes(platform_graph);
201   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
202     if(i == 0) {
203       // this is the first node, let's keep it somewhere
204       first_node = graph_node;
205     } else {
206       //connect each node to the previous one
207       platf_node_connect(graph_node, old_node);
208     }
209     old_node = graph_node;
210   }
211   //we still have to connect the first and the last node together
212   platf_node_connect(first_node, graph_node);
213 }
214
215 /**
216  * \brief Create a simple topology where all nodes are connected to each other, in a clique manner
217  */
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;
222   unsigned int i,j;
223
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);
228     }
229   }
230 }
231
232 /**
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
235  */
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;
240   unsigned int i,j;
241
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) {
245       if(j>=i)
246         break;
247       if(RngStream_RandU01(rng_stream) < alpha) {
248         platf_node_connect(first_node, second_node);
249       }
250     }
251   }
252 }
253
254 /**
255  * \brief Create a topology where the probability follows an exponential law
256  * \param alpha Number of edges increases with alpha
257  */
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;
262   unsigned int i,j;
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) {
267       if(j>=i)
268         break;
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);
272       }
273     }
274   }
275 }
276
277 /**
278  * \brief Create a topology where the probability follows the model of Waxman
279  *
280  * see Waxman, Routing of Multipoint Connections, IEEE J. on Selected Areas in Comm., 1988
281  *
282  * \param alpha Number of edges increases with alpha
283  * \param beta Edge length heterogeneity increases with beta
284  */
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;
289   unsigned int i,j;
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) {
294       if(j>=i)
295         break;
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);
299       }
300     }
301   }
302 }
303
304 /**
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.
308  *
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)
312  */
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;
317   unsigned int i,j;
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) {
321       if(j>=i)
322         break;
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);
327       }
328     }
329   }
330 }
331
332 /**
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.
335  */
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;
341   unsigned int i,j;
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) {
346       if(j>=i)
347         break;
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);
351         sum += 2;
352       }
353     }
354   }
355 }
356
357 /**
358  * \brief Check if the produced graph is connected
359  *
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...
362  *
363  * \return TRUE if the graph is connected, FALSE otherwise
364  */
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;
372   unsigned int i;
373   dynar_nodes = xbt_graph_get_nodes(platform_graph);
374   connected_nodes = xbt_dynar_new(sizeof(xbt_node_t), NULL);
375
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);
379   iterator = 0;
380   do {
381     //Get the next node
382     xbt_dynar_get_cpy(connected_nodes, iterator, &graph_node);
383
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);
391       }
392       if(!xbt_dynar_member(connected_nodes, &dst)) {
393         xbt_dynar_push(connected_nodes, &dst);
394       }
395     }
396   } while(++iterator < xbt_dynar_length(connected_nodes));
397
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);
401 }
402
403
404 /**
405  * \brief Remove the links in the created topology
406  *
407  * This is useful when the created topology is not connected, and you want
408  * to generate a new one.
409  */
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;
416   unsigned int i;
417
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);
422   }
423
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;
429   }
430 }
431
432 /**
433  * \brief Promote a node to a host
434  *
435  * This function should be called in callbacks registered with the
436  * platf_graph_promoter function.
437  *
438  * \param node The node to promote
439  * \param parameters The parameters needed to build the host
440  */
441 void platf_graph_promote_to_host(context_node_t node, sg_platf_host_cbarg_t parameters) {
442   node->kind = HOST;
443   memcpy(&(node->host_parameters), parameters, sizeof(s_sg_platf_host_cbarg_t));
444 }
445
446 /**
447  * \brief Promote a node to a cluster
448  *
449  * This function should be called in callbacks registered with the
450  * platf_graph_promoter function.
451  *
452  * \param node The node to promote
453  * \param parameters The parameters needed to build the cluster
454  */
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));
458 }
459
460 /**
461  * \brief Set the parameters of a network link.
462  *
463  * This function should be called in callbacks registered with the
464  * platf_graph_labeler function.
465  *
466  * \param edge The edge to modify
467  * \param parameters The parameters of the network link
468  */
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;
472 }
473
474 /**
475  * \brief Register a callback to promote nodes
476  *
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...
484  *
485  * \param promoter_callback The callback function
486  */
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);
490   }
491   xbt_dynar_push(promoter_dynar, &promoter_callback);
492 }
493
494 /**
495  * \brief Register a callback to label links
496  *
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
503  * all the calls.
504  *
505  * \param labeler_callback The callback function
506  */
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);
510   }
511   xbt_dynar_push(labeler_dynar, &labeler_callback);
512 }
513
514 /**
515  * \brief Call the registered promoters on all nodes
516  *
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
519  */
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;
525   unsigned int i, j;
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)
531         break;
532       promoter_callback(node);
533     }
534   }
535 }
536
537 /**
538  * \brief Call the registered labelers on all links
539  */
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;
545   unsigned int i, j;
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) {
550       if(edge->labeled)
551         break;
552       labeler_callback(edge);
553     }
554     if(!edge->labeled) {
555       XBT_ERROR("All links of the generated platform are not labeled.");
556       xbt_die("Please check your generation parameters.");
557     }
558   }
559 }
560
561 /**
562  * \brief putting into SURF the generated platform
563  *
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.
569  */
570 void platf_generate(void) {
571
572   xbt_dynar_t nodes = NULL;
573   xbt_node_t graph_node = NULL;
574   context_node_t node_data = NULL;
575   unsigned int i;
576
577   unsigned int last_host = 0;
578   unsigned int last_router = 0;
579   unsigned int last_cluster = 0;
580
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;
584
585   router_parameters.coord = NULL;
586
587   nodes = xbt_graph_get_nodes(platform_graph);
588
589   sg_platf_begin();
590   surf_parse_init_callbacks();
591   routing_register_callbacks();
592
593
594   sg_platf_new_AS_begin("random platform", A_surfxml_AS_routing_Floyd);
595
596   xbt_dynar_foreach(nodes, i, graph_node) {
597     node_data = xbt_graph_node_get_data(graph_node);
598     switch(node_data->kind) {
599       case HOST:
600         host_parameters = &node_data->host_parameters;
601         last_host++;
602         if(host_parameters->id == NULL) {
603           host_parameters->id = bprintf("host-%d", last_host);
604         }
605         sg_platf_new_host(host_parameters);
606         break;
607       case CLUSTER:
608         cluster_parameters = &node_data->cluster_parameters;
609         last_cluster++;
610         if(cluster_parameters->prefix == NULL) {
611           cluster_parameters->prefix = "host-";
612         }
613         if(cluster_parameters->suffix == NULL) {
614           cluster_parameters->suffix = bprintf(".cluster-%d", last_cluster);
615         }
616         if(cluster_parameters->id == NULL) {
617           cluster_parameters->id = bprintf("cluster-%d", last_cluster);
618         }
619         sg_platf_new_cluster(cluster_parameters);
620         break;
621       case ROUTER:
622         router_parameters.id = bprintf("router-%d", ++last_router);
623         sg_platf_new_router(&router_parameters);
624     }
625   }
626
627
628   sg_platf_new_AS_end();
629   sg_platf_end();
630 }
631
632 /* Functions used to generate interesting random values */
633
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
639   if (res>max) {
640     return max;
641   }
642   return res;
643 }