Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
693e169576a2ada05af2f9a5892f155e72a58612
[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
11 static RngStream rng_stream = NULL;
12
13 static unsigned long last_link_id = 0;
14
15 xbt_graph_t platf_graph_get(void) {
16   // We need some debug, so let's add this function
17   // WARNING : shold be removed when it becomes useless
18   return platform_graph;
19 }
20
21 void platf_random_seed(unsigned long seed[6]) {
22
23   if(rng_stream == NULL) {
24     //stream not created yet, we do it now
25     rng_stream = RngStream_CreateStream(NULL);
26   }
27   if(seed != NULL) {
28     RngStream_SetSeed(rng_stream, seed);
29   }
30 }
31
32 void platf_graph_init(unsigned long node_count) {
33   unsigned long i;
34   platform_graph = xbt_graph_new_graph(FALSE, NULL);
35   if(rng_stream == NULL) {
36     rng_stream = RngStream_CreateStream(NULL);
37   }
38
39   for(i=0 ; i<node_count ; i++) {
40     context_node_t node_data = NULL;
41     node_data = xbt_new(s_context_node_t, 1);
42     node_data->id = i+1;
43     node_data->x = 0;
44     node_data->y = 0;
45     node_data->degree = 0;
46     node_data->kind = ROUTER;
47     xbt_graph_new_node(platform_graph, (void*) node_data);
48   }
49
50   last_link_id = 0;
51
52 }
53
54 void platf_node_connect(xbt_node_t node1, xbt_node_t node2) {
55   context_node_t node1_data;
56   context_node_t node2_data;
57   node1_data = (context_node_t) xbt_graph_node_get_data(node1);
58   node2_data = (context_node_t) xbt_graph_node_get_data(node2);
59   node1_data->degree++;
60   node2_data->degree++;
61
62   unsigned long *link_id = xbt_new(unsigned long, 1);
63   *link_id = ++last_link_id;
64
65   xbt_graph_new_edge(platform_graph, node1, node2, (void*)link_id);
66 }
67
68 void platf_graph_uniform(unsigned long node_count) {
69   xbt_dynar_t dynar_nodes = NULL;
70   xbt_node_t graph_node = NULL;
71   context_node_t node_data = NULL;
72   unsigned int i;
73   platf_graph_init(node_count);
74   dynar_nodes = xbt_graph_get_nodes(platform_graph);
75   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
76     node_data = (context_node_t) xbt_graph_node_get_data(graph_node);
77     node_data->x = RngStream_RandU01(rng_stream);
78     node_data->y = RngStream_RandU01(rng_stream);
79   }
80 }
81
82 void platf_graph_heavytailed(unsigned long node_count) {
83   xbt_dynar_t dynar_nodes = NULL;
84   xbt_node_t graph_node = NULL;
85   context_node_t node_data = NULL;
86   unsigned int i;
87   platf_graph_init(node_count);
88   dynar_nodes = xbt_graph_get_nodes(platform_graph);
89   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
90     node_data = (context_node_t) xbt_graph_node_get_data(graph_node);
91     node_data->x = random_pareto(0, 1, 1.0/*K*/, 10e9/*P*/, 1.0/*alpha*/);
92     node_data->y = random_pareto(0, 1, 1.0/*K*/, 10e9/*P*/, 1.0/*alpha*/);
93   }
94 }
95
96 void platf_graph_interconnect_star(void) {
97   /* All the nodes are connected to the first one */
98   xbt_dynar_t dynar_nodes = NULL;
99   xbt_node_t graph_node = NULL;
100   xbt_node_t first_node = NULL;
101   unsigned int i;
102
103   dynar_nodes = xbt_graph_get_nodes(platform_graph);
104   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
105     if(i==0) {
106       //Ok, we get the first node, let's keep it somewhere...
107       first_node = graph_node;
108     } else {
109       //All the other nodes are connected to the first one
110       platf_node_connect(graph_node, first_node);
111     }
112   }
113 }
114
115 void platf_graph_interconnect_line(void) {
116   /* Node are connected to the previous and the next node, in a line */
117   xbt_dynar_t dynar_nodes = NULL;
118   xbt_node_t graph_node = NULL;
119   xbt_node_t old_node = NULL;
120   unsigned int i;
121
122   dynar_nodes = xbt_graph_get_nodes(platform_graph);
123   xbt_dynar_foreach(dynar_nodes, i, graph_node) {
124     if(old_node != NULL) {
125       platf_node_connect(graph_node, old_node);
126     }
127     old_node = graph_node;
128   }
129 }
130
131
132
133 /* Functions used to generate interesting random values */
134
135 double random_pareto(double min, double max, double K, double P, double ALPHA) {
136   double x = RngStream_RandU01(rng_stream);
137   double den = pow(1.0 - x + x*pow(K/P, ALPHA), 1.0/ALPHA);
138   double res = (1/den);
139   res += min - 1; // pareto is on [1, infinity) by default
140   if (res>max) {
141     return max;
142   }
143   return res;
144 }