1 /* Copyright (c) 2009-2013. 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"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_floyd, surf, "Routing part of surf");
14 #define TO_FLOYD_COST(i,j) (p_costTable)[(i)+(j)*table_size]
15 #define TO_FLOYD_PRED(i,j) (p_predecessorTable)[(i)+(j)*table_size]
16 #define TO_FLOYD_LINK(i,j) (p_linkTable)[(i)+(j)*table_size]
18 AS_t model_floyd_create(void)
23 void model_floyd_end(AS_t current_routing)
25 static_cast<AsFloydPtr>(current_routing)->end();
28 AsFloyd::AsFloyd(): AsGeneric() {
29 p_predecessorTable = NULL;
37 table_size = (int)xbt_dynar_length(p_indexNetworkElm);
38 /* Delete link_table */
39 for (i = 0; i < table_size; i++)
40 for (j = 0; j < table_size; j++)
41 generic_free_route(TO_FLOYD_LINK(i, j));
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);
50 /* Business methods */
51 xbt_dynar_t AsFloyd::getOneLinkRoutes()
53 xbt_dynar_t ret = xbt_dynar_new(sizeof(OnelinkPtr), xbt_free);
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);
58 sg_routing_edge_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, RoutingEdgePtr);
64 dst_elm = xbt_dynar_get_as(p_indexNetworkElm, dst, RoutingEdgePtr);
65 this->getRouteAndLatency(src_elm, dst_elm, route, NULL);
67 if (xbt_dynar_length(route->link_list) == 1) {
68 void *link = *(void **) xbt_dynar_get_ptr(route->link_list, 0);
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);
75 onelink = new Onelink(link, NULL, NULL);
76 xbt_dynar_push(ret, &onelink);
84 void AsFloyd::getRouteAndLatency(RoutingEdgePtr src, RoutingEdgePtr dst, sg_platf_route_cbarg_t res, double *lat)
88 size_t table_size = xbt_dynar_length(p_indexNetworkElm);
90 this->srcDstCheck(src, dst);
92 /* create a result route */
93 xbt_dynar_t route_stack = xbt_dynar_new(sizeof(sg_platf_route_cbarg_t), NULL);
97 pred = TO_FLOYD_PRED(src->m_id, cur);
99 THROWF(arg_error, 0, "No route from '%s' to '%s'", src->p_name, dst->p_name);
100 xbt_dynar_push_as(route_stack, sg_platf_route_cbarg_t, TO_FLOYD_LINK(pred, cur));
102 } while (cur != src->m_id);
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;
109 sg_routing_edge_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);
116 if (p_hierarchy == SURF_ROUTING_RECURSIVE && prev_dst_gw != NULL
117 && strcmp(prev_dst_gw->p_name, e_route->gw_src->p_name)) {
118 routing_get_route_and_latency(prev_dst_gw, e_route->gw_src,
119 &res->link_list, lat);
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);
126 *lat += dynamic_cast<NetworkCm02LinkPtr>(static_cast<ResourcePtr>(link))->getLatency();
129 prev_dst_gw = e_route->gw_dst;
131 xbt_dynar_free(&route_stack);
134 static int floyd_pointer_resource_cmp(const void *a, const void *b) {
138 void AsFloyd::parseASroute(sg_platf_route_cbarg_t route){
142 void AsFloyd::parseRoute(sg_platf_route_cbarg_t route)
144 char *src = (char*)(route->src);
145 char *dst = (char*)(route->dst);
149 /* set the size of table routing */
150 int table_size = (int)xbt_dynar_length(p_indexNetworkElm);
151 RoutingEdgePtr src_net_elm, dst_net_elm;
153 src_net_elm = sg_routing_edge_by_name_or_null(src);
154 dst_net_elm = sg_routing_edge_by_name_or_null(dst);
156 xbt_assert(src_net_elm, "Network elements %s not found", src);
157 xbt_assert(dst_net_elm, "Network elements %s not found", dst);
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 */
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 */
175 if(!route->gw_dst && !route->gw_src)
176 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
179 XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
180 route->gw_src->p_name, dst, route->gw_dst->p_name);
181 if(route->gw_dst->p_rcType == SURF_NETWORK_ELEMENT_NULL)
182 xbt_die("The dst_gateway '%s' does not exist!",route->gw_dst->p_name);
183 if(route->gw_src->p_rcType == SURF_NETWORK_ELEMENT_NULL)
184 xbt_die("The src_gateway '%s' does not exist!",route->gw_src->p_name);
187 if(TO_FLOYD_LINK(src_net_elm->m_id, dst_net_elm->m_id))
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)
195 void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
196 xbt_assert(link,"Link : '%s' doesn't exists.",link_name);
197 xbt_dynar_push(link_route_to_test,&link);
199 xbt_assert(!xbt_dynar_compare(
200 TO_FLOYD_LINK(src_net_elm->m_id, dst_net_elm->m_id)->link_list,
202 (int_f_cpvoid_cpvoid_t) floyd_pointer_resource_cmp),
203 "The route between \"%s\" and \"%s\" already exists", src,dst);
207 TO_FLOYD_LINK(src_net_elm->m_id, dst_net_elm->m_id) =
208 newExtendedRoute(p_hierarchy, route, 1);
209 TO_FLOYD_PRED(src_net_elm->m_id, dst_net_elm->m_id) = src_net_elm->m_id;
210 TO_FLOYD_COST(src_net_elm->m_id, dst_net_elm->m_id) =
211 ((TO_FLOYD_LINK(src_net_elm->m_id, dst_net_elm->m_id))->link_list)->used; /* count of links, old model assume 1 */
214 if ( (route->symmetrical == TRUE && as_route == 0)
215 || (route->symmetrical == TRUE && as_route == 1)
218 if(TO_FLOYD_LINK(dst_net_elm->m_id, src_net_elm->m_id))
220 if(!route->gw_dst && !route->gw_src)
221 XBT_DEBUG("See Route from \"%s\" to \"%s\"", dst, src);
223 XBT_DEBUG("See ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
224 route->gw_src->p_name, src, route->gw_dst->p_name);
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--)
230 link_name = xbt_dynar_get_as(route->link_list,i-1,char *);
231 void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
232 xbt_assert(link,"Link : '%s' doesn't exists.",link_name);
233 xbt_dynar_push(link_route_to_test,&link);
235 xbt_assert(!xbt_dynar_compare(
236 TO_FLOYD_LINK(dst_net_elm->m_id, src_net_elm->m_id)->link_list,
238 (int_f_cpvoid_cpvoid_t) floyd_pointer_resource_cmp),
239 "The route between \"%s\" and \"%s\" already exists", src,dst);
243 if(route->gw_dst && route->gw_src)
245 sg_routing_edge_t gw_src = route->gw_src;
246 sg_routing_edge_t gw_dst = route->gw_dst;
247 route->gw_src = gw_dst;
248 route->gw_dst = gw_src;
251 if(!route->gw_src && !route->gw_dst)
252 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
254 XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
255 route->gw_src->p_name, src, route->gw_dst->p_name);
257 TO_FLOYD_LINK(dst_net_elm->m_id, src_net_elm->m_id) =
258 newExtendedRoute(p_hierarchy, route, 0);
259 TO_FLOYD_PRED(dst_net_elm->m_id, src_net_elm->m_id) = dst_net_elm->m_id;
260 TO_FLOYD_COST(dst_net_elm->m_id, src_net_elm->m_id) =
261 ((TO_FLOYD_LINK(dst_net_elm->m_id, src_net_elm->m_id))->link_list)->used; /* count of links, old model assume 1 */
264 xbt_dynar_free(&route->link_list);
268 unsigned int i, j, a, b, c;
270 /* set the size of table routing */
271 size_t table_size = xbt_dynar_length(p_indexNetworkElm);
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 */
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 */
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);
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;
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);