Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Platform generation : REALLY check if the graph is connected
[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 : shold be removed when it becomes useless
22   return platform_graph;
23 }
24
25 void platf_random_seed(unsigned long seed[6]) {
26
27   if(rng_stream == NULL) {
28     //stream not created yet, we do it now
29     rng_stream = RngStream_CreateStream(NULL);
30   }
31   if(seed != NULL) {
32     RngStream_SetSeed(rng_stream, seed);
33   }
34 }
35
36 void platf_graph_init(unsigned long node_count) {
37   unsigned long i;
38   platform_graph = xbt_graph_new_graph(FALSE, NULL);
39   if(rng_stream == NULL) {
40     rng_stream = RngStream_CreateStream(NULL);
41   }
42
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);
46     node_data->id = i+1;
47     node_data->x = 0;
48     node_data->y = 0;
49     node_data->degree = 0;
50     node_data->kind = ROUTER;
51     xbt_graph_new_node(platform_graph, (void*) node_data);
52   }
53
54   last_link_id = 0;
55
56 }
57
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);
63   node1_data->degree++;
64   node2_data->degree++;
65
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);
72 }
73
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;
77   double delta_x;
78   double delta_y;
79   double distance;
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);
85   return distance;
86 }
87
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;
92   unsigned int i;
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);
99   }
100 }
101
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;
106   unsigned int i;
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*/);
113   }
114 }
115
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;
121   unsigned int i;
122
123   dynar_nodes = xbt_graph_get_nodes(platform_graph);
124   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
125     if(i==0) {
126       //Ok, we get the first node, let's keep it somewhere...
127       first_node = graph_node;
128     } else {
129       //All the other nodes are connected to the first one
130       platf_node_connect(graph_node, first_node);
131     }
132   }
133 }
134
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;
140   unsigned int i;
141
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);
146     }
147     old_node = graph_node;
148   }
149 }
150
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;
157   unsigned int i;
158
159   dynar_nodes = xbt_graph_get_nodes(platform_graph);
160   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
161     if(i == 0) {
162       // this is the first node, let's keep it somewhere
163       first_node = graph_node;
164     } else {
165       //connect each node to the previous one
166       platf_node_connect(graph_node, old_node);
167     }
168     old_node = graph_node;
169   }
170   //we still have to connect the first and the last node together
171   platf_node_connect(first_node, graph_node);
172 }
173
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;
179   unsigned int i,j;
180
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);
185     }
186   }
187 }
188
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;
195   unsigned int i,j;
196
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) {
200       if(j>=i)
201         break;
202       if(RngStream_RandU01(rng_stream) < alpha) {
203         platf_node_connect(first_node, second_node);
204       }
205     }
206   }
207 }
208
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;
215   unsigned int i,j;
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) {
220       if(j>=i)
221         break;
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);
225       }
226     }
227   }
228 }
229
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)
233    *
234    * Number of edges increases with alpha
235    * edge length heterogeneity increases with beta
236    */
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   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) {
245       if(j>=i)
246         break;
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);
250       }
251     }
252   }
253 }
254
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.)
259    *
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)
263    */
264   xbt_dynar_t dynar_nodes = NULL;
265   xbt_node_t first_node = NULL;
266   xbt_node_t second_node = NULL;
267   unsigned int i,j;
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) {
271       if(j>=i)
272         break;
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);
277       }
278     }
279   }
280 }
281
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;
289   unsigned int i,j;
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) {
294       if(j>=i)
295         break;
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);
299         sum += 2;
300       }
301     }
302   }
303 }
304
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;
312   unsigned int i;
313   dynar_nodes = xbt_graph_get_nodes(platform_graph);
314   connected_nodes = xbt_dynar_new(sizeof(xbt_node_t), NULL);
315
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);
319   iterator = 0;
320   do {
321     //Get the next node
322     xbt_dynar_get_cpy(connected_nodes, iterator, &graph_node);
323
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);
331       }
332       if(!xbt_dynar_member(connected_nodes, &dst)) {
333         xbt_dynar_push(connected_nodes, &dst);
334       }
335     }
336   } while(++iterator < xbt_dynar_length(connected_nodes));
337
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);
341 }
342
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;
349   unsigned int i;
350
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);
355   }
356
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;
362   }
363 }
364
365 void platf_graph_promote_to_host(context_node_t node, sg_platf_host_cbarg_t parameters) {
366   node->kind = HOST;
367   memcpy(&(node->host_parameters), parameters, sizeof(s_sg_platf_host_cbarg_t));
368 }
369
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));
373 }
374
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));
377 }
378
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);
382   }
383   xbt_dynar_push(promoter_dynar, &promoter_callback);
384 }
385
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);
389   }
390   xbt_dynar_push(labeler_dynar, &labeler_callback);
391 }
392
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;
398   unsigned int i, j;
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)
404         break;
405       promoter_callback(node);
406     }
407   }
408 }
409
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;
415   unsigned int i, j;
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)
421         break;
422       labeler_callback(edge);
423     }
424   }
425 }
426
427 void platf_generate(void) {
428
429   xbt_dynar_t nodes = NULL;
430   xbt_node_t graph_node = NULL;
431   context_node_t node_data = NULL;
432   unsigned int i;
433
434   unsigned int last_host = 0;
435   unsigned int last_router = 0;
436   unsigned int last_cluster = 0;
437
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;
441
442   router_parameters.coord = NULL;
443
444   nodes = xbt_graph_get_nodes(platform_graph);
445
446   sg_platf_begin();
447   surf_parse_init_callbacks();
448   routing_register_callbacks();
449
450
451   sg_platf_new_AS_begin("random platform", A_surfxml_AS_routing_Floyd);
452
453   xbt_dynar_foreach(nodes, i, graph_node) {
454     node_data = xbt_graph_node_get_data(graph_node);
455     switch(node_data->kind) {
456       case HOST:
457         host_parameters = &node_data->host_parameters;
458         last_host++;
459         if(host_parameters->id == NULL) {
460           host_parameters->id = bprintf("host-%d", last_host);
461         }
462         sg_platf_new_host(host_parameters);
463         break;
464       case CLUSTER:
465         cluster_parameters = &node_data->cluster_parameters;
466         last_cluster++;
467         if(cluster_parameters->prefix == NULL) {
468           cluster_parameters->prefix = "host-";
469         }
470         if(cluster_parameters->suffix == NULL) {
471           cluster_parameters->suffix = bprintf(".cluster-%d", last_cluster);
472         }
473         if(cluster_parameters->id == NULL) {
474           cluster_parameters->id = bprintf("cluster-%d", last_cluster);
475         }
476         sg_platf_new_cluster(cluster_parameters);
477         break;
478       case ROUTER:
479         router_parameters.id = bprintf("router-%d", ++last_router);
480         sg_platf_new_router(&router_parameters);
481     }
482   }
483
484
485   sg_platf_new_AS_end();
486   sg_platf_end();
487 }
488
489 /* Functions used to generate interesting random values */
490
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
496   if (res>max) {
497     return max;
498   }
499   return res;
500 }