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]
71 static void routing_full_parse_Srouter(void) {
72 int *val = xbt_malloc(sizeof(int));
73 DEBUG2("Seen router %s (#%d)",A_surfxml_host_id,used_routing->host_count);
74 *val = used_routing->host_count++;
75 xbt_dict_set(used_routing->host_id,A_surfxml_host_id,val,xbt_free);
78 static void routing_full_parse_Shost(void) {
79 int *val = xbt_malloc(sizeof(int));
80 DEBUG2("Seen host %s (#%d)",A_surfxml_host_id,used_routing->host_count);
81 *val = used_routing->host_count++;
82 xbt_dict_set(used_routing->host_id,A_surfxml_host_id,val,xbt_free);
84 static int src_id = -1;
85 static int dst_id = -1;
86 static void routing_full_parse_Sroute_set_endpoints(void)
88 src_id = *(int*)xbt_dict_get(used_routing->host_id,A_surfxml_route_src);
89 dst_id = *(int*)xbt_dict_get(used_routing->host_id,A_surfxml_route_dst);
90 DEBUG4("Route %s %d -> %s %d",A_surfxml_route_src,src_id,A_surfxml_route_dst,dst_id);
91 route_action = A_surfxml_route_action;
93 static void routing_full_parse_Eroute(void)
96 if (src_id != -1 && dst_id != -1) {
97 name = bprintf("%x#%x", src_id, dst_id);
98 manage_route(route_table, name, route_action, 0);
103 /* Cluster tag functions */
105 static void routing_full_parse_change_cpu_data(const char *hostName,
106 const char *surfxml_host_power,
107 const char *surfxml_host_availability,
108 const char *surfxml_host_availability_file,
109 const char *surfxml_host_state_file)
113 SURFXML_BUFFER_SET(host_id, hostName);
114 SURFXML_BUFFER_SET(host_power, surfxml_host_power /*hostPower */ );
115 SURFXML_BUFFER_SET(host_availability, surfxml_host_availability);
116 SURFXML_BUFFER_SET(host_availability_file, surfxml_host_availability_file);
117 SURFXML_BUFFER_SET(host_state_file, surfxml_host_state_file);
120 static void routing_full_parse_change_link_data(const char *linkName,
121 const char *surfxml_link_bandwidth,
122 const char *surfxml_link_bandwidth_file,
123 const char *surfxml_link_latency,
124 const char *surfxml_link_latency_file,
125 const char *surfxml_link_state_file)
129 SURFXML_BUFFER_SET(link_id, linkName);
130 SURFXML_BUFFER_SET(link_bandwidth, surfxml_link_bandwidth);
131 SURFXML_BUFFER_SET(link_bandwidth_file, surfxml_link_bandwidth_file);
132 SURFXML_BUFFER_SET(link_latency, surfxml_link_latency);
133 SURFXML_BUFFER_SET(link_latency_file, surfxml_link_latency_file);
134 SURFXML_BUFFER_SET(link_state_file, surfxml_link_state_file);
137 static void routing_full_parse_Scluster(void)
139 static int AX_ptr = 0;
141 char *cluster_id = A_surfxml_cluster_id;
142 char *cluster_prefix = A_surfxml_cluster_prefix;
143 char *cluster_suffix = A_surfxml_cluster_suffix;
144 char *cluster_radical = A_surfxml_cluster_radical;
145 char *cluster_power = A_surfxml_cluster_power;
146 char *cluster_bw = A_surfxml_cluster_bw;
147 char *cluster_lat = A_surfxml_cluster_lat;
148 char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
149 char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
152 surfxml_bufferstack_push(1);
155 SURFXML_BUFFER_SET(set_id, cluster_id);
156 SURFXML_BUFFER_SET(set_prefix, cluster_prefix);
157 SURFXML_BUFFER_SET(set_suffix, cluster_suffix);
158 SURFXML_BUFFER_SET(set_radical, cluster_radical);
160 SURFXML_START_TAG(set);
161 SURFXML_END_TAG(set);
164 SURFXML_BUFFER_SET(foreach_set_id, cluster_id);
166 SURFXML_START_TAG(foreach);
168 /* Make host for the foreach */
169 routing_full_parse_change_cpu_data("$1", cluster_power, "1.0", "", "");
170 A_surfxml_host_state = A_surfxml_host_state_ON;
172 SURFXML_START_TAG(host);
173 SURFXML_END_TAG(host);
175 /* Make link for the foreach */
176 routing_full_parse_change_link_data("$1", cluster_bw, "", cluster_lat, "", "");
177 A_surfxml_link_state = A_surfxml_link_state_ON;
178 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
180 SURFXML_START_TAG(link);
181 SURFXML_END_TAG(link);
183 SURFXML_END_TAG(foreach);
185 /* Make backbone link */
186 backbone_name = bprintf("%s_bb", cluster_id);
187 routing_full_parse_change_link_data(backbone_name, cluster_bb_bw, "", cluster_bb_lat, "",
189 A_surfxml_link_state = A_surfxml_link_state_ON;
190 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
192 SURFXML_START_TAG(link);
193 SURFXML_END_TAG(link);
195 /* Make route multi with the outside world, i.e. cluster->$* */
196 SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
197 SURFXML_BUFFER_SET(route_c_multi_dst, "$*");
198 A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
199 A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_OVERRIDE;
201 SURFXML_START_TAG(route_c_multi);
203 SURFXML_BUFFER_SET(link_c_ctn_id, "$src");
205 SURFXML_START_TAG(link_c_ctn);
206 SURFXML_END_TAG(link_c_ctn);
208 SURFXML_END_TAG(route_c_multi);
210 /* Make route multi between cluster hosts, i.e. cluster->cluster */
211 SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
212 SURFXML_BUFFER_SET(route_c_multi_dst, cluster_id);
213 A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_POSTPEND;
214 A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
216 SURFXML_START_TAG(route_c_multi);
218 SURFXML_BUFFER_SET(link_c_ctn_id, backbone_name);
220 SURFXML_START_TAG(link_c_ctn);
221 SURFXML_END_TAG(link_c_ctn);
223 SURFXML_END_TAG(route_c_multi);
228 surfxml_bufferstack_pop(1);
232 static void routing_full_parse_end(void) {
233 routing_full_t routing = (routing_full_t) used_routing;
235 unsigned int cpt = 0;
236 xbt_dict_cursor_t cursor = NULL;
237 char *key, *data, *end;
238 const char *sep = "#";
239 xbt_dynar_t links, keys;
240 char *link_name = NULL;
243 int host_count = routing->generic_routing.host_count;
245 /* Create the routing table */
246 routing->routing_table = xbt_new0(xbt_dynar_t, host_count * host_count);
247 for (i=0;i<host_count;i++)
248 for (j=0;j<host_count;j++)
249 ROUTE_FULL(i,j) = xbt_dynar_new(routing->size_of_link,NULL);
251 /* Put the routes in position */
252 xbt_dict_foreach(route_table, cursor, key, data) {
254 links = (xbt_dynar_t) data;
255 keys = xbt_str_split_str(key, sep);
257 src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
258 dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
259 xbt_dynar_free(&keys);
261 DEBUG4("Handle %d %d (from %d hosts): %ld links",
262 src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
263 xbt_dynar_foreach(links, cpt, link_name) {
264 void* link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
266 xbt_dynar_push(ROUTE_FULL(src_id,dst_id),&link);
268 THROW1(mismatch_error,0,"Link %s not found", link_name);
272 /* Add the loopback if needed */
273 for (i = 0; i < host_count; i++)
274 if (!xbt_dynar_length(ROUTE_FULL(i, i)))
275 xbt_dynar_push(ROUTE_FULL(i,i),&routing->loopback);
277 /* Shrink the dynar routes (save unused slots) */
278 for (i=0;i<host_count;i++)
279 for (j=0;j<host_count;j++)
280 xbt_dynar_shrink(ROUTE_FULL(i,j),0);
286 static xbt_dynar_t routing_full_get_route(int src,int dst) {
287 return ROUTE_FULL(src,dst);
290 static void routing_full_finalize(void) {
291 routing_full_t routing = (routing_full_t)used_routing;
295 for (i = 0; i < used_routing->host_count; i++)
296 for (j = 0; j < used_routing->host_count; j++)
297 xbt_dynar_free(&ROUTE_FULL(i, j));
298 free(routing->routing_table);
299 xbt_dict_free(&used_routing->host_id);
305 static void routing_model_full_create(size_t size_of_link,void *loopback) {
306 /* initialize our structure */
307 routing_full_t routing = xbt_new0(s_routing_full_t,1);
308 routing->generic_routing.name = "Full";
309 routing->generic_routing.host_count = 0;
310 routing->generic_routing.get_route = routing_full_get_route;
311 routing->generic_routing.finalize = routing_full_finalize;
312 routing->size_of_link = size_of_link;
313 routing->loopback = loopback;
315 /* Set it in position */
316 used_routing = (routing_t) routing;
318 /* Setup the parsing callbacks we need */
319 routing->generic_routing.host_id = xbt_dict_new();
320 surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
321 surfxml_add_callback(STag_surfxml_router_cb_list, &routing_full_parse_Srouter);
322 surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_full_parse_end);
323 surfxml_add_callback(STag_surfxml_route_cb_list,
324 &routing_full_parse_Sroute_set_endpoints);
325 surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);
326 surfxml_add_callback(STag_surfxml_cluster_cb_list, &routing_full_parse_Scluster);
329 /* ************************************************************************** */
331 static void routing_shortest_path_parse_Scluster(void)
333 static int AX_ptr = 0;
335 char *cluster_id = A_surfxml_cluster_id;
336 char *cluster_prefix = A_surfxml_cluster_prefix;
337 char *cluster_suffix = A_surfxml_cluster_suffix;
338 char *cluster_radical = A_surfxml_cluster_radical;
339 char *cluster_power = A_surfxml_cluster_power;
340 char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
341 char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
344 surfxml_bufferstack_push(1);
347 SURFXML_BUFFER_SET(set_id, cluster_id);
348 SURFXML_BUFFER_SET(set_prefix, cluster_prefix);
349 SURFXML_BUFFER_SET(set_suffix, cluster_suffix);
350 SURFXML_BUFFER_SET(set_radical, cluster_radical);
352 SURFXML_START_TAG(set);
353 SURFXML_END_TAG(set);
356 SURFXML_BUFFER_SET(foreach_set_id, cluster_id);
358 SURFXML_START_TAG(foreach);
360 /* Make host for the foreach */
361 routing_full_parse_change_cpu_data("$1", cluster_power, "1.0", "", "");
362 A_surfxml_host_state = A_surfxml_host_state_ON;
364 SURFXML_START_TAG(host);
365 SURFXML_END_TAG(host);
367 SURFXML_END_TAG(foreach);
369 /* Make backbone link */
370 backbone_name = bprintf("%s_bb", cluster_id);
371 routing_full_parse_change_link_data(backbone_name, cluster_bb_bw, "", cluster_bb_lat, "",
373 A_surfxml_link_state = A_surfxml_link_state_ON;
374 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
376 SURFXML_START_TAG(link);
377 SURFXML_END_TAG(link);
382 surfxml_bufferstack_pop(1);
385 /* ************************************************************************** */
386 /* *************************** FLOYD ROUTING ********************************* */
388 s_routing_t generic_routing;
389 int *predecessor_table;
391 xbt_dynar_t last_route;
394 } s_routing_floyd_t,*routing_floyd_t;
396 #define FLOYD_COST(i,j) cost_table[(i)+(j)*(used_routing)->host_count]
397 #define FLOYD_PRED(i,j) ((routing_floyd_t)used_routing)->predecessor_table[(i)+(j)*(used_routing)->host_count]
398 #define FLOYD_LINK(i,j) ((routing_floyd_t)used_routing)->link_table[(i)+(j)*(used_routing)->host_count]
400 static void routing_floyd_parse_end(void) {
402 routing_floyd_t routing = (routing_floyd_t) used_routing;
404 void* link_list = NULL;
406 xbt_dict_cursor_t cursor = NULL;
407 char *key,*data, *end;
408 const char *sep = "#";
409 xbt_dynar_t links, keys;
413 int host_count = routing->generic_routing.host_count;
415 /* Create Cost, Predecessor and Link tables */
416 cost_table = xbt_new0(double, host_count * host_count); //link cost from host to host
417 routing->predecessor_table = xbt_new0(int, host_count*host_count); //predecessor host numbers
418 routing->link_table = xbt_new0(void*,host_count*host_count); //actual link between src and dst
419 routing->last_route = xbt_dynar_new(routing->size_of_link, NULL);
421 /* Initialize costs and predecessors*/
422 for(i = 0; i<host_count;i++)
423 for(j = 0; j<host_count;j++) {
424 FLOYD_COST(i,j) = DBL_MAX;
425 FLOYD_PRED(i,j) = -1;
428 /* Put the routes in position */
429 xbt_dict_foreach(route_table, cursor, key, data) {
431 links = (xbt_dynar_t)data;
432 keys = xbt_str_split_str(key, sep);
435 src_id = strtol(xbt_dynar_get_as(keys, 0, char*), &end, 16);
436 dst_id = strtol(xbt_dynar_get_as(keys, 1, char*), &end, 16);
437 xbt_dynar_free(&keys);
439 DEBUG4("Handle %d %d (from %d hosts): %ld links",
440 src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
441 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);
443 char * link_name = xbt_dynar_getfirst_as(links, char*);
444 void * link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
448 THROW1(mismatch_error,0,"Link %s not found", link_name);
451 FLOYD_LINK(src_id,dst_id) = link_list;
452 FLOYD_PRED(src_id, dst_id) = src_id;
455 FLOYD_COST(src_id, dst_id) = 1; // assume 1 for now
459 /* Add the loopback if needed */
460 for (i = 0; i < host_count; i++)
461 if (!FLOYD_PRED(i, i)) {
462 FLOYD_PRED(i, i) = i;
463 FLOYD_COST(i, i) = 1;
464 FLOYD_LINK(i, i) = routing->loopback;
468 //Calculate path costs
470 for(c=0;c<host_count;c++) {
471 for(a=0;a<host_count;a++) {
472 for(b=0;b<host_count;b++) {
473 if(FLOYD_COST(a,c) < DBL_MAX && FLOYD_COST(c,b) < DBL_MAX) {
474 if(FLOYD_COST(a,b) == DBL_MAX || (FLOYD_COST(a,c)+FLOYD_COST(c,b) < FLOYD_COST(a,b))) {
475 FLOYD_COST(a,b) = FLOYD_COST(a,c)+FLOYD_COST(c,b);
476 FLOYD_PRED(a,b) = FLOYD_PRED(c,b);
490 static xbt_dynar_t routing_floyd_get_route(int src_id,int dst_id) {
492 routing_floyd_t routing = (routing_floyd_t) used_routing;
497 xbt_dynar_reset(routing->last_route);
501 pred = FLOYD_PRED(src_id, pred);
503 if(pred == -1) // if no pred in route -> no route to host
506 xbt_dynar_unshift(routing->last_route, &FLOYD_LINK(pred,prev_pred));
508 } while(pred != src_id);
510 xbt_assert2(pred != -1, "no route from host %d to %d", src_id, dst_id);
512 return routing->last_route;
515 static void routing_floyd_finalize(void) {
516 routing_floyd_t routing = (routing_floyd_t)used_routing;
519 free(routing->link_table);
520 free(routing->predecessor_table);
521 xbt_dynar_free(&routing->last_route);
522 xbt_dict_free(&used_routing->host_id);
528 static void routing_model_floyd_create(size_t size_of_link,void *loopback) {
529 /* initialize our structure */
530 routing_floyd_t routing = xbt_new0(s_routing_floyd_t,1);
531 routing->generic_routing.name = "Floyd";
532 routing->generic_routing.host_count = 0;
533 routing->generic_routing.host_id = xbt_dict_new();
534 routing->generic_routing.get_route = routing_floyd_get_route;
535 routing->generic_routing.finalize = routing_floyd_finalize;
536 routing->size_of_link = size_of_link;
537 routing->loopback = loopback;
539 /* Set it in position */
540 used_routing = (routing_t) routing;
542 /* Setup the parsing callbacks we need */
543 surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
544 surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_floyd_parse_end);
545 surfxml_add_callback(STag_surfxml_route_cb_list,
546 &routing_full_parse_Sroute_set_endpoints);
547 surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);
548 surfxml_add_callback(STag_surfxml_cluster_cb_list, &routing_shortest_path_parse_Scluster);
552 /* ************************************************************************** */
553 /* ********** Dijkstra & Dijkstra Cached ROUTING **************************** */
555 s_routing_t generic_routing;
556 xbt_graph_t route_graph;
557 xbt_dict_t graph_node_map;
558 xbt_dict_t route_cache;
559 xbt_dynar_t last_route;
563 } s_routing_dijkstra_t,*routing_dijkstra_t;
566 typedef struct graph_node_data {
568 int graph_id; //used for caching internal graph id's
569 } s_graph_node_data_t, * graph_node_data_t;
571 typedef struct graph_node_map_element {
573 } s_graph_node_map_element_t, * graph_node_map_element_t;
575 typedef struct route_cache_element {
578 } s_route_cache_element_t, * route_cache_element_t;
583 static void route_cache_elem_free(void *e) {
584 route_cache_element_t elm=(route_cache_element_t)e;
592 static void graph_node_map_elem_free(void *e) {
593 graph_node_map_element_t elm = (graph_node_map_element_t)e;
603 static xbt_node_t route_graph_new_node(int id, int graph_id) {
604 routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
606 graph_node_data_t data = xbt_new0(struct graph_node_data, sizeof(struct graph_node_data));
608 data->graph_id = graph_id;
609 xbt_node_t node = xbt_graph_new_node(routing->route_graph, data);
611 graph_node_map_element_t elm = xbt_new0(struct graph_node_map_element, sizeof(struct graph_node_map_element));
613 xbt_dict_set_ext(routing->graph_node_map, (char*)(&id), sizeof(int), (xbt_set_elm_t)elm, &graph_node_map_elem_free);
618 static graph_node_map_element_t graph_node_map_search(int id) {
619 routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
621 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));
629 static void route_new_dijkstra(int src_id, int dst_id, void* link) {
630 routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
632 xbt_node_t src = NULL;
633 xbt_node_t dst = NULL;
634 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));
635 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));
643 //add nodes if they don't exist in the graph
644 if(src_id == dst_id && src == NULL && dst == NULL) {
645 src = route_graph_new_node(src_id, -1);
649 src = route_graph_new_node(src_id, -1);
653 dst = route_graph_new_node(dst_id, -1);
657 //add link as edge to graph
658 xbt_graph_new_edge(routing->route_graph, src, dst, link);
662 static void add_loopback_dijkstra(void) {
663 routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
665 xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
667 xbt_node_t node = NULL;
668 unsigned int cursor2;
669 xbt_dynar_foreach(nodes, cursor2, node) {
670 xbt_dynar_t out_edges = xbt_graph_node_get_outedges(node);
671 xbt_edge_t edge = NULL;
675 xbt_dynar_foreach(out_edges, cursor, edge) {
676 xbt_node_t other_node = xbt_graph_edge_get_target(edge);
677 if(other_node == node) {
684 xbt_graph_new_edge(routing->route_graph, node, node, &routing->loopback);
688 static void routing_dijkstra_parse_end(void) {
689 routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
691 xbt_dict_cursor_t cursor = NULL;
692 char *key, *data, *end;
693 const char *sep = "#";
694 xbt_dynar_t links, keys;
696 /* Create the topology graph */
697 routing->route_graph = xbt_graph_new_graph(1, NULL);
698 routing->graph_node_map = xbt_dict_new();
699 routing->last_route = xbt_dynar_new(routing->size_of_link, NULL);
701 routing->route_cache = xbt_dict_new();
704 /* Put the routes in position */
705 xbt_dict_foreach(route_table, cursor, key, data) {
707 links = (xbt_dynar_t) data;
708 keys = xbt_str_split_str(key, sep);
710 src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
711 dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
712 xbt_dynar_free(&keys);
714 DEBUG4("Handle %d %d (from %d hosts): %ld links",
715 src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
717 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);
719 char* link_name = xbt_dynar_getfirst_as(links, char*);
720 void* link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
722 route_new_dijkstra(src_id,dst_id,link);
724 THROW1(mismatch_error,0,"Link %s not found", link_name);
728 /* Add the loopback if needed */
729 add_loopback_dijkstra();
731 /* initialize graph indexes in nodes after graph has been built */
732 xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
734 xbt_node_t node = NULL;
735 unsigned int cursor2;
736 xbt_dynar_foreach(nodes, cursor2, node) {
737 graph_node_data_t data = xbt_graph_node_get_data(node);
738 data->graph_id = cursor2;
746 static xbt_dynar_t routing_dijkstra_get_route(int src_id,int dst_id) {
748 routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
749 int * pred_arr = NULL;
751 xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
753 /*Use the graph_node id mapping set to quickly find the nodes */
754 graph_node_map_element_t src_elm = graph_node_map_search(src_id);
755 graph_node_map_element_t dst_elm = graph_node_map_search(dst_id);
756 xbt_assert2(src_elm != NULL && dst_elm != NULL, "src %d or dst %d does not exist", src_id, dst_id);
757 int src_node_id = ((graph_node_data_t)xbt_graph_node_get_data(src_elm->node))->graph_id;
758 int dst_node_id = ((graph_node_data_t)xbt_graph_node_get_data(dst_elm->node))->graph_id;
760 route_cache_element_t elm = NULL;
761 if(routing->cached) {
762 /*check if there is a cached predecessor list avail */
763 elm = (route_cache_element_t)xbt_dict_get_or_null_ext(routing->route_cache, (char*)(&src_id), sizeof(int));
766 if(elm) { //cached mode and cache hit
767 pred_arr = elm->pred_arr;
768 } else { //not cached mode or cache miss
769 double * cost_arr = NULL;
770 xbt_heap_t pqueue = NULL;
773 int nr_nodes = xbt_dynar_length(nodes);
774 cost_arr = xbt_new0(double, nr_nodes); //link cost from src to other hosts
775 pred_arr = xbt_new0(int, nr_nodes); //predecessors in path from src
776 pqueue = xbt_heap_new(nr_nodes, free);
779 cost_arr[src_node_id] = 0.0;
781 for(i = 0; i < nr_nodes; i++) {
782 if(i != src_node_id) {
783 cost_arr[i] = DBL_MAX;
788 //initialize priority queue
789 int * nodeid = xbt_new0(int, 1);
791 xbt_heap_push(pqueue, nodeid, cost_arr[i]);
795 // apply dijkstra using the indexes from the graph's node array
796 while(xbt_heap_size(pqueue) > 0) {
797 int * v_id = xbt_heap_pop(pqueue);
798 xbt_node_t v_node = xbt_dynar_get_as(nodes, *v_id, xbt_node_t);
799 xbt_dynar_t out_edges = xbt_graph_node_get_outedges(v_node);
800 xbt_edge_t edge = NULL;
803 xbt_dynar_foreach(out_edges, cursor, edge) {
804 xbt_node_t u_node = xbt_graph_edge_get_target(edge);
805 graph_node_data_t data = xbt_graph_node_get_data(u_node);
806 int u_id = data->graph_id;
807 int cost_v_u = 1; //fixed link cost for now
809 if(cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
810 pred_arr[u_id] = *v_id;
811 cost_arr[u_id] = cost_v_u + cost_arr[*v_id];
812 int * nodeid = xbt_new0(int, 1);
814 xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
818 //free item popped from pqueue
823 xbt_heap_free(pqueue);
827 //compose route path with links
828 xbt_dynar_reset(routing->last_route);
832 for(v = dst_node_id; v != src_node_id; v = pred_arr[v]) {
833 xbt_node_t node_pred_v = xbt_dynar_get_as(nodes, pred_arr[v], xbt_node_t);
834 xbt_node_t node_v = xbt_dynar_get_as(nodes, v, xbt_node_t);
835 xbt_edge_t edge = xbt_graph_get_edge(routing->route_graph, node_pred_v, node_v);
837 xbt_assert2(edge != NULL, "no route between host %d and %d", src_id, dst_id);
839 void * link = xbt_graph_edge_get_data(edge);
840 xbt_dynar_unshift(routing->last_route, &link);
845 if(routing->cached && elm == NULL) {
846 //add to predecessor list of the current src-host to cache
847 elm = xbt_new0(struct route_cache_element, sizeof(struct route_cache_element));
848 elm->pred_arr = pred_arr;
850 xbt_dict_set_ext(routing->route_cache, (char*)(&src_id), sizeof(int), (xbt_set_elm_t)elm, &route_cache_elem_free);
856 return routing->last_route;
860 static void routing_dijkstra_finalize(void) {
861 routing_dijkstra_t routing = (routing_dijkstra_t)used_routing;
864 xbt_graph_free_graph(routing->route_graph, &free, NULL, &free);
865 xbt_dict_free(&routing->graph_node_map);
867 xbt_dict_free(&routing->route_cache);
868 xbt_dynar_free(&routing->last_route);
869 xbt_dict_free(&used_routing->host_id);
878 static void routing_model_dijkstraboth_create(size_t size_of_link,void *loopback, int cached) {
879 /* initialize our structure */
880 routing_dijkstra_t routing = xbt_new0(s_routing_dijkstra_t,1);
881 routing->generic_routing.name = "Dijkstra";
882 routing->generic_routing.host_count = 0;
883 routing->generic_routing.get_route = routing_dijkstra_get_route;
884 routing->generic_routing.finalize = routing_dijkstra_finalize;
885 routing->size_of_link = size_of_link;
886 routing->loopback = loopback;
887 routing->cached = cached;
889 /* Set it in position */
890 used_routing = (routing_t) routing;
892 /* Setup the parsing callbacks we need */
893 routing->generic_routing.host_id = xbt_dict_new();
894 surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
895 surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_dijkstra_parse_end);
896 surfxml_add_callback(STag_surfxml_route_cb_list,
897 &routing_full_parse_Sroute_set_endpoints);
898 surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);
899 surfxml_add_callback(STag_surfxml_cluster_cb_list, &routing_shortest_path_parse_Scluster);
902 static void routing_model_dijkstra_create(size_t size_of_link,void *loopback) {
903 routing_model_dijkstraboth_create(size_of_link, loopback, 0);
906 static void routing_model_dijkstracache_create(size_t size_of_link,void *loopback) {
907 routing_model_dijkstraboth_create(size_of_link, loopback, 1);
910 /* ************************************************** */
911 /* ********** NO ROUTING **************************** */
913 static void routing_none_finalize(void) {
915 xbt_dict_free(&used_routing->host_id);
921 static void routing_model_none_create(size_t size_of_link,void *loopback) {
922 routing_t routing = xbt_new0(s_routing_t,1);
923 INFO0("Null routing");
924 routing->name = "none";
925 routing->host_count = 0;
926 routing->host_id = xbt_dict_new();
927 routing->get_route = NULL;
928 routing->finalize = routing_none_finalize;
930 /* Set it in position */
931 used_routing = (routing_t) routing;