Logo AND Algorithmique Numérique Distribuée

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