Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
pull up another (useless) method in the surf::Host hierarchy
[simgrid.git] / src / surf / surf_routing_floyd.cpp
1 /* Copyright (c) 2009-2015. 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_floyd.hpp"
8 #include "network_interface.hpp"
9
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_floyd, surf, "Routing part of surf");
11
12 #define TO_FLOYD_COST(i,j) (p_costTable)[(i)+(j)*table_size]
13 #define TO_FLOYD_PRED(i,j) (p_predecessorTable)[(i)+(j)*table_size]
14 #define TO_FLOYD_LINK(i,j) (p_linkTable)[(i)+(j)*table_size]
15
16 AS_t model_floyd_create(void)
17 {
18   return new simgrid::surf::AsFloyd();
19 }
20
21 void model_floyd_end(AS_t current_routing)
22 {
23   static_cast<simgrid::surf::AsFloyd*>(current_routing)->end();
24 }
25
26 namespace simgrid {
27 namespace surf {
28
29 AsFloyd::AsFloyd(): AsGeneric() {
30   p_predecessorTable = NULL;
31   p_costTable = NULL;
32   p_linkTable = NULL;
33 }
34
35 AsFloyd::~AsFloyd(){
36   int i, j;
37   int table_size;
38   table_size = (int)xbt_dynar_length(p_indexNetworkElm);
39   if (p_linkTable == NULL) // Dealing with a parse error in the file?
40           return;
41   /* Delete link_table */
42   for (i = 0; i < table_size; i++)
43           for (j = 0; j < table_size; j++) {
44                   generic_free_route(TO_FLOYD_LINK(i, j));
45           }
46   xbt_free(p_linkTable);
47   /* Delete bypass dict */
48   xbt_dict_free(&p_bypassRoutes);
49   /* Delete predecessor and cost table */
50   xbt_free(p_predecessorTable);
51   xbt_free(p_costTable);
52 }
53
54 /* Business methods */
55 xbt_dynar_t AsFloyd::getOneLinkRoutes()
56 {
57   xbt_dynar_t ret = xbt_dynar_new(sizeof(Onelink*), xbt_free_f);
58   sg_platf_route_cbarg_t route =   xbt_new0(s_sg_platf_route_cbarg_t, 1);
59   route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
60
61   int src,dst;
62   sg_routing_edge_t src_elm, dst_elm;
63   int table_size = xbt_dynar_length(p_indexNetworkElm);
64   for(src=0; src < table_size; src++) {
65     for(dst=0; dst< table_size; dst++) {
66       xbt_dynar_reset(route->link_list);
67       src_elm = xbt_dynar_get_as(p_indexNetworkElm, src, RoutingEdge*);
68       dst_elm = xbt_dynar_get_as(p_indexNetworkElm, dst, RoutingEdge*);
69       this->getRouteAndLatency(src_elm, dst_elm, route, NULL);
70
71       if (xbt_dynar_length(route->link_list) == 1) {
72         void *link = *(void **) xbt_dynar_get_ptr(route->link_list, 0);
73         Onelink *onelink;
74         if (p_hierarchy == SURF_ROUTING_BASE)
75           onelink = new Onelink(link, src_elm, dst_elm);
76         else if (p_hierarchy == SURF_ROUTING_RECURSIVE)
77           onelink = new Onelink(link, route->gw_src, route->gw_dst);
78         else
79           onelink = new Onelink(link, NULL, NULL);
80         xbt_dynar_push(ret, &onelink);
81       }
82     }
83   }
84
85   return ret;
86 }
87
88 void AsFloyd::getRouteAndLatency(RoutingEdge *src, RoutingEdge *dst, sg_platf_route_cbarg_t res, double *lat)
89 {
90
91   /* set utils vars */
92   size_t table_size = xbt_dynar_length(p_indexNetworkElm);
93
94   this->srcDstCheck(src, dst);
95
96   /* create a result route */
97   xbt_dynar_t route_stack = xbt_dynar_new(sizeof(sg_platf_route_cbarg_t), NULL);
98   int pred;
99   int cur = dst->getId();
100   do {
101     pred = TO_FLOYD_PRED(src->getId(), cur);
102     if (pred == -1)
103       THROWF(arg_error, 0, "No route from '%s' to '%s'", src->getName(), dst->getName());
104     xbt_dynar_push_as(route_stack, sg_platf_route_cbarg_t, TO_FLOYD_LINK(pred, cur));
105     cur = pred;
106   } while (cur != src->getId());
107
108   if (p_hierarchy == SURF_ROUTING_RECURSIVE) {
109     res->gw_src = xbt_dynar_getlast_as(route_stack, sg_platf_route_cbarg_t)->gw_src;
110     res->gw_dst = xbt_dynar_getfirst_as(route_stack, sg_platf_route_cbarg_t)->gw_dst;
111   }
112
113   sg_routing_edge_t prev_dst_gw = NULL;
114   while (!xbt_dynar_is_empty(route_stack)) {
115     sg_platf_route_cbarg_t e_route = xbt_dynar_pop_as(route_stack, sg_platf_route_cbarg_t);
116     xbt_dynar_t links;
117     void *link;
118     unsigned int cpt;
119
120     if (p_hierarchy == SURF_ROUTING_RECURSIVE && prev_dst_gw != NULL
121         && strcmp(prev_dst_gw->getName(), e_route->gw_src->getName())) {
122       routing_get_route_and_latency(prev_dst_gw, e_route->gw_src,
123                                     &res->link_list, lat);
124     }
125
126     links = e_route->link_list;
127     xbt_dynar_foreach(links, cpt, link) {
128       xbt_dynar_push_as(res->link_list, sg_routing_link_t, link);
129       if (lat)
130         *lat += static_cast<Link*>(link)->getLatency();
131     }
132
133     prev_dst_gw = e_route->gw_dst;
134   }
135   xbt_dynar_free(&route_stack);
136 }
137
138 static int floyd_pointer_resource_cmp(const void *a, const void *b) {
139   return a != b;
140 }
141
142 void AsFloyd::parseASroute(sg_platf_route_cbarg_t route){
143   parseRoute(route);
144 }
145
146 void AsFloyd::parseRoute(sg_platf_route_cbarg_t route)
147 {
148   char *src = (char*)(route->src);
149   char *dst = (char*)(route->dst);
150
151   int as_route = 0;
152
153   /* set the size of table routing */
154   int table_size = (int)xbt_dynar_length(p_indexNetworkElm);
155   RoutingEdge *src_net_elm, *dst_net_elm;
156
157   src_net_elm = sg_routing_edge_by_name_or_null(src);
158   dst_net_elm = sg_routing_edge_by_name_or_null(dst);
159
160   xbt_assert(src_net_elm, "Network elements %s not found", src);
161   xbt_assert(dst_net_elm, "Network elements %s not found", dst);
162
163   if(!p_linkTable)
164   {
165     int i,j;
166     /* Create Cost, Predecessor and Link tables */
167     p_costTable = xbt_new0(double, table_size * table_size);       /* link cost from host to host */
168     p_predecessorTable = xbt_new0(int, table_size * table_size);  /* predecessor host numbers */
169     p_linkTable = 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   if(!route->gw_dst && !route->gw_src)
180     XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
181   else{
182     as_route = 1;
183     XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
184         route->gw_src->getName(), dst, route->gw_dst->getName());
185     if(route->gw_dst->getRcType() == SURF_NETWORK_ELEMENT_NULL)
186       surf_parse_error("The dst_gateway '%s' does not exist!",route->gw_dst->getName());
187     if(route->gw_src->getRcType() == SURF_NETWORK_ELEMENT_NULL)
188       surf_parse_error("The src_gateway '%s' does not exist!",route->gw_src->getName());
189   }
190
191   if(TO_FLOYD_LINK(src_net_elm->getId(), dst_net_elm->getId()))
192   {
193
194     char * link_name;
195     unsigned int cpt;
196     xbt_dynar_t link_route_to_test = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
197     xbt_dynar_foreach(route->link_list,cpt,link_name)
198     {
199       void *link = Link::byName(link_name);
200       xbt_assert(link,"Link : '%s' doesn't exists.",link_name);
201       xbt_dynar_push(link_route_to_test,&link);
202     }
203     xbt_assert(!xbt_dynar_compare(
204         TO_FLOYD_LINK(src_net_elm->getId(), dst_net_elm->getId())->link_list,
205         link_route_to_test,
206         (int_f_cpvoid_cpvoid_t) floyd_pointer_resource_cmp),
207         "The route between \"%s\" and \"%s\" already exists", src,dst);
208   }
209   else
210   {
211     TO_FLOYD_LINK(src_net_elm->getId(), dst_net_elm->getId()) =
212         newExtendedRoute(p_hierarchy, route, 1);
213     TO_FLOYD_PRED(src_net_elm->getId(), dst_net_elm->getId()) = src_net_elm->getId();
214     TO_FLOYD_COST(src_net_elm->getId(), dst_net_elm->getId()) =
215         ((TO_FLOYD_LINK(src_net_elm->getId(), dst_net_elm->getId()))->link_list)->used;   /* count of links, old model assume 1 */
216   }
217
218   if ( (route->symmetrical == TRUE && as_route == 0)
219       || (route->symmetrical == TRUE && as_route == 1)
220   )
221   {
222     if(TO_FLOYD_LINK(dst_net_elm->getId(), src_net_elm->getId()))
223     {
224       if(!route->gw_dst && !route->gw_src)
225         XBT_DEBUG("See Route from \"%s\" to \"%s\"", dst, src);
226       else
227         XBT_DEBUG("See ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
228             route->gw_src->getName(), src, route->gw_dst->getName());
229       char * link_name;
230       unsigned int i;
231       xbt_dynar_t link_route_to_test = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
232       for(i=xbt_dynar_length(route->link_list) ;i>0 ;i--)
233       {
234         link_name = xbt_dynar_get_as(route->link_list,i-1,char *);
235         void *link = Link::byName(link_name);
236         xbt_assert(link,"Link : '%s' doesn't exists.",link_name);
237         xbt_dynar_push(link_route_to_test,&link);
238       }
239       xbt_assert(!xbt_dynar_compare(
240           TO_FLOYD_LINK(dst_net_elm->getId(), src_net_elm->getId())->link_list,
241           link_route_to_test,
242           (int_f_cpvoid_cpvoid_t) floyd_pointer_resource_cmp),
243           "The route between \"%s\" and \"%s\" already exists", src,dst);
244     }
245     else
246     {
247       if(route->gw_dst && route->gw_src)
248       {
249         sg_routing_edge_t gw_src = route->gw_src;
250         sg_routing_edge_t gw_dst = route->gw_dst;
251         route->gw_src = gw_dst;
252         route->gw_dst = gw_src;
253       }
254
255       if(!route->gw_src && !route->gw_dst)
256         XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
257       else
258         XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
259             route->gw_src->getName(), src, route->gw_dst->getName());
260
261       TO_FLOYD_LINK(dst_net_elm->getId(), src_net_elm->getId()) =
262                  newExtendedRoute(p_hierarchy, route, 0);
263       TO_FLOYD_PRED(dst_net_elm->getId(), src_net_elm->getId()) = dst_net_elm->getId();
264       TO_FLOYD_COST(dst_net_elm->getId(), src_net_elm->getId()) =
265           ((TO_FLOYD_LINK(dst_net_elm->getId(), src_net_elm->getId()))->link_list)->used;   /* count of links, old model assume 1 */
266     }
267   }
268   xbt_dynar_free(&route->link_list);
269 }
270
271 void AsFloyd::end(){
272   unsigned int i, j, a, b, c;
273
274   /* set the size of table routing */
275   size_t table_size = xbt_dynar_length(p_indexNetworkElm);
276
277   if(!p_linkTable) {
278     /* Create Cost, Predecessor and Link tables */
279     p_costTable = xbt_new0(double, table_size * table_size);       /* link cost from host to host */
280     p_predecessorTable = xbt_new0(int, table_size * table_size);  /* predecessor host numbers */
281     p_linkTable = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);    /* actual link between src and dst */
282
283     /* Initialize costs and predecessors */
284     for (i = 0; i < table_size; i++)
285       for (j = 0; j < table_size; j++) {
286         TO_FLOYD_COST(i, j) = DBL_MAX;
287         TO_FLOYD_PRED(i, j) = -1;
288         TO_FLOYD_LINK(i, j) = NULL;       /* fixed, missing in the previous version */
289       }
290   }
291
292   /* Add the loopback if needed */
293   if (routing_platf->p_loopback && p_hierarchy == SURF_ROUTING_BASE) {
294     for (i = 0; i < table_size; i++) {
295       sg_platf_route_cbarg_t e_route = TO_FLOYD_LINK(i, i);
296       if (!e_route) {
297         e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
298         e_route->gw_src = NULL;
299         e_route->gw_dst = NULL;
300         e_route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
301         xbt_dynar_push(e_route->link_list, &routing_platf->p_loopback);
302         TO_FLOYD_LINK(i, i) = e_route;
303         TO_FLOYD_PRED(i, i) = i;
304         TO_FLOYD_COST(i, i) = 1;
305       }
306     }
307   }
308   /* Calculate path costs */
309   for (c = 0; c < table_size; c++) {
310     for (a = 0; a < table_size; a++) {
311       for (b = 0; b < table_size; b++) {
312         if (TO_FLOYD_COST(a, c) < DBL_MAX && TO_FLOYD_COST(c, b) < DBL_MAX) {
313           if (TO_FLOYD_COST(a, b) == DBL_MAX ||
314               (TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b) <
315                   TO_FLOYD_COST(a, b))) {
316             TO_FLOYD_COST(a, b) =
317                 TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b);
318             TO_FLOYD_PRED(a, b) = TO_FLOYD_PRED(c, b);
319           }
320         }
321       }
322     }
323   }
324 }
325
326 }
327 }