Logo AND Algorithmique Numérique Distribuée

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