Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove c surf files
[simgrid.git] / src / surf / surf_routing_floyd.cpp
1 #include "surf_routing_private.h"
2 #include "surf_routing_floyd.hpp"
3 #include "network.hpp"
4
5 extern "C" {
6 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_floyd, surf, "Routing part of surf");
7 }
8
9 #define TO_FLOYD_COST(i,j) (p_costTable)[(i)+(j)*table_size]
10 #define TO_FLOYD_PRED(i,j) (p_predecessorTable)[(i)+(j)*table_size]
11 #define TO_FLOYD_LINK(i,j) (p_linkTable)[(i)+(j)*table_size]
12
13 AS_t model_floyd_create(void)
14 {
15   return new AsFloyd();
16 }
17
18 void model_floyd_end(AS_t current_routing)
19 {
20   static_cast<AsFloydPtr>(current_routing)->end();
21 }
22
23 AsFloyd::AsFloyd(): AsGeneric() {
24 }
25
26 AsFloyd::~AsFloyd(){
27   int i, j;
28   size_t table_size;
29   table_size = xbt_dynar_length(p_indexNetworkElm);
30     /* Delete link_table */
31     for (i = 0; i < table_size; i++)
32       for (j = 0; j < table_size; j++)
33         generic_free_route(TO_FLOYD_LINK(i, j));
34     xbt_free(p_linkTable);
35     /* Delete bypass dict */
36     xbt_dict_free(&p_bypassRoutes);
37     /* Delete predecessor and cost table */
38     xbt_free(p_predecessorTable);
39     xbt_free(p_costTable);
40 }
41
42 /* Business methods */
43 xbt_dynar_t AsFloyd::getOneLinkRoutes()
44 {
45   xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
46   sg_platf_route_cbarg_t route =   xbt_new0(s_sg_platf_route_cbarg_t, 1);
47   route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
48
49   int src,dst;
50   sg_routing_edge_t src_elm, dst_elm;
51   int table_size = xbt_dynar_length(p_indexNetworkElm);
52   for(src=0; src < table_size; src++) {
53     for(dst=0; dst< table_size; dst++) {
54       xbt_dynar_reset(route->link_list);
55       src_elm = xbt_dynar_get_as(p_indexNetworkElm, src, RoutingEdgePtr);
56       dst_elm = xbt_dynar_get_as(p_indexNetworkElm, dst, RoutingEdgePtr);
57       this->getRouteAndLatency(src_elm, dst_elm, route, NULL);
58
59       if (xbt_dynar_length(route->link_list) == 1) {
60         void *link = *(void **) xbt_dynar_get_ptr(route->link_list, 0);
61         onelink_t onelink = xbt_new0(s_onelink_t, 1);
62         onelink->link_ptr = link;
63         if (p_hierarchy == SURF_ROUTING_BASE) {
64           onelink->src = src_elm;
65           onelink->dst = dst_elm;
66         } else if (p_hierarchy == SURF_ROUTING_RECURSIVE) {
67           onelink->src = route->gw_src;
68           onelink->dst = route->gw_dst;
69         }
70         xbt_dynar_push(ret, &onelink);
71       }
72     }
73   }
74
75   return ret;
76 }
77
78 void AsFloyd::getRouteAndLatency(RoutingEdgePtr src, RoutingEdgePtr dst, sg_platf_route_cbarg_t res, double *lat)
79 {
80
81   /* set utils vars */
82   size_t table_size = xbt_dynar_length(p_indexNetworkElm);
83
84   this->srcDstCheck(src, dst);
85
86   /* create a result route */
87   xbt_dynar_t route_stack = xbt_dynar_new(sizeof(sg_platf_route_cbarg_t), NULL);
88   int pred;
89   int cur = dst->m_id;
90   do {
91     pred = TO_FLOYD_PRED(src->m_id, cur);
92     if (pred == -1)
93       THROWF(arg_error, 0, "No route from '%s' to '%s'", src->p_name, dst->p_name);
94     xbt_dynar_push_as(route_stack, sg_platf_route_cbarg_t, TO_FLOYD_LINK(pred, cur));
95     cur = pred;
96   } while (cur != src->m_id);
97
98   if (p_hierarchy == SURF_ROUTING_RECURSIVE) {
99     res->gw_src = xbt_dynar_getlast_as(route_stack, sg_platf_route_cbarg_t)->gw_src;
100     res->gw_dst = xbt_dynar_getfirst_as(route_stack, sg_platf_route_cbarg_t)->gw_dst;
101   }
102
103   sg_routing_edge_t prev_dst_gw = NULL;
104   while (!xbt_dynar_is_empty(route_stack)) {
105     sg_platf_route_cbarg_t e_route = xbt_dynar_pop_as(route_stack, sg_platf_route_cbarg_t);
106     xbt_dynar_t links;
107     void *link;
108     unsigned int cpt;
109
110     if (p_hierarchy == SURF_ROUTING_RECURSIVE && prev_dst_gw != NULL
111         && strcmp(prev_dst_gw->p_name, e_route->gw_src->p_name)) {
112       routing_get_route_and_latency(prev_dst_gw, e_route->gw_src,
113                                     &res->link_list, lat);
114     }
115
116     links = e_route->link_list;
117     xbt_dynar_foreach(links, cpt, link) {
118       xbt_dynar_push_as(res->link_list, sg_routing_link_t, link);
119       if (lat)
120         *lat += dynamic_cast<NetworkCm02LinkPtr>(static_cast<ResourcePtr>(link))->getLatency();
121     }
122
123     prev_dst_gw = e_route->gw_dst;
124   }
125   xbt_dynar_free(&route_stack);
126 }
127
128 static int floyd_pointer_resource_cmp(const void *a, const void *b) {
129   return a != b;
130 }
131
132 void AsFloyd::parseASroute(sg_platf_route_cbarg_t route){
133   parseRoute(route);
134 }
135
136 void AsFloyd::parseRoute(sg_platf_route_cbarg_t route)
137 {
138   char *src = (char*)(route->src);
139   char *dst = (char*)(route->dst);
140
141   int as_route = 0;
142
143   /* set the size of table routing */
144   size_t table_size = xbt_dynar_length(p_indexNetworkElm);
145   RoutingEdgePtr src_net_elm, dst_net_elm;
146
147   src_net_elm = sg_routing_edge_by_name_or_null(src);
148   dst_net_elm = sg_routing_edge_by_name_or_null(dst);
149
150   xbt_assert(src_net_elm, "Network elements %s not found", src);
151   xbt_assert(dst_net_elm, "Network elements %s not found", dst);
152
153   if(!p_linkTable)
154   {
155     int i,j;
156     /* Create Cost, Predecessor and Link tables */
157     p_costTable = xbt_new0(double, table_size * table_size);       /* link cost from host to host */
158     p_predecessorTable = xbt_new0(int, table_size * table_size);  /* predecessor host numbers */
159     p_linkTable = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);    /* actual link between src and dst */
160
161     /* Initialize costs and predecessors */
162     for (i = 0; i < table_size; i++)
163       for (j = 0; j < table_size; j++) {
164         TO_FLOYD_COST(i, j) = DBL_MAX;
165         TO_FLOYD_PRED(i, j) = -1;
166         TO_FLOYD_LINK(i, j) = NULL;       /* fixed, missing in the previous version */
167       }
168   }
169   if(!route->gw_dst && !route->gw_src)
170     XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
171   else{
172     as_route = 1;
173     XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
174         route->gw_src->p_name, dst, route->gw_dst->p_name);
175     if(route->gw_dst->p_rcType == SURF_NETWORK_ELEMENT_NULL)
176       xbt_die("The dst_gateway '%s' does not exist!",route->gw_dst->p_name);
177     if(route->gw_src->p_rcType == SURF_NETWORK_ELEMENT_NULL)
178       xbt_die("The src_gateway '%s' does not exist!",route->gw_src->p_name);
179   }
180
181   if(TO_FLOYD_LINK(src_net_elm->m_id, dst_net_elm->m_id))
182   {
183
184     char * link_name;
185     unsigned int cpt;
186     xbt_dynar_t link_route_to_test = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
187     xbt_dynar_foreach(route->link_list,cpt,link_name)
188     {
189       void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
190       xbt_assert(link,"Link : '%s' doesn't exists.",link_name);
191       xbt_dynar_push(link_route_to_test,&link);
192     }
193     xbt_assert(!xbt_dynar_compare(
194         TO_FLOYD_LINK(src_net_elm->m_id, dst_net_elm->m_id)->link_list,
195         link_route_to_test,
196         (int_f_cpvoid_cpvoid_t) floyd_pointer_resource_cmp),
197         "The route between \"%s\" and \"%s\" already exists", src,dst);
198   }
199   else
200   {
201     TO_FLOYD_LINK(src_net_elm->m_id, dst_net_elm->m_id) =
202         newExtendedRoute(p_hierarchy, route, 1);
203     TO_FLOYD_PRED(src_net_elm->m_id, dst_net_elm->m_id) = src_net_elm->m_id;
204     TO_FLOYD_COST(src_net_elm->m_id, dst_net_elm->m_id) =
205         ((TO_FLOYD_LINK(src_net_elm->m_id, dst_net_elm->m_id))->link_list)->used;   /* count of links, old model assume 1 */
206   }
207
208   if ( (route->symmetrical == TRUE && as_route == 0)
209       || (route->symmetrical == TRUE && as_route == 1)
210   )
211   {
212     if(TO_FLOYD_LINK(dst_net_elm->m_id, src_net_elm->m_id))
213     {
214       if(!route->gw_dst && !route->gw_src)
215         XBT_DEBUG("See Route from \"%s\" to \"%s\"", dst, src);
216       else
217         XBT_DEBUG("See ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
218             route->gw_src->p_name, src, route->gw_dst->p_name);
219       char * link_name;
220       unsigned int i;
221       xbt_dynar_t link_route_to_test = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
222       for(i=xbt_dynar_length(route->link_list) ;i>0 ;i--)
223       {
224         link_name = xbt_dynar_get_as(route->link_list,i-1,char *);
225         void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
226         xbt_assert(link,"Link : '%s' doesn't exists.",link_name);
227         xbt_dynar_push(link_route_to_test,&link);
228       }
229       xbt_assert(!xbt_dynar_compare(
230           TO_FLOYD_LINK(dst_net_elm->m_id, src_net_elm->m_id)->link_list,
231           link_route_to_test,
232           (int_f_cpvoid_cpvoid_t) floyd_pointer_resource_cmp),
233           "The route between \"%s\" and \"%s\" already exists", src,dst);
234     }
235     else
236     {
237       if(route->gw_dst && route->gw_src)
238       {
239         sg_routing_edge_t gw_src = route->gw_src;
240         sg_routing_edge_t gw_dst = route->gw_dst;
241         route->gw_src = gw_dst;
242         route->gw_dst = gw_src;
243       }
244
245       if(!route->gw_src && !route->gw_dst)
246         XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
247       else
248         XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
249             route->gw_src->p_name, src, route->gw_dst->p_name);
250
251       TO_FLOYD_LINK(dst_net_elm->m_id, src_net_elm->m_id) =
252                  newExtendedRoute(p_hierarchy, route, 0);
253       TO_FLOYD_PRED(dst_net_elm->m_id, src_net_elm->m_id) = dst_net_elm->m_id;
254       TO_FLOYD_COST(dst_net_elm->m_id, src_net_elm->m_id) =
255           ((TO_FLOYD_LINK(dst_net_elm->m_id, src_net_elm->m_id))->link_list)->used;   /* count of links, old model assume 1 */
256     }
257   }
258   xbt_dynar_free(&route->link_list);
259 }
260
261 void AsFloyd::end(){
262   unsigned int i, j, a, b, c;
263
264   /* set the size of table routing */
265   size_t table_size = xbt_dynar_length(p_indexNetworkElm);
266
267   if(!p_linkTable) {
268     /* Create Cost, Predecessor and Link tables */
269     p_costTable = xbt_new0(double, table_size * table_size);       /* link cost from host to host */
270     p_predecessorTable = xbt_new0(int, table_size * table_size);  /* predecessor host numbers */
271     p_linkTable = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);    /* actual link between src and dst */
272
273     /* Initialize costs and predecessors */
274     for (i = 0; i < table_size; i++)
275       for (j = 0; j < table_size; j++) {
276         TO_FLOYD_COST(i, j) = DBL_MAX;
277         TO_FLOYD_PRED(i, j) = -1;
278         TO_FLOYD_LINK(i, j) = NULL;       /* fixed, missing in the previous version */
279       }
280   }
281
282   /* Add the loopback if needed */
283   if (routing_platf->p_loopback && p_hierarchy == SURF_ROUTING_BASE) {
284     for (i = 0; i < table_size; i++) {
285       sg_platf_route_cbarg_t e_route = TO_FLOYD_LINK(i, i);
286       if (!e_route) {
287         e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
288         e_route->gw_src = NULL;
289         e_route->gw_dst = NULL;
290         e_route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
291         xbt_dynar_push(e_route->link_list, &routing_platf->p_loopback);
292         TO_FLOYD_LINK(i, i) = e_route;
293         TO_FLOYD_PRED(i, i) = i;
294         TO_FLOYD_COST(i, i) = 1;
295       }
296     }
297   }
298   /* Calculate path costs */
299   for (c = 0; c < table_size; c++) {
300     for (a = 0; a < table_size; a++) {
301       for (b = 0; b < table_size; b++) {
302         if (TO_FLOYD_COST(a, c) < DBL_MAX && TO_FLOYD_COST(c, b) < DBL_MAX) {
303           if (TO_FLOYD_COST(a, b) == DBL_MAX ||
304               (TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b) <
305                   TO_FLOYD_COST(a, b))) {
306             TO_FLOYD_COST(a, b) =
307                 TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b);
308             TO_FLOYD_PRED(a, b) = TO_FLOYD_PRED(c, b);
309           }
310         }
311       }
312     }
313   }
314 }