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 "surf_routing_floyd.hpp"
8 #include "network_interface.hpp"
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_floyd, surf, "Routing part of surf");
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]
16 AS_t model_floyd_create(void)
18 return new simgrid::surf::AsFloyd();
21 void model_floyd_end(AS_t current_routing)
23 static_cast<simgrid::surf::AsFloyd*>(current_routing)->end();
29 AsFloyd::AsFloyd(): AsGeneric() {
30 p_predecessorTable = NULL;
38 table_size = (int)xbt_dynar_length(p_indexNetworkElm);
39 if (p_linkTable == NULL) // Dealing with a parse error in the file?
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));
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);
54 /* Business methods */
55 xbt_dynar_t AsFloyd::getOneLinkRoutes()
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);
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);
71 if (xbt_dynar_length(route->link_list) == 1) {
72 void *link = *(void **) xbt_dynar_get_ptr(route->link_list, 0);
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);
79 onelink = new Onelink(link, NULL, NULL);
80 xbt_dynar_push(ret, &onelink);
88 void AsFloyd::getRouteAndLatency(RoutingEdge *src, RoutingEdge *dst, sg_platf_route_cbarg_t res, double *lat)
92 size_t table_size = xbt_dynar_length(p_indexNetworkElm);
94 this->srcDstCheck(src, dst);
96 /* create a result route */
97 xbt_dynar_t route_stack = xbt_dynar_new(sizeof(sg_platf_route_cbarg_t), NULL);
99 int cur = dst->getId();
101 pred = TO_FLOYD_PRED(src->getId(), cur);
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));
106 } while (cur != src->getId());
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;
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);
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);
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);
130 *lat += static_cast<Link*>(link)->getLatency();
133 prev_dst_gw = e_route->gw_dst;
135 xbt_dynar_free(&route_stack);
138 static int floyd_pointer_resource_cmp(const void *a, const void *b) {
142 void AsFloyd::parseASroute(sg_platf_route_cbarg_t route){
146 void AsFloyd::parseRoute(sg_platf_route_cbarg_t route)
148 char *src = (char*)(route->src);
149 char *dst = (char*)(route->dst);
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;
157 src_net_elm = sg_routing_edge_by_name_or_null(src);
158 dst_net_elm = sg_routing_edge_by_name_or_null(dst);
160 xbt_assert(src_net_elm, "Network elements %s not found", src);
161 xbt_assert(dst_net_elm, "Network elements %s not found", dst);
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 */
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 */
179 if(!route->gw_dst && !route->gw_src)
180 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
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());
191 if(TO_FLOYD_LINK(src_net_elm->getId(), dst_net_elm->getId()))
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)
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);
203 xbt_assert(!xbt_dynar_compare(
204 TO_FLOYD_LINK(src_net_elm->getId(), dst_net_elm->getId())->link_list,
206 (int_f_cpvoid_cpvoid_t) floyd_pointer_resource_cmp),
207 "The route between \"%s\" and \"%s\" already exists", src,dst);
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 */
218 if ( (route->symmetrical == TRUE && as_route == 0)
219 || (route->symmetrical == TRUE && as_route == 1)
222 if(TO_FLOYD_LINK(dst_net_elm->getId(), src_net_elm->getId()))
224 if(!route->gw_dst && !route->gw_src)
225 XBT_DEBUG("See Route from \"%s\" to \"%s\"", dst, src);
227 XBT_DEBUG("See ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
228 route->gw_src->getName(), src, route->gw_dst->getName());
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--)
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);
239 xbt_assert(!xbt_dynar_compare(
240 TO_FLOYD_LINK(dst_net_elm->getId(), src_net_elm->getId())->link_list,
242 (int_f_cpvoid_cpvoid_t) floyd_pointer_resource_cmp),
243 "The route between \"%s\" and \"%s\" already exists", src,dst);
247 if(route->gw_dst && route->gw_src)
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;
255 if(!route->gw_src && !route->gw_dst)
256 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
258 XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
259 route->gw_src->getName(), src, route->gw_dst->getName());
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 */
268 xbt_dynar_free(&route->link_list);
272 unsigned int i, j, a, b, c;
274 /* set the size of table routing */
275 size_t table_size = xbt_dynar_length(p_indexNetworkElm);
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 */
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 */
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);
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;
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);