Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
update
[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_platf_t routing_platf;
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   sg_platf_route_cbarg_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     sg_platf_route_cbarg_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   sg_platf_route_cbarg_t route =   xbt_new0(s_sg_platf_route_cbarg_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->gw_src;
57           onelink->dst = route->gw_dst;
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     sg_platf_route_cbarg_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
77   /* create a result route */
78   xbt_dynar_t route_stack = xbt_dynar_new(sizeof(sg_platf_route_cbarg_t), NULL);
79   int pred;
80   int cur = dst->id;
81   do {
82     pred = TO_FLOYD_PRED(src->id, cur);
83     if (pred == -1)
84       THROWF(arg_error, 0, "No route from '%s' to '%s'", src->name, dst->name);
85     xbt_dynar_push_as(route_stack, sg_platf_route_cbarg_t, TO_FLOYD_LINK(pred, cur));
86     cur = pred;
87   } while (cur != src->id);
88
89   if (asg->hierarchy == SURF_ROUTING_RECURSIVE) {
90     res->gw_src = xbt_dynar_getlast_as(route_stack, sg_platf_route_cbarg_t)->gw_src;
91     res->gw_dst = xbt_dynar_getfirst_as(route_stack, sg_platf_route_cbarg_t)->gw_dst;
92   }
93
94   sg_routing_edge_t prev_dst_gw = NULL;
95   while (!xbt_dynar_is_empty(route_stack)) {
96     sg_platf_route_cbarg_t e_route = xbt_dynar_pop_as(route_stack, sg_platf_route_cbarg_t);
97     xbt_dynar_t links;
98     sg_routing_link_t link;
99     unsigned int cpt;
100
101     if (asg->hierarchy == SURF_ROUTING_RECURSIVE && prev_dst_gw != NULL
102         && strcmp(prev_dst_gw->name, e_route->gw_src->name)) {
103       routing_get_route_and_latency(prev_dst_gw, e_route->gw_src,
104                                     &res->link_list, lat);
105     }
106
107     links = e_route->link_list;
108     xbt_dynar_foreach(links, cpt, link) {
109       xbt_dynar_push_as(res->link_list, sg_routing_link_t, link);
110       if (lat)
111         *lat += surf_network_model->extension.network.get_link_latency(link);
112     }
113
114     prev_dst_gw = e_route->gw_dst;
115   }
116   xbt_dynar_free(&route_stack);
117 }
118
119 static void floyd_finalize(AS_t rc)
120 {
121   as_floyd_t as = (as_floyd_t) rc;
122   int i, j;
123   size_t table_size;
124   if (as) {
125     table_size = xbt_dynar_length(as->generic_routing.index_network_elm);
126     /* Delete link_table */
127     for (i = 0; i < table_size; i++)
128       for (j = 0; j < table_size; j++)
129         generic_free_route(TO_FLOYD_LINK(i, j));
130     xbt_free(as->link_table);
131     /* Delete bypass dict */
132     xbt_dict_free(&as->generic_routing.bypassRoutes);
133     /* Delete predecessor and cost table */
134     xbt_free(as->predecessor_table);
135     xbt_free(as->cost_table);
136
137     model_generic_finalize(rc);
138   }
139 }
140
141 AS_t model_floyd_create(void)
142 {
143   as_floyd_t new_component = (as_floyd_t)model_generic_create_sized(sizeof(s_as_floyd_t));
144   new_component->generic_routing.parse_route = model_floyd_parse_route;
145   new_component->generic_routing.parse_ASroute = model_floyd_parse_route;
146   new_component->generic_routing.get_route_and_latency = floyd_get_route_and_latency;
147   new_component->generic_routing.get_onelink_routes =
148       floyd_get_onelink_routes;
149   new_component->generic_routing.finalize = floyd_finalize;
150   return (AS_t)new_component;
151 }
152
153 void model_floyd_end(AS_t current_routing)
154 {
155
156   as_floyd_t as =
157       ((as_floyd_t) current_routing);
158
159   unsigned int i, j, a, b, c;
160
161   /* set the size of table routing */
162   size_t table_size = xbt_dynar_length(as->generic_routing.index_network_elm);
163
164   if(!as->link_table)
165   {
166     /* Create Cost, Predecessor and Link tables */
167     as->cost_table = xbt_new0(double, table_size * table_size);       /* link cost from host to host */
168     as->predecessor_table = xbt_new0(int, table_size * table_size);  /* predecessor host numbers */
169     as->link_table = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);    /* actual link between src and dst */
170
171     /* Initialize costs and predecessors */
172     for (i = 0; i < table_size; i++)
173       for (j = 0; j < table_size; j++) {
174         TO_FLOYD_COST(i, j) = DBL_MAX;
175         TO_FLOYD_PRED(i, j) = -1;
176         TO_FLOYD_LINK(i, j) = NULL;       /* fixed, missing in the previous version */
177       }
178   }
179
180   /* Add the loopback if needed */
181   if (routing_platf->loopback && current_routing->hierarchy == SURF_ROUTING_BASE) {
182     for (i = 0; i < table_size; i++) {
183       sg_platf_route_cbarg_t e_route = TO_FLOYD_LINK(i, i);
184       if (!e_route) {
185         e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
186         e_route->gw_src = NULL;
187         e_route->gw_dst = NULL;
188         e_route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
189         xbt_dynar_push(e_route->link_list, &routing_platf->loopback);
190         TO_FLOYD_LINK(i, i) = e_route;
191         TO_FLOYD_PRED(i, i) = i;
192         TO_FLOYD_COST(i, i) = 1;
193       }
194     }
195   }
196   /* Calculate path costs */
197   for (c = 0; c < table_size; c++) {
198     for (a = 0; a < table_size; a++) {
199       for (b = 0; b < table_size; b++) {
200         if (TO_FLOYD_COST(a, c) < DBL_MAX && TO_FLOYD_COST(c, b) < DBL_MAX) {
201           if (TO_FLOYD_COST(a, b) == DBL_MAX ||
202               (TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b) <
203                   TO_FLOYD_COST(a, b))) {
204             TO_FLOYD_COST(a, b) =
205                 TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b);
206             TO_FLOYD_PRED(a, b) = TO_FLOYD_PRED(c, b);
207           }
208         }
209       }
210     }
211   }
212 }
213
214 static int floyd_pointer_resource_cmp(const void *a, const void *b) {
215   return a != b;
216 }
217
218 //FIXME: kill dupplicates in next function with full routing
219
220 void model_floyd_parse_route(AS_t rc, sg_platf_route_cbarg_t route)
221 {
222   char *src = (char*)(route->src);
223   char *dst = (char*)(route->dst);
224
225   int as_route = 0;
226   as_floyd_t as = (as_floyd_t) rc;
227
228   /* set the size of table routing */
229   size_t table_size = xbt_dynar_length(rc->index_network_elm);
230   sg_routing_edge_t src_net_elm, dst_net_elm;
231
232   src_net_elm = sg_routing_edge_by_name_or_null(src);
233   dst_net_elm = sg_routing_edge_by_name_or_null(dst);
234
235   xbt_assert(src_net_elm, "Network elements %s not found", src);
236   xbt_assert(dst_net_elm, "Network elements %s not found", dst);
237
238   if(!as->link_table)
239   {
240     int i,j;
241     /* Create Cost, Predecessor and Link tables */
242     as->cost_table = xbt_new0(double, table_size * table_size);       /* link cost from host to host */
243     as->predecessor_table = xbt_new0(int, table_size * table_size);  /* predecessor host numbers */
244     as->link_table = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);    /* actual link between src and dst */
245
246     /* Initialize costs and predecessors */
247     for (i = 0; i < table_size; i++)
248       for (j = 0; j < table_size; j++) {
249         TO_FLOYD_COST(i, j) = DBL_MAX;
250         TO_FLOYD_PRED(i, j) = -1;
251         TO_FLOYD_LINK(i, j) = NULL;       /* fixed, missing in the previous version */
252       }
253   }
254   if(!route->gw_dst && !route->gw_src)
255     XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
256   else{
257     as_route = 1;
258     XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
259         route->gw_src->name, dst, route->gw_dst->name);
260     if(route->gw_dst->rc_type == SURF_NETWORK_ELEMENT_NULL)
261       xbt_die("The dst_gateway '%s' does not exist!",route->gw_dst->name);
262     if(route->gw_src->rc_type == SURF_NETWORK_ELEMENT_NULL)
263       xbt_die("The src_gateway '%s' does not exist!",route->gw_src->name);
264   }
265
266   if(TO_FLOYD_LINK(src_net_elm->id, dst_net_elm->id))
267   {
268
269     char * link_name;
270     unsigned int cpt;
271     xbt_dynar_t link_route_to_test = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
272     xbt_dynar_foreach(route->link_list,cpt,link_name)
273     {
274       void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
275       xbt_assert(link,"Link : '%s' doesn't exists.",link_name);
276       xbt_dynar_push(link_route_to_test,&link);
277     }
278     xbt_assert(!xbt_dynar_compare(
279         (void*)TO_FLOYD_LINK(src_net_elm->id, dst_net_elm->id)->link_list,
280         (void*)link_route_to_test,
281         (int_f_cpvoid_cpvoid_t) floyd_pointer_resource_cmp),
282         "The route between \"%s\" and \"%s\" already exists", src,dst);
283   }
284   else
285   {
286     TO_FLOYD_LINK(src_net_elm->id, dst_net_elm->id) =
287         generic_new_extended_route(rc->hierarchy, route, 1);
288     TO_FLOYD_PRED(src_net_elm->id, dst_net_elm->id) = src_net_elm->id;
289     TO_FLOYD_COST(src_net_elm->id, dst_net_elm->id) =
290         ((TO_FLOYD_LINK(src_net_elm->id, dst_net_elm->id))->link_list)->used;   /* count of links, old model assume 1 */
291   }
292
293   if ( (route->symmetrical == TRUE && as_route == 0)
294       || (route->symmetrical == TRUE && as_route == 1)
295   )
296   {
297     if(TO_FLOYD_LINK(dst_net_elm->id, src_net_elm->id))
298     {
299       if(!route->gw_dst && !route->gw_src)
300         XBT_DEBUG("See Route from \"%s\" to \"%s\"", dst, src);
301       else
302         XBT_DEBUG("See ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
303             route->gw_src->name, src, route->gw_dst->name);
304       char * link_name;
305       unsigned int i;
306       xbt_dynar_t link_route_to_test = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
307       for(i=xbt_dynar_length(route->link_list) ;i>0 ;i--)
308       {
309         link_name = xbt_dynar_get_as(route->link_list,i-1,void *);
310         void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
311         xbt_assert(link,"Link : '%s' doesn't exists.",link_name);
312         xbt_dynar_push(link_route_to_test,&link);
313       }
314       xbt_assert(!xbt_dynar_compare(
315           (void*)TO_FLOYD_LINK(dst_net_elm->id, src_net_elm->id)->link_list,
316           (void*)link_route_to_test,
317           (int_f_cpvoid_cpvoid_t) floyd_pointer_resource_cmp),
318           "The route between \"%s\" and \"%s\" already exists", src,dst);
319     }
320     else
321     {
322       if(route->gw_dst && route->gw_src)
323       {
324         sg_routing_edge_t gw_src = route->gw_src;
325         sg_routing_edge_t gw_dst = route->gw_dst;
326         route->gw_src = gw_dst;
327         route->gw_dst = gw_src;
328       }
329
330       if(!route->gw_src && !route->gw_dst)
331         XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
332       else
333         XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
334             route->gw_src->name, src, route->gw_dst->name);
335
336       TO_FLOYD_LINK(dst_net_elm->id, src_net_elm->id) =
337           generic_new_extended_route(rc->hierarchy, route, 0);
338       TO_FLOYD_PRED(dst_net_elm->id, src_net_elm->id) = dst_net_elm->id;
339       TO_FLOYD_COST(dst_net_elm->id, src_net_elm->id) =
340           ((TO_FLOYD_LINK(dst_net_elm->id, src_net_elm->id))->link_list)->used;   /* count of links, old model assume 1 */
341     }
342   }
343   xbt_dynar_free(&route->link_list);
344 }