Logo AND Algorithmique Numérique Distribuée

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