3 #include "simgrid/platf_generator.h"
4 #include "platf_generator_private.h"
6 #include "xbt/RngStream.h"
9 static xbt_graph_t platform_graph = NULL;
10 static xbt_dynar_t promoter_dynar = NULL;
11 static xbt_dynar_t labeler_dynar = NULL;
13 static RngStream rng_stream = NULL;
15 static unsigned long last_link_id = 0;
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;
23 void platf_random_seed(unsigned long seed[6]) {
25 if(rng_stream == NULL) {
26 //stream not created yet, we do it now
27 rng_stream = RngStream_CreateStream(NULL);
30 RngStream_SetSeed(rng_stream, seed);
34 void platf_graph_init(unsigned long node_count) {
36 platform_graph = xbt_graph_new_graph(FALSE, NULL);
37 if(rng_stream == NULL) {
38 rng_stream = RngStream_CreateStream(NULL);
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);
47 node_data->degree = 0;
48 node_data->kind = ROUTER;
49 xbt_graph_new_node(platform_graph, (void*) node_data);
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);
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);
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;
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);
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;
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);
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;
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*/);
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;
120 dynar_nodes = xbt_graph_get_nodes(platform_graph);
121 xbt_dynar_foreach(dynar_nodes, i, graph_node) {
123 //Ok, we get the first node, let's keep it somewhere...
124 first_node = graph_node;
126 //All the other nodes are connected to the first one
127 platf_node_connect(graph_node, first_node);
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;
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);
144 old_node = graph_node;
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;
156 dynar_nodes = xbt_graph_get_nodes(platform_graph);
157 xbt_dynar_foreach(dynar_nodes, i, graph_node) {
159 // this is the first node, let's keep it somewhere
160 first_node = graph_node;
162 //connect each node to the previous one
163 platf_node_connect(graph_node, old_node);
165 old_node = graph_node;
167 //we still have to connect the first and the last node together
168 platf_node_connect(first_node, graph_node);
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;
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);
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;
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) {
199 if(RngStream_RandU01(rng_stream) < alpha) {
200 platf_node_connect(first_node, second_node);
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;
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) {
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);
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;
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) {
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);
250 void platf_graph_promote_to_host(context_node_t node, sg_platf_host_cbarg_t parameters) {
252 memcpy(&(node->host_parameters), parameters, sizeof(s_sg_platf_host_cbarg_t));
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));
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));
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);
268 xbt_dynar_push(promoter_dynar, &promoter_callback);
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);
275 xbt_dynar_push(labeler_dynar, &labeler_callback);
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;
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)
290 promoter_callback(node);
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;
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)
307 labeler_callback(edge);
312 /* Functions used to generate interesting random values */
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