1 /* Copyright (c) 2009-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "src/surf/surf_routing_private.hpp"
8 #include "src/surf/surf_routing_floyd.hpp"
9 #include "src/surf/network_interface.hpp"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_floyd, surf, "Routing part of surf");
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]
17 AS_t model_floyd_create(void)
19 return new simgrid::surf::AsFloyd();
22 void model_floyd_end(AS_t current_routing)
24 static_cast<simgrid::surf::AsFloyd*>(current_routing)->end();
30 AsFloyd::AsFloyd(): AsGeneric() {
31 p_predecessorTable = NULL;
39 table_size = (int)xbt_dynar_length(p_indexNetworkElm);
40 if (p_linkTable == NULL) // Dealing with a parse error in the file?
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));
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);
55 /* Business methods */
56 xbt_dynar_t AsFloyd::getOneLinkRoutes()
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);
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);
72 if (xbt_dynar_length(route->link_list) == 1) {
73 void *link = *(void **) xbt_dynar_get_ptr(route->link_list, 0);
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);
80 onelink = new Onelink(link, NULL, NULL);
81 xbt_dynar_push(ret, &onelink);
89 void AsFloyd::getRouteAndLatency(NetCard *src, NetCard *dst, sg_platf_route_cbarg_t res, double *lat)
93 size_t table_size = xbt_dynar_length(p_indexNetworkElm);
95 this->srcDstCheck(src, dst);
97 /* create a result route */
98 xbt_dynar_t route_stack = xbt_dynar_new(sizeof(sg_platf_route_cbarg_t), NULL);
100 int cur = dst->getId();
102 pred = TO_FLOYD_PRED(src->getId(), cur);
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));
107 } while (cur != src->getId());
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;
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);
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);
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);
131 *lat += static_cast<Link*>(link)->getLatency();
134 prev_dst_gw = e_route->gw_dst;
136 xbt_dynar_free(&route_stack);
139 static int floyd_pointer_resource_cmp(const void *a, const void *b) {
143 void AsFloyd::parseASroute(sg_platf_route_cbarg_t route){
147 void AsFloyd::parseRoute(sg_platf_route_cbarg_t route)
149 char *src = (char*)(route->src);
150 char *dst = (char*)(route->dst);
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;
158 src_net_elm = sg_netcard_by_name_or_null(src);
159 dst_net_elm = sg_netcard_by_name_or_null(dst);
161 xbt_assert(src_net_elm, "Network elements %s not found", src);
162 xbt_assert(dst_net_elm, "Network elements %s not found", dst);
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 */
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 */
180 if(!route->gw_dst && !route->gw_src)
181 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
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());
192 if(TO_FLOYD_LINK(src_net_elm->getId(), dst_net_elm->getId()))
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)
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);
204 xbt_assert(!xbt_dynar_compare(
205 TO_FLOYD_LINK(src_net_elm->getId(), dst_net_elm->getId())->link_list,
207 (int_f_cpvoid_cpvoid_t) floyd_pointer_resource_cmp),
208 "The route between \"%s\" and \"%s\" already exists", src,dst);
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 */
219 if ( (route->symmetrical == TRUE && as_route == 0)
220 || (route->symmetrical == TRUE && as_route == 1)
223 if(TO_FLOYD_LINK(dst_net_elm->getId(), src_net_elm->getId()))
225 if(!route->gw_dst && !route->gw_src)
226 XBT_DEBUG("See Route from \"%s\" to \"%s\"", dst, src);
228 XBT_DEBUG("See ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
229 route->gw_src->getName(), src, route->gw_dst->getName());
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--)
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);
240 xbt_assert(!xbt_dynar_compare(
241 TO_FLOYD_LINK(dst_net_elm->getId(), src_net_elm->getId())->link_list,
243 (int_f_cpvoid_cpvoid_t) floyd_pointer_resource_cmp),
244 "The route between \"%s\" and \"%s\" already exists", src,dst);
248 if(route->gw_dst && route->gw_src)
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;
256 if(!route->gw_src && !route->gw_dst)
257 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
259 XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
260 route->gw_src->getName(), src, route->gw_dst->getName());
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 */
269 xbt_dynar_free(&route->link_list);
273 unsigned int i, j, a, b, c;
275 /* set the size of table routing */
276 size_t table_size = xbt_dynar_length(p_indexNetworkElm);
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 */
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 */
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);
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;
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);