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_dijkstra.hpp"
8 #include "network_interface.hpp"
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_dijkstra, surf, "Routing part of surf -- dijkstra routing logic");
14 static void route_cache_elem_free(void *e)
16 route_cache_element_t elm = (route_cache_element_t) e;
18 xbt_free(elm->pred_arr);
23 static void graph_node_map_elem_free(void *e)
25 graph_node_map_element_t elm = (graph_node_map_element_t) e;
29 static void graph_edge_data_free(void *e) // FIXME: useless code duplication
31 sg_platf_route_cbarg_t e_route = (sg_platf_route_cbarg_t) e;
33 xbt_dynar_free(&(e_route->link_list));
38 AS_t model_dijkstra_create(void){
39 return new simgrid::surf::AsDijkstra(0);
42 AS_t model_dijkstracache_create(void){
43 return new simgrid::surf::AsDijkstra(1);
46 void model_dijkstra_both_end(AS_t as)
48 simgrid::surf::AsDijkstra *THIS_AS
49 = static_cast<simgrid::surf::AsDijkstra*>(as);
50 xbt_node_t node = NULL;
52 xbt_dynar_t nodes = NULL;
54 /* Create the topology graph */
55 if(!THIS_AS->p_routeGraph)
56 THIS_AS->p_routeGraph = xbt_graph_new_graph(1, NULL);
57 if(!THIS_AS->p_graphNodeMap)
58 THIS_AS->p_graphNodeMap = xbt_dict_new_homogeneous(&graph_node_map_elem_free);
60 if (THIS_AS->m_cached && !THIS_AS->p_routeCache)
61 THIS_AS->p_routeCache = xbt_dict_new_homogeneous(&route_cache_elem_free);
63 /* Add the loopback if needed */
64 if (routing_platf->p_loopback && as->p_hierarchy == SURF_ROUTING_BASE)
65 THIS_AS->addLoopback();
67 /* initialize graph indexes in nodes after graph has been built */
68 nodes = xbt_graph_get_nodes(THIS_AS->p_routeGraph);
70 xbt_dynar_foreach(nodes, cursor2, node) {
71 graph_node_data_t data = (graph_node_data_t) xbt_graph_node_get_data(node);
72 data->graph_id = cursor2;
76 /* Utility functions */
81 xbt_node_t AsDijkstra::routeGraphNewNode(int id, int graph_id)
83 xbt_node_t node = NULL;
84 graph_node_data_t data = NULL;
85 graph_node_map_element_t elm = NULL;
87 data = xbt_new0(struct graph_node_data, 1);
89 data->graph_id = graph_id;
90 node = xbt_graph_new_node(p_routeGraph, data);
92 elm = xbt_new0(struct graph_node_map_element, 1);
94 xbt_dict_set_ext(p_graphNodeMap, (char *) (&id), sizeof(int),
95 (xbt_set_elm_t) elm, NULL);
100 graph_node_map_element_t AsDijkstra::nodeMapSearch(int id)
102 graph_node_map_element_t elm = (graph_node_map_element_t)
103 xbt_dict_get_or_null_ext(p_graphNodeMap,
111 void AsDijkstra::newRoute(int src_id, int dst_id, sg_platf_route_cbarg_t e_route)
113 XBT_DEBUG("Load Route from \"%d\" to \"%d\"", src_id, dst_id);
114 xbt_node_t src = NULL;
115 xbt_node_t dst = NULL;
117 graph_node_map_element_t src_elm = (graph_node_map_element_t)
118 xbt_dict_get_or_null_ext(p_graphNodeMap,
121 graph_node_map_element_t dst_elm = (graph_node_map_element_t)
122 xbt_dict_get_or_null_ext(p_graphNodeMap,
133 /* add nodes if they don't exist in the graph */
134 if (src_id == dst_id && src == NULL && dst == NULL) {
135 src = this->routeGraphNewNode(src_id, -1);
139 src = this->routeGraphNewNode(src_id, -1);
142 dst = this->routeGraphNewNode(dst_id, -1);
146 /* add link as edge to graph */
147 xbt_graph_new_edge(p_routeGraph, src, dst, e_route);
150 void AsDijkstra::addLoopback() {
151 xbt_dynar_t nodes = xbt_graph_get_nodes(p_routeGraph);
153 xbt_node_t node = NULL;
154 unsigned int cursor2;
155 xbt_dynar_foreach(nodes, cursor2, node) {
156 xbt_dynar_t out_edges = xbt_graph_node_get_outedges(node);
157 xbt_edge_t edge = NULL;
161 xbt_dynar_foreach(out_edges, cursor, edge) {
162 xbt_node_t other_node = xbt_graph_edge_get_target(edge);
163 if (other_node == node) {
170 sg_platf_route_cbarg_t e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
171 e_route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
172 xbt_dynar_push(e_route->link_list, &routing_platf->p_loopback);
173 xbt_graph_new_edge(p_routeGraph, node, node, e_route);
178 xbt_dynar_t AsDijkstra::getOnelinkRoutes()
180 xbt_dynar_t ret = xbt_dynar_new(sizeof(Onelink*), xbt_free_f);
181 sg_platf_route_cbarg_t route = xbt_new0(s_sg_platf_route_cbarg_t,1);
182 route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t),NULL);
185 RoutingEdge *src_elm, *dst_elm;
186 int table_size = (int)xbt_dynar_length(p_indexNetworkElm);
187 for(src=0; src < table_size; src++) {
188 for(dst=0; dst< table_size; dst++) {
189 xbt_dynar_reset(route->link_list);
190 src_elm = xbt_dynar_get_as(p_indexNetworkElm, src, RoutingEdge*);
191 dst_elm = xbt_dynar_get_as(p_indexNetworkElm, dst, RoutingEdge*);
192 this->getRouteAndLatency(src_elm, dst_elm,route, NULL);
194 if (xbt_dynar_length(route->link_list) == 1) {
195 void *link = *(void **) xbt_dynar_get_ptr(route->link_list, 0);
197 if (p_hierarchy == SURF_ROUTING_BASE)
198 onelink = new Onelink(link, src_elm, dst_elm);
199 else if (p_hierarchy == SURF_ROUTING_RECURSIVE)
200 onelink = new Onelink(link, route->gw_src, route->gw_dst);
202 onelink = new Onelink(link, NULL, NULL);
203 xbt_dynar_push(ret, &onelink);
210 void AsDijkstra::getRouteAndLatency(RoutingEdge *src, RoutingEdge *dst, sg_platf_route_cbarg_t route, double *lat)
215 srcDstCheck(src, dst);
216 int *src_id = src->getIdPtr();
217 int *dst_id = dst->getIdPtr();
219 if (!src_id || !dst_id)
220 THROWF(arg_error,0,"No route from '%s' to '%s'",src->getName(),dst->getName());
222 int *pred_arr = NULL;
227 sg_platf_route_cbarg_t e_route;
231 xbt_dynar_t links = NULL;
232 route_cache_element_t elm = NULL;
233 xbt_dynar_t nodes = xbt_graph_get_nodes(p_routeGraph);
235 /* Use the graph_node id mapping set to quickly find the nodes */
236 graph_node_map_element_t src_elm = nodeMapSearch(*src_id);
237 graph_node_map_element_t dst_elm = nodeMapSearch(*dst_id);
239 src_node_id = ((graph_node_data_t)
240 xbt_graph_node_get_data(src_elm->node))->graph_id;
241 dst_node_id = ((graph_node_data_t)
242 xbt_graph_node_get_data(dst_elm->node))->graph_id;
244 /* if the src and dst are the same */
245 if (src_node_id == dst_node_id) {
247 xbt_node_t node_s_v = xbt_dynar_get_as(nodes, src_node_id, xbt_node_t);
248 xbt_node_t node_e_v = xbt_dynar_get_as(nodes, dst_node_id, xbt_node_t);
249 xbt_edge_t edge = xbt_graph_get_edge(p_routeGraph, node_s_v, node_e_v);
252 THROWF(arg_error, 0, "No route from '%s' to '%s'", src->getName(), dst->getName());
254 e_route = (sg_platf_route_cbarg_t) xbt_graph_edge_get_data(edge);
256 links = e_route->link_list;
257 xbt_dynar_foreach(links, cpt, link) {
258 xbt_dynar_unshift(route->link_list, &link);
260 *lat += static_cast<Link*>(link)->getLatency();
266 /*check if there is a cached predecessor list avail */
267 elm = (route_cache_element_t)
268 xbt_dict_get_or_null_ext(p_routeCache, (char *) (&src_id),
272 if (elm) { /* cached mode and cache hit */
273 pred_arr = elm->pred_arr;
274 } else { /* not cached mode or cache miss */
275 double *cost_arr = NULL;
276 xbt_heap_t pqueue = NULL;
279 int nr_nodes = xbt_dynar_length(nodes);
280 cost_arr = xbt_new0(double, nr_nodes); /* link cost from src to other hosts */
281 pred_arr = xbt_new0(int, nr_nodes); /* predecessors in path from src */
282 pqueue = xbt_heap_new(nr_nodes, xbt_free_f);
285 cost_arr[src_node_id] = 0.0;
287 for (i = 0; i < nr_nodes; i++) {
288 if (i != src_node_id) {
289 cost_arr[i] = DBL_MAX;
294 /* initialize priority queue */
295 nodeid = xbt_new0(int, 1);
297 xbt_heap_push(pqueue, nodeid, cost_arr[i]);
301 /* apply dijkstra using the indexes from the graph's node array */
302 while (xbt_heap_size(pqueue) > 0) {
303 int *v_id = (int *) xbt_heap_pop(pqueue);
304 xbt_node_t v_node = xbt_dynar_get_as(nodes, *v_id, xbt_node_t);
305 xbt_dynar_t out_edges = xbt_graph_node_get_outedges(v_node);
306 xbt_edge_t edge = NULL;
309 xbt_dynar_foreach(out_edges, cursor, edge) {
310 xbt_node_t u_node = xbt_graph_edge_get_target(edge);
311 graph_node_data_t data = (graph_node_data_t) xbt_graph_node_get_data(u_node);
312 int u_id = data->graph_id;
313 sg_platf_route_cbarg_t tmp_e_route = (sg_platf_route_cbarg_t) xbt_graph_edge_get_data(edge);
314 int cost_v_u = (tmp_e_route->link_list)->used; /* count of links, old model assume 1 */
316 if (cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
317 pred_arr[u_id] = *v_id;
318 cost_arr[u_id] = cost_v_u + cost_arr[*v_id];
319 nodeid = xbt_new0(int, 1);
321 xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
325 /* free item popped from pqueue */
330 xbt_heap_free(pqueue);
333 /* compose route path with links */
334 RoutingEdge *gw_src = NULL, *gw_dst, *prev_gw_src, *first_gw = NULL;
335 RoutingEdge *gw_dst_net_elm = NULL, *prev_gw_src_net_elm = NULL;
337 for (v = dst_node_id; v != src_node_id; v = pred_arr[v]) {
338 xbt_node_t node_pred_v =
339 xbt_dynar_get_as(nodes, pred_arr[v], xbt_node_t);
340 xbt_node_t node_v = xbt_dynar_get_as(nodes, v, xbt_node_t);
342 xbt_graph_get_edge(p_routeGraph, node_pred_v, node_v);
345 THROWF(arg_error, 0, "No route from '%s' to '%s'", src->getName(), dst->getName());
347 prev_gw_src = gw_src;
349 e_route = (sg_platf_route_cbarg_t) xbt_graph_edge_get_data(edge);
350 gw_src = e_route->gw_src;
351 gw_dst = e_route->gw_dst;
353 if (v == dst_node_id)
356 if (p_hierarchy == SURF_ROUTING_RECURSIVE && v != dst_node_id
357 && strcmp(gw_dst->getName(), prev_gw_src->getName())) {
358 xbt_dynar_t e_route_as_to_as=NULL;
360 routing_platf->getRouteAndLatency(gw_dst_net_elm, prev_gw_src_net_elm, &e_route_as_to_as, NULL);
362 THROWF(arg_error,0,"No route from '%s' to '%s'", src->getName(), dst->getName());
363 links = e_route_as_to_as;
365 xbt_dynar_foreach(links, cpt, link) {
366 xbt_dynar_insert_at(route->link_list, pos, &link);
368 *lat += static_cast<Link*>(link)->getLatency();
373 links = e_route->link_list;
374 xbt_dynar_foreach(links, cpt, link) {
375 xbt_dynar_unshift(route->link_list, &link);
377 *lat += static_cast<Link*>(link)->getLatency();
382 if (p_hierarchy == SURF_ROUTING_RECURSIVE) {
383 route->gw_src = gw_src;
384 route->gw_dst = first_gw;
387 if (m_cached && elm == NULL) {
388 /* add to predecessor list of the current src-host to cache */
389 elm = xbt_new0(struct route_cache_element, 1);
390 elm->pred_arr = pred_arr;
392 xbt_dict_set_ext(p_routeCache, (char *) (&src_id), sizeof(int),
393 (xbt_set_elm_t) elm, NULL);
400 AsDijkstra::~AsDijkstra()
402 xbt_graph_free_graph(p_routeGraph, &xbt_free_f,
403 &graph_edge_data_free, &xbt_free_f);
404 xbt_dict_free(&p_graphNodeMap);
406 xbt_dict_free(&p_routeCache);
409 /* Creation routing model functions */
411 AsDijkstra::AsDijkstra() : AsGeneric(), m_cached(0) {
413 p_graphNodeMap = NULL;
417 AsDijkstra::AsDijkstra(int cached) : AsGeneric(), m_cached(cached)
420 p_graphNodeMap = NULL;
422 /*new_component->generic_routing.parse_route = model_dijkstra_both_parse_route;
423 new_component->generic_routing.parse_ASroute = model_dijkstra_both_parse_route;
424 new_component->generic_routing.get_route_and_latency = dijkstra_get_route_and_latency;
425 new_component->generic_routing.get_onelink_routes =
426 dijkstra_get_onelink_routes;
427 new_component->generic_routing.get_graph = generic_get_graph;
428 new_component->generic_routing.finalize = dijkstra_finalize;
429 new_component->cached = cached;*/
432 void AsDijkstra::end()
434 xbt_node_t node = NULL;
435 unsigned int cursor2;
436 xbt_dynar_t nodes = NULL;
438 /* Create the topology graph */
440 p_routeGraph = xbt_graph_new_graph(1, NULL);
442 p_graphNodeMap = xbt_dict_new_homogeneous(&graph_node_map_elem_free);
444 if (m_cached && !p_routeCache)
445 p_routeCache = xbt_dict_new_homogeneous(&route_cache_elem_free);
447 /* Add the loopback if needed */
448 if (routing_platf->p_loopback && p_hierarchy == SURF_ROUTING_BASE)
451 /* initialize graph indexes in nodes after graph has been built */
452 nodes = xbt_graph_get_nodes(p_routeGraph);
454 xbt_dynar_foreach(nodes, cursor2, node) {
455 graph_node_data_t data = (graph_node_data_t) xbt_graph_node_get_data(node);
456 data->graph_id = cursor2;
461 void AsDijkstra::parseASroute(sg_platf_route_cbarg_t route)
466 void AsDijkstra::parseRoute(sg_platf_route_cbarg_t route)
468 char *src = (char*)(route->src);
469 char *dst = (char*)(route->dst);
472 if(!route->gw_dst && !route->gw_src)
473 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
475 XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
476 route->gw_src->getName(), dst, route->gw_dst->getName());
478 if(route->gw_dst->getRcType() == SURF_NETWORK_ELEMENT_NULL)
479 surf_parse_error("The gw_dst '%s' does not exist!",route->gw_dst->getName());
480 if(route->gw_src->getRcType() == SURF_NETWORK_ELEMENT_NULL)
481 surf_parse_error("The gw_src '%s' does not exist!",route->gw_src->getName());
484 RoutingEdge *src_net_elm, *dst_net_elm;
486 src_net_elm = sg_routing_edge_by_name_or_null(src);
487 dst_net_elm = sg_routing_edge_by_name_or_null(dst);
489 xbt_assert(src_net_elm, "Network elements %s not found", src);
490 xbt_assert(dst_net_elm, "Network elements %s not found", dst);
492 /* Create the topology graph */
494 p_routeGraph = xbt_graph_new_graph(1, NULL);
496 p_graphNodeMap = xbt_dict_new_homogeneous(&graph_node_map_elem_free);
498 if (m_cached && !p_routeCache)
499 p_routeCache = xbt_dict_new_homogeneous(&route_cache_elem_free);
501 sg_platf_route_cbarg_t e_route = newExtendedRoute(p_hierarchy, route, 1);
502 newRoute(src_net_elm->getId(), dst_net_elm->getId(), e_route);
505 if ( (route->symmetrical == TRUE && as_route == 0)
506 || (route->symmetrical == TRUE && as_route == 1)
509 if(!route->gw_dst && !route->gw_src)
510 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
512 XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
513 route->gw_dst->getName(), src, route->gw_src->getName());
515 xbt_dynar_t nodes = xbt_graph_get_nodes(p_routeGraph);
516 xbt_node_t node_s_v = xbt_dynar_get_as(nodes, src_net_elm->getId(), xbt_node_t);
517 xbt_node_t node_e_v = xbt_dynar_get_as(nodes, dst_net_elm->getId(), xbt_node_t);
519 xbt_graph_get_edge(p_routeGraph, node_e_v, node_s_v);
522 THROWF(arg_error,0,"(AS)Route from '%s' to '%s' already exists",src,dst);
524 if (route->gw_dst && route->gw_src) {
526 gw_tmp = route->gw_src;
527 route->gw_src = route->gw_dst;
528 route->gw_dst = gw_tmp;
530 sg_platf_route_cbarg_t link_route_back = newExtendedRoute(p_hierarchy, route, 0);
531 newRoute(dst_net_elm->getId(), src_net_elm->getId(), link_route_back);
533 xbt_dynar_free(&route->link_list);