Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Start fixing the OO organization of the routing
[simgrid.git] / src / surf / surf_routing_floyd.c
1 /* Copyright (c) 2009, 2010, 2011. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "surf_routing_private.h"
8
9 /* Global vars */
10 extern routing_global_t global_routing;
11 extern routing_component_t current_routing;
12 extern model_type_t current_routing_model;
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_floyd, surf, "Routing part of surf");
15
16 #define TO_FLOYD_COST(i,j) (routing->cost_table)[(i)+(j)*table_size]
17 #define TO_FLOYD_PRED(i,j) (routing->predecessor_table)[(i)+(j)*table_size]
18 #define TO_FLOYD_LINK(i,j) (routing->link_table)[(i)+(j)*table_size]
19
20 /* Routing model structure */
21
22 typedef struct {
23   s_routing_component_t generic_routing;
24   /* vars for calculate the floyd algorith. */
25   int *predecessor_table;
26   double *cost_table;
27   route_extended_t *link_table; /* char* -> int* */
28 } s_routing_component_floyd_t, *routing_component_floyd_t;
29
30 static route_extended_t floyd_get_route(routing_component_t rc,
31                                         const char *src, const char *dst);
32
33 /* Business methods */
34 static xbt_dynar_t floyd_get_onelink_routes(routing_component_t rc)
35 {
36   xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
37
38   routing_component_floyd_t routing = (routing_component_floyd_t) rc;
39   //size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
40   xbt_dict_cursor_t c1 = NULL, c2 = NULL;
41   char *k1, *d1, *k2, *d2;
42   xbt_dict_foreach(routing->generic_routing.to_index, c1, k1, d1) {
43     xbt_dict_foreach(routing->generic_routing.to_index, c2, k2, d2) {
44       route_extended_t route = floyd_get_route(rc, k1, k2);
45       if (route) {
46         if (xbt_dynar_length(route->generic_route.link_list) == 1) {
47           void *link =
48               *(void **) xbt_dynar_get_ptr(route->generic_route.link_list,
49                                            0);
50           onelink_t onelink = xbt_new0(s_onelink_t, 1);
51           onelink->link_ptr = link;
52           if (routing->generic_routing.hierarchy == SURF_ROUTING_BASE) {
53             onelink->src = xbt_strdup(k1);
54             onelink->dst = xbt_strdup(k2);
55           } else if (routing->generic_routing.hierarchy ==
56                      SURF_ROUTING_RECURSIVE) {
57             onelink->src = xbt_strdup(route->src_gateway);
58             onelink->dst = xbt_strdup(route->dst_gateway);
59           }
60           xbt_dynar_push(ret, &onelink);
61         }
62       }
63     }
64   }
65   return ret;
66 }
67
68 static route_extended_t floyd_get_route(routing_component_t rc,
69                                         const char *src, const char *dst)
70 {
71   xbt_assert(rc && src
72               && dst,
73               "Invalid params for \"get_route\" function at AS \"%s\"",
74               rc->name);
75
76   /* set utils vars */
77   routing_component_floyd_t routing = (routing_component_floyd_t) rc;
78   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
79
80   generic_src_dst_check(rc, src, dst);
81   int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, src);
82   int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, dst);
83   xbt_assert(src_id
84               && dst_id,
85               "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",
86               src, dst);
87
88   /* create a result route */
89   route_extended_t new_e_route = xbt_new0(s_route_extended_t, 1);
90   new_e_route->generic_route.link_list =
91       xbt_dynar_new(global_routing->size_of_link, NULL);
92   new_e_route->src_gateway = NULL;
93   new_e_route->dst_gateway = NULL;
94
95   int first = 1;
96   int pred = *dst_id;
97   int prev_pred = 0;
98   char *gw_src = NULL, *gw_dst = NULL, *prev_gw_src, *first_gw = NULL;
99   unsigned int cpt;
100   void *link;
101   xbt_dynar_t links;
102
103   do {
104     prev_pred = pred;
105     pred = TO_FLOYD_PRED(*src_id, pred);
106     if (pred == -1)             /* if no pred in route -> no route to host */
107       break;
108     xbt_assert(TO_FLOYD_LINK(pred, prev_pred),
109                 "Invalid link for the route between \"%s\" or \"%s\"", src,
110                 dst);
111
112     prev_gw_src = gw_src;
113
114     route_extended_t e_route = TO_FLOYD_LINK(pred, prev_pred);
115     gw_src = e_route->src_gateway;
116     gw_dst = e_route->dst_gateway;
117
118     if (first)
119       first_gw = gw_dst;
120
121     if (rc->hierarchy == SURF_ROUTING_RECURSIVE && !first
122         && strcmp(gw_dst, prev_gw_src)) {
123       xbt_dynar_t e_route_as_to_as =
124           (*(global_routing->get_route)) (gw_dst, prev_gw_src);
125       xbt_assert(e_route_as_to_as, "no route between \"%s\" and \"%s\"",
126                   gw_dst, prev_gw_src);
127       links = e_route_as_to_as;
128       int pos = 0;
129       xbt_dynar_foreach(links, cpt, link) {
130         xbt_dynar_insert_at(new_e_route->generic_route.link_list, pos,
131                             &link);
132         pos++;
133       }
134     }
135
136     links = e_route->generic_route.link_list;
137     xbt_dynar_foreach(links, cpt, link) {
138       xbt_dynar_unshift(new_e_route->generic_route.link_list, &link);
139     }
140     first = 0;
141
142   } while (pred != *src_id);
143   xbt_assert(pred != -1, "no route from host %d to %d (\"%s\" to \"%s\")",
144               *src_id, *dst_id, src, dst);
145
146   if (rc->hierarchy == SURF_ROUTING_RECURSIVE) {
147     new_e_route->src_gateway = xbt_strdup(gw_src);
148     new_e_route->dst_gateway = xbt_strdup(first_gw);
149   }
150
151   return new_e_route;
152 }
153
154 static void floyd_finalize(routing_component_t rc)
155 {
156   routing_component_floyd_t routing = (routing_component_floyd_t) rc;
157   int i, j;
158   size_t table_size;
159   if (routing) {
160     table_size = xbt_dict_length(routing->generic_routing.to_index);
161     /* Delete link_table */
162     for (i = 0; i < table_size; i++)
163       for (j = 0; j < table_size; j++)
164         generic_free_extended_route(TO_FLOYD_LINK(i, j));
165     xbt_free(routing->link_table);
166     /* Delete bypass dict */
167     xbt_dict_free(&routing->generic_routing.bypassRoutes);
168     /* Delete index dict */
169     xbt_dict_free(&(routing->generic_routing.to_index));
170     /* Delete dictionary index dict, predecessor and links table */
171     xbt_free(routing->predecessor_table);
172     /* Delete structure */
173     xbt_free(rc);
174   }
175 }
176
177 routing_component_t model_floyd_create(void)
178 {
179   routing_component_floyd_t new_component =
180       xbt_new0(s_routing_component_floyd_t, 1);
181   new_component->generic_routing.set_processing_unit =
182       generic_set_processing_unit;
183   new_component->generic_routing.set_autonomous_system =
184       generic_set_autonomous_system;
185   new_component->generic_routing.set_route = model_floyd_set_route;
186   new_component->generic_routing.set_ASroute = model_floyd_set_route;
187   new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
188   new_component->generic_routing.get_route = floyd_get_route;
189   new_component->generic_routing.get_latency = generic_get_link_latency;
190   new_component->generic_routing.get_onelink_routes =
191       floyd_get_onelink_routes;
192   new_component->generic_routing.get_bypass_route =
193       generic_get_bypassroute;
194   new_component->generic_routing.finalize = floyd_finalize;
195   new_component->generic_routing.to_index = xbt_dict_new();
196   new_component->generic_routing.bypassRoutes = xbt_dict_new();
197   new_component->generic_routing.get_network_element_type = get_network_element_type;
198   return (routing_component_t)new_component;
199 }
200
201 void model_floyd_end(void)
202 {
203
204         routing_component_floyd_t routing =
205           ((routing_component_floyd_t) current_routing);
206
207         unsigned int i, j, a, b, c;
208
209         /* set the size of table routing */
210         size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
211
212         if(!routing->link_table)
213         {
214                 /* Create Cost, Predecessor and Link tables */
215                 routing->cost_table = xbt_new0(double, table_size * table_size);       /* link cost from host to host */
216                 routing->predecessor_table = xbt_new0(int, table_size * table_size);  /* predecessor host numbers */
217                 routing->link_table = xbt_new0(route_extended_t, table_size * table_size);    /* actual link between src and dst */
218
219                 /* Initialize costs and predecessors */
220                 for (i = 0; i < table_size; i++)
221                 for (j = 0; j < table_size; j++) {
222                   TO_FLOYD_COST(i, j) = DBL_MAX;
223                   TO_FLOYD_PRED(i, j) = -1;
224                   TO_FLOYD_LINK(i, j) = NULL;       /* fixed, missing in the previous version */
225                 }
226         }
227
228         /* Add the loopback if needed */
229         if (current_routing->hierarchy == SURF_ROUTING_BASE) {
230                 for (i = 0; i < table_size; i++) {
231                   route_extended_t e_route = TO_FLOYD_LINK(i, i);
232                   if (!e_route) {
233                         e_route = xbt_new0(s_route_extended_t, 1);
234                         e_route->src_gateway = NULL;
235                         e_route->dst_gateway = NULL;
236                         e_route->generic_route.link_list =
237                                 xbt_dynar_new(global_routing->size_of_link, NULL);
238                         xbt_dynar_push(e_route->generic_route.link_list,
239                                                    &global_routing->loopback);
240                         TO_FLOYD_LINK(i, i) = e_route;
241                         TO_FLOYD_PRED(i, i) = i;
242                         TO_FLOYD_COST(i, i) = 1;
243                   }
244                 }
245         }
246         /* Calculate path costs */
247         for (c = 0; c < table_size; c++) {
248                 for (a = 0; a < table_size; a++) {
249                   for (b = 0; b < table_size; b++) {
250                         if (TO_FLOYD_COST(a, c) < DBL_MAX && TO_FLOYD_COST(c, b) < DBL_MAX) {
251                           if (TO_FLOYD_COST(a, b) == DBL_MAX ||
252                                   (TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b) <
253                                    TO_FLOYD_COST(a, b))) {
254                                 TO_FLOYD_COST(a, b) =
255                                         TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b);
256                                 TO_FLOYD_PRED(a, b) = TO_FLOYD_PRED(c, b);
257                           }
258                         }
259                   }
260                 }
261         }
262 }
263
264 static int surf_pointer_resource_cmp(const void *a, const void *b) {
265   return a != b;
266 }
267
268 //FIXME: kill dupplicates in next function with full routing
269
270 void model_floyd_set_route(routing_component_t rc, const char *src,
271         const char *dst, name_route_extended_t route)
272 {
273         routing_component_floyd_t routing = (routing_component_floyd_t) rc;
274
275         /* set the size of table routing */
276         size_t table_size = xbt_dict_length(rc->to_index);
277         int *src_id, *dst_id;
278         int i,j;
279
280         src_id = xbt_dict_get_or_null(rc->to_index, src);
281         dst_id = xbt_dict_get_or_null(rc->to_index, dst);
282
283         xbt_assert(src_id, "Network elements %s not found", src);
284         xbt_assert(dst_id, "Network elements %s not found", dst);
285
286         if(!routing->link_table)
287         {
288                 /* Create Cost, Predecessor and Link tables */
289                 routing->cost_table = xbt_new0(double, table_size * table_size);       /* link cost from host to host */
290                 routing->predecessor_table = xbt_new0(int, table_size * table_size);  /* predecessor host numbers */
291                 routing->link_table = xbt_new0(route_extended_t, table_size * table_size);    /* actual link between src and dst */
292
293                 /* Initialize costs and predecessors */
294                 for (i = 0; i < table_size; i++)
295                 for (j = 0; j < table_size; j++) {
296                   TO_FLOYD_COST(i, j) = DBL_MAX;
297                   TO_FLOYD_PRED(i, j) = -1;
298                   TO_FLOYD_LINK(i, j) = NULL;       /* fixed, missing in the previous version */
299                 }
300         }
301
302         if(TO_FLOYD_LINK(*src_id, *dst_id))
303         {
304                 if(!route->dst_gateway && !route->src_gateway)
305                         XBT_DEBUG("See Route from \"%s\" to \"%s\"", src, dst);
306                 else
307                         XBT_DEBUG("See ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
308                                  route->src_gateway, dst, route->dst_gateway);
309                 char * link_name;
310                 unsigned int cpt;
311                 xbt_dynar_t link_route_to_test = xbt_dynar_new(global_routing->size_of_link, NULL);
312                 xbt_dynar_foreach(route->generic_route.link_list,cpt,link_name)
313                 {
314                         void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
315                         xbt_assert(link,"Link : '%s' doesn't exists.",link_name);
316                         xbt_dynar_push(link_route_to_test,&link);
317                 }
318                 xbt_assert(!xbt_dynar_compare(
319                           (void*)TO_FLOYD_LINK(*src_id, *dst_id)->generic_route.link_list,
320                           (void*)link_route_to_test,
321                           (int_f_cpvoid_cpvoid_t) surf_pointer_resource_cmp),
322                           "The route between \"%s\" and \"%s\" already exists", src,dst);
323         }
324         else
325         {
326                 if(!route->dst_gateway && !route->src_gateway)
327                   XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
328                 else{
329                         XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
330                                  route->src_gateway, dst, route->dst_gateway);
331                         if(global_routing->get_network_element_type((const char*)route->dst_gateway) == SURF_NETWORK_ELEMENT_NULL)
332                                 xbt_die("The dst_gateway '%s' does not exist!",route->dst_gateway);
333                         if(global_routing->get_network_element_type((const char*)route->src_gateway) == SURF_NETWORK_ELEMENT_NULL)
334                                 xbt_die("The src_gateway '%s' does not exist!",route->src_gateway);
335                 }
336             TO_FLOYD_LINK(*src_id, *dst_id) =
337                         generic_new_extended_route(rc->hierarchy, route, 1);
338             TO_FLOYD_PRED(*src_id, *dst_id) = *src_id;
339             TO_FLOYD_COST(*src_id, *dst_id) =
340                         ((TO_FLOYD_LINK(*src_id, *dst_id))->generic_route.link_list)->used;   /* count of links, old model assume 1 */
341         }
342
343         if( A_surfxml_route_symmetrical == A_surfxml_route_symmetrical_YES
344                 || A_surfxml_ASroute_symmetrical == A_surfxml_ASroute_symmetrical_YES )
345         {
346                 if(TO_FLOYD_LINK(*dst_id, *src_id))
347                 {
348                         if(!route->dst_gateway && !route->src_gateway)
349                           XBT_DEBUG("See Route from \"%s\" to \"%s\"", dst, src);
350                         else
351                           XBT_DEBUG("See ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
352                                          route->src_gateway, src, route->dst_gateway);
353                         char * link_name;
354                         unsigned int i;
355                         xbt_dynar_t link_route_to_test = xbt_dynar_new(global_routing->size_of_link, NULL);
356                         for(i=xbt_dynar_length(route->generic_route.link_list) ;i>0 ;i--)
357                         {
358                                 link_name = xbt_dynar_get_as(route->generic_route.link_list,i-1,void *);
359                                 void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
360                                 xbt_assert(link,"Link : '%s' doesn't exists.",link_name);
361                                 xbt_dynar_push(link_route_to_test,&link);
362                         }
363                         xbt_assert(!xbt_dynar_compare(
364                                   (void*)TO_FLOYD_LINK(*dst_id, *src_id)->generic_route.link_list,
365                               (void*)link_route_to_test,
366                                   (int_f_cpvoid_cpvoid_t) surf_pointer_resource_cmp),
367                                   "The route between \"%s\" and \"%s\" already exists", src,dst);
368                 }
369                 else
370                 {
371                         if(route->dst_gateway && route->src_gateway)
372                         {
373                           char *gw_src = xbt_strdup(route->src_gateway);
374                           char *gw_dst = xbt_strdup(route->dst_gateway);
375                           route->src_gateway = gw_dst;
376                           route->dst_gateway = gw_src;
377                         }
378
379                         if(!route->dst_gateway && !route->src_gateway)
380                           XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
381                         else
382                           XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
383                                          route->src_gateway, src, route->dst_gateway);
384
385                     TO_FLOYD_LINK(*dst_id, *src_id) =
386                                 generic_new_extended_route(rc->hierarchy, route, 0);
387                     TO_FLOYD_PRED(*dst_id, *src_id) = *dst_id;
388                     TO_FLOYD_COST(*dst_id, *src_id) =
389                                 ((TO_FLOYD_LINK(*dst_id, *src_id))->generic_route.link_list)->used;   /* count of links, old model assume 1 */
390                 }
391         }
392 }