Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Platform generation : add the edge length in its data
[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 <math.h>
8
9 static xbt_graph_t platform_graph = NULL;
10 static xbt_dynar_t promoter_dynar = NULL;
11 static xbt_dynar_t labeler_dynar = NULL;
12
13 static RngStream rng_stream = NULL;
14
15 static unsigned long last_link_id = 0;
16
17 xbt_graph_t platf_graph_get(void) {
18   // We need some debug, so let's add this function
19   // WARNING : shold be removed when it becomes useless
20   return platform_graph;
21 }
22
23 void platf_random_seed(unsigned long seed[6]) {
24
25   if(rng_stream == NULL) {
26     //stream not created yet, we do it now
27     rng_stream = RngStream_CreateStream(NULL);
28   }
29   if(seed != NULL) {
30     RngStream_SetSeed(rng_stream, seed);
31   }
32 }
33
34 void platf_graph_init(unsigned long node_count) {
35   unsigned long i;
36   platform_graph = xbt_graph_new_graph(FALSE, NULL);
37   if(rng_stream == NULL) {
38     rng_stream = RngStream_CreateStream(NULL);
39   }
40
41   for(i=0 ; i<node_count ; i++) {
42     context_node_t node_data = NULL;
43     node_data = xbt_new0(s_context_node_t, 1);
44     node_data->id = i+1;
45     node_data->x = 0;
46     node_data->y = 0;
47     node_data->degree = 0;
48     node_data->kind = ROUTER;
49     xbt_graph_new_node(platform_graph, (void*) node_data);
50   }
51
52   last_link_id = 0;
53
54 }
55
56 void platf_node_connect(xbt_node_t node1, xbt_node_t node2) {
57   context_node_t node1_data;
58   context_node_t node2_data;
59   node1_data = (context_node_t) xbt_graph_node_get_data(node1);
60   node2_data = (context_node_t) xbt_graph_node_get_data(node2);
61   node1_data->degree++;
62   node2_data->degree++;
63
64   context_edge_t edge_data = NULL;
65   edge_data = xbt_new0(s_context_edge_t, 1);
66   edge_data->id = ++last_link_id;
67   edge_data->length = platf_node_distance(node1, node2);
68   edge_data->labeled = FALSE;
69   xbt_graph_new_edge(platform_graph, node1, node2, (void*)edge_data);
70 }
71
72 double platf_node_distance(xbt_node_t node1, xbt_node_t node2) {
73   context_node_t node1_data;
74   context_node_t node2_data;
75   double delta_x;
76   double delta_y;
77   double distance;
78   node1_data = (context_node_t) xbt_graph_node_get_data(node1);
79   node2_data = (context_node_t) xbt_graph_node_get_data(node2);
80   delta_x = node1_data->x - node2_data->x;
81   delta_y = node1_data->y - node2_data->y;
82   distance = sqrt(delta_x*delta_x + delta_y*delta_y);
83   return distance;
84 }
85
86 void platf_graph_uniform(unsigned long node_count) {
87   xbt_dynar_t dynar_nodes = NULL;
88   xbt_node_t graph_node = NULL;
89   context_node_t node_data = NULL;
90   unsigned int i;
91   platf_graph_init(node_count);
92   dynar_nodes = xbt_graph_get_nodes(platform_graph);
93   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
94     node_data = (context_node_t) xbt_graph_node_get_data(graph_node);
95     node_data->x = RngStream_RandU01(rng_stream);
96     node_data->y = RngStream_RandU01(rng_stream);
97   }
98 }
99
100 void platf_graph_heavytailed(unsigned long node_count) {
101   xbt_dynar_t dynar_nodes = NULL;
102   xbt_node_t graph_node = NULL;
103   context_node_t node_data = NULL;
104   unsigned int i;
105   platf_graph_init(node_count);
106   dynar_nodes = xbt_graph_get_nodes(platform_graph);
107   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
108     node_data = (context_node_t) xbt_graph_node_get_data(graph_node);
109     node_data->x = random_pareto(0, 1, 1.0/*K*/, 10e9/*P*/, 1.0/*alpha*/);
110     node_data->y = random_pareto(0, 1, 1.0/*K*/, 10e9/*P*/, 1.0/*alpha*/);
111   }
112 }
113
114 void platf_graph_interconnect_star(void) {
115   /* All the nodes are connected to the first one */
116   xbt_dynar_t dynar_nodes = NULL;
117   xbt_node_t graph_node = NULL;
118   xbt_node_t first_node = NULL;
119   unsigned int i;
120
121   dynar_nodes = xbt_graph_get_nodes(platform_graph);
122   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
123     if(i==0) {
124       //Ok, we get the first node, let's keep it somewhere...
125       first_node = graph_node;
126     } else {
127       //All the other nodes are connected to the first one
128       platf_node_connect(graph_node, first_node);
129     }
130   }
131 }
132
133 void platf_graph_interconnect_line(void) {
134   /* Node are connected to the previous and the next node, in a line */
135   xbt_dynar_t dynar_nodes = NULL;
136   xbt_node_t graph_node = NULL;
137   xbt_node_t old_node = NULL;
138   unsigned int i;
139
140   dynar_nodes = xbt_graph_get_nodes(platform_graph);
141   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
142     if(old_node != NULL) {
143       platf_node_connect(graph_node, old_node);
144     }
145     old_node = graph_node;
146   }
147 }
148
149 void platf_graph_interconnect_ring(void) {
150   /* Create a simple topology where all nodes are connected along a ring */
151   xbt_dynar_t dynar_nodes = NULL;
152   xbt_node_t graph_node = NULL;
153   xbt_node_t old_node = NULL;
154   xbt_node_t first_node = NULL;
155   unsigned int i;
156
157   dynar_nodes = xbt_graph_get_nodes(platform_graph);
158   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
159     if(i == 0) {
160       // this is the first node, let's keep it somewhere
161       first_node = graph_node;
162     } else {
163       //connect each node to the previous one
164       platf_node_connect(graph_node, old_node);
165     }
166     old_node = graph_node;
167   }
168   //we still have to connect the first and the last node together
169   platf_node_connect(first_node, graph_node);
170 }
171
172 void platf_graph_interconnect_clique(void) {
173   /* Create a simple topology where all nodes are connected to each other, in a clique manner */
174   xbt_dynar_t dynar_nodes = NULL;
175   xbt_node_t first_node = NULL;
176   xbt_node_t second_node = NULL;
177   unsigned int i,j;
178
179   dynar_nodes = xbt_graph_get_nodes(platform_graph);
180   xbt_dynar_foreach(dynar_nodes, i, first_node) {
181     xbt_dynar_foreach(dynar_nodes, j, second_node) {
182       platf_node_connect(first_node, second_node);
183     }
184   }
185 }
186
187 void platf_graph_interconnect_uniform(double alpha) {
188   /* Creates a topology where the probability to connect two nodes is uniform (unrealistic, but simple)
189      alpha : Probability for two nodes to get connected */
190   xbt_dynar_t dynar_nodes = NULL;
191   xbt_node_t first_node = NULL;
192   xbt_node_t second_node = NULL;
193   unsigned int i,j;
194
195   dynar_nodes = xbt_graph_get_nodes(platform_graph);
196   xbt_dynar_foreach(dynar_nodes, i, first_node) {
197     xbt_dynar_foreach(dynar_nodes, j, second_node) {
198       if(j>=i)
199         break;
200       if(RngStream_RandU01(rng_stream) < alpha) {
201         platf_node_connect(first_node, second_node);
202       }
203     }
204   }
205 }
206
207 void platf_graph_interconnect_exponential(double alpha) {
208   /* Create a topology where the probability follows an exponential law
209      Number of edges increases with alpha */
210   xbt_dynar_t dynar_nodes = NULL;
211   xbt_node_t first_node = NULL;
212   xbt_node_t second_node = NULL;
213   unsigned int i,j;
214   double L = sqrt(2.0); /*  L = c*sqrt(2); c=side of placement square */
215   dynar_nodes = xbt_graph_get_nodes(platform_graph);
216   xbt_dynar_foreach(dynar_nodes, i, first_node) {
217     xbt_dynar_foreach(dynar_nodes, j, second_node) {
218       if(j>=i)
219         break;
220       double d = platf_node_distance(first_node, second_node);
221       if(RngStream_RandU01(rng_stream) < alpha*exp(-d/(L-d))) {
222         platf_node_connect(first_node, second_node);
223       }
224     }
225   }
226 }
227
228 void platf_graph_interconnect_waxman(double alpha, double beta) {
229   /* Create a topology where the probability follows the model of Waxman
230    * (see Waxman, Routing of Multipoint Connections, IEEE J. on Selected Areas in Comm., 1988)
231    *
232    * Number of edges increases with alpha
233    * edge length heterogeneity increases with beta
234    */
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   double L = sqrt(2.0); /*  L = c*sqrt(2); c=side of placement square */
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       double d = platf_node_distance(first_node, second_node);
246       if(RngStream_RandU01(rng_stream) < alpha*exp(-d/(L*beta))) {
247         platf_node_connect(first_node, second_node);
248       }
249     }
250   }
251 }
252
253 void platf_graph_interconnect_zegura(double alpha, double beta, double r) {
254   /* Create a topology where the probability follows the model of Zegura
255    * (see Zegura, Calvert, Donahoo, A quantitative comparison of graph-based models
256    * for Internet topology, IEEE/ACM Transactions on Networking, 1997.)
257    *
258    * alpha : Probability of connexion for short edges
259    * beta : Probability of connexion for long edges
260    * r : Limit between long and short edges (between 0 and sqrt(2) since nodes are placed on the unit square)
261    */
262   xbt_dynar_t dynar_nodes = NULL;
263   xbt_node_t first_node = NULL;
264   xbt_node_t second_node = NULL;
265   unsigned int i,j;
266   dynar_nodes = xbt_graph_get_nodes(platform_graph);
267   xbt_dynar_foreach(dynar_nodes, i, first_node) {
268     xbt_dynar_foreach(dynar_nodes, j, second_node) {
269       if(j>=i)
270         break;
271       double d = platf_node_distance(first_node, second_node);
272       double proba = d < r ? alpha : beta;
273       if(RngStream_RandU01(rng_stream) < proba) {
274         platf_node_connect(first_node, second_node);
275       }
276     }
277   }
278 }
279
280 void platf_graph_interconnect_barabasi(void) {
281   /* Create a topology constructed according to the Barabasi-Albert algorithm (follows power laws)
282      (see Barabasi and Albert, Emergence of scaling in random networks, Science 1999, num 59, p509­-512.) */
283   xbt_dynar_t dynar_nodes = NULL;
284   xbt_node_t first_node = NULL;
285   xbt_node_t second_node = NULL;
286   context_node_t node_data = NULL;
287   unsigned int i,j;
288   unsigned long sum = 0;
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       node_data = xbt_graph_node_get_data(second_node);
295       if(sum==0 || RngStream_RandU01(rng_stream) < ((double)(node_data->degree)/ (double)sum)) {
296         platf_node_connect(first_node, second_node);
297         sum += 2;
298       }
299     }
300   }
301 }
302
303 void platf_graph_promote_to_host(context_node_t node, sg_platf_host_cbarg_t parameters) {
304   node->kind = HOST;
305   memcpy(&(node->host_parameters), parameters, sizeof(s_sg_platf_host_cbarg_t));
306 }
307
308 void platf_graph_promote_to_cluster(context_node_t node, sg_platf_cluster_cbarg_t parameters) {
309   node->kind = CLUSTER;
310   memcpy(&(node->cluster_parameters), parameters, sizeof(s_sg_platf_cluster_cbarg_t));
311 }
312
313 void platf_graph_link_label(context_edge_t edge, sg_platf_link_cbarg_t parameters) {
314   memcpy(&(edge->link_parameters), parameters, sizeof(s_sg_platf_link_cbarg_t));
315 }
316
317 void platf_graph_promoter(platf_promoter_cb_t promoter_callback) {
318   if(promoter_dynar == NULL) {
319     promoter_dynar = xbt_dynar_new(sizeof(platf_promoter_cb_t), NULL);
320   }
321   xbt_dynar_push(promoter_dynar, &promoter_callback);
322 }
323
324 void platf_graph_labeler(platf_labeler_cb_t labeler_callback) {
325   if(labeler_dynar == NULL) {
326     labeler_dynar = xbt_dynar_new(sizeof(void*), NULL);
327   }
328   xbt_dynar_push(labeler_dynar, &labeler_callback);
329 }
330
331 void platf_do_promote(void) {
332   platf_promoter_cb_t promoter_callback;
333   xbt_node_t graph_node = NULL;
334   xbt_dynar_t dynar_nodes = NULL;
335   context_node_t node = NULL;
336   unsigned int i, j;
337   dynar_nodes = xbt_graph_get_nodes(platform_graph);
338   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
339     node = (context_node_t) xbt_graph_node_get_data(graph_node);
340     xbt_dynar_foreach(promoter_dynar, j, promoter_callback) {
341       if(node->kind != ROUTER)
342         break;
343       promoter_callback(node);
344     }
345   }
346 }
347
348 void platf_do_label(void) {
349   platf_labeler_cb_t labeler_callback;
350   xbt_edge_t graph_edge = NULL;
351   xbt_dynar_t dynar_edges = NULL;
352   context_edge_t edge = NULL;
353   unsigned int i, j;
354   dynar_edges = xbt_graph_get_edges(platform_graph);
355   xbt_dynar_foreach(dynar_edges, i, graph_edge) {
356     edge = (context_edge_t) xbt_graph_edge_get_data(graph_edge);
357     xbt_dynar_foreach(promoter_dynar, j, labeler_callback) {
358       if(edge->labeled == TRUE)
359         break;
360       labeler_callback(edge);
361     }
362   }
363 }
364
365 /* Functions used to generate interesting random values */
366
367 double random_pareto(double min, double max, double K, double P, double ALPHA) {
368   double x = RngStream_RandU01(rng_stream);
369   double den = pow(1.0 - x + x*pow(K/P, ALPHA), 1.0/ALPHA);
370   double res = (1/den);
371   res += min - 1; // pareto is on [1, infinity) by default
372   if (res>max) {
373     return max;
374   }
375   return res;
376 }