1 /* Copyright (c) 2009, 2010, 2011. 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_private.h"
10 extern routing_platf_t routing_platf;
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_dijkstra, surf, "Routing part of surf -- dijkstra routing logic");
15 s_as_t generic_routing;
16 xbt_graph_t route_graph; /* xbt_graph */
17 xbt_dict_t graph_node_map; /* map */
18 xbt_dict_t route_cache; /* use in cache mode */
20 } s_as_dijkstra_t, *as_dijkstra_t;
23 typedef struct graph_node_data {
25 int graph_id; /* used for caching internal graph id's */
26 } s_graph_node_data_t, *graph_node_data_t;
28 typedef struct graph_node_map_element {
30 } s_graph_node_map_element_t, *graph_node_map_element_t;
32 typedef struct route_cache_element {
35 } s_route_cache_element_t, *route_cache_element_t;
39 static void route_cache_elem_free(void *e)
41 route_cache_element_t elm = (route_cache_element_t) e;
43 xbt_free(elm->pred_arr);
48 static void graph_node_map_elem_free(void *e)
50 graph_node_map_element_t elm = (graph_node_map_element_t) e;
54 static void graph_edge_data_free(void *e) // FIXME: useless code dupplication
56 sg_platf_route_cbarg_t e_route = (sg_platf_route_cbarg_t) e;
58 xbt_dynar_free(&(e_route->link_list));
63 /* Utility functions */
65 static xbt_node_t route_graph_new_node(as_dijkstra_t as,
68 xbt_node_t node = NULL;
69 graph_node_data_t data = NULL;
70 graph_node_map_element_t elm = NULL;
72 data = xbt_new0(struct graph_node_data, 1);
74 data->graph_id = graph_id;
75 node = xbt_graph_new_node(as->route_graph, data);
77 elm = xbt_new0(struct graph_node_map_element, 1);
79 xbt_dict_set_ext(as->graph_node_map, (char *) (&id), sizeof(int),
80 (xbt_set_elm_t) elm, NULL);
85 static graph_node_map_element_t
86 graph_node_map_search(as_dijkstra_t as, int id)
88 graph_node_map_element_t elm = (graph_node_map_element_t)
89 xbt_dict_get_or_null_ext(as->graph_node_map,
97 static void route_new_dijkstra(as_dijkstra_t as, int src_id,
98 int dst_id, sg_platf_route_cbarg_t e_route)
100 XBT_DEBUG("Load Route from \"%d\" to \"%d\"", src_id, dst_id);
101 xbt_node_t src = NULL;
102 xbt_node_t dst = NULL;
104 graph_node_map_element_t src_elm = (graph_node_map_element_t)
105 xbt_dict_get_or_null_ext(as->graph_node_map,
108 graph_node_map_element_t dst_elm = (graph_node_map_element_t)
109 xbt_dict_get_or_null_ext(as->graph_node_map,
120 /* add nodes if they don't exist in the graph */
121 if (src_id == dst_id && src == NULL && dst == NULL) {
122 src = route_graph_new_node(as, src_id, -1);
126 src = route_graph_new_node(as, src_id, -1);
129 dst = route_graph_new_node(as, dst_id, -1);
133 /* add link as edge to graph */
134 xbt_graph_new_edge(as->route_graph, src, dst, e_route);
137 static void add_loopback_dijkstra(as_dijkstra_t as) {
138 xbt_dynar_t nodes = xbt_graph_get_nodes(as->route_graph);
140 xbt_node_t node = NULL;
141 unsigned int cursor2;
142 xbt_dynar_foreach(nodes, cursor2, node) {
143 xbt_dynar_t out_edges = xbt_graph_node_get_outedges(node);
144 xbt_edge_t edge = NULL;
148 xbt_dynar_foreach(out_edges, cursor, edge) {
149 xbt_node_t other_node = xbt_graph_edge_get_target(edge);
150 if (other_node == node) {
157 sg_platf_route_cbarg_t e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
158 e_route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
159 xbt_dynar_push(e_route->link_list, &routing_platf->loopback);
160 xbt_graph_new_edge(as->route_graph, node, node, e_route);
165 static void dijkstra_get_route_and_latency(AS_t as_generic,
166 sg_routing_edge_t src, sg_routing_edge_t dst, sg_platf_route_cbarg_t route, double *lat);
168 static xbt_dynar_t dijkstra_get_onelink_routes(AS_t as)
170 xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
171 sg_platf_route_cbarg_t route = xbt_new0(s_sg_platf_route_cbarg_t,1);
172 route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t),NULL);
175 sg_routing_edge_t src_elm, dst_elm;
176 size_t table_size = xbt_dynar_length(as->index_network_elm);
177 for(src=0; src < table_size; src++) {
178 for(dst=0; dst< table_size; dst++) {
179 xbt_dynar_reset(route->link_list);
180 src_elm = xbt_dynar_get_as(as->index_network_elm,src,sg_routing_edge_t);
181 dst_elm = xbt_dynar_get_as(as->index_network_elm,dst,sg_routing_edge_t);
182 dijkstra_get_route_and_latency(as, src_elm, dst_elm,route, NULL);
184 if (xbt_dynar_length(route->link_list) == 1) {
185 void *link = *(void **) xbt_dynar_get_ptr(route->link_list, 0);
186 onelink_t onelink = xbt_new0(s_onelink_t, 1);
187 onelink->link_ptr = link;
188 if (as->hierarchy == SURF_ROUTING_BASE) {
189 onelink->src = src_elm;
190 onelink->dst = dst_elm;
191 } else if (as->hierarchy == SURF_ROUTING_RECURSIVE) {
192 onelink->src = route->gw_src;
193 onelink->dst = route->gw_dst;
195 xbt_dynar_push(ret, &onelink);
202 static void dijkstra_get_route_and_latency(AS_t asg,
203 sg_routing_edge_t src, sg_routing_edge_t dst,
204 sg_platf_route_cbarg_t route, double *lat)
208 as_dijkstra_t as = (as_dijkstra_t) asg;
210 generic_src_dst_check(asg, src, dst);
211 int *src_id = &(src->id);
212 int *dst_id = &(dst->id);
214 if (!src_id || !dst_id)
215 THROWF(arg_error,0,"No route from '%s' to '%s'",src->name,dst->name);
217 int *pred_arr = NULL;
222 sg_platf_route_cbarg_t e_route;
226 xbt_dynar_t links = NULL;
227 route_cache_element_t elm = NULL;
228 xbt_dynar_t nodes = xbt_graph_get_nodes(as->route_graph);
230 /* Use the graph_node id mapping set to quickly find the nodes */
231 graph_node_map_element_t src_elm =
232 graph_node_map_search(as, *src_id);
233 graph_node_map_element_t dst_elm =
234 graph_node_map_search(as, *dst_id);
236 src_node_id = ((graph_node_data_t)
237 xbt_graph_node_get_data(src_elm->node))->graph_id;
238 dst_node_id = ((graph_node_data_t)
239 xbt_graph_node_get_data(dst_elm->node))->graph_id;
241 /* if the src and dst are the same */
242 if (src_node_id == dst_node_id) {
244 xbt_node_t node_s_v = xbt_dynar_get_as(nodes, src_node_id, xbt_node_t);
245 xbt_node_t node_e_v = xbt_dynar_get_as(nodes, dst_node_id, xbt_node_t);
247 xbt_graph_get_edge(as->route_graph, node_s_v, node_e_v);
250 THROWF(arg_error,0,"No route from '%s' to '%s'",src->name,dst->name);
252 e_route = (sg_platf_route_cbarg_t) xbt_graph_edge_get_data(edge);
254 links = e_route->link_list;
255 xbt_dynar_foreach(links, cpt, link) {
256 xbt_dynar_unshift(route->link_list, &link);
258 *lat += surf_network_model->extension.network.get_link_latency(link);
264 /*check if there is a cached predecessor list avail */
265 elm = (route_cache_element_t)
266 xbt_dict_get_or_null_ext(as->route_cache, (char *) (&src_id),
270 if (elm) { /* cached mode and cache hit */
271 pred_arr = elm->pred_arr;
272 } else { /* not cached mode or cache miss */
273 double *cost_arr = NULL;
274 xbt_heap_t pqueue = NULL;
277 int nr_nodes = xbt_dynar_length(nodes);
278 cost_arr = xbt_new0(double, nr_nodes); /* link cost from src to other hosts */
279 pred_arr = xbt_new0(int, nr_nodes); /* predecessors in path from src */
280 pqueue = xbt_heap_new(nr_nodes, xbt_free);
283 cost_arr[src_node_id] = 0.0;
285 for (i = 0; i < nr_nodes; i++) {
286 if (i != src_node_id) {
287 cost_arr[i] = DBL_MAX;
292 /* initialize priority queue */
293 nodeid = xbt_new0(int, 1);
295 xbt_heap_push(pqueue, nodeid, cost_arr[i]);
299 /* apply dijkstra using the indexes from the graph's node array */
300 while (xbt_heap_size(pqueue) > 0) {
301 int *v_id = xbt_heap_pop(pqueue);
302 xbt_node_t v_node = xbt_dynar_get_as(nodes, *v_id, xbt_node_t);
303 xbt_dynar_t out_edges = xbt_graph_node_get_outedges(v_node);
304 xbt_edge_t edge = NULL;
307 xbt_dynar_foreach(out_edges, cursor, edge) {
308 xbt_node_t u_node = xbt_graph_edge_get_target(edge);
309 graph_node_data_t data = xbt_graph_node_get_data(u_node);
310 int u_id = data->graph_id;
311 sg_platf_route_cbarg_t tmp_e_route = (sg_platf_route_cbarg_t) xbt_graph_edge_get_data(edge);
312 int cost_v_u = (tmp_e_route->link_list)->used; /* count of links, old model assume 1 */
314 if (cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
315 pred_arr[u_id] = *v_id;
316 cost_arr[u_id] = cost_v_u + cost_arr[*v_id];
317 nodeid = xbt_new0(int, 1);
319 xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
323 /* free item popped from pqueue */
328 xbt_heap_free(pqueue);
331 /* compose route path with links */
332 sg_routing_edge_t gw_src = NULL, gw_dst, prev_gw_src, first_gw = NULL;
333 sg_routing_edge_t gw_dst_net_elm = NULL, prev_gw_src_net_elm = NULL;
335 for (v = dst_node_id; v != src_node_id; v = pred_arr[v]) {
336 xbt_node_t node_pred_v =
337 xbt_dynar_get_as(nodes, pred_arr[v], xbt_node_t);
338 xbt_node_t node_v = xbt_dynar_get_as(nodes, v, xbt_node_t);
340 xbt_graph_get_edge(as->route_graph, node_pred_v, node_v);
343 THROWF(arg_error,0,"No route from '%s' to '%s'",src->name,dst->name);
345 prev_gw_src = gw_src;
347 e_route = (sg_platf_route_cbarg_t) xbt_graph_edge_get_data(edge);
348 gw_src = e_route->gw_src;
349 gw_dst = e_route->gw_dst;
351 if (v == dst_node_id)
354 if (asg->hierarchy == SURF_ROUTING_RECURSIVE && v != dst_node_id
355 && strcmp(gw_dst->name, prev_gw_src->name)) {
356 xbt_dynar_t e_route_as_to_as=NULL;
357 routing_get_route_and_latency(gw_dst_net_elm, prev_gw_src_net_elm, &e_route_as_to_as, NULL);
359 THROWF(arg_error,0,"No route from '%s' to '%s'",src->name,dst->name);
360 links = e_route_as_to_as;
362 xbt_dynar_foreach(links, cpt, link) {
363 xbt_dynar_insert_at(route->link_list, pos, &link);
365 *lat += surf_network_model->extension.network.get_link_latency(link);
370 links = e_route->link_list;
371 xbt_dynar_foreach(links, cpt, link) {
372 xbt_dynar_unshift(route->link_list, &link);
374 *lat += surf_network_model->extension.network.get_link_latency(link);
379 if (asg->hierarchy == SURF_ROUTING_RECURSIVE) {
380 route->gw_src = gw_src;
381 route->gw_dst = first_gw;
384 if (as->cached && elm == NULL) {
385 /* add to predecessor list of the current src-host to cache */
386 elm = xbt_new0(struct route_cache_element, 1);
387 elm->pred_arr = pred_arr;
389 xbt_dict_set_ext(as->route_cache, (char *) (&src_id), sizeof(int),
390 (xbt_set_elm_t) elm, NULL);
397 static void dijkstra_finalize(AS_t asg)
399 as_dijkstra_t as = (as_dijkstra_t) asg;
401 xbt_graph_free_graph(as->route_graph, &xbt_free,
402 &graph_edge_data_free, &xbt_free);
403 xbt_dict_free(&as->graph_node_map);
405 xbt_dict_free(&as->route_cache);
407 model_generic_finalize(asg);
410 /* Creation routing model functions */
412 AS_t model_dijkstra_both_create(int cached)
414 as_dijkstra_t new_component = (as_dijkstra_t)
415 model_generic_create_sized(sizeof(s_as_dijkstra_t));
417 new_component->generic_routing.parse_route = model_dijkstra_both_parse_route;
418 new_component->generic_routing.parse_ASroute = model_dijkstra_both_parse_route;
419 new_component->generic_routing.get_route_and_latency = dijkstra_get_route_and_latency;
420 new_component->generic_routing.get_onelink_routes =
421 dijkstra_get_onelink_routes;
422 new_component->generic_routing.get_graph = generic_get_graph;
423 new_component->generic_routing.finalize = dijkstra_finalize;
424 new_component->cached = cached;
426 return (AS_t)new_component;
429 AS_t model_dijkstra_create(void)
431 return model_dijkstra_both_create(0);
434 AS_t model_dijkstracache_create(void)
436 return model_dijkstra_both_create(1);
439 void model_dijkstra_both_end(AS_t as)
441 as_dijkstra_t THIS_AS = (as_dijkstra_t) as;
443 xbt_node_t node = NULL;
444 unsigned int cursor2;
445 xbt_dynar_t nodes = NULL;
447 /* Create the topology graph */
448 if(!THIS_AS->route_graph)
449 THIS_AS->route_graph = xbt_graph_new_graph(1, NULL);
450 if(!THIS_AS->graph_node_map)
451 THIS_AS->graph_node_map = xbt_dict_new_homogeneous(&graph_node_map_elem_free);
453 if (THIS_AS->cached && !THIS_AS->route_cache)
454 THIS_AS->route_cache = xbt_dict_new_homogeneous(&route_cache_elem_free);
456 /* Add the loopback if needed */
457 if (routing_platf->loopback && as->hierarchy == SURF_ROUTING_BASE)
458 add_loopback_dijkstra(THIS_AS);
460 /* initialize graph indexes in nodes after graph has been built */
461 nodes = xbt_graph_get_nodes(THIS_AS->route_graph);
463 xbt_dynar_foreach(nodes, cursor2, node) {
464 graph_node_data_t data = xbt_graph_node_get_data(node);
465 data->graph_id = cursor2;
469 void model_dijkstra_both_parse_route (AS_t asg, sg_platf_route_cbarg_t route)
471 char *src = (char*)(route->src);
472 char *dst = (char*)(route->dst);
475 if(!route->gw_dst && !route->gw_src)
476 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
478 XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
479 route->gw_src->name, dst, route->gw_dst->name);
481 if(route->gw_dst->rc_type == SURF_NETWORK_ELEMENT_NULL)
482 xbt_die("The gw_dst '%s' does not exist!",route->gw_dst->name);
483 if(route->gw_src->rc_type == SURF_NETWORK_ELEMENT_NULL)
484 xbt_die("The gw_src '%s' does not exist!",route->gw_src->name);
487 as_dijkstra_t as = (as_dijkstra_t) asg;
488 sg_routing_edge_t src_net_elm, dst_net_elm;
490 src_net_elm = sg_routing_edge_by_name_or_null(src);
491 dst_net_elm = sg_routing_edge_by_name_or_null(dst);
493 xbt_assert(src_net_elm, "Network elements %s not found", src);
494 xbt_assert(dst_net_elm, "Network elements %s not found", dst);
496 /* Create the topology graph */
498 as->route_graph = xbt_graph_new_graph(1, NULL);
499 if(!as->graph_node_map)
500 as->graph_node_map = xbt_dict_new_homogeneous(&graph_node_map_elem_free);
502 if (as->cached && !as->route_cache)
503 as->route_cache = xbt_dict_new_homogeneous(&route_cache_elem_free);
505 sg_platf_route_cbarg_t e_route = generic_new_extended_route(asg->hierarchy, route, 1);
506 route_new_dijkstra(as, src_net_elm->id, dst_net_elm->id, e_route);
509 if ( (route->symmetrical == TRUE && as_route == 0)
510 || (route->symmetrical == TRUE && as_route == 1)
513 if(!route->gw_dst && !route->gw_src)
514 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
516 XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
517 route->gw_dst->name, src, route->gw_src->name);
519 xbt_dynar_t nodes = xbt_graph_get_nodes(as->route_graph);
520 xbt_node_t node_s_v = xbt_dynar_get_as(nodes, src_net_elm->id, xbt_node_t);
521 xbt_node_t node_e_v = xbt_dynar_get_as(nodes, dst_net_elm->id, xbt_node_t);
523 xbt_graph_get_edge(as->route_graph, node_e_v, node_s_v);
526 THROWF(arg_error,0,"(AS)Route from '%s' to '%s' already exists",src,dst);
528 if (route->gw_dst && route->gw_src) {
529 sg_routing_edge_t gw_tmp;
530 gw_tmp = route->gw_src;
531 route->gw_src = route->gw_dst;
532 route->gw_dst = gw_tmp;
534 sg_platf_route_cbarg_t link_route_back = generic_new_extended_route(asg->hierarchy, route, 0);
535 route_new_dijkstra(as, dst_net_elm->id, src_net_elm->id, link_route_back);
537 xbt_dynar_free(&route->link_list);