1 /* Copyright (c) 2009-2014. 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_dijkstra.hpp"
8 #include "network_interface.hpp"
11 extern routing_platf_t routing_platf;
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_dijkstra, surf, "Routing part of surf -- dijkstra routing logic");
17 static void route_cache_elem_free(void *e)
19 route_cache_element_t elm = (route_cache_element_t) e;
21 xbt_free(elm->pred_arr);
26 static void graph_node_map_elem_free(void *e)
28 graph_node_map_element_t elm = (graph_node_map_element_t) e;
32 static void graph_edge_data_free(void *e) // FIXME: useless code duplication
34 sg_platf_route_cbarg_t e_route = (sg_platf_route_cbarg_t) e;
36 xbt_dynar_free(&(e_route->link_list));
41 AS_t model_dijkstra_create(void){
42 return new AsDijkstra(0);
45 AS_t model_dijkstracache_create(void){
46 return new AsDijkstra(1);
49 void model_dijkstra_both_end(AS_t as)
51 AsDijkstraPtr THIS_AS = static_cast<AsDijkstraPtr>(as);
52 xbt_node_t node = NULL;
54 xbt_dynar_t nodes = NULL;
56 /* Create the topology graph */
57 if(!THIS_AS->p_routeGraph)
58 THIS_AS->p_routeGraph = xbt_graph_new_graph(1, NULL);
59 if(!THIS_AS->p_graphNodeMap)
60 THIS_AS->p_graphNodeMap = xbt_dict_new_homogeneous(&graph_node_map_elem_free);
62 if (THIS_AS->m_cached && !THIS_AS->p_routeCache)
63 THIS_AS->p_routeCache = xbt_dict_new_homogeneous(&route_cache_elem_free);
65 /* Add the loopback if needed */
66 if (routing_platf->p_loopback && as->p_hierarchy == SURF_ROUTING_BASE)
67 THIS_AS->addLoopback();
69 /* initialize graph indexes in nodes after graph has been built */
70 nodes = xbt_graph_get_nodes(THIS_AS->p_routeGraph);
72 xbt_dynar_foreach(nodes, cursor2, node) {
73 graph_node_data_t data = (graph_node_data_t) xbt_graph_node_get_data(node);
74 data->graph_id = cursor2;
78 /* Utility functions */
80 xbt_node_t AsDijkstra::routeGraphNewNode(int id, int graph_id)
82 xbt_node_t node = NULL;
83 graph_node_data_t data = NULL;
84 graph_node_map_element_t elm = NULL;
86 data = xbt_new0(struct graph_node_data, 1);
88 data->graph_id = graph_id;
89 node = xbt_graph_new_node(p_routeGraph, data);
91 elm = xbt_new0(struct graph_node_map_element, 1);
93 xbt_dict_set_ext(p_graphNodeMap, (char *) (&id), sizeof(int),
94 (xbt_set_elm_t) elm, NULL);
99 graph_node_map_element_t AsDijkstra::nodeMapSearch(int id)
101 graph_node_map_element_t elm = (graph_node_map_element_t)
102 xbt_dict_get_or_null_ext(p_graphNodeMap,
110 void AsDijkstra::newRoute(int src_id, int dst_id, sg_platf_route_cbarg_t e_route)
112 XBT_DEBUG("Load Route from \"%d\" to \"%d\"", src_id, dst_id);
113 xbt_node_t src = NULL;
114 xbt_node_t dst = NULL;
116 graph_node_map_element_t src_elm = (graph_node_map_element_t)
117 xbt_dict_get_or_null_ext(p_graphNodeMap,
120 graph_node_map_element_t dst_elm = (graph_node_map_element_t)
121 xbt_dict_get_or_null_ext(p_graphNodeMap,
132 /* add nodes if they don't exist in the graph */
133 if (src_id == dst_id && src == NULL && dst == NULL) {
134 src = this->routeGraphNewNode(src_id, -1);
138 src = this->routeGraphNewNode(src_id, -1);
141 dst = this->routeGraphNewNode(dst_id, -1);
145 /* add link as edge to graph */
146 xbt_graph_new_edge(p_routeGraph, src, dst, e_route);
149 void AsDijkstra::addLoopback() {
150 xbt_dynar_t nodes = xbt_graph_get_nodes(p_routeGraph);
152 xbt_node_t node = NULL;
153 unsigned int cursor2;
154 xbt_dynar_foreach(nodes, cursor2, node) {
155 xbt_dynar_t out_edges = xbt_graph_node_get_outedges(node);
156 xbt_edge_t edge = NULL;
160 xbt_dynar_foreach(out_edges, cursor, edge) {
161 xbt_node_t other_node = xbt_graph_edge_get_target(edge);
162 if (other_node == node) {
169 sg_platf_route_cbarg_t e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
170 e_route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
171 xbt_dynar_push(e_route->link_list, &routing_platf->p_loopback);
172 xbt_graph_new_edge(p_routeGraph, node, node, e_route);
177 xbt_dynar_t AsDijkstra::getOnelinkRoutes()
179 xbt_dynar_t ret = xbt_dynar_new(sizeof(OnelinkPtr), xbt_free);
180 sg_platf_route_cbarg_t route = xbt_new0(s_sg_platf_route_cbarg_t,1);
181 route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t),NULL);
184 RoutingEdgePtr src_elm, dst_elm;
185 int table_size = (int)xbt_dynar_length(p_indexNetworkElm);
186 for(src=0; src < table_size; src++) {
187 for(dst=0; dst< table_size; dst++) {
188 xbt_dynar_reset(route->link_list);
189 src_elm = xbt_dynar_get_as(p_indexNetworkElm, src, RoutingEdgePtr);
190 dst_elm = xbt_dynar_get_as(p_indexNetworkElm, dst, RoutingEdgePtr);
191 this->getRouteAndLatency(src_elm, dst_elm,route, NULL);
193 if (xbt_dynar_length(route->link_list) == 1) {
194 void *link = *(void **) xbt_dynar_get_ptr(route->link_list, 0);
196 if (p_hierarchy == SURF_ROUTING_BASE)
197 onelink = new Onelink(link, src_elm, dst_elm);
198 else if (p_hierarchy == SURF_ROUTING_RECURSIVE)
199 onelink = new Onelink(link, route->gw_src, route->gw_dst);
201 onelink = new Onelink(link, NULL, NULL);
202 xbt_dynar_push(ret, &onelink);
209 void AsDijkstra::getRouteAndLatency(RoutingEdgePtr src, RoutingEdgePtr dst, sg_platf_route_cbarg_t route, double *lat)
214 srcDstCheck(src, dst);
215 int *src_id = src->getIdPtr();
216 int *dst_id = dst->getIdPtr();
218 if (!src_id || !dst_id)
219 THROWF(arg_error,0,"No route from '%s' to '%s'",src->getName(),dst->getName());
221 int *pred_arr = NULL;
226 sg_platf_route_cbarg_t e_route;
230 xbt_dynar_t links = NULL;
231 route_cache_element_t elm = NULL;
232 xbt_dynar_t nodes = xbt_graph_get_nodes(p_routeGraph);
234 /* Use the graph_node id mapping set to quickly find the nodes */
235 graph_node_map_element_t src_elm = nodeMapSearch(*src_id);
236 graph_node_map_element_t dst_elm = nodeMapSearch(*dst_id);
238 src_node_id = ((graph_node_data_t)
239 xbt_graph_node_get_data(src_elm->node))->graph_id;
240 dst_node_id = ((graph_node_data_t)
241 xbt_graph_node_get_data(dst_elm->node))->graph_id;
243 /* if the src and dst are the same */
244 if (src_node_id == dst_node_id) {
246 xbt_node_t node_s_v = xbt_dynar_get_as(nodes, src_node_id, xbt_node_t);
247 xbt_node_t node_e_v = xbt_dynar_get_as(nodes, dst_node_id, xbt_node_t);
248 xbt_edge_t edge = xbt_graph_get_edge(p_routeGraph, node_s_v, node_e_v);
251 THROWF(arg_error, 0, "No route from '%s' to '%s'", src->getName(), dst->getName());
253 e_route = (sg_platf_route_cbarg_t) xbt_graph_edge_get_data(edge);
255 links = e_route->link_list;
256 xbt_dynar_foreach(links, cpt, link) {
257 xbt_dynar_unshift(route->link_list, &link);
259 *lat += static_cast<NetworkLinkPtr>(link)->getLatency();
265 /*check if there is a cached predecessor list avail */
266 elm = (route_cache_element_t)
267 xbt_dict_get_or_null_ext(p_routeCache, (char *) (&src_id),
271 if (elm) { /* cached mode and cache hit */
272 pred_arr = elm->pred_arr;
273 } else { /* not cached mode or cache miss */
274 double *cost_arr = NULL;
275 xbt_heap_t pqueue = NULL;
278 int nr_nodes = xbt_dynar_length(nodes);
279 cost_arr = xbt_new0(double, nr_nodes); /* link cost from src to other hosts */
280 pred_arr = xbt_new0(int, nr_nodes); /* predecessors in path from src */
281 pqueue = xbt_heap_new(nr_nodes, xbt_free);
284 cost_arr[src_node_id] = 0.0;
286 for (i = 0; i < nr_nodes; i++) {
287 if (i != src_node_id) {
288 cost_arr[i] = DBL_MAX;
293 /* initialize priority queue */
294 nodeid = xbt_new0(int, 1);
296 xbt_heap_push(pqueue, nodeid, cost_arr[i]);
300 /* apply dijkstra using the indexes from the graph's node array */
301 while (xbt_heap_size(pqueue) > 0) {
302 int *v_id = (int *) xbt_heap_pop(pqueue);
303 xbt_node_t v_node = xbt_dynar_get_as(nodes, *v_id, xbt_node_t);
304 xbt_dynar_t out_edges = xbt_graph_node_get_outedges(v_node);
305 xbt_edge_t edge = NULL;
308 xbt_dynar_foreach(out_edges, cursor, edge) {
309 xbt_node_t u_node = xbt_graph_edge_get_target(edge);
310 graph_node_data_t data = (graph_node_data_t) xbt_graph_node_get_data(u_node);
311 int u_id = data->graph_id;
312 sg_platf_route_cbarg_t tmp_e_route = (sg_platf_route_cbarg_t) xbt_graph_edge_get_data(edge);
313 int cost_v_u = (tmp_e_route->link_list)->used; /* count of links, old model assume 1 */
315 if (cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
316 pred_arr[u_id] = *v_id;
317 cost_arr[u_id] = cost_v_u + cost_arr[*v_id];
318 nodeid = xbt_new0(int, 1);
320 xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
324 /* free item popped from pqueue */
329 xbt_heap_free(pqueue);
332 /* compose route path with links */
333 RoutingEdgePtr gw_src = NULL, gw_dst, prev_gw_src, first_gw = NULL;
334 RoutingEdgePtr gw_dst_net_elm = NULL, prev_gw_src_net_elm = NULL;
336 for (v = dst_node_id; v != src_node_id; v = pred_arr[v]) {
337 xbt_node_t node_pred_v =
338 xbt_dynar_get_as(nodes, pred_arr[v], xbt_node_t);
339 xbt_node_t node_v = xbt_dynar_get_as(nodes, v, xbt_node_t);
341 xbt_graph_get_edge(p_routeGraph, node_pred_v, node_v);
344 THROWF(arg_error, 0, "No route from '%s' to '%s'", src->getName(), dst->getName());
346 prev_gw_src = gw_src;
348 e_route = (sg_platf_route_cbarg_t) xbt_graph_edge_get_data(edge);
349 gw_src = e_route->gw_src;
350 gw_dst = e_route->gw_dst;
352 if (v == dst_node_id)
355 if (p_hierarchy == SURF_ROUTING_RECURSIVE && v != dst_node_id
356 && strcmp(gw_dst->getName(), prev_gw_src->getName())) {
357 xbt_dynar_t e_route_as_to_as=NULL;
359 routing_platf->getRouteAndLatency(gw_dst_net_elm, prev_gw_src_net_elm, &e_route_as_to_as, NULL);
361 THROWF(arg_error,0,"No route from '%s' to '%s'", src->getName(), dst->getName());
362 links = e_route_as_to_as;
364 xbt_dynar_foreach(links, cpt, link) {
365 xbt_dynar_insert_at(route->link_list, pos, &link);
367 *lat += static_cast<NetworkLinkPtr>(link)->getLatency();
372 links = e_route->link_list;
373 xbt_dynar_foreach(links, cpt, link) {
374 xbt_dynar_unshift(route->link_list, &link);
376 *lat += static_cast<NetworkLinkPtr>(link)->getLatency();
381 if (p_hierarchy == SURF_ROUTING_RECURSIVE) {
382 route->gw_src = gw_src;
383 route->gw_dst = first_gw;
386 if (m_cached && elm == NULL) {
387 /* add to predecessor list of the current src-host to cache */
388 elm = xbt_new0(struct route_cache_element, 1);
389 elm->pred_arr = pred_arr;
391 xbt_dict_set_ext(p_routeCache, (char *) (&src_id), sizeof(int),
392 (xbt_set_elm_t) elm, NULL);
399 AsDijkstra::~AsDijkstra()
401 xbt_graph_free_graph(p_routeGraph, &xbt_free,
402 &graph_edge_data_free, &xbt_free);
403 xbt_dict_free(&p_graphNodeMap);
405 xbt_dict_free(&p_routeCache);
408 /* Creation routing model functions */
410 AsDijkstra::AsDijkstra() : AsGeneric(), m_cached(0) {
412 p_graphNodeMap = NULL;
416 AsDijkstra::AsDijkstra(int cached) : AsGeneric(), m_cached(cached)
419 p_graphNodeMap = NULL;
421 /*new_component->generic_routing.parse_route = model_dijkstra_both_parse_route;
422 new_component->generic_routing.parse_ASroute = model_dijkstra_both_parse_route;
423 new_component->generic_routing.get_route_and_latency = dijkstra_get_route_and_latency;
424 new_component->generic_routing.get_onelink_routes =
425 dijkstra_get_onelink_routes;
426 new_component->generic_routing.get_graph = generic_get_graph;
427 new_component->generic_routing.finalize = dijkstra_finalize;
428 new_component->cached = cached;*/
431 void AsDijkstra::end()
433 xbt_node_t node = NULL;
434 unsigned int cursor2;
435 xbt_dynar_t nodes = NULL;
437 /* Create the topology graph */
439 p_routeGraph = xbt_graph_new_graph(1, NULL);
441 p_graphNodeMap = xbt_dict_new_homogeneous(&graph_node_map_elem_free);
443 if (m_cached && !p_routeCache)
444 p_routeCache = xbt_dict_new_homogeneous(&route_cache_elem_free);
446 /* Add the loopback if needed */
447 if (routing_platf->p_loopback && p_hierarchy == SURF_ROUTING_BASE)
450 /* initialize graph indexes in nodes after graph has been built */
451 nodes = xbt_graph_get_nodes(p_routeGraph);
453 xbt_dynar_foreach(nodes, cursor2, node) {
454 graph_node_data_t data = (graph_node_data_t) xbt_graph_node_get_data(node);
455 data->graph_id = cursor2;
460 void AsDijkstra::parseASroute(sg_platf_route_cbarg_t route)
465 void AsDijkstra::parseRoute(sg_platf_route_cbarg_t route)
467 char *src = (char*)(route->src);
468 char *dst = (char*)(route->dst);
471 if(!route->gw_dst && !route->gw_src)
472 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
474 XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
475 route->gw_src->getName(), dst, route->gw_dst->getName());
477 if(route->gw_dst->getRcType() == SURF_NETWORK_ELEMENT_NULL)
478 surf_parse_error("The gw_dst '%s' does not exist!",route->gw_dst->getName());
479 if(route->gw_src->getRcType() == SURF_NETWORK_ELEMENT_NULL)
480 surf_parse_error("The gw_src '%s' does not exist!",route->gw_src->getName());
483 RoutingEdgePtr src_net_elm, dst_net_elm;
485 src_net_elm = sg_routing_edge_by_name_or_null(src);
486 dst_net_elm = sg_routing_edge_by_name_or_null(dst);
488 xbt_assert(src_net_elm, "Network elements %s not found", src);
489 xbt_assert(dst_net_elm, "Network elements %s not found", dst);
491 /* Create the topology graph */
493 p_routeGraph = xbt_graph_new_graph(1, NULL);
495 p_graphNodeMap = xbt_dict_new_homogeneous(&graph_node_map_elem_free);
497 if (m_cached && !p_routeCache)
498 p_routeCache = xbt_dict_new_homogeneous(&route_cache_elem_free);
500 sg_platf_route_cbarg_t e_route = newExtendedRoute(p_hierarchy, route, 1);
501 newRoute(src_net_elm->getId(), dst_net_elm->getId(), e_route);
504 if ( (route->symmetrical == TRUE && as_route == 0)
505 || (route->symmetrical == TRUE && as_route == 1)
508 if(!route->gw_dst && !route->gw_src)
509 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
511 XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
512 route->gw_dst->getName(), src, route->gw_src->getName());
514 xbt_dynar_t nodes = xbt_graph_get_nodes(p_routeGraph);
515 xbt_node_t node_s_v = xbt_dynar_get_as(nodes, src_net_elm->getId(), xbt_node_t);
516 xbt_node_t node_e_v = xbt_dynar_get_as(nodes, dst_net_elm->getId(), xbt_node_t);
518 xbt_graph_get_edge(p_routeGraph, node_e_v, node_s_v);
521 THROWF(arg_error,0,"(AS)Route from '%s' to '%s' already exists",src,dst);
523 if (route->gw_dst && route->gw_src) {
524 RoutingEdgePtr gw_tmp;
525 gw_tmp = route->gw_src;
526 route->gw_src = route->gw_dst;
527 route->gw_dst = gw_tmp;
529 sg_platf_route_cbarg_t link_route_back = newExtendedRoute(p_hierarchy, route, 0);
530 newRoute(dst_net_elm->getId(), src_net_elm->getId(), link_route_back);
532 xbt_dynar_free(&route->link_list);