1 #include "surf_routing_private.h"
2 #include "surf_routing_floyd.hpp"
6 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_floyd, surf, "Routing part of surf");
9 #define TO_FLOYD_COST(i,j) (p_costTable)[(i)+(j)*table_size]
10 #define TO_FLOYD_PRED(i,j) (p_predecessorTable)[(i)+(j)*table_size]
11 #define TO_FLOYD_LINK(i,j) (p_linkTable)[(i)+(j)*table_size]
13 AS_t model_floyd_create(void)
18 void model_floyd_end(AS_t current_routing)
20 static_cast<AsFloydPtr>(current_routing)->end();
23 AsFloyd::AsFloyd(): AsGeneric() {
29 table_size = xbt_dynar_length(p_indexNetworkElm);
30 /* Delete link_table */
31 for (i = 0; i < table_size; i++)
32 for (j = 0; j < table_size; j++)
33 generic_free_route(TO_FLOYD_LINK(i, j));
34 xbt_free(p_linkTable);
35 /* Delete bypass dict */
36 xbt_dict_free(&p_bypassRoutes);
37 /* Delete predecessor and cost table */
38 xbt_free(p_predecessorTable);
39 xbt_free(p_costTable);
42 /* Business methods */
43 xbt_dynar_t AsFloyd::getOneLinkRoutes()
45 xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
46 sg_platf_route_cbarg_t route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
47 route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
50 sg_routing_edge_t src_elm, dst_elm;
51 int table_size = xbt_dynar_length(p_indexNetworkElm);
52 for(src=0; src < table_size; src++) {
53 for(dst=0; dst< table_size; dst++) {
54 xbt_dynar_reset(route->link_list);
55 src_elm = xbt_dynar_get_as(p_indexNetworkElm, src, RoutingEdgePtr);
56 dst_elm = xbt_dynar_get_as(p_indexNetworkElm, dst, RoutingEdgePtr);
57 this->getRouteAndLatency(src_elm, dst_elm, route, NULL);
59 if (xbt_dynar_length(route->link_list) == 1) {
60 void *link = *(void **) xbt_dynar_get_ptr(route->link_list, 0);
61 onelink_t onelink = xbt_new0(s_onelink_t, 1);
62 onelink->link_ptr = link;
63 if (p_hierarchy == SURF_ROUTING_BASE) {
64 onelink->src = src_elm;
65 onelink->dst = dst_elm;
66 } else if (p_hierarchy == SURF_ROUTING_RECURSIVE) {
67 onelink->src = route->gw_src;
68 onelink->dst = route->gw_dst;
70 xbt_dynar_push(ret, &onelink);
78 void AsFloyd::getRouteAndLatency(RoutingEdgePtr src, RoutingEdgePtr dst, sg_platf_route_cbarg_t res, double *lat)
82 size_t table_size = xbt_dynar_length(p_indexNetworkElm);
84 this->srcDstCheck(src, dst);
86 /* create a result route */
87 xbt_dynar_t route_stack = xbt_dynar_new(sizeof(sg_platf_route_cbarg_t), NULL);
91 pred = TO_FLOYD_PRED(src->m_id, cur);
93 THROWF(arg_error, 0, "No route from '%s' to '%s'", src->p_name, dst->p_name);
94 xbt_dynar_push_as(route_stack, sg_platf_route_cbarg_t, TO_FLOYD_LINK(pred, cur));
96 } while (cur != src->m_id);
98 if (p_hierarchy == SURF_ROUTING_RECURSIVE) {
99 res->gw_src = xbt_dynar_getlast_as(route_stack, sg_platf_route_cbarg_t)->gw_src;
100 res->gw_dst = xbt_dynar_getfirst_as(route_stack, sg_platf_route_cbarg_t)->gw_dst;
103 sg_routing_edge_t prev_dst_gw = NULL;
104 while (!xbt_dynar_is_empty(route_stack)) {
105 sg_platf_route_cbarg_t e_route = xbt_dynar_pop_as(route_stack, sg_platf_route_cbarg_t);
110 if (p_hierarchy == SURF_ROUTING_RECURSIVE && prev_dst_gw != NULL
111 && strcmp(prev_dst_gw->p_name, e_route->gw_src->p_name)) {
112 routing_get_route_and_latency(prev_dst_gw, e_route->gw_src,
113 &res->link_list, lat);
116 links = e_route->link_list;
117 xbt_dynar_foreach(links, cpt, link) {
118 xbt_dynar_push_as(res->link_list, sg_routing_link_t, link);
120 *lat += dynamic_cast<NetworkCm02LinkPtr>(static_cast<ResourcePtr>(link))->getLatency();
123 prev_dst_gw = e_route->gw_dst;
125 xbt_dynar_free(&route_stack);
128 static int floyd_pointer_resource_cmp(const void *a, const void *b) {
132 void AsFloyd::parseRoute(sg_platf_route_cbarg_t route)
134 char *src = (char*)(route->src);
135 char *dst = (char*)(route->dst);
139 /* set the size of table routing */
140 size_t table_size = xbt_dynar_length(p_indexNetworkElm);
141 RoutingEdgePtr src_net_elm, dst_net_elm;
143 src_net_elm = sg_routing_edge_by_name_or_null(src);
144 dst_net_elm = sg_routing_edge_by_name_or_null(dst);
146 xbt_assert(src_net_elm, "Network elements %s not found", src);
147 xbt_assert(dst_net_elm, "Network elements %s not found", dst);
152 /* Create Cost, Predecessor and Link tables */
153 p_costTable = xbt_new0(double, table_size * table_size); /* link cost from host to host */
154 p_predecessorTable = xbt_new0(int, table_size * table_size); /* predecessor host numbers */
155 p_linkTable = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size); /* actual link between src and dst */
157 /* Initialize costs and predecessors */
158 for (i = 0; i < table_size; i++)
159 for (j = 0; j < table_size; j++) {
160 TO_FLOYD_COST(i, j) = DBL_MAX;
161 TO_FLOYD_PRED(i, j) = -1;
162 TO_FLOYD_LINK(i, j) = NULL; /* fixed, missing in the previous version */
165 if(!route->gw_dst && !route->gw_src)
166 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
169 XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
170 route->gw_src->p_name, dst, route->gw_dst->p_name);
171 if(route->gw_dst->p_rcType == SURF_NETWORK_ELEMENT_NULL)
172 xbt_die("The dst_gateway '%s' does not exist!",route->gw_dst->p_name);
173 if(route->gw_src->p_rcType == SURF_NETWORK_ELEMENT_NULL)
174 xbt_die("The src_gateway '%s' does not exist!",route->gw_src->p_name);
177 if(TO_FLOYD_LINK(src_net_elm->m_id, dst_net_elm->m_id))
182 xbt_dynar_t link_route_to_test = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
183 xbt_dynar_foreach(route->link_list,cpt,link_name)
185 void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
186 xbt_assert(link,"Link : '%s' doesn't exists.",link_name);
187 xbt_dynar_push(link_route_to_test,&link);
189 xbt_assert(!xbt_dynar_compare(
190 TO_FLOYD_LINK(src_net_elm->m_id, dst_net_elm->m_id)->link_list,
192 (int_f_cpvoid_cpvoid_t) floyd_pointer_resource_cmp),
193 "The route between \"%s\" and \"%s\" already exists", src,dst);
197 TO_FLOYD_LINK(src_net_elm->m_id, dst_net_elm->m_id) =
198 newExtendedRoute(p_hierarchy, route, 1);
199 TO_FLOYD_PRED(src_net_elm->m_id, dst_net_elm->m_id) = src_net_elm->m_id;
200 TO_FLOYD_COST(src_net_elm->m_id, dst_net_elm->m_id) =
201 ((TO_FLOYD_LINK(src_net_elm->m_id, dst_net_elm->m_id))->link_list)->used; /* count of links, old model assume 1 */
204 if ( (route->symmetrical == TRUE && as_route == 0)
205 || (route->symmetrical == TRUE && as_route == 1)
208 if(TO_FLOYD_LINK(dst_net_elm->m_id, src_net_elm->m_id))
210 if(!route->gw_dst && !route->gw_src)
211 XBT_DEBUG("See Route from \"%s\" to \"%s\"", dst, src);
213 XBT_DEBUG("See ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
214 route->gw_src->p_name, src, route->gw_dst->p_name);
217 xbt_dynar_t link_route_to_test = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
218 for(i=xbt_dynar_length(route->link_list) ;i>0 ;i--)
220 link_name = xbt_dynar_get_as(route->link_list,i-1,char *);
221 void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
222 xbt_assert(link,"Link : '%s' doesn't exists.",link_name);
223 xbt_dynar_push(link_route_to_test,&link);
225 xbt_assert(!xbt_dynar_compare(
226 TO_FLOYD_LINK(dst_net_elm->m_id, src_net_elm->m_id)->link_list,
228 (int_f_cpvoid_cpvoid_t) floyd_pointer_resource_cmp),
229 "The route between \"%s\" and \"%s\" already exists", src,dst);
233 if(route->gw_dst && route->gw_src)
235 sg_routing_edge_t gw_src = route->gw_src;
236 sg_routing_edge_t gw_dst = route->gw_dst;
237 route->gw_src = gw_dst;
238 route->gw_dst = gw_src;
241 if(!route->gw_src && !route->gw_dst)
242 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
244 XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
245 route->gw_src->p_name, src, route->gw_dst->p_name);
247 TO_FLOYD_LINK(dst_net_elm->m_id, src_net_elm->m_id) =
248 newExtendedRoute(p_hierarchy, route, 0);
249 TO_FLOYD_PRED(dst_net_elm->m_id, src_net_elm->m_id) = dst_net_elm->m_id;
250 TO_FLOYD_COST(dst_net_elm->m_id, src_net_elm->m_id) =
251 ((TO_FLOYD_LINK(dst_net_elm->m_id, src_net_elm->m_id))->link_list)->used; /* count of links, old model assume 1 */
254 xbt_dynar_free(&route->link_list);
258 unsigned int i, j, a, b, c;
260 /* set the size of table routing */
261 size_t table_size = xbt_dynar_length(p_indexNetworkElm);
264 /* Create Cost, Predecessor and Link tables */
265 p_costTable = xbt_new0(double, table_size * table_size); /* link cost from host to host */
266 p_predecessorTable = xbt_new0(int, table_size * table_size); /* predecessor host numbers */
267 p_linkTable = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size); /* actual link between src and dst */
269 /* Initialize costs and predecessors */
270 for (i = 0; i < table_size; i++)
271 for (j = 0; j < table_size; j++) {
272 TO_FLOYD_COST(i, j) = DBL_MAX;
273 TO_FLOYD_PRED(i, j) = -1;
274 TO_FLOYD_LINK(i, j) = NULL; /* fixed, missing in the previous version */
278 /* Add the loopback if needed */
279 if (routing_platf->p_loopback && p_hierarchy == SURF_ROUTING_BASE) {
280 for (i = 0; i < table_size; i++) {
281 sg_platf_route_cbarg_t e_route = TO_FLOYD_LINK(i, i);
283 e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
284 e_route->gw_src = NULL;
285 e_route->gw_dst = NULL;
286 e_route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
287 xbt_dynar_push(e_route->link_list, &routing_platf->p_loopback);
288 TO_FLOYD_LINK(i, i) = e_route;
289 TO_FLOYD_PRED(i, i) = i;
290 TO_FLOYD_COST(i, i) = 1;
294 /* Calculate path costs */
295 for (c = 0; c < table_size; c++) {
296 for (a = 0; a < table_size; a++) {
297 for (b = 0; b < table_size; b++) {
298 if (TO_FLOYD_COST(a, c) < DBL_MAX && TO_FLOYD_COST(c, b) < DBL_MAX) {
299 if (TO_FLOYD_COST(a, b) == DBL_MAX ||
300 (TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b) <
301 TO_FLOYD_COST(a, b))) {
302 TO_FLOYD_COST(a, b) =
303 TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b);
304 TO_FLOYD_PRED(a, b) = TO_FLOYD_PRED(c, b);