Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
when parallel ctests are performed, using the default tracing filename may cause...
[simgrid.git] / src / surf / surf_routing_floyd.c
1 /* Copyright (c) 2009-2013. 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 algorithm. */
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.get_graph = generic_get_graph;
150   new_component->generic_routing.finalize = floyd_finalize;
151   return (AS_t)new_component;
152 }
153
154 void model_floyd_end(AS_t current_routing)
155 {
156
157   as_floyd_t as =
158       ((as_floyd_t) current_routing);
159
160   unsigned int i, j, a, b, c;
161
162   /* set the size of table routing */
163   size_t table_size = xbt_dynar_length(as->generic_routing.index_network_elm);
164
165   if(!as->link_table)
166   {
167     /* Create Cost, Predecessor and Link tables */
168     as->cost_table = xbt_new0(double, table_size * table_size);       /* link cost from host to host */
169     as->predecessor_table = xbt_new0(int, table_size * table_size);  /* predecessor host numbers */
170     as->link_table = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);    /* actual link between src and dst */
171
172     /* Initialize costs and predecessors */
173     for (i = 0; i < table_size; i++)
174       for (j = 0; j < table_size; j++) {
175         TO_FLOYD_COST(i, j) = DBL_MAX;
176         TO_FLOYD_PRED(i, j) = -1;
177         TO_FLOYD_LINK(i, j) = NULL;       /* fixed, missing in the previous version */
178       }
179   }
180
181   /* Add the loopback if needed */
182   if (routing_platf->loopback && current_routing->hierarchy == SURF_ROUTING_BASE) {
183     for (i = 0; i < table_size; i++) {
184       sg_platf_route_cbarg_t e_route = TO_FLOYD_LINK(i, i);
185       if (!e_route) {
186         e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
187         e_route->gw_src = NULL;
188         e_route->gw_dst = NULL;
189         e_route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
190         xbt_dynar_push(e_route->link_list, &routing_platf->loopback);
191         TO_FLOYD_LINK(i, i) = e_route;
192         TO_FLOYD_PRED(i, i) = i;
193         TO_FLOYD_COST(i, i) = 1;
194       }
195     }
196   }
197   /* Calculate path costs */
198   for (c = 0; c < table_size; c++) {
199     for (a = 0; a < table_size; a++) {
200       for (b = 0; b < table_size; b++) {
201         if (TO_FLOYD_COST(a, c) < DBL_MAX && TO_FLOYD_COST(c, b) < DBL_MAX) {
202           if (TO_FLOYD_COST(a, b) == DBL_MAX ||
203               (TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b) <
204                   TO_FLOYD_COST(a, b))) {
205             TO_FLOYD_COST(a, b) =
206                 TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b);
207             TO_FLOYD_PRED(a, b) = TO_FLOYD_PRED(c, b);
208           }
209         }
210       }
211     }
212   }
213 }
214
215 static int floyd_pointer_resource_cmp(const void *a, const void *b) {
216   return a != b;
217 }
218
219 //FIXME: kill dupplicates in next function with full routing
220
221 void model_floyd_parse_route(AS_t rc, sg_platf_route_cbarg_t route)
222 {
223   char *src = (char*)(route->src);
224   char *dst = (char*)(route->dst);
225
226   int as_route = 0;
227   as_floyd_t as = (as_floyd_t) rc;
228
229   /* set the size of table routing */
230   size_t table_size = xbt_dynar_length(rc->index_network_elm);
231   sg_routing_edge_t src_net_elm, dst_net_elm;
232
233   src_net_elm = sg_routing_edge_by_name_or_null(src);
234   dst_net_elm = sg_routing_edge_by_name_or_null(dst);
235
236   xbt_assert(src_net_elm, "Network elements %s not found", src);
237   xbt_assert(dst_net_elm, "Network elements %s not found", dst);
238
239   if(!as->link_table)
240   {
241     int i,j;
242     /* Create Cost, Predecessor and Link tables */
243     as->cost_table = xbt_new0(double, table_size * table_size);       /* link cost from host to host */
244     as->predecessor_table = xbt_new0(int, table_size * table_size);  /* predecessor host numbers */
245     as->link_table = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);    /* actual link between src and dst */
246
247     /* Initialize costs and predecessors */
248     for (i = 0; i < table_size; i++)
249       for (j = 0; j < table_size; j++) {
250         TO_FLOYD_COST(i, j) = DBL_MAX;
251         TO_FLOYD_PRED(i, j) = -1;
252         TO_FLOYD_LINK(i, j) = NULL;       /* fixed, missing in the previous version */
253       }
254   }
255   if(!route->gw_dst && !route->gw_src)
256     XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
257   else{
258     as_route = 1;
259     XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
260         route->gw_src->name, dst, route->gw_dst->name);
261     if(route->gw_dst->rc_type == SURF_NETWORK_ELEMENT_NULL)
262       xbt_die("The dst_gateway '%s' does not exist!",route->gw_dst->name);
263     if(route->gw_src->rc_type == SURF_NETWORK_ELEMENT_NULL)
264       xbt_die("The src_gateway '%s' does not exist!",route->gw_src->name);
265   }
266
267   if(TO_FLOYD_LINK(src_net_elm->id, dst_net_elm->id))
268   {
269
270     char * link_name;
271     unsigned int cpt;
272     xbt_dynar_t link_route_to_test = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
273     xbt_dynar_foreach(route->link_list,cpt,link_name)
274     {
275       void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
276       xbt_assert(link,"Link : '%s' doesn't exists.",link_name);
277       xbt_dynar_push(link_route_to_test,&link);
278     }
279     xbt_assert(!xbt_dynar_compare(
280         (void*)TO_FLOYD_LINK(src_net_elm->id, dst_net_elm->id)->link_list,
281         (void*)link_route_to_test,
282         (int_f_cpvoid_cpvoid_t) floyd_pointer_resource_cmp),
283         "The route between \"%s\" and \"%s\" already exists", src,dst);
284   }
285   else
286   {
287     TO_FLOYD_LINK(src_net_elm->id, dst_net_elm->id) =
288         generic_new_extended_route(rc->hierarchy, route, 1);
289     TO_FLOYD_PRED(src_net_elm->id, dst_net_elm->id) = src_net_elm->id;
290     TO_FLOYD_COST(src_net_elm->id, dst_net_elm->id) =
291         ((TO_FLOYD_LINK(src_net_elm->id, dst_net_elm->id))->link_list)->used;   /* count of links, old model assume 1 */
292   }
293
294   if ( (route->symmetrical == TRUE && as_route == 0)
295       || (route->symmetrical == TRUE && as_route == 1)
296   )
297   {
298     if(TO_FLOYD_LINK(dst_net_elm->id, src_net_elm->id))
299     {
300       if(!route->gw_dst && !route->gw_src)
301         XBT_DEBUG("See Route from \"%s\" to \"%s\"", dst, src);
302       else
303         XBT_DEBUG("See ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
304             route->gw_src->name, src, route->gw_dst->name);
305       char * link_name;
306       unsigned int i;
307       xbt_dynar_t link_route_to_test = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
308       for(i=xbt_dynar_length(route->link_list) ;i>0 ;i--)
309       {
310         link_name = xbt_dynar_get_as(route->link_list,i-1,void *);
311         void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
312         xbt_assert(link,"Link : '%s' doesn't exists.",link_name);
313         xbt_dynar_push(link_route_to_test,&link);
314       }
315       xbt_assert(!xbt_dynar_compare(
316           (void*)TO_FLOYD_LINK(dst_net_elm->id, src_net_elm->id)->link_list,
317           (void*)link_route_to_test,
318           (int_f_cpvoid_cpvoid_t) floyd_pointer_resource_cmp),
319           "The route between \"%s\" and \"%s\" already exists", src,dst);
320     }
321     else
322     {
323       if(route->gw_dst && route->gw_src)
324       {
325         sg_routing_edge_t gw_src = route->gw_src;
326         sg_routing_edge_t gw_dst = route->gw_dst;
327         route->gw_src = gw_dst;
328         route->gw_dst = gw_src;
329       }
330
331       if(!route->gw_src && !route->gw_dst)
332         XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
333       else
334         XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
335             route->gw_src->name, src, route->gw_dst->name);
336
337       TO_FLOYD_LINK(dst_net_elm->id, src_net_elm->id) =
338           generic_new_extended_route(rc->hierarchy, route, 0);
339       TO_FLOYD_PRED(dst_net_elm->id, src_net_elm->id) = dst_net_elm->id;
340       TO_FLOYD_COST(dst_net_elm->id, src_net_elm->id) =
341           ((TO_FLOYD_LINK(dst_net_elm->id, src_net_elm->id))->link_list)->used;   /* count of links, old model assume 1 */
342     }
343   }
344   xbt_dynar_free(&route->link_list);
345 }