3 #include "simgrid/platf_generator.h"
4 #include "platf_generator_private.h"
6 #include "xbt/RngStream.h"
7 #include "surf/simgrid_dtd.h"
8 #include "surf_private.h"
11 static xbt_graph_t platform_graph = NULL;
12 static xbt_dynar_t promoter_dynar = NULL;
13 static xbt_dynar_t labeler_dynar = NULL;
15 static RngStream rng_stream = NULL;
17 static unsigned long last_link_id = 0;
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;
25 void platf_random_seed(unsigned long seed[6]) {
27 if(rng_stream == NULL) {
28 //stream not created yet, we do it now
29 rng_stream = RngStream_CreateStream(NULL);
32 RngStream_SetSeed(rng_stream, seed);
36 void platf_graph_init(unsigned long node_count) {
38 platform_graph = xbt_graph_new_graph(FALSE, NULL);
39 if(rng_stream == NULL) {
40 rng_stream = RngStream_CreateStream(NULL);
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);
49 node_data->degree = 0;
50 node_data->kind = ROUTER;
51 xbt_graph_new_node(platform_graph, (void*) node_data);
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);
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);
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;
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);
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;
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);
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;
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*/);
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;
123 dynar_nodes = xbt_graph_get_nodes(platform_graph);
124 xbt_dynar_foreach(dynar_nodes, i, graph_node) {
126 //Ok, we get the first node, let's keep it somewhere...
127 first_node = graph_node;
129 //All the other nodes are connected to the first one
130 platf_node_connect(graph_node, first_node);
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;
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);
147 old_node = graph_node;
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;
159 dynar_nodes = xbt_graph_get_nodes(platform_graph);
160 xbt_dynar_foreach(dynar_nodes, i, graph_node) {
162 // this is the first node, let's keep it somewhere
163 first_node = graph_node;
165 //connect each node to the previous one
166 platf_node_connect(graph_node, old_node);
168 old_node = graph_node;
170 //we still have to connect the first and the last node together
171 platf_node_connect(first_node, graph_node);
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;
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);
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;
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) {
202 if(RngStream_RandU01(rng_stream) < alpha) {
203 platf_node_connect(first_node, second_node);
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;
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) {
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);
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)
234 * Number of edges increases with alpha
235 * edge length heterogeneity increases with beta
237 xbt_dynar_t dynar_nodes = NULL;
238 xbt_node_t first_node = NULL;
239 xbt_node_t second_node = NULL;
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) {
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);
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.)
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)
264 xbt_dynar_t dynar_nodes = NULL;
265 xbt_node_t first_node = NULL;
266 xbt_node_t second_node = NULL;
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) {
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);
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;
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) {
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);
305 void platf_graph_promote_to_host(context_node_t node, sg_platf_host_cbarg_t parameters) {
307 memcpy(&(node->host_parameters), parameters, sizeof(s_sg_platf_host_cbarg_t));
310 void platf_graph_promote_to_cluster(context_node_t node, sg_platf_cluster_cbarg_t parameters) {
311 node->kind = CLUSTER;
312 memcpy(&(node->cluster_parameters), parameters, sizeof(s_sg_platf_cluster_cbarg_t));
315 void platf_graph_link_label(context_edge_t edge, sg_platf_link_cbarg_t parameters) {
316 memcpy(&(edge->link_parameters), parameters, sizeof(s_sg_platf_link_cbarg_t));
319 void platf_graph_promoter(platf_promoter_cb_t promoter_callback) {
320 if(promoter_dynar == NULL) {
321 promoter_dynar = xbt_dynar_new(sizeof(platf_promoter_cb_t), NULL);
323 xbt_dynar_push(promoter_dynar, &promoter_callback);
326 void platf_graph_labeler(platf_labeler_cb_t labeler_callback) {
327 if(labeler_dynar == NULL) {
328 labeler_dynar = xbt_dynar_new(sizeof(void*), NULL);
330 xbt_dynar_push(labeler_dynar, &labeler_callback);
333 void platf_do_promote(void) {
334 platf_promoter_cb_t promoter_callback;
335 xbt_node_t graph_node = NULL;
336 xbt_dynar_t dynar_nodes = NULL;
337 context_node_t node = NULL;
339 dynar_nodes = xbt_graph_get_nodes(platform_graph);
340 xbt_dynar_foreach(dynar_nodes, i, graph_node) {
341 node = (context_node_t) xbt_graph_node_get_data(graph_node);
342 xbt_dynar_foreach(promoter_dynar, j, promoter_callback) {
343 if(node->kind != ROUTER)
345 promoter_callback(node);
350 void platf_do_label(void) {
351 platf_labeler_cb_t labeler_callback;
352 xbt_edge_t graph_edge = NULL;
353 xbt_dynar_t dynar_edges = NULL;
354 context_edge_t edge = NULL;
356 dynar_edges = xbt_graph_get_edges(platform_graph);
357 xbt_dynar_foreach(dynar_edges, i, graph_edge) {
358 edge = (context_edge_t) xbt_graph_edge_get_data(graph_edge);
359 xbt_dynar_foreach(promoter_dynar, j, labeler_callback) {
360 if(edge->labeled == TRUE)
362 labeler_callback(edge);
367 void platf_generate(void) {
369 xbt_dynar_t nodes = NULL;
370 xbt_node_t graph_node = NULL;
371 context_node_t node_data = NULL;
374 unsigned int last_host = 0;
375 unsigned int last_router = 0;
376 unsigned int last_cluster = 0;
378 sg_platf_host_cbarg_t host_parameters;
379 s_sg_platf_router_cbarg_t router_parameters; /* This one is not a pointer! */
380 sg_platf_cluster_cbarg_t cluster_parameters;
382 router_parameters.coord = NULL;
384 nodes = xbt_graph_get_nodes(platform_graph);
387 surf_parse_init_callbacks();
388 routing_register_callbacks();
391 sg_platf_new_AS_begin("random platform", A_surfxml_AS_routing_Floyd);
393 xbt_dynar_foreach(nodes, i, graph_node) {
394 node_data = xbt_graph_node_get_data(graph_node);
395 switch(node_data->kind) {
397 host_parameters = &node_data->host_parameters;
399 if(host_parameters->id == NULL) {
400 host_parameters->id = bprintf("host-%d", last_host);
402 sg_platf_new_host(host_parameters);
405 cluster_parameters = &node_data->cluster_parameters;
407 if(cluster_parameters->prefix == NULL) {
408 cluster_parameters->prefix = "host-";
410 if(cluster_parameters->suffix == NULL) {
411 cluster_parameters->suffix = bprintf(".cluster-%d", last_cluster);
413 if(cluster_parameters->id == NULL) {
414 cluster_parameters->id = bprintf("cluster-%d", last_cluster);
416 sg_platf_new_cluster(cluster_parameters);
419 router_parameters.id = bprintf("router-%d", ++last_router);
420 sg_platf_new_router(&router_parameters);
425 sg_platf_new_AS_end();
429 /* Functions used to generate interesting random values */
431 double random_pareto(double min, double max, double K, double P, double ALPHA) {
432 double x = RngStream_RandU01(rng_stream);
433 double den = pow(1.0 - x + x*pow(K/P, ALPHA), 1.0/ALPHA);
434 double res = (1/den);
435 res += min - 1; // pareto is on [1, infinity) by default