Logo AND Algorithmique Numérique Distribuée

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