Logo AND Algorithmique Numérique Distribuée

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