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 "src/surf/surf_routing_private.hpp"
8 #include "src/surf/surf_routing_dijkstra.hpp"
9 #include "src/surf/network_interface.hpp"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_dijkstra, surf, "Routing part of surf -- dijkstra routing logic");
15 static void route_cache_elem_free(void *e)
17 route_cache_element_t elm = (route_cache_element_t) e;
19 xbt_free(elm->pred_arr);
24 static void graph_node_map_elem_free(void *e)
26 graph_node_map_element_t elm = (graph_node_map_element_t) e;
30 static void graph_edge_data_free(void *e) // FIXME: useless code duplication
32 sg_platf_route_cbarg_t e_route = (sg_platf_route_cbarg_t) e;
34 xbt_dynar_free(&(e_route->link_list));
39 /* Utility functions */
43 void AsDijkstra::Seal()
45 /* Create the topology graph */
47 p_routeGraph = xbt_graph_new_graph(1, NULL);
49 p_graphNodeMap = xbt_dict_new_homogeneous(&graph_node_map_elem_free);
51 if (m_cached && !p_routeCache)
52 p_routeCache = xbt_dict_new_homogeneous(&route_cache_elem_free);
54 /* Add the loopback if needed */
55 if (routing_platf->p_loopback && hierarchy_ == SURF_ROUTING_BASE)
58 /* initialize graph indexes in nodes after graph has been built */
59 xbt_dynar_t nodes = xbt_graph_get_nodes(p_routeGraph);
61 xbt_node_t node = NULL;
63 xbt_dynar_foreach(nodes, cursor2, node) {
64 graph_node_data_t data = (graph_node_data_t) xbt_graph_node_get_data(node);
65 data->graph_id = cursor2;
69 xbt_node_t AsDijkstra::routeGraphNewNode(int id, int graph_id)
71 xbt_node_t node = NULL;
72 graph_node_data_t data = NULL;
73 graph_node_map_element_t elm = NULL;
75 data = xbt_new0(struct graph_node_data, 1);
77 data->graph_id = graph_id;
78 node = xbt_graph_new_node(p_routeGraph, data);
80 elm = xbt_new0(struct graph_node_map_element, 1);
82 xbt_dict_set_ext(p_graphNodeMap, (char *) (&id), sizeof(int),
83 (xbt_dictelm_t) elm, NULL);
88 graph_node_map_element_t AsDijkstra::nodeMapSearch(int id)
90 graph_node_map_element_t elm = (graph_node_map_element_t)
91 xbt_dict_get_or_null_ext(p_graphNodeMap,
99 void AsDijkstra::newRoute(int src_id, int dst_id, sg_platf_route_cbarg_t e_route)
101 XBT_DEBUG("Load Route from \"%d\" to \"%d\"", src_id, dst_id);
102 xbt_node_t src = NULL;
103 xbt_node_t dst = NULL;
105 graph_node_map_element_t src_elm = (graph_node_map_element_t)
106 xbt_dict_get_or_null_ext(p_graphNodeMap,
109 graph_node_map_element_t dst_elm = (graph_node_map_element_t)
110 xbt_dict_get_or_null_ext(p_graphNodeMap,
121 /* add nodes if they don't exist in the graph */
122 if (src_id == dst_id && src == NULL && dst == NULL) {
123 src = this->routeGraphNewNode(src_id, -1);
127 src = this->routeGraphNewNode(src_id, -1);
130 dst = this->routeGraphNewNode(dst_id, -1);
134 /* add link as edge to graph */
135 xbt_graph_new_edge(p_routeGraph, src, dst, e_route);
138 void AsDijkstra::addLoopback() {
139 xbt_dynar_t nodes = xbt_graph_get_nodes(p_routeGraph);
141 xbt_node_t node = NULL;
142 unsigned int cursor2;
143 xbt_dynar_foreach(nodes, cursor2, node) {
144 xbt_dynar_t out_edges = xbt_graph_node_get_outedges(node);
145 xbt_edge_t edge = NULL;
149 xbt_dynar_foreach(out_edges, cursor, edge) {
150 xbt_node_t other_node = xbt_graph_edge_get_target(edge);
151 if (other_node == node) {
158 sg_platf_route_cbarg_t e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
159 e_route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
160 xbt_dynar_push(e_route->link_list, &routing_platf->p_loopback);
161 xbt_graph_new_edge(p_routeGraph, node, node, e_route);
166 xbt_dynar_t AsDijkstra::getOneLinkRoutes()
168 xbt_dynar_t ret = xbt_dynar_new(sizeof(Onelink*), xbt_free_f);
169 sg_platf_route_cbarg_t route = xbt_new0(s_sg_platf_route_cbarg_t,1);
170 route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t),NULL);
173 NetCard *src_elm, *dst_elm;
174 int table_size = (int)xbt_dynar_length(vertices_);
175 for(src=0; src < table_size; src++) {
176 for(dst=0; dst< table_size; dst++) {
177 xbt_dynar_reset(route->link_list);
178 src_elm = xbt_dynar_get_as(vertices_, src, NetCard*);
179 dst_elm = xbt_dynar_get_as(vertices_, dst, NetCard*);
180 this->getRouteAndLatency(src_elm, dst_elm,route, NULL);
182 if (xbt_dynar_length(route->link_list) == 1) {
183 void *link = *(void **) xbt_dynar_get_ptr(route->link_list, 0);
185 if (hierarchy_ == SURF_ROUTING_BASE)
186 onelink = new Onelink(link, src_elm, dst_elm);
187 else if (hierarchy_ == SURF_ROUTING_RECURSIVE)
188 onelink = new Onelink(link, route->gw_src, route->gw_dst);
190 onelink = new Onelink(link, NULL, NULL);
191 xbt_dynar_push(ret, &onelink);
198 void AsDijkstra::getRouteAndLatency(NetCard *src, NetCard *dst, sg_platf_route_cbarg_t route, double *lat)
203 srcDstCheck(src, dst);
204 int *src_id = src->getIdPtr();
205 int *dst_id = dst->getIdPtr();
207 if (!src_id || !dst_id)
208 THROWF(arg_error,0,"No route from '%s' to '%s'",src->getName(),dst->getName());
210 int *pred_arr = NULL;
215 sg_platf_route_cbarg_t e_route;
219 xbt_dynar_t links = NULL;
220 route_cache_element_t elm = NULL;
221 xbt_dynar_t nodes = xbt_graph_get_nodes(p_routeGraph);
223 /* Use the graph_node id mapping set to quickly find the nodes */
224 graph_node_map_element_t src_elm = nodeMapSearch(*src_id);
225 graph_node_map_element_t dst_elm = nodeMapSearch(*dst_id);
227 src_node_id = ((graph_node_data_t)
228 xbt_graph_node_get_data(src_elm->node))->graph_id;
229 dst_node_id = ((graph_node_data_t)
230 xbt_graph_node_get_data(dst_elm->node))->graph_id;
232 /* if the src and dst are the same */
233 if (src_node_id == dst_node_id) {
235 xbt_node_t node_s_v = xbt_dynar_get_as(nodes, src_node_id, xbt_node_t);
236 xbt_node_t node_e_v = xbt_dynar_get_as(nodes, dst_node_id, xbt_node_t);
237 xbt_edge_t edge = xbt_graph_get_edge(p_routeGraph, node_s_v, node_e_v);
240 THROWF(arg_error, 0, "No route from '%s' to '%s'", src->getName(), dst->getName());
242 e_route = (sg_platf_route_cbarg_t) xbt_graph_edge_get_data(edge);
244 links = e_route->link_list;
245 xbt_dynar_foreach(links, cpt, link) {
246 xbt_dynar_unshift(route->link_list, &link);
248 *lat += static_cast<Link*>(link)->getLatency();
254 /*check if there is a cached predecessor list avail */
255 elm = (route_cache_element_t)
256 xbt_dict_get_or_null_ext(p_routeCache, (char *) (&src_id),
260 if (elm) { /* cached mode and cache hit */
261 pred_arr = elm->pred_arr;
262 } else { /* not cached mode or cache miss */
263 double *cost_arr = NULL;
264 xbt_heap_t pqueue = NULL;
267 int nr_nodes = xbt_dynar_length(nodes);
268 cost_arr = xbt_new0(double, nr_nodes); /* link cost from src to other hosts */
269 pred_arr = xbt_new0(int, nr_nodes); /* predecessors in path from src */
270 pqueue = xbt_heap_new(nr_nodes, xbt_free_f);
273 cost_arr[src_node_id] = 0.0;
275 for (i = 0; i < nr_nodes; i++) {
276 if (i != src_node_id) {
277 cost_arr[i] = DBL_MAX;
282 /* initialize priority queue */
283 nodeid = xbt_new0(int, 1);
285 xbt_heap_push(pqueue, nodeid, cost_arr[i]);
289 /* apply dijkstra using the indexes from the graph's node array */
290 while (xbt_heap_size(pqueue) > 0) {
291 int *v_id = (int *) xbt_heap_pop(pqueue);
292 xbt_node_t v_node = xbt_dynar_get_as(nodes, *v_id, xbt_node_t);
293 xbt_dynar_t out_edges = xbt_graph_node_get_outedges(v_node);
294 xbt_edge_t edge = NULL;
297 xbt_dynar_foreach(out_edges, cursor, edge) {
298 xbt_node_t u_node = xbt_graph_edge_get_target(edge);
299 graph_node_data_t data = (graph_node_data_t) xbt_graph_node_get_data(u_node);
300 int u_id = data->graph_id;
301 sg_platf_route_cbarg_t tmp_e_route = (sg_platf_route_cbarg_t) xbt_graph_edge_get_data(edge);
302 int cost_v_u = (tmp_e_route->link_list)->used; /* count of links, old model assume 1 */
304 if (cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
305 pred_arr[u_id] = *v_id;
306 cost_arr[u_id] = cost_v_u + cost_arr[*v_id];
307 nodeid = xbt_new0(int, 1);
309 xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
313 /* free item popped from pqueue */
318 xbt_heap_free(pqueue);
321 /* compose route path with links */
322 NetCard *gw_src = NULL, *gw_dst, *prev_gw_src, *first_gw = NULL;
323 NetCard *gw_dst_net_elm = NULL, *prev_gw_src_net_elm = NULL;
325 for (v = dst_node_id; v != src_node_id; v = pred_arr[v]) {
326 xbt_node_t node_pred_v =
327 xbt_dynar_get_as(nodes, pred_arr[v], xbt_node_t);
328 xbt_node_t node_v = xbt_dynar_get_as(nodes, v, xbt_node_t);
330 xbt_graph_get_edge(p_routeGraph, node_pred_v, node_v);
333 THROWF(arg_error, 0, "No route from '%s' to '%s'", src->getName(), dst->getName());
335 prev_gw_src = gw_src;
337 e_route = (sg_platf_route_cbarg_t) xbt_graph_edge_get_data(edge);
338 gw_src = e_route->gw_src;
339 gw_dst = e_route->gw_dst;
341 if (v == dst_node_id)
344 if (hierarchy_ == SURF_ROUTING_RECURSIVE && v != dst_node_id
345 && strcmp(gw_dst->getName(), prev_gw_src->getName())) {
346 xbt_dynar_t e_route_as_to_as=NULL;
348 routing_platf->getRouteAndLatency(gw_dst_net_elm, prev_gw_src_net_elm, &e_route_as_to_as, NULL);
350 THROWF(arg_error,0,"No route from '%s' to '%s'", src->getName(), dst->getName());
351 links = e_route_as_to_as;
353 xbt_dynar_foreach(links, cpt, link) {
354 xbt_dynar_insert_at(route->link_list, pos, &link);
356 *lat += static_cast<Link*>(link)->getLatency();
361 links = e_route->link_list;
362 xbt_dynar_foreach(links, cpt, link) {
363 xbt_dynar_unshift(route->link_list, &link);
365 *lat += static_cast<Link*>(link)->getLatency();
370 if (hierarchy_ == SURF_ROUTING_RECURSIVE) {
371 route->gw_src = gw_src;
372 route->gw_dst = first_gw;
375 if (m_cached && elm == NULL) {
376 /* add to predecessor list of the current src-host to cache */
377 elm = xbt_new0(struct route_cache_element, 1);
378 elm->pred_arr = pred_arr;
380 xbt_dict_set_ext(p_routeCache, (char *) (&src_id), sizeof(int),
381 (xbt_dictelm_t) elm, NULL);
388 AsDijkstra::~AsDijkstra()
390 xbt_graph_free_graph(p_routeGraph, &xbt_free_f,
391 &graph_edge_data_free, &xbt_free_f);
392 xbt_dict_free(&p_graphNodeMap);
394 xbt_dict_free(&p_routeCache);
397 /* Creation routing model functions */
399 AsDijkstra::AsDijkstra(const char*name, bool cached)
404 p_graphNodeMap = NULL;
408 void AsDijkstra::end()
410 xbt_node_t node = NULL;
411 unsigned int cursor2;
412 xbt_dynar_t nodes = NULL;
414 /* Create the topology graph */
416 p_routeGraph = xbt_graph_new_graph(1, NULL);
418 p_graphNodeMap = xbt_dict_new_homogeneous(&graph_node_map_elem_free);
420 if (m_cached && !p_routeCache)
421 p_routeCache = xbt_dict_new_homogeneous(&route_cache_elem_free);
423 /* Add the loopback if needed */
424 if (routing_platf->p_loopback && hierarchy_ == SURF_ROUTING_BASE)
427 /* initialize graph indexes in nodes after graph has been built */
428 nodes = xbt_graph_get_nodes(p_routeGraph);
430 xbt_dynar_foreach(nodes, cursor2, node) {
431 graph_node_data_t data = (graph_node_data_t) xbt_graph_node_get_data(node);
432 data->graph_id = cursor2;
437 void AsDijkstra::parseRoute(sg_platf_route_cbarg_t route)
439 char *src = (char*)(route->src);
440 char *dst = (char*)(route->dst);
443 if(!route->gw_dst && !route->gw_src)
444 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
446 XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
447 route->gw_src->getName(), dst, route->gw_dst->getName());
449 if(route->gw_dst->getRcType() == SURF_NETWORK_ELEMENT_NULL)
450 surf_parse_error("The gw_dst '%s' does not exist!",route->gw_dst->getName());
451 if(route->gw_src->getRcType() == SURF_NETWORK_ELEMENT_NULL)
452 surf_parse_error("The gw_src '%s' does not exist!",route->gw_src->getName());
455 NetCard *src_net_elm, *dst_net_elm;
457 src_net_elm = sg_netcard_by_name_or_null(src);
458 dst_net_elm = sg_netcard_by_name_or_null(dst);
460 xbt_assert(src_net_elm, "Network elements %s not found", src);
461 xbt_assert(dst_net_elm, "Network elements %s not found", dst);
463 /* Create the topology graph */
465 p_routeGraph = xbt_graph_new_graph(1, NULL);
467 p_graphNodeMap = xbt_dict_new_homogeneous(&graph_node_map_elem_free);
469 if (m_cached && !p_routeCache)
470 p_routeCache = xbt_dict_new_homogeneous(&route_cache_elem_free);
472 sg_platf_route_cbarg_t e_route = newExtendedRoute(hierarchy_, route, 1);
473 newRoute(src_net_elm->getId(), dst_net_elm->getId(), e_route);
476 if ( (route->symmetrical == TRUE && as_route == 0)
477 || (route->symmetrical == TRUE && as_route == 1)
480 if(!route->gw_dst && !route->gw_src)
481 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
483 XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
484 route->gw_dst->getName(), src, route->gw_src->getName());
486 xbt_dynar_t nodes = xbt_graph_get_nodes(p_routeGraph);
487 xbt_node_t node_s_v = xbt_dynar_get_as(nodes, src_net_elm->getId(), xbt_node_t);
488 xbt_node_t node_e_v = xbt_dynar_get_as(nodes, dst_net_elm->getId(), xbt_node_t);
490 xbt_graph_get_edge(p_routeGraph, node_e_v, node_s_v);
493 THROWF(arg_error,0,"(AS)Route from '%s' to '%s' already exists",src,dst);
495 if (route->gw_dst && route->gw_src) {
497 gw_tmp = route->gw_src;
498 route->gw_src = route->gw_dst;
499 route->gw_dst = gw_tmp;
501 sg_platf_route_cbarg_t link_route_back = newExtendedRoute(hierarchy_, route, 0);
502 newRoute(dst_net_elm->getId(), src_net_elm->getId(), link_route_back);
504 xbt_dynar_free(&route->link_list);