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_dijkstra.hpp"
8 #include "surf_routing_private.h"
12 extern routing_platf_t routing_platf;
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_dijkstra, surf, "Routing part of surf -- dijkstra routing logic");
20 static void route_cache_elem_free(void *e)
22 route_cache_element_t elm = (route_cache_element_t) e;
24 xbt_free(elm->pred_arr);
29 static void graph_node_map_elem_free(void *e)
31 graph_node_map_element_t elm = (graph_node_map_element_t) e;
35 static void graph_edge_data_free(void *e) // FIXME: useless code duplication
37 sg_platf_route_cbarg_t e_route = (sg_platf_route_cbarg_t) e;
39 xbt_dynar_free(&(e_route->link_list));
44 AS_t model_dijkstra_create(void){
45 return new AsDijkstra(0);
48 AS_t model_dijkstracache_create(void){
49 return new AsDijkstra(1);
52 void model_dijkstra_both_end(AS_t as)
54 AsDijkstraPtr THIS_AS = static_cast<AsDijkstraPtr>(as);
55 xbt_node_t node = NULL;
57 xbt_dynar_t nodes = NULL;
59 /* Create the topology graph */
60 if(!THIS_AS->p_routeGraph)
61 THIS_AS->p_routeGraph = xbt_graph_new_graph(1, NULL);
62 if(!THIS_AS->p_graphNodeMap)
63 THIS_AS->p_graphNodeMap = xbt_dict_new_homogeneous(&graph_node_map_elem_free);
65 if (THIS_AS->m_cached && !THIS_AS->p_routeCache)
66 THIS_AS->p_routeCache = xbt_dict_new_homogeneous(&route_cache_elem_free);
68 /* Add the loopback if needed */
69 if (routing_platf->p_loopback && as->p_hierarchy == SURF_ROUTING_BASE)
70 THIS_AS->addLoopback();
72 /* initialize graph indexes in nodes after graph has been built */
73 nodes = xbt_graph_get_nodes(THIS_AS->p_routeGraph);
75 xbt_dynar_foreach(nodes, cursor2, node) {
76 graph_node_data_t data = (graph_node_data_t) xbt_graph_node_get_data(node);
77 data->graph_id = cursor2;
81 /* Utility functions */
83 xbt_node_t AsDijkstra::routeGraphNewNode(int id, int graph_id)
85 xbt_node_t node = NULL;
86 graph_node_data_t data = NULL;
87 graph_node_map_element_t elm = NULL;
89 data = xbt_new0(struct graph_node_data, 1);
91 data->graph_id = graph_id;
92 node = xbt_graph_new_node(p_routeGraph, data);
94 elm = xbt_new0(struct graph_node_map_element, 1);
96 xbt_dict_set_ext(p_graphNodeMap, (char *) (&id), sizeof(int),
97 (xbt_set_elm_t) elm, NULL);
102 graph_node_map_element_t AsDijkstra::nodeMapSearch(int id)
104 graph_node_map_element_t elm = (graph_node_map_element_t)
105 xbt_dict_get_or_null_ext(p_graphNodeMap,
113 void AsDijkstra::newRoute(int src_id, int dst_id, sg_platf_route_cbarg_t e_route)
115 XBT_DEBUG("Load Route from \"%d\" to \"%d\"", src_id, dst_id);
116 xbt_node_t src = NULL;
117 xbt_node_t dst = NULL;
119 graph_node_map_element_t src_elm = (graph_node_map_element_t)
120 xbt_dict_get_or_null_ext(p_graphNodeMap,
123 graph_node_map_element_t dst_elm = (graph_node_map_element_t)
124 xbt_dict_get_or_null_ext(p_graphNodeMap,
135 /* add nodes if they don't exist in the graph */
136 if (src_id == dst_id && src == NULL && dst == NULL) {
137 src = this->routeGraphNewNode(src_id, -1);
141 src = this->routeGraphNewNode(src_id, -1);
144 dst = this->routeGraphNewNode(dst_id, -1);
148 /* add link as edge to graph */
149 xbt_graph_new_edge(p_routeGraph, src, dst, e_route);
152 void AsDijkstra::addLoopback() {
153 xbt_dynar_t nodes = xbt_graph_get_nodes(p_routeGraph);
155 xbt_node_t node = NULL;
156 unsigned int cursor2;
157 xbt_dynar_foreach(nodes, cursor2, node) {
158 xbt_dynar_t out_edges = xbt_graph_node_get_outedges(node);
159 xbt_edge_t edge = NULL;
163 xbt_dynar_foreach(out_edges, cursor, edge) {
164 xbt_node_t other_node = xbt_graph_edge_get_target(edge);
165 if (other_node == node) {
172 sg_platf_route_cbarg_t e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
173 e_route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
174 xbt_dynar_push(e_route->link_list, &routing_platf->p_loopback);
175 xbt_graph_new_edge(p_routeGraph, node, node, e_route);
180 xbt_dynar_t AsDijkstra::getOnelinkRoutes()
182 xbt_dynar_t ret = xbt_dynar_new(sizeof(OnelinkPtr), xbt_free);
183 sg_platf_route_cbarg_t route = xbt_new0(s_sg_platf_route_cbarg_t,1);
184 route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t),NULL);
187 RoutingEdgePtr src_elm, dst_elm;
188 size_t table_size = xbt_dynar_length(p_indexNetworkElm);
189 for(src=0; src < table_size; src++) {
190 for(dst=0; dst< table_size; dst++) {
191 xbt_dynar_reset(route->link_list);
192 src_elm = xbt_dynar_get_as(p_indexNetworkElm, src, RoutingEdgePtr);
193 dst_elm = xbt_dynar_get_as(p_indexNetworkElm, dst, RoutingEdgePtr);
194 this->getRouteAndLatency(src_elm, dst_elm,route, NULL);
196 if (xbt_dynar_length(route->link_list) == 1) {
197 void *link = *(void **) xbt_dynar_get_ptr(route->link_list, 0);
198 OnelinkPtr onelink = new Onelink();
199 onelink->p_linkPtr = link;
200 if (p_hierarchy == SURF_ROUTING_BASE) {
201 onelink->p_src = src_elm;
202 onelink->p_dst = dst_elm;
203 } else if (p_hierarchy == SURF_ROUTING_RECURSIVE) {
204 onelink->p_src = route->gw_src;
205 onelink->p_dst = route->gw_dst;
207 xbt_dynar_push(ret, &onelink);
214 void AsDijkstra::getRouteAndLatency(RoutingEdgePtr src, RoutingEdgePtr dst, sg_platf_route_cbarg_t route, double *lat)
219 srcDstCheck(src, dst);
220 int *src_id = &(src->m_id);
221 int *dst_id = &(dst->m_id);
223 if (!src_id || !dst_id)
224 THROWF(arg_error,0,"No route from '%s' to '%s'",src->p_name,dst->p_name);
226 int *pred_arr = NULL;
231 sg_platf_route_cbarg_t e_route;
235 xbt_dynar_t links = NULL;
236 route_cache_element_t elm = NULL;
237 xbt_dynar_t nodes = xbt_graph_get_nodes(p_routeGraph);
239 /* Use the graph_node id mapping set to quickly find the nodes */
240 graph_node_map_element_t src_elm = nodeMapSearch(*src_id);
241 graph_node_map_element_t dst_elm = nodeMapSearch(*dst_id);
243 src_node_id = ((graph_node_data_t)
244 xbt_graph_node_get_data(src_elm->node))->graph_id;
245 dst_node_id = ((graph_node_data_t)
246 xbt_graph_node_get_data(dst_elm->node))->graph_id;
248 /* if the src and dst are the same */
249 if (src_node_id == dst_node_id) {
251 xbt_node_t node_s_v = xbt_dynar_get_as(nodes, src_node_id, xbt_node_t);
252 xbt_node_t node_e_v = xbt_dynar_get_as(nodes, dst_node_id, xbt_node_t);
253 xbt_edge_t edge = xbt_graph_get_edge(p_routeGraph, node_s_v, node_e_v);
256 THROWF(arg_error, 0, "No route from '%s' to '%s'", src->p_name, dst->p_name);
258 e_route = (sg_platf_route_cbarg_t) xbt_graph_edge_get_data(edge);
260 links = e_route->link_list;
261 xbt_dynar_foreach(links, cpt, link) {
262 xbt_dynar_unshift(route->link_list, &link);
264 *lat += dynamic_cast<NetworkCm02LinkPtr>(static_cast<ResourcePtr>(link))->getLatency();
270 /*check if there is a cached predecessor list avail */
271 elm = (route_cache_element_t)
272 xbt_dict_get_or_null_ext(p_routeCache, (char *) (&src_id),
276 if (elm) { /* cached mode and cache hit */
277 pred_arr = elm->pred_arr;
278 } else { /* not cached mode or cache miss */
279 double *cost_arr = NULL;
280 xbt_heap_t pqueue = NULL;
283 int nr_nodes = xbt_dynar_length(nodes);
284 cost_arr = xbt_new0(double, nr_nodes); /* link cost from src to other hosts */
285 pred_arr = xbt_new0(int, nr_nodes); /* predecessors in path from src */
286 pqueue = xbt_heap_new(nr_nodes, xbt_free);
289 cost_arr[src_node_id] = 0.0;
291 for (i = 0; i < nr_nodes; i++) {
292 if (i != src_node_id) {
293 cost_arr[i] = DBL_MAX;
298 /* initialize priority queue */
299 nodeid = xbt_new0(int, 1);
301 xbt_heap_push(pqueue, nodeid, cost_arr[i]);
305 /* apply dijkstra using the indexes from the graph's node array */
306 while (xbt_heap_size(pqueue) > 0) {
307 int *v_id = (int *) xbt_heap_pop(pqueue);
308 xbt_node_t v_node = xbt_dynar_get_as(nodes, *v_id, xbt_node_t);
309 xbt_dynar_t out_edges = xbt_graph_node_get_outedges(v_node);
310 xbt_edge_t edge = NULL;
313 xbt_dynar_foreach(out_edges, cursor, edge) {
314 xbt_node_t u_node = xbt_graph_edge_get_target(edge);
315 graph_node_data_t data = (graph_node_data_t) xbt_graph_node_get_data(u_node);
316 int u_id = data->graph_id;
317 sg_platf_route_cbarg_t tmp_e_route = (sg_platf_route_cbarg_t) xbt_graph_edge_get_data(edge);
318 int cost_v_u = (tmp_e_route->link_list)->used; /* count of links, old model assume 1 */
320 if (cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
321 pred_arr[u_id] = *v_id;
322 cost_arr[u_id] = cost_v_u + cost_arr[*v_id];
323 nodeid = xbt_new0(int, 1);
325 xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
329 /* free item popped from pqueue */
334 xbt_heap_free(pqueue);
337 /* compose route path with links */
338 RoutingEdgePtr gw_src = NULL, gw_dst, prev_gw_src, first_gw = NULL;
339 RoutingEdgePtr gw_dst_net_elm = NULL, prev_gw_src_net_elm = NULL;
341 for (v = dst_node_id; v != src_node_id; v = pred_arr[v]) {
342 xbt_node_t node_pred_v =
343 xbt_dynar_get_as(nodes, pred_arr[v], xbt_node_t);
344 xbt_node_t node_v = xbt_dynar_get_as(nodes, v, xbt_node_t);
346 xbt_graph_get_edge(p_routeGraph, node_pred_v, node_v);
349 THROWF(arg_error, 0, "No route from '%s' to '%s'", src->p_name, dst->p_name);
351 prev_gw_src = gw_src;
353 e_route = (sg_platf_route_cbarg_t) xbt_graph_edge_get_data(edge);
354 gw_src = e_route->gw_src;
355 gw_dst = e_route->gw_dst;
357 if (v == dst_node_id)
360 if (p_hierarchy == SURF_ROUTING_RECURSIVE && v != dst_node_id
361 && strcmp(gw_dst->p_name, prev_gw_src->p_name)) {
362 xbt_dynar_t e_route_as_to_as=NULL;
364 routing_platf->getRouteAndLatency(gw_dst_net_elm, prev_gw_src_net_elm, &e_route_as_to_as, NULL);
366 THROWF(arg_error,0,"No route from '%s' to '%s'", src->p_name, dst->p_name);
367 links = e_route_as_to_as;
369 xbt_dynar_foreach(links, cpt, link) {
370 xbt_dynar_insert_at(route->link_list, pos, &link);
372 *lat += dynamic_cast<NetworkCm02LinkPtr>(static_cast<ResourcePtr>(link))->getLatency();
377 links = e_route->link_list;
378 xbt_dynar_foreach(links, cpt, link) {
379 xbt_dynar_unshift(route->link_list, &link);
381 *lat += dynamic_cast<NetworkCm02LinkPtr>(static_cast<ResourcePtr>(link))->getLatency();
386 if (p_hierarchy == SURF_ROUTING_RECURSIVE) {
387 route->gw_src = gw_src;
388 route->gw_dst = first_gw;
391 if (m_cached && elm == NULL) {
392 /* add to predecessor list of the current src-host to cache */
393 elm = xbt_new0(struct route_cache_element, 1);
394 elm->pred_arr = pred_arr;
396 xbt_dict_set_ext(p_routeCache, (char *) (&src_id), sizeof(int),
397 (xbt_set_elm_t) elm, NULL);
404 AsDijkstra::~AsDijkstra()
406 xbt_graph_free_graph(p_routeGraph, &xbt_free,
407 &graph_edge_data_free, &xbt_free);
408 xbt_dict_free(&p_graphNodeMap);
410 xbt_dict_free(&p_routeCache);
413 /* Creation routing model functions */
415 AsDijkstra::AsDijkstra() : AsGeneric(), m_cached(0) {}
417 AsDijkstra::AsDijkstra(int cached) : AsGeneric(), m_cached(cached)
419 /*new_component->generic_routing.parse_route = model_dijkstra_both_parse_route;
420 new_component->generic_routing.parse_ASroute = model_dijkstra_both_parse_route;
421 new_component->generic_routing.get_route_and_latency = dijkstra_get_route_and_latency;
422 new_component->generic_routing.get_onelink_routes =
423 dijkstra_get_onelink_routes;
424 new_component->generic_routing.get_graph = generic_get_graph;
425 new_component->generic_routing.finalize = dijkstra_finalize;
426 new_component->cached = cached;*/
429 void AsDijkstra::end()
431 xbt_node_t node = NULL;
432 unsigned int cursor2;
433 xbt_dynar_t nodes = NULL;
435 /* Create the topology graph */
437 p_routeGraph = xbt_graph_new_graph(1, NULL);
439 p_graphNodeMap = xbt_dict_new_homogeneous(&graph_node_map_elem_free);
441 if (m_cached && p_routeCache)
442 p_routeCache = xbt_dict_new_homogeneous(&route_cache_elem_free);
444 /* Add the loopback if needed */
445 if (routing_platf->p_loopback && p_hierarchy == SURF_ROUTING_BASE)
448 /* initialize graph indexes in nodes after graph has been built */
449 nodes = xbt_graph_get_nodes(p_routeGraph);
451 xbt_dynar_foreach(nodes, cursor2, node) {
452 graph_node_data_t data = (graph_node_data_t) xbt_graph_node_get_data(node);
453 data->graph_id = cursor2;
458 void AsDijkstra::parseASroute(sg_platf_route_cbarg_t route)
463 void AsDijkstra::parseRoute(sg_platf_route_cbarg_t route)
465 char *src = (char*)(route->src);
466 char *dst = (char*)(route->dst);
469 if(!route->gw_dst && !route->gw_src)
470 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
472 XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
473 route->gw_src->p_name, dst, route->gw_dst->p_name);
475 if(route->gw_dst->p_rcType == SURF_NETWORK_ELEMENT_NULL)
476 xbt_die("The gw_dst '%s' does not exist!",route->gw_dst->p_name);
477 if(route->gw_src->p_rcType == SURF_NETWORK_ELEMENT_NULL)
478 xbt_die("The gw_src '%s' does not exist!",route->gw_src->p_name);
481 RoutingEdgePtr src_net_elm, dst_net_elm;
483 src_net_elm = sg_routing_edge_by_name_or_null(src);
484 dst_net_elm = sg_routing_edge_by_name_or_null(dst);
486 xbt_assert(src_net_elm, "Network elements %s not found", src);
487 xbt_assert(dst_net_elm, "Network elements %s not found", dst);
489 /* Create the topology graph */
491 p_routeGraph = xbt_graph_new_graph(1, NULL);
493 p_graphNodeMap = xbt_dict_new_homogeneous(&graph_node_map_elem_free);
495 if (m_cached && !p_routeCache)
496 p_routeCache = xbt_dict_new_homogeneous(&route_cache_elem_free);
498 sg_platf_route_cbarg_t e_route = newExtendedRoute(p_hierarchy, route, 1);
499 newRoute(src_net_elm->m_id, dst_net_elm->m_id, e_route);
502 if ( (route->symmetrical == TRUE && as_route == 0)
503 || (route->symmetrical == TRUE && as_route == 1)
506 if(!route->gw_dst && !route->gw_src)
507 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
509 XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
510 route->gw_dst->p_name, src, route->gw_src->p_name);
512 xbt_dynar_t nodes = xbt_graph_get_nodes(p_routeGraph);
513 xbt_node_t node_s_v = xbt_dynar_get_as(nodes, src_net_elm->m_id, xbt_node_t);
514 xbt_node_t node_e_v = xbt_dynar_get_as(nodes, dst_net_elm->m_id, xbt_node_t);
516 xbt_graph_get_edge(p_routeGraph, node_e_v, node_s_v);
519 THROWF(arg_error,0,"(AS)Route from '%s' to '%s' already exists",src,dst);
521 if (route->gw_dst && route->gw_src) {
522 RoutingEdgePtr gw_tmp;
523 gw_tmp = route->gw_src;
524 route->gw_src = route->gw_dst;
525 route->gw_dst = gw_tmp;
527 sg_platf_route_cbarg_t link_route_back = newExtendedRoute(p_hierarchy, route, 0);
528 newRoute(dst_net_elm->m_id, src_net_elm->m_id, link_route_back);
530 xbt_dynar_free(&route->link_list);