1 /* Copyright (c) 2009 The SimGrid team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "surf_private.h"
10 #include "xbt/config.h"
11 #include "xbt/graph.h"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route,surf,"Routing part of surf");
16 routing_t used_routing = NULL;
18 /* Prototypes of each model */
19 static void routing_model_full_create(size_t size_of_link,void *loopback);
20 static void routing_model_floyd_create(size_t size_of_link, void*loopback);
21 static void routing_model_dijkstra_create(size_t size_of_link, void*loopback);
22 static void routing_model_dijkstracache_create(size_t size_of_link, void*loopback);
23 static void routing_model_none_create(size_t size_of_link, void*loopback);
25 /* Definition of each model */
29 void (*fun)(size_t,void*);
31 struct model_type models[] =
32 { {"Full", "Full routing data (fast, large memory requirements, fully expressive)", routing_model_full_create },
33 {"Floyd", "Floyd routing data (slow initialization, fast lookup, lesser memory requirements, shortest path routing only)", routing_model_floyd_create },
34 {"Dijkstra", "Dijkstra routing data (fast initialization, slow lookup, small memory requirements, shortest path routing only)", routing_model_dijkstra_create },
35 {"DijkstraCache", "Dijkstra routing data (fast initialization, fast lookup, small memory requirements, shortest path routing only)", routing_model_dijkstracache_create },
36 {"none", "No routing (usable with Constant network only)", routing_model_none_create },
40 void routing_model_create(size_t size_of_links, void* loopback) {
42 char * wanted=xbt_cfg_get_string(_surf_cfg_set,"routing");
44 for (cpt=0;models[cpt].name;cpt++) {
45 if (!strcmp(wanted,models[cpt].name)) {
46 (*(models[cpt].fun))(size_of_links,loopback);
50 fprintf(stderr,"Routing model %s not found. Existing models:\n",wanted);
51 for (cpt=0;models[cpt].name;cpt++)
52 if (!strcmp(wanted,models[cpt].name))
53 fprintf(stderr," %s: %s\n",models[cpt].name,models[cpt].desc);
57 /* ************************************************************************** */
58 /* *************************** FULL ROUTING ********************************* */
60 s_routing_t generic_routing;
61 xbt_dynar_t *routing_table;
64 } s_routing_full_t,*routing_full_t;
66 #define ROUTE_FULL(i,j) ((routing_full_t)used_routing)->routing_table[(i)+(j)*(used_routing)->host_count]
67 #define HOST2ROUTER(id) ((id)+(2<<29))
68 #define ROUTER2HOST(id) ((id)-(2>>20))
69 #define ROUTER(id) ((id)>=(2<<29))
74 static void routing_full_parse_Shost(void) {
75 int *val = xbt_malloc(sizeof(int));
76 DEBUG2("Seen host %s (#%d)",A_surfxml_host_id,used_routing->host_count);
77 *val = used_routing->host_count++;
78 xbt_dict_set(used_routing->host_id,A_surfxml_host_id,val,xbt_free);
81 static void routing_full_parse_Srouter(void) {
82 int *val = xbt_malloc(sizeof(int));
83 DEBUG3("Seen router %s (%d -> #%d)",A_surfxml_router_id,used_routing->router_count,
84 HOST2ROUTER(used_routing->router_count));
85 *val = HOST2ROUTER(used_routing->router_count++);
86 xbt_dict_set(used_routing->host_id,A_surfxml_router_id,val,xbt_free);
89 static int src_id = -1;
90 static int dst_id = -1;
91 static void routing_full_parse_Sroute_set_endpoints(void)
93 src_id = *(int*)xbt_dict_get(used_routing->host_id,A_surfxml_route_src);
94 dst_id = *(int*)xbt_dict_get(used_routing->host_id,A_surfxml_route_dst);
95 DEBUG4("Route %s %d -> %s %d",A_surfxml_route_src,src_id,A_surfxml_route_dst,dst_id);
96 route_action = A_surfxml_route_action;
98 static void routing_full_parse_Eroute(void)
101 if (src_id != -1 && dst_id != -1) {
102 name = bprintf("%x#%x", src_id, dst_id);
103 manage_route(route_table, name, route_action, 0);
108 /* Cluster tag functions */
110 static void routing_full_parse_change_cpu_data(const char *hostName,
111 const char *surfxml_host_power,
112 const char *surfxml_host_availability,
113 const char *surfxml_host_availability_file,
114 const char *surfxml_host_state_file)
118 SURFXML_BUFFER_SET(host_id, hostName);
119 SURFXML_BUFFER_SET(host_power, surfxml_host_power /*hostPower */ );
120 SURFXML_BUFFER_SET(host_availability, surfxml_host_availability);
121 SURFXML_BUFFER_SET(host_availability_file, surfxml_host_availability_file);
122 SURFXML_BUFFER_SET(host_state_file, surfxml_host_state_file);
125 static void routing_full_parse_change_link_data(const char *linkName,
126 const char *surfxml_link_bandwidth,
127 const char *surfxml_link_bandwidth_file,
128 const char *surfxml_link_latency,
129 const char *surfxml_link_latency_file,
130 const char *surfxml_link_state_file)
134 SURFXML_BUFFER_SET(link_id, linkName);
135 SURFXML_BUFFER_SET(link_bandwidth, surfxml_link_bandwidth);
136 SURFXML_BUFFER_SET(link_bandwidth_file, surfxml_link_bandwidth_file);
137 SURFXML_BUFFER_SET(link_latency, surfxml_link_latency);
138 SURFXML_BUFFER_SET(link_latency_file, surfxml_link_latency_file);
139 SURFXML_BUFFER_SET(link_state_file, surfxml_link_state_file);
142 static void routing_full_parse_Scluster(void)
144 static int AX_ptr = 0;
146 char *cluster_id = A_surfxml_cluster_id;
147 char *cluster_prefix = A_surfxml_cluster_prefix;
148 char *cluster_suffix = A_surfxml_cluster_suffix;
149 char *cluster_radical = A_surfxml_cluster_radical;
150 char *cluster_power = A_surfxml_cluster_power;
151 char *cluster_bw = A_surfxml_cluster_bw;
152 char *cluster_lat = A_surfxml_cluster_lat;
153 char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
154 char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
157 surfxml_bufferstack_push(1);
160 SURFXML_BUFFER_SET(set_id, cluster_id);
161 SURFXML_BUFFER_SET(set_prefix, cluster_prefix);
162 SURFXML_BUFFER_SET(set_suffix, cluster_suffix);
163 SURFXML_BUFFER_SET(set_radical, cluster_radical);
165 SURFXML_START_TAG(set);
166 SURFXML_END_TAG(set);
169 SURFXML_BUFFER_SET(foreach_set_id, cluster_id);
171 SURFXML_START_TAG(foreach);
173 /* Make host for the foreach */
174 routing_full_parse_change_cpu_data("$1", cluster_power, "1.0", "", "");
175 A_surfxml_host_state = A_surfxml_host_state_ON;
177 SURFXML_START_TAG(host);
178 SURFXML_END_TAG(host);
180 /* Make link for the foreach */
181 routing_full_parse_change_link_data("$1", cluster_bw, "", cluster_lat, "", "");
182 A_surfxml_link_state = A_surfxml_link_state_ON;
183 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
185 SURFXML_START_TAG(link);
186 SURFXML_END_TAG(link);
188 SURFXML_END_TAG(foreach);
190 /* Make backbone link */
191 backbone_name = bprintf("%s_bb", cluster_id);
192 routing_full_parse_change_link_data(backbone_name, cluster_bb_bw, "", cluster_bb_lat, "",
194 A_surfxml_link_state = A_surfxml_link_state_ON;
195 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
197 SURFXML_START_TAG(link);
198 SURFXML_END_TAG(link);
200 /* Make route multi with the outside world, i.e. cluster->$* */
201 SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
202 SURFXML_BUFFER_SET(route_c_multi_dst, "$*");
203 A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
204 A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_OVERRIDE;
206 SURFXML_START_TAG(route_c_multi);
208 SURFXML_BUFFER_SET(link_c_ctn_id, "$src");
210 SURFXML_START_TAG(link_c_ctn);
211 SURFXML_END_TAG(link_c_ctn);
213 SURFXML_END_TAG(route_c_multi);
215 /* Make route multi between cluster hosts, i.e. cluster->cluster */
216 SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
217 SURFXML_BUFFER_SET(route_c_multi_dst, cluster_id);
218 A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_POSTPEND;
219 A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
221 SURFXML_START_TAG(route_c_multi);
223 SURFXML_BUFFER_SET(link_c_ctn_id, backbone_name);
225 SURFXML_START_TAG(link_c_ctn);
226 SURFXML_END_TAG(link_c_ctn);
228 SURFXML_END_TAG(route_c_multi);
233 surfxml_bufferstack_pop(1);
237 static void routing_full_parse_end(void) {
238 routing_full_t routing = (routing_full_t) used_routing;
240 unsigned int cpt = 0;
241 xbt_dict_cursor_t cursor = NULL;
242 char *key, *data, *end;
243 const char *sep = "#";
244 xbt_dynar_t links, keys;
245 char *link_name = NULL;
248 int host_count = routing->generic_routing.host_count;
250 /* Create the routing table */
251 routing->routing_table = xbt_new0(xbt_dynar_t, host_count * host_count);
252 for (i=0;i<host_count;i++)
253 for (j=0;j<host_count;j++)
254 ROUTE_FULL(i,j) = xbt_dynar_new(routing->size_of_link,NULL);
256 /* Put the routes in position */
257 xbt_dict_foreach(route_table, cursor, key, data) {
259 links = (xbt_dynar_t) data;
260 keys = xbt_str_split_str(key, sep);
262 src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
263 dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
264 xbt_dynar_free(&keys);
266 if(ROUTER(src_id) || ROUTER(dst_id)) {
267 DEBUG2("There is route with a router here: (%d ,%d)",src_id,dst_id);
268 /* Check there is only one link in the route and store the information */
272 DEBUG4("Handle %d %d (from %d hosts): %ld links",
273 src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
274 xbt_dynar_foreach(links, cpt, link_name) {
275 void* link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
277 xbt_dynar_push(ROUTE_FULL(src_id,dst_id),&link);
279 THROW1(mismatch_error,0,"Link %s not found", link_name);
283 /* Add the loopback if needed */
284 for (i = 0; i < host_count; i++)
285 if (!xbt_dynar_length(ROUTE_FULL(i, i)))
286 xbt_dynar_push(ROUTE_FULL(i,i),&routing->loopback);
288 /* Shrink the dynar routes (save unused slots) */
289 for (i=0;i<host_count;i++)
290 for (j=0;j<host_count;j++)
291 xbt_dynar_shrink(ROUTE_FULL(i,j),0);
297 static xbt_dynar_t routing_full_get_route(int src,int dst) {
298 return ROUTE_FULL(src,dst);
301 static void routing_full_finalize(void) {
302 routing_full_t routing = (routing_full_t)used_routing;
306 for (i = 0; i < used_routing->host_count; i++)
307 for (j = 0; j < used_routing->host_count; j++)
308 xbt_dynar_free(&ROUTE_FULL(i, j));
309 free(routing->routing_table);
310 xbt_dict_free(&used_routing->host_id);
316 static void routing_model_full_create(size_t size_of_link,void *loopback) {
317 /* initialize our structure */
318 routing_full_t routing = xbt_new0(s_routing_full_t,1);
319 routing->generic_routing.name = "Full";
320 routing->generic_routing.host_count = 0;
321 routing->generic_routing.get_route = routing_full_get_route;
322 routing->generic_routing.finalize = routing_full_finalize;
323 routing->size_of_link = size_of_link;
324 routing->loopback = loopback;
326 /* Set it in position */
327 used_routing = (routing_t) routing;
329 /* Setup the parsing callbacks we need */
330 routing->generic_routing.host_id = xbt_dict_new();
331 surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
332 surfxml_add_callback(STag_surfxml_router_cb_list, &routing_full_parse_Srouter);
333 surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_full_parse_end);
334 surfxml_add_callback(STag_surfxml_route_cb_list,
335 &routing_full_parse_Sroute_set_endpoints);
336 surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);
337 surfxml_add_callback(STag_surfxml_cluster_cb_list, &routing_full_parse_Scluster);
340 /* ************************************************************************** */
342 static void routing_shortest_path_parse_Scluster(void)
344 static int AX_ptr = 0;
346 char *cluster_id = A_surfxml_cluster_id;
347 char *cluster_prefix = A_surfxml_cluster_prefix;
348 char *cluster_suffix = A_surfxml_cluster_suffix;
349 char *cluster_radical = A_surfxml_cluster_radical;
350 char *cluster_power = A_surfxml_cluster_power;
351 char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
352 char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
355 surfxml_bufferstack_push(1);
358 SURFXML_BUFFER_SET(set_id, cluster_id);
359 SURFXML_BUFFER_SET(set_prefix, cluster_prefix);
360 SURFXML_BUFFER_SET(set_suffix, cluster_suffix);
361 SURFXML_BUFFER_SET(set_radical, cluster_radical);
363 SURFXML_START_TAG(set);
364 SURFXML_END_TAG(set);
367 SURFXML_BUFFER_SET(foreach_set_id, cluster_id);
369 SURFXML_START_TAG(foreach);
371 /* Make host for the foreach */
372 routing_full_parse_change_cpu_data("$1", cluster_power, "1.0", "", "");
373 A_surfxml_host_state = A_surfxml_host_state_ON;
375 SURFXML_START_TAG(host);
376 SURFXML_END_TAG(host);
378 SURFXML_END_TAG(foreach);
380 /* Make backbone link */
381 backbone_name = bprintf("%s_bb", cluster_id);
382 routing_full_parse_change_link_data(backbone_name, cluster_bb_bw, "", cluster_bb_lat, "",
384 A_surfxml_link_state = A_surfxml_link_state_ON;
385 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
387 SURFXML_START_TAG(link);
388 SURFXML_END_TAG(link);
393 surfxml_bufferstack_pop(1);
396 /* ************************************************************************** */
397 /* *************************** FLOYD ROUTING ********************************* */
399 s_routing_t generic_routing;
400 int *predecessor_table;
402 xbt_dynar_t last_route;
405 } s_routing_floyd_t,*routing_floyd_t;
407 #define FLOYD_COST(i,j) cost_table[(i)+(j)*(used_routing)->host_count]
408 #define FLOYD_PRED(i,j) ((routing_floyd_t)used_routing)->predecessor_table[(i)+(j)*(used_routing)->host_count]
409 #define FLOYD_LINK(i,j) ((routing_floyd_t)used_routing)->link_table[(i)+(j)*(used_routing)->host_count]
411 static void routing_floyd_parse_end(void) {
413 routing_floyd_t routing = (routing_floyd_t) used_routing;
415 void* link_list = NULL;
417 xbt_dict_cursor_t cursor = NULL;
418 char *key,*data, *end;
419 const char *sep = "#";
420 xbt_dynar_t links, keys;
424 int host_count = routing->generic_routing.host_count;
426 /* Create Cost, Predecessor and Link tables */
427 cost_table = xbt_new0(double, host_count * host_count); //link cost from host to host
428 routing->predecessor_table = xbt_new0(int, host_count*host_count); //predecessor host numbers
429 routing->link_table = xbt_new0(void*,host_count*host_count); //actual link between src and dst
430 routing->last_route = xbt_dynar_new(routing->size_of_link, NULL);
432 /* Initialize costs and predecessors*/
433 for(i = 0; i<host_count;i++)
434 for(j = 0; j<host_count;j++) {
435 FLOYD_COST(i,j) = DBL_MAX;
436 FLOYD_PRED(i,j) = -1;
439 /* Put the routes in position */
440 xbt_dict_foreach(route_table, cursor, key, data) {
442 links = (xbt_dynar_t)data;
443 keys = xbt_str_split_str(key, sep);
446 src_id = strtol(xbt_dynar_get_as(keys, 0, char*), &end, 16);
447 dst_id = strtol(xbt_dynar_get_as(keys, 1, char*), &end, 16);
448 xbt_dynar_free(&keys);
450 DEBUG4("Handle %d %d (from %d hosts): %ld links",
451 src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
452 xbt_assert3(xbt_dynar_length(links) == 1, "%ld links in route between host %d and %d, should be 1", xbt_dynar_length(links), src_id, dst_id);
454 char * link_name = xbt_dynar_getfirst_as(links, char*);
455 void * link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
459 THROW1(mismatch_error,0,"Link %s not found", link_name);
462 FLOYD_LINK(src_id,dst_id) = link_list;
463 FLOYD_PRED(src_id, dst_id) = src_id;
466 FLOYD_COST(src_id, dst_id) = 1; // assume 1 for now
470 /* Add the loopback if needed */
471 for (i = 0; i < host_count; i++)
472 if (!FLOYD_PRED(i, i)) {
473 FLOYD_PRED(i, i) = i;
474 FLOYD_COST(i, i) = 1;
475 FLOYD_LINK(i, i) = routing->loopback;
479 //Calculate path costs
481 for(c=0;c<host_count;c++) {
482 for(a=0;a<host_count;a++) {
483 for(b=0;b<host_count;b++) {
484 if(FLOYD_COST(a,c) < DBL_MAX && FLOYD_COST(c,b) < DBL_MAX) {
485 if(FLOYD_COST(a,b) == DBL_MAX || (FLOYD_COST(a,c)+FLOYD_COST(c,b) < FLOYD_COST(a,b))) {
486 FLOYD_COST(a,b) = FLOYD_COST(a,c)+FLOYD_COST(c,b);
487 FLOYD_PRED(a,b) = FLOYD_PRED(c,b);
501 static xbt_dynar_t routing_floyd_get_route(int src_id,int dst_id) {
503 routing_floyd_t routing = (routing_floyd_t) used_routing;
508 xbt_dynar_reset(routing->last_route);
512 pred = FLOYD_PRED(src_id, pred);
514 if(pred == -1) // if no pred in route -> no route to host
517 xbt_dynar_unshift(routing->last_route, &FLOYD_LINK(pred,prev_pred));
519 } while(pred != src_id);
521 xbt_assert2(pred != -1, "no route from host %d to %d", src_id, dst_id);
523 return routing->last_route;
526 static void routing_floyd_finalize(void) {
527 routing_floyd_t routing = (routing_floyd_t)used_routing;
530 free(routing->link_table);
531 free(routing->predecessor_table);
532 xbt_dynar_free(&routing->last_route);
533 xbt_dict_free(&used_routing->host_id);
539 static void routing_model_floyd_create(size_t size_of_link,void *loopback) {
540 /* initialize our structure */
541 routing_floyd_t routing = xbt_new0(s_routing_floyd_t,1);
542 routing->generic_routing.name = "Floyd";
543 routing->generic_routing.host_count = 0;
544 routing->generic_routing.host_id = xbt_dict_new();
545 routing->generic_routing.get_route = routing_floyd_get_route;
546 routing->generic_routing.finalize = routing_floyd_finalize;
547 routing->size_of_link = size_of_link;
548 routing->loopback = loopback;
550 /* Set it in position */
551 used_routing = (routing_t) routing;
553 /* Setup the parsing callbacks we need */
554 surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
555 surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_floyd_parse_end);
556 surfxml_add_callback(STag_surfxml_route_cb_list,
557 &routing_full_parse_Sroute_set_endpoints);
558 surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);
559 surfxml_add_callback(STag_surfxml_cluster_cb_list, &routing_shortest_path_parse_Scluster);
563 /* ************************************************************************** */
564 /* ********** Dijkstra & Dijkstra Cached ROUTING **************************** */
566 s_routing_t generic_routing;
567 xbt_graph_t route_graph;
568 xbt_dict_t graph_node_map;
569 xbt_dict_t route_cache;
570 xbt_dynar_t last_route;
574 } s_routing_dijkstra_t,*routing_dijkstra_t;
577 typedef struct graph_node_data {
579 int graph_id; //used for caching internal graph id's
580 } s_graph_node_data_t, * graph_node_data_t;
582 typedef struct graph_node_map_element {
584 } s_graph_node_map_element_t, * graph_node_map_element_t;
586 typedef struct route_cache_element {
589 } s_route_cache_element_t, * route_cache_element_t;
594 static void route_cache_elem_free(void *e) {
595 route_cache_element_t elm=(route_cache_element_t)e;
603 static void graph_node_map_elem_free(void *e) {
604 graph_node_map_element_t elm = (graph_node_map_element_t)e;
614 static xbt_node_t route_graph_new_node(int id, int graph_id) {
615 routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
617 graph_node_data_t data = xbt_new0(struct graph_node_data, sizeof(struct graph_node_data));
619 data->graph_id = graph_id;
620 xbt_node_t node = xbt_graph_new_node(routing->route_graph, data);
622 graph_node_map_element_t elm = xbt_new0(struct graph_node_map_element, sizeof(struct graph_node_map_element));
624 xbt_dict_set_ext(routing->graph_node_map, (char*)(&id), sizeof(int), (xbt_set_elm_t)elm, &graph_node_map_elem_free);
629 static graph_node_map_element_t graph_node_map_search(int id) {
630 routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
632 graph_node_map_element_t elm = (graph_node_map_element_t)xbt_dict_get_or_null_ext(routing->graph_node_map, (char*)(&id), sizeof(int));
640 static void route_new_dijkstra(int src_id, int dst_id, void* link) {
641 routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
643 xbt_node_t src = NULL;
644 xbt_node_t dst = NULL;
645 graph_node_map_element_t src_elm = (graph_node_map_element_t)xbt_dict_get_or_null_ext(routing->graph_node_map, (char*)(&src_id), sizeof(int));
646 graph_node_map_element_t dst_elm = (graph_node_map_element_t)xbt_dict_get_or_null_ext(routing->graph_node_map, (char*)(&dst_id), sizeof(int));
654 //add nodes if they don't exist in the graph
655 if(src_id == dst_id && src == NULL && dst == NULL) {
656 src = route_graph_new_node(src_id, -1);
660 src = route_graph_new_node(src_id, -1);
664 dst = route_graph_new_node(dst_id, -1);
668 //add link as edge to graph
669 xbt_graph_new_edge(routing->route_graph, src, dst, link);
673 static void add_loopback_dijkstra(void) {
674 routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
676 xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
678 xbt_node_t node = NULL;
679 unsigned int cursor2;
680 xbt_dynar_foreach(nodes, cursor2, node) {
681 xbt_dynar_t out_edges = xbt_graph_node_get_outedges(node);
682 xbt_edge_t edge = NULL;
686 xbt_dynar_foreach(out_edges, cursor, edge) {
687 xbt_node_t other_node = xbt_graph_edge_get_target(edge);
688 if(other_node == node) {
695 xbt_graph_new_edge(routing->route_graph, node, node, &routing->loopback);
699 static void routing_dijkstra_parse_end(void) {
700 routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
702 xbt_dict_cursor_t cursor = NULL;
703 char *key, *data, *end;
704 const char *sep = "#";
705 xbt_dynar_t links, keys;
707 /* Create the topology graph */
708 routing->route_graph = xbt_graph_new_graph(1, NULL);
709 routing->graph_node_map = xbt_dict_new();
710 routing->last_route = xbt_dynar_new(routing->size_of_link, NULL);
712 routing->route_cache = xbt_dict_new();
715 /* Put the routes in position */
716 xbt_dict_foreach(route_table, cursor, key, data) {
718 links = (xbt_dynar_t) data;
719 keys = xbt_str_split_str(key, sep);
721 src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
722 dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
723 xbt_dynar_free(&keys);
725 DEBUG4("Handle %d %d (from %d hosts): %ld links",
726 src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
728 xbt_assert3(xbt_dynar_length(links) == 1, "%ld links in route between host %d and %d, should be 1", xbt_dynar_length(links), src_id, dst_id);
730 char* link_name = xbt_dynar_getfirst_as(links, char*);
731 void* link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
733 route_new_dijkstra(src_id,dst_id,link);
735 THROW1(mismatch_error,0,"Link %s not found", link_name);
739 /* Add the loopback if needed */
740 add_loopback_dijkstra();
742 /* initialize graph indexes in nodes after graph has been built */
743 xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
745 xbt_node_t node = NULL;
746 unsigned int cursor2;
747 xbt_dynar_foreach(nodes, cursor2, node) {
748 graph_node_data_t data = xbt_graph_node_get_data(node);
749 data->graph_id = cursor2;
757 static xbt_dynar_t routing_dijkstra_get_route(int src_id,int dst_id) {
759 routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
760 int * pred_arr = NULL;
762 xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
764 /*Use the graph_node id mapping set to quickly find the nodes */
765 graph_node_map_element_t src_elm = graph_node_map_search(src_id);
766 graph_node_map_element_t dst_elm = graph_node_map_search(dst_id);
767 xbt_assert2(src_elm != NULL && dst_elm != NULL, "src %d or dst %d does not exist", src_id, dst_id);
768 int src_node_id = ((graph_node_data_t)xbt_graph_node_get_data(src_elm->node))->graph_id;
769 int dst_node_id = ((graph_node_data_t)xbt_graph_node_get_data(dst_elm->node))->graph_id;
771 route_cache_element_t elm = NULL;
772 if(routing->cached) {
773 /*check if there is a cached predecessor list avail */
774 elm = (route_cache_element_t)xbt_dict_get_or_null_ext(routing->route_cache, (char*)(&src_id), sizeof(int));
777 if(elm) { //cached mode and cache hit
778 pred_arr = elm->pred_arr;
779 } else { //not cached mode or cache miss
780 double * cost_arr = NULL;
781 xbt_heap_t pqueue = NULL;
784 int nr_nodes = xbt_dynar_length(nodes);
785 cost_arr = xbt_new0(double, nr_nodes); //link cost from src to other hosts
786 pred_arr = xbt_new0(int, nr_nodes); //predecessors in path from src
787 pqueue = xbt_heap_new(nr_nodes, free);
790 cost_arr[src_node_id] = 0.0;
792 for(i = 0; i < nr_nodes; i++) {
793 if(i != src_node_id) {
794 cost_arr[i] = DBL_MAX;
799 //initialize priority queue
800 int * nodeid = xbt_new0(int, 1);
802 xbt_heap_push(pqueue, nodeid, cost_arr[i]);
806 // apply dijkstra using the indexes from the graph's node array
807 while(xbt_heap_size(pqueue) > 0) {
808 int * v_id = xbt_heap_pop(pqueue);
809 xbt_node_t v_node = xbt_dynar_get_as(nodes, *v_id, xbt_node_t);
810 xbt_dynar_t out_edges = xbt_graph_node_get_outedges(v_node);
811 xbt_edge_t edge = NULL;
814 xbt_dynar_foreach(out_edges, cursor, edge) {
815 xbt_node_t u_node = xbt_graph_edge_get_target(edge);
816 graph_node_data_t data = xbt_graph_node_get_data(u_node);
817 int u_id = data->graph_id;
818 int cost_v_u = 1; //fixed link cost for now
820 if(cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
821 pred_arr[u_id] = *v_id;
822 cost_arr[u_id] = cost_v_u + cost_arr[*v_id];
823 int * nodeid = xbt_new0(int, 1);
825 xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
829 //free item popped from pqueue
834 xbt_heap_free(pqueue);
838 //compose route path with links
839 xbt_dynar_reset(routing->last_route);
843 for(v = dst_node_id; v != src_node_id; v = pred_arr[v]) {
844 xbt_node_t node_pred_v = xbt_dynar_get_as(nodes, pred_arr[v], xbt_node_t);
845 xbt_node_t node_v = xbt_dynar_get_as(nodes, v, xbt_node_t);
846 xbt_edge_t edge = xbt_graph_get_edge(routing->route_graph, node_pred_v, node_v);
848 xbt_assert2(edge != NULL, "no route between host %d and %d", src_id, dst_id);
850 void * link = xbt_graph_edge_get_data(edge);
851 xbt_dynar_unshift(routing->last_route, &link);
856 if(routing->cached && elm == NULL) {
857 //add to predecessor list of the current src-host to cache
858 elm = xbt_new0(struct route_cache_element, sizeof(struct route_cache_element));
859 elm->pred_arr = pred_arr;
861 xbt_dict_set_ext(routing->route_cache, (char*)(&src_id), sizeof(int), (xbt_set_elm_t)elm, &route_cache_elem_free);
867 return routing->last_route;
871 static void routing_dijkstra_finalize(void) {
872 routing_dijkstra_t routing = (routing_dijkstra_t)used_routing;
875 xbt_graph_free_graph(routing->route_graph, &free, NULL, &free);
876 xbt_dict_free(&routing->graph_node_map);
878 xbt_dict_free(&routing->route_cache);
879 xbt_dynar_free(&routing->last_route);
880 xbt_dict_free(&used_routing->host_id);
889 static void routing_model_dijkstraboth_create(size_t size_of_link,void *loopback, int cached) {
890 /* initialize our structure */
891 routing_dijkstra_t routing = xbt_new0(s_routing_dijkstra_t,1);
892 routing->generic_routing.name = "Dijkstra";
893 routing->generic_routing.host_count = 0;
894 routing->generic_routing.get_route = routing_dijkstra_get_route;
895 routing->generic_routing.finalize = routing_dijkstra_finalize;
896 routing->size_of_link = size_of_link;
897 routing->loopback = loopback;
898 routing->cached = cached;
900 /* Set it in position */
901 used_routing = (routing_t) routing;
903 /* Setup the parsing callbacks we need */
904 routing->generic_routing.host_id = xbt_dict_new();
905 surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
906 surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_dijkstra_parse_end);
907 surfxml_add_callback(STag_surfxml_route_cb_list,
908 &routing_full_parse_Sroute_set_endpoints);
909 surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);
910 surfxml_add_callback(STag_surfxml_cluster_cb_list, &routing_shortest_path_parse_Scluster);
913 static void routing_model_dijkstra_create(size_t size_of_link,void *loopback) {
914 routing_model_dijkstraboth_create(size_of_link, loopback, 0);
917 static void routing_model_dijkstracache_create(size_t size_of_link,void *loopback) {
918 routing_model_dijkstraboth_create(size_of_link, loopback, 1);
921 /* ************************************************** */
922 /* ********** NO ROUTING **************************** */
924 static void routing_none_finalize(void) {
926 xbt_dict_free(&used_routing->host_id);
932 static void routing_model_none_create(size_t size_of_link,void *loopback) {
933 routing_t routing = xbt_new0(s_routing_t,1);
934 INFO0("Null routing");
935 routing->name = "none";
936 routing->host_count = 0;
937 routing->host_id = xbt_dict_new();
938 routing->get_route = NULL;
939 routing->finalize = routing_none_finalize;
941 /* Set it in position */
942 used_routing = (routing_t) routing;