Logo AND Algorithmique Numérique Distribuée

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