Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Platform generation: add some documentation
[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 static xbt_graph_t platform_graph = NULL;
12 static xbt_dynar_t promoter_dynar = NULL;
13 static xbt_dynar_t labeler_dynar = NULL;
14
15 static RngStream rng_stream = NULL;
16
17 static unsigned long last_link_id = 0;
18
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;
23 }
24
25 /**
26  * \brief Set the seed of the platform generator RngStream
27  *
28  * This RngStream is used to generate all the random values needed to
29  * generate the platform
30  *
31  * \param seed A array of six integer; if NULL, the default seed will be used.
32  */
33 void platf_random_seed(unsigned long seed[6]) {
34
35   if(rng_stream == NULL) {
36     //stream not created yet, we do it now
37     rng_stream = RngStream_CreateStream(NULL);
38   }
39   if(seed != NULL) {
40     RngStream_SetSeed(rng_stream, seed);
41   }
42 }
43
44 /**
45  * \brief Initialize the platform generator
46  *
47  * This function create the graph and add node_count nodes to it
48  * \param node_count The number of nodes of the platform
49  */
50 void platf_graph_init(unsigned long node_count) {
51   unsigned long i;
52   platform_graph = xbt_graph_new_graph(FALSE, NULL);
53   if(rng_stream == NULL) {
54     rng_stream = RngStream_CreateStream(NULL);
55   }
56
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);
60     node_data->id = i+1;
61     node_data->x = 0;
62     node_data->y = 0;
63     node_data->degree = 0;
64     node_data->kind = ROUTER;
65     xbt_graph_new_node(platform_graph, (void*) node_data);
66   }
67
68   last_link_id = 0;
69
70 }
71
72 /**
73  * \brief Connect two nodes
74  * \param node1 The first node to connect
75  * \param node2 The second node to connect
76  */
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);
82   node1_data->degree++;
83   node2_data->degree++;
84
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);
91 }
92
93 /**
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
98  */
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;
102   double delta_x;
103   double delta_y;
104   double distance;
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);
110   return distance;
111 }
112
113 /**
114  * \brief Initialize the platform, placing nodes uniformly on the unit square
115  * \param node_count The number of node
116  */
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;
121   unsigned int i;
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);
128   }
129 }
130
131 /**
132  * \brief Initialize the platform, placing nodes in little clusters on the unit square
133  * \param node_count The number of node
134  */
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;
139   unsigned int i;
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*/);
146   }
147 }
148
149 /**
150  * \brief Creates a simple topology where all nodes are connected to the first one in a star fashion
151  */
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;
156   unsigned int i;
157
158   dynar_nodes = xbt_graph_get_nodes(platform_graph);
159   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
160     if(i==0) {
161       //Ok, we get the first node, let's keep it somewhere...
162       first_node = graph_node;
163     } else {
164       //All the other nodes are connected to the first one
165       platf_node_connect(graph_node, first_node);
166     }
167   }
168 }
169
170 /**
171  * \brief Creates a simple topology where all nodes are connected in line
172  */
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;
177   unsigned int i;
178
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);
183     }
184     old_node = graph_node;
185   }
186 }
187
188 /**
189  * \brief Create a simple topology where all nodes are connected along a ring
190  */
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;
196   unsigned int i;
197
198   dynar_nodes = xbt_graph_get_nodes(platform_graph);
199   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
200     if(i == 0) {
201       // this is the first node, let's keep it somewhere
202       first_node = graph_node;
203     } else {
204       //connect each node to the previous one
205       platf_node_connect(graph_node, old_node);
206     }
207     old_node = graph_node;
208   }
209   //we still have to connect the first and the last node together
210   platf_node_connect(first_node, graph_node);
211 }
212
213 /**
214  * \brief Create a simple topology where all nodes are connected to each other, in a clique manner
215  */
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;
220   unsigned int i,j;
221
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);
226     }
227   }
228 }
229
230 /**
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
233  */
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;
238   unsigned int i,j;
239
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) {
243       if(j>=i)
244         break;
245       if(RngStream_RandU01(rng_stream) < alpha) {
246         platf_node_connect(first_node, second_node);
247       }
248     }
249   }
250 }
251
252 /**
253  * \brief Create a topology where the probability follows an exponential law
254  * \param alpha Number of edges increases with alpha
255  */
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;
260   unsigned int i,j;
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) {
265       if(j>=i)
266         break;
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);
270       }
271     }
272   }
273 }
274
275 /**
276  * \brief Create a topology where the probability follows the model of Waxman
277  *
278  * see Waxman, Routing of Multipoint Connections, IEEE J. on Selected Areas in Comm., 1988
279  *
280  * \param alpha Number of edges increases with alpha
281  * \param beta Edge length heterogeneity increases with beta
282  */
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;
287   unsigned int i,j;
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) {
292       if(j>=i)
293         break;
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);
297       }
298     }
299   }
300 }
301
302 /**
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.
306  *
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)
310  */
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;
315   unsigned int i,j;
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) {
319       if(j>=i)
320         break;
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);
325       }
326     }
327   }
328 }
329
330 /**
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.
333  */
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;
339   unsigned int i,j;
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) {
344       if(j>=i)
345         break;
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);
349         sum += 2;
350       }
351     }
352   }
353 }
354
355 /**
356  * \brief Check if the produced graph is connected
357  *
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...
360  *
361  * \return TRUE if the graph is connected, FALSE otherwise
362  */
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;
370   unsigned int i;
371   dynar_nodes = xbt_graph_get_nodes(platform_graph);
372   connected_nodes = xbt_dynar_new(sizeof(xbt_node_t), NULL);
373
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);
377   iterator = 0;
378   do {
379     //Get the next node
380     xbt_dynar_get_cpy(connected_nodes, iterator, &graph_node);
381
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);
389       }
390       if(!xbt_dynar_member(connected_nodes, &dst)) {
391         xbt_dynar_push(connected_nodes, &dst);
392       }
393     }
394   } while(++iterator < xbt_dynar_length(connected_nodes));
395
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);
399 }
400
401
402 /**
403  * \brief Remove the links in the created topology
404  *
405  * This is useful when the created topology is not connected, and you want
406  * to generate a new one.
407  */
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;
414   unsigned int i;
415
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);
420   }
421
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;
427   }
428 }
429
430 /**
431  * \brief Promote a node to a host
432  *
433  * This function should be called in callbacks registered with the
434  * platf_graph_promoter function.
435  *
436  * \param node The node to promote
437  * \param parameters The parameters needed to build the host
438  */
439 void platf_graph_promote_to_host(context_node_t node, sg_platf_host_cbarg_t parameters) {
440   node->kind = HOST;
441   memcpy(&(node->host_parameters), parameters, sizeof(s_sg_platf_host_cbarg_t));
442 }
443
444 /**
445  * \brief Promote a node to a cluster
446  *
447  * This function should be called in callbacks registered with the
448  * platf_graph_promoter function.
449  *
450  * \param node The node to promote
451  * \param parameters The parameters needed to build the cluster
452  */
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));
456 }
457
458 /**
459  * \brief Set the parameters of a network link.
460  *
461  * This function should be called in callbacks registered with the
462  * platf_graph_labeler function.
463  *
464  * \param edge The edge to modify
465  * \param parameters The parameters of the network link
466  */
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));
469 }
470
471 /**
472  * \brief Register a callback to promote nodes
473  *
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...
481  *
482  * \param promoter_callback The callback function
483  */
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);
487   }
488   xbt_dynar_push(promoter_dynar, &promoter_callback);
489 }
490
491 /**
492  * \brief Register a callback to label links
493  *
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...
500  *
501  * \param labeler_callback The callback function
502  */
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);
506   }
507   xbt_dynar_push(labeler_dynar, &labeler_callback);
508 }
509
510 /**
511  * \brief Call the registered promoters on all nodes
512  *
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
515  */
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;
521   unsigned int i, j;
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)
527         break;
528       promoter_callback(node);
529     }
530   }
531 }
532
533 /**
534  * \brief Call the registered labelers on all links
535  */
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;
541   unsigned int i, j;
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)
547         break;
548       labeler_callback(edge);
549     }
550   }
551 }
552
553 /**
554  * \brief putting into SURF the generated platform
555  *
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.
561  */
562 void platf_generate(void) {
563
564   xbt_dynar_t nodes = NULL;
565   xbt_node_t graph_node = NULL;
566   context_node_t node_data = NULL;
567   unsigned int i;
568
569   unsigned int last_host = 0;
570   unsigned int last_router = 0;
571   unsigned int last_cluster = 0;
572
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;
576
577   router_parameters.coord = NULL;
578
579   nodes = xbt_graph_get_nodes(platform_graph);
580
581   sg_platf_begin();
582   surf_parse_init_callbacks();
583   routing_register_callbacks();
584
585
586   sg_platf_new_AS_begin("random platform", A_surfxml_AS_routing_Floyd);
587
588   xbt_dynar_foreach(nodes, i, graph_node) {
589     node_data = xbt_graph_node_get_data(graph_node);
590     switch(node_data->kind) {
591       case HOST:
592         host_parameters = &node_data->host_parameters;
593         last_host++;
594         if(host_parameters->id == NULL) {
595           host_parameters->id = bprintf("host-%d", last_host);
596         }
597         sg_platf_new_host(host_parameters);
598         break;
599       case CLUSTER:
600         cluster_parameters = &node_data->cluster_parameters;
601         last_cluster++;
602         if(cluster_parameters->prefix == NULL) {
603           cluster_parameters->prefix = "host-";
604         }
605         if(cluster_parameters->suffix == NULL) {
606           cluster_parameters->suffix = bprintf(".cluster-%d", last_cluster);
607         }
608         if(cluster_parameters->id == NULL) {
609           cluster_parameters->id = bprintf("cluster-%d", last_cluster);
610         }
611         sg_platf_new_cluster(cluster_parameters);
612         break;
613       case ROUTER:
614         router_parameters.id = bprintf("router-%d", ++last_router);
615         sg_platf_new_router(&router_parameters);
616     }
617   }
618
619
620   sg_platf_new_AS_end();
621   sg_platf_end();
622 }
623
624 /* Functions used to generate interesting random values */
625
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
631   if (res>max) {
632     return max;
633   }
634   return res;
635 }