Logo AND Algorithmique Numérique Distribuée

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