Logo AND Algorithmique Numérique Distribuée

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