Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
The test is always true
[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, network_element_t src, network_element_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(global_routing->size_of_link, NULL);
37
38   int src,dst;
39   network_element_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,network_element_t);
45       dst_elm = xbt_dynar_get_as(asg->index_network_elm,dst,network_element_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, network_element_t src, network_element_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   network_element_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(global_routing->size_of_link, 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 =
218                                 xbt_dynar_new(global_routing->size_of_link, NULL);
219                         xbt_dynar_push(e_route->link_list, &global_routing->loopback);
220                         TO_FLOYD_LINK(i, i) = e_route;
221                         TO_FLOYD_PRED(i, i) = i;
222                         TO_FLOYD_COST(i, i) = 1;
223                   }
224                 }
225         }
226         /* Calculate path costs */
227         for (c = 0; c < table_size; c++) {
228                 for (a = 0; a < table_size; a++) {
229                   for (b = 0; b < table_size; b++) {
230                         if (TO_FLOYD_COST(a, c) < DBL_MAX && TO_FLOYD_COST(c, b) < DBL_MAX) {
231                           if (TO_FLOYD_COST(a, b) == DBL_MAX ||
232                                   (TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b) <
233                                    TO_FLOYD_COST(a, b))) {
234                                 TO_FLOYD_COST(a, b) =
235                                         TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b);
236                                 TO_FLOYD_PRED(a, b) = TO_FLOYD_PRED(c, b);
237                           }
238                         }
239                   }
240                 }
241         }
242 }
243
244 static int floyd_pointer_resource_cmp(const void *a, const void *b) {
245   return a != b;
246 }
247
248 //FIXME: kill dupplicates in next function with full routing
249
250 void model_floyd_parse_route(AS_t rc, const char *src,
251         const char *dst, route_t route)
252 {
253     int as_route = 0;
254     as_floyd_t as = (as_floyd_t) rc;
255
256         /* set the size of table routing */
257         size_t table_size = xbt_dynar_length(rc->index_network_elm);
258     network_element_t src_net_elm, dst_net_elm;
259
260     src_net_elm = xbt_lib_get_or_null(host_lib, src, ROUTING_HOST_LEVEL);
261     dst_net_elm = xbt_lib_get_or_null(host_lib, dst, ROUTING_HOST_LEVEL);
262     if (!src_net_elm)
263       src_net_elm = xbt_lib_get_or_null(as_router_lib, src, ROUTING_ASR_LEVEL);
264     if (!dst_net_elm)
265       dst_net_elm = xbt_lib_get_or_null(as_router_lib, dst, ROUTING_ASR_LEVEL);
266
267         xbt_assert(src_net_elm, "Network elements %s not found", src);
268         xbt_assert(dst_net_elm, "Network elements %s not found", dst);
269
270         if(!as->link_table)
271         {
272           int i,j;
273                 /* Create Cost, Predecessor and Link tables */
274                 as->cost_table = xbt_new0(double, table_size * table_size);       /* link cost from host to host */
275                 as->predecessor_table = xbt_new0(int, table_size * table_size);  /* predecessor host numbers */
276                 as->link_table = xbt_new0(route_t, table_size * table_size);    /* actual link between src and dst */
277
278                 /* Initialize costs and predecessors */
279                 for (i = 0; i < table_size; i++)
280                 for (j = 0; j < table_size; j++) {
281                   TO_FLOYD_COST(i, j) = DBL_MAX;
282                   TO_FLOYD_PRED(i, j) = -1;
283                   TO_FLOYD_LINK(i, j) = NULL;       /* fixed, missing in the previous version */
284                 }
285         }
286     if(!route->dst_gateway && !route->src_gateway)
287       XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
288     else{
289         as_route = 1;
290         XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
291              route->src_gateway->name, dst, route->dst_gateway->name);
292         if(route->dst_gateway->rc_type == SURF_NETWORK_ELEMENT_NULL)
293             xbt_die("The dst_gateway '%s' does not exist!",route->dst_gateway->name);
294         if(route->src_gateway->rc_type == SURF_NETWORK_ELEMENT_NULL)
295             xbt_die("The src_gateway '%s' does not exist!",route->src_gateway->name);
296     }
297
298         if(TO_FLOYD_LINK(src_net_elm->id, dst_net_elm->id))
299         {
300
301                 char * link_name;
302                 unsigned int cpt;
303                 xbt_dynar_t link_route_to_test = xbt_dynar_new(global_routing->size_of_link, NULL);
304                 xbt_dynar_foreach(route->link_list,cpt,link_name)
305                 {
306                         void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
307                         xbt_assert(link,"Link : '%s' doesn't exists.",link_name);
308                         xbt_dynar_push(link_route_to_test,&link);
309                 }
310                 xbt_assert(!xbt_dynar_compare(
311                           (void*)TO_FLOYD_LINK(src_net_elm->id, dst_net_elm->id)->link_list,
312                           (void*)link_route_to_test,
313                           (int_f_cpvoid_cpvoid_t) floyd_pointer_resource_cmp),
314                           "The route between \"%s\" and \"%s\" already exists", src,dst);
315         }
316         else
317         {
318             TO_FLOYD_LINK(src_net_elm->id, dst_net_elm->id) =
319                         generic_new_extended_route(rc->hierarchy, route, 1);
320             TO_FLOYD_PRED(src_net_elm->id, dst_net_elm->id) = src_net_elm->id;
321             TO_FLOYD_COST(src_net_elm->id, dst_net_elm->id) =
322                         ((TO_FLOYD_LINK(src_net_elm->id, dst_net_elm->id))->link_list)->used;   /* count of links, old model assume 1 */
323         }
324
325     if ( (A_surfxml_route_symmetrical == A_surfxml_route_symmetrical_YES && as_route == 0)
326          || (A_surfxml_ASroute_symmetrical == A_surfxml_ASroute_symmetrical_YES && as_route == 1)
327        )
328         {
329                 if(TO_FLOYD_LINK(dst_net_elm->id, src_net_elm->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->name, src, route->dst_gateway->name);
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->link_list) ;i>0 ;i--)
340                         {
341                                 link_name = xbt_dynar_get_as(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_net_elm->id, src_net_elm->id)->link_list,
348                               (void*)link_route_to_test,
349                                   (int_f_cpvoid_cpvoid_t) floyd_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                           network_element_t gw_src = route->src_gateway;
357                           network_element_t gw_dst = 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->name, src, route->dst_gateway->name);
367
368                     TO_FLOYD_LINK(dst_net_elm->id, src_net_elm->id) =
369                                 generic_new_extended_route(rc->hierarchy, route, 0);
370                     TO_FLOYD_PRED(dst_net_elm->id, src_net_elm->id) = dst_net_elm->id;
371                     TO_FLOYD_COST(dst_net_elm->id, src_net_elm->id) =
372                                 ((TO_FLOYD_LINK(dst_net_elm->id, src_net_elm->id))->link_list)->used;   /* count of links, old model assume 1 */
373                 }
374         }
375 }