Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Platform generation : add the Barabasi-Albert algorithm to build the graph
[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->labeled = FALSE;
68   xbt_graph_new_edge(platform_graph, node1, node2, (void*)edge_data);
69 }
70
71 double platf_node_distance(xbt_node_t node1, xbt_node_t node2) {
72   context_node_t node1_data;
73   context_node_t node2_data;
74   double delta_x;
75   double delta_y;
76   double distance;
77   node1_data = (context_node_t) xbt_graph_node_get_data(node1);
78   node2_data = (context_node_t) xbt_graph_node_get_data(node2);
79   delta_x = node1_data->x - node2_data->x;
80   delta_y = node1_data->y - node2_data->y;
81   distance = sqrt(delta_x*delta_x + delta_y*delta_y);
82   return distance;
83 }
84
85 void platf_graph_uniform(unsigned long node_count) {
86   xbt_dynar_t dynar_nodes = NULL;
87   xbt_node_t graph_node = NULL;
88   context_node_t node_data = NULL;
89   unsigned int i;
90   platf_graph_init(node_count);
91   dynar_nodes = xbt_graph_get_nodes(platform_graph);
92   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
93     node_data = (context_node_t) xbt_graph_node_get_data(graph_node);
94     node_data->x = RngStream_RandU01(rng_stream);
95     node_data->y = RngStream_RandU01(rng_stream);
96   }
97 }
98
99 void platf_graph_heavytailed(unsigned long node_count) {
100   xbt_dynar_t dynar_nodes = NULL;
101   xbt_node_t graph_node = NULL;
102   context_node_t node_data = NULL;
103   unsigned int i;
104   platf_graph_init(node_count);
105   dynar_nodes = xbt_graph_get_nodes(platform_graph);
106   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
107     node_data = (context_node_t) xbt_graph_node_get_data(graph_node);
108     node_data->x = random_pareto(0, 1, 1.0/*K*/, 10e9/*P*/, 1.0/*alpha*/);
109     node_data->y = random_pareto(0, 1, 1.0/*K*/, 10e9/*P*/, 1.0/*alpha*/);
110   }
111 }
112
113 void platf_graph_interconnect_star(void) {
114   /* All the nodes are connected to the first one */
115   xbt_dynar_t dynar_nodes = NULL;
116   xbt_node_t graph_node = NULL;
117   xbt_node_t first_node = NULL;
118   unsigned int i;
119
120   dynar_nodes = xbt_graph_get_nodes(platform_graph);
121   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
122     if(i==0) {
123       //Ok, we get the first node, let's keep it somewhere...
124       first_node = graph_node;
125     } else {
126       //All the other nodes are connected to the first one
127       platf_node_connect(graph_node, first_node);
128     }
129   }
130 }
131
132 void platf_graph_interconnect_line(void) {
133   /* Node are connected to the previous and the next node, in a line */
134   xbt_dynar_t dynar_nodes = NULL;
135   xbt_node_t graph_node = NULL;
136   xbt_node_t old_node = NULL;
137   unsigned int i;
138
139   dynar_nodes = xbt_graph_get_nodes(platform_graph);
140   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
141     if(old_node != NULL) {
142       platf_node_connect(graph_node, old_node);
143     }
144     old_node = graph_node;
145   }
146 }
147
148 void platf_graph_interconnect_ring(void) {
149   /* Create a simple topology where all nodes are connected along a ring */
150   xbt_dynar_t dynar_nodes = NULL;
151   xbt_node_t graph_node = NULL;
152   xbt_node_t old_node = NULL;
153   xbt_node_t first_node = NULL;
154   unsigned int i;
155
156   dynar_nodes = xbt_graph_get_nodes(platform_graph);
157   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
158     if(i == 0) {
159       // this is the first node, let's keep it somewhere
160       first_node = graph_node;
161     } else {
162       //connect each node to the previous one
163       platf_node_connect(graph_node, old_node);
164     }
165     old_node = graph_node;
166   }
167   //we still have to connect the first and the last node together
168   platf_node_connect(first_node, graph_node);
169 }
170
171 void platf_graph_interconnect_clique(void) {
172   /* Create a simple topology where all nodes are connected to each other, in a clique manner */
173   xbt_dynar_t dynar_nodes = NULL;
174   xbt_node_t first_node = NULL;
175   xbt_node_t second_node = NULL;
176   unsigned int i,j;
177
178   dynar_nodes = xbt_graph_get_nodes(platform_graph);
179   xbt_dynar_foreach(dynar_nodes, i, first_node) {
180     xbt_dynar_foreach(dynar_nodes, j, second_node) {
181       platf_node_connect(first_node, second_node);
182     }
183   }
184 }
185
186 void platf_graph_interconnect_uniform(double alpha) {
187   /* Creates a topology where the probability to connect two nodes is uniform (unrealistic, but simple)
188      alpha : Probability for two nodes to get connected */
189   xbt_dynar_t dynar_nodes = NULL;
190   xbt_node_t first_node = NULL;
191   xbt_node_t second_node = NULL;
192   unsigned int i,j;
193
194   dynar_nodes = xbt_graph_get_nodes(platform_graph);
195   xbt_dynar_foreach(dynar_nodes, i, first_node) {
196     xbt_dynar_foreach(dynar_nodes, j, second_node) {
197       if(j>=i)
198         break;
199       if(RngStream_RandU01(rng_stream) < alpha) {
200         platf_node_connect(first_node, second_node);
201       }
202     }
203   }
204 }
205
206 void platf_graph_interconnect_exponential(double alpha) {
207   /* Create a topology where the probability follows an exponential law
208      Number of edges increases with alpha */
209   xbt_dynar_t dynar_nodes = NULL;
210   xbt_node_t first_node = NULL;
211   xbt_node_t second_node = NULL;
212   unsigned int i,j;
213   double L = sqrt(2.0); /*  L = c*sqrt(2); c=side of placement square */
214   dynar_nodes = xbt_graph_get_nodes(platform_graph);
215   xbt_dynar_foreach(dynar_nodes, i, first_node) {
216     xbt_dynar_foreach(dynar_nodes, j, second_node) {
217       if(j>=i)
218         break;
219       double d = platf_node_distance(first_node, second_node);
220       if(RngStream_RandU01(rng_stream) < alpha*exp(-d/(L-d))) {
221         platf_node_connect(first_node, second_node);
222       }
223     }
224   }
225 }
226
227 void platf_graph_interconnect_barabasi(void) {
228   /* Create a topology constructed according to the Barabasi-Albert algorithm (follows power laws)
229      (see Barabasi and Albert, Emergence of scaling in random networks, Science 1999, num 59, p509­-512.) */
230   xbt_dynar_t dynar_nodes = NULL;
231   xbt_node_t first_node = NULL;
232   xbt_node_t second_node = NULL;
233   context_node_t node_data = NULL;
234   unsigned int i,j;
235   unsigned long sum = 0;
236   dynar_nodes = xbt_graph_get_nodes(platform_graph);
237   xbt_dynar_foreach(dynar_nodes, i, first_node) {
238     xbt_dynar_foreach(dynar_nodes, j, second_node) {
239       if(j>=i)
240         break;
241       node_data = xbt_graph_node_get_data(second_node);
242       if(sum==0 || RngStream_RandU01(rng_stream) < ((double)(node_data->degree)/ (double)sum)) {
243         platf_node_connect(first_node, second_node);
244         sum += 2;
245       }
246     }
247   }
248 }
249
250 void platf_graph_promote_to_host(context_node_t node, sg_platf_host_cbarg_t parameters) {
251   node->kind = HOST;
252   memcpy(&(node->host_parameters), parameters, sizeof(s_sg_platf_host_cbarg_t));
253 }
254
255 void platf_graph_promote_to_cluster(context_node_t node, sg_platf_cluster_cbarg_t parameters) {
256   node->kind = CLUSTER;
257   memcpy(&(node->cluster_parameters), parameters, sizeof(s_sg_platf_cluster_cbarg_t));
258 }
259
260 void platf_graph_link_label(context_edge_t edge, sg_platf_link_cbarg_t parameters) {
261   memcpy(&(edge->link_parameters), parameters, sizeof(s_sg_platf_link_cbarg_t));
262 }
263
264 void platf_graph_promoter(platf_promoter_cb_t promoter_callback) {
265   if(promoter_dynar == NULL) {
266     promoter_dynar = xbt_dynar_new(sizeof(platf_promoter_cb_t), NULL);
267   }
268   xbt_dynar_push(promoter_dynar, &promoter_callback);
269 }
270
271 void platf_graph_labeler(platf_labeler_cb_t labeler_callback) {
272   if(labeler_dynar == NULL) {
273     labeler_dynar = xbt_dynar_new(sizeof(void*), NULL);
274   }
275   xbt_dynar_push(labeler_dynar, &labeler_callback);
276 }
277
278 void platf_do_promote(void) {
279   platf_promoter_cb_t promoter_callback;
280   xbt_node_t graph_node = NULL;
281   xbt_dynar_t dynar_nodes = NULL;
282   context_node_t node = NULL;
283   unsigned int i, j;
284   dynar_nodes = xbt_graph_get_nodes(platform_graph);
285   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
286     node = (context_node_t) xbt_graph_node_get_data(graph_node);
287     xbt_dynar_foreach(promoter_dynar, j, promoter_callback) {
288       if(node->kind != ROUTER)
289         break;
290       promoter_callback(node);
291     }
292   }
293 }
294
295 void platf_do_label(void) {
296   platf_labeler_cb_t labeler_callback;
297   xbt_edge_t graph_edge = NULL;
298   xbt_dynar_t dynar_edges = NULL;
299   context_edge_t edge = NULL;
300   unsigned int i, j;
301   dynar_edges = xbt_graph_get_edges(platform_graph);
302   xbt_dynar_foreach(dynar_edges, i, graph_edge) {
303     edge = (context_edge_t) xbt_graph_edge_get_data(graph_edge);
304     xbt_dynar_foreach(promoter_dynar, j, labeler_callback) {
305       if(edge->labeled == TRUE)
306         break;
307       labeler_callback(edge);
308     }
309   }
310 }
311
312 /* Functions used to generate interesting random values */
313
314 double random_pareto(double min, double max, double K, double P, double ALPHA) {
315   double x = RngStream_RandU01(rng_stream);
316   double den = pow(1.0 - x + x*pow(K/P, ALPHA), 1.0/ALPHA);
317   double res = (1/den);
318   res += min - 1; // pareto is on [1, infinity) by default
319   if (res>max) {
320     return max;
321   }
322   return res;
323 }