Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Allow to instanciate the constant network model. It uses a newly created routing...
[simgrid.git] / src / surf / surf_routing.c
1 /* Copyright (c) 2009 The SimGrid team. All rights reserved.                */
2
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. */
5
6 #include <float.h>
7 #include "surf_private.h"
8 #include "xbt/dynar.h"
9 #include "xbt/str.h"
10 #include "xbt/config.h"
11 #include "xbt/graph.h"
12 #include "xbt/set.h"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route,surf,"Routing part of surf");// FIXME: connect this under windows
15
16 routing_t used_routing = NULL;
17
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);
24
25 /* Definition of each model */
26 struct model_type {
27   const char *name;
28   const char *desc;
29   void (*fun)(size_t,void*);
30 };
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 },
37     {NULL,NULL,NULL}};
38
39
40 void routing_model_create(size_t size_of_links, void* loopback) {
41
42   char * wanted=xbt_cfg_get_string(_surf_cfg_set,"routing");
43   int cpt;
44   for (cpt=0;models[cpt].name;cpt++) {
45     if (!strcmp(wanted,models[cpt].name)) {
46       (*(models[cpt].fun))(size_of_links,loopback);
47       return;
48     }
49   }
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);
54   exit(1);
55 }
56
57 /* ************************************************************************** */
58 /* *************************** FULL ROUTING ********************************* */
59 typedef struct {
60   s_routing_t generic_routing;
61   xbt_dynar_t *routing_table;
62   void *loopback;
63   size_t size_of_link;
64 } s_routing_full_t,*routing_full_t;
65
66 #define ROUTE_FULL(i,j) ((routing_full_t)used_routing)->routing_table[(i)+(j)*(used_routing)->host_count]
67
68 /*
69  * Parsing
70  */
71 static void routing_full_parse_Shost(void) {
72   int *val = xbt_malloc(sizeof(int));
73   DEBUG2("Seen host %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);
76 }
77 static int src_id = -1;
78 static int dst_id = -1;
79 static void routing_full_parse_Sroute_set_endpoints(void)
80 {
81   src_id = *(int*)xbt_dict_get(used_routing->host_id,A_surfxml_route_src);
82   dst_id = *(int*)xbt_dict_get(used_routing->host_id,A_surfxml_route_dst);
83   DEBUG4("Route %s %d -> %s %d",A_surfxml_route_src,src_id,A_surfxml_route_dst,dst_id);
84   route_action = A_surfxml_route_action;
85 }
86 static void routing_full_parse_Eroute(void)
87 {
88   char *name;
89   if (src_id != -1 && dst_id != -1) {
90     name = bprintf("%x#%x", src_id, dst_id);
91     manage_route(route_table, name, route_action, 0);
92     free(name);
93   }
94 }
95
96
97 static void routing_full_parse_end(void) {
98   routing_full_t routing = (routing_full_t) used_routing;
99   int nb_link = 0;
100   unsigned int cpt = 0;
101   xbt_dict_cursor_t cursor = NULL;
102   char *key, *data, *end;
103   const char *sep = "#";
104   xbt_dynar_t links, keys;
105   char *link_name = NULL;
106   int i,j;
107
108   int host_count = routing->generic_routing.host_count;
109
110   /* Create the routing table */
111   routing->routing_table = xbt_new0(xbt_dynar_t, host_count * host_count);
112   for (i=0;i<host_count;i++)
113     for (j=0;j<host_count;j++)
114       ROUTE_FULL(i,j) = xbt_dynar_new(routing->size_of_link,NULL);
115
116   /* Put the routes in position */
117   xbt_dict_foreach(route_table, cursor, key, data) {
118     nb_link = 0;
119     links = (xbt_dynar_t) data;
120     keys = xbt_str_split_str(key, sep);
121
122     src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
123     dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
124     xbt_dynar_free(&keys);
125
126     DEBUG4("Handle %d %d (from %d hosts): %ld links",
127         src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
128     xbt_dynar_foreach(links, cpt, link_name) {
129       void* link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
130       if (link)
131         xbt_dynar_push(ROUTE_FULL(src_id,dst_id),&link);
132       else
133         THROW1(mismatch_error,0,"Link %s not found", link_name);
134     }
135   }
136
137   /* Add the loopback if needed */
138   for (i = 0; i < host_count; i++)
139     if (!xbt_dynar_length(ROUTE_FULL(i, i)))
140       xbt_dynar_push(ROUTE_FULL(i,i),&routing->loopback);
141
142   /* Shrink the dynar routes (save unused slots) */
143   for (i=0;i<host_count;i++)
144     for (j=0;j<host_count;j++)
145       xbt_dynar_shrink(ROUTE_FULL(i,j),0);
146 }
147
148 /*
149  * Business methods
150  */
151 static xbt_dynar_t routing_full_get_route(int src,int dst) {
152   return ROUTE_FULL(src,dst);
153 }
154
155 static void routing_full_finalize(void) {
156   routing_full_t routing = (routing_full_t)used_routing;
157   int i,j;
158
159   if (routing) {
160     for (i = 0; i < used_routing->host_count; i++)
161       for (j = 0; j < used_routing->host_count; j++)
162         xbt_dynar_free(&ROUTE_FULL(i, j));
163     free(routing->routing_table);
164     xbt_dict_free(&used_routing->host_id);
165     free(routing);
166     routing=NULL;
167   }
168 }
169
170 static void routing_model_full_create(size_t size_of_link,void *loopback) {
171   /* initialize our structure */
172   routing_full_t routing = xbt_new0(s_routing_full_t,1);
173   routing->generic_routing.name = "Full";
174   routing->generic_routing.host_count = 0;
175   routing->generic_routing.get_route = routing_full_get_route;
176   routing->generic_routing.finalize = routing_full_finalize;
177   routing->size_of_link = size_of_link;
178   routing->loopback = loopback;
179
180   /* Set it in position */
181   used_routing = (routing_t) routing;
182
183   /* Setup the parsing callbacks we need */
184   routing->generic_routing.host_id = xbt_dict_new();
185   surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
186   surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_full_parse_end);
187   surfxml_add_callback(STag_surfxml_route_cb_list,
188       &routing_full_parse_Sroute_set_endpoints);
189   surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);
190 }
191
192 /* ************************************************************************** */
193 /* *************************** FLOYD ROUTING ********************************* */
194 typedef struct {
195   s_routing_t generic_routing;
196   double *cost_table;
197   int *predecessor_table;
198   void** link_table;
199   
200   void *loopback;
201   size_t size_of_link;
202 } s_routing_floyd_t,*routing_floyd_t;
203
204
205
206 #define FLOYD_COST(i,j) ((routing_floyd_t)used_routing)->cost_table[(i)+(j)*(used_routing)->host_count]
207 #define FLOYD_PRED(i,j) ((routing_floyd_t)used_routing)->predecessor_table[(i)+(j)*(used_routing)->host_count]
208 #define FLOYD_LINK(i,j) ((routing_floyd_t)used_routing)->link_table[(i)+(j)*(used_routing)->host_count]
209
210 static void routing_floyd_parse_end(void) {
211
212   routing_floyd_t routing = (routing_floyd_t) used_routing;
213   int nb_link = 0;
214   unsigned int cpt = 0;    
215   void* link_list = NULL;
216   xbt_dict_cursor_t cursor = NULL;
217   char *key,*data, *end;
218   const char *sep = "#";
219   xbt_dynar_t links, keys;
220   char *link_name = NULL;
221
222   unsigned int i,j;
223
224   int host_count = routing->generic_routing.host_count;
225
226   /* Create Cost, Predecessor and Link tables */
227   routing->cost_table = xbt_new0(double, host_count * host_count); //link cost from host to host
228   routing->predecessor_table = xbt_new0(int, host_count*host_count); //predecessor host numbers
229   routing->link_table = xbt_new0(void*,host_count*host_count); //actual link between src and dst
230
231   /* Initialize costs and predecessors*/
232   for(i = 0; i<host_count;i++)
233     for(j = 0; j<host_count;j++) {
234         FLOYD_COST(i,j) = DBL_MAX;
235         FLOYD_PRED(i,j) = -1;
236     }
237
238    /* Put the routes in position */
239   xbt_dict_foreach(route_table, cursor, key, data) {
240     nb_link = 0;
241     links = (xbt_dynar_t)data;
242     keys = xbt_str_split_str(key, sep);
243
244     
245     src_id = strtol(xbt_dynar_get_as(keys, 0, char*), &end, 16);
246     dst_id = strtol(xbt_dynar_get_as(keys, 1, char*), &end, 16);
247     xbt_dynar_free(&keys);
248  
249     DEBUG4("Handle %d %d (from %d hosts): %ld links",
250         src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
251     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);
252     
253     xbt_dynar_foreach (links, cpt, link_name) {
254       void* link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
255       if (link)
256         link_list = link;
257       else
258         THROW1(mismatch_error,0,"Link %s not found", link_name);
259     
260     }
261
262     FLOYD_LINK(src_id,dst_id) = link_list;
263     FLOYD_PRED(src_id, dst_id) = src_id;
264
265     //link cost
266     FLOYD_COST(src_id, dst_id) = 1; // assume 1 for now
267
268   }
269
270     /* Add the loopback if needed */
271   for (i = 0; i < host_count; i++)
272     if (!FLOYD_PRED(i, i)) {
273       FLOYD_PRED(i, i) = i;
274       FLOYD_COST(i, i) = 1;
275       FLOYD_LINK(i, i) = routing->loopback;
276     }
277
278
279   //Calculate path costs 
280   unsigned int a,b,c;
281   for(c=0;c<host_count;c++) {
282     for(a=0;a<host_count;a++) {
283       for(b=0;b<host_count;b++) {
284         if(FLOYD_COST(a,c) < DBL_MAX && FLOYD_COST(c,b) < DBL_MAX) {
285           if(FLOYD_COST(a,b) == DBL_MAX || (FLOYD_COST(a,c)+FLOYD_COST(c,b) < FLOYD_COST(a,b))) {
286             FLOYD_COST(a,b) = FLOYD_COST(a,c)+FLOYD_COST(c,b);
287             FLOYD_PRED(a,b) = FLOYD_PRED(c,b);
288           }
289         }
290       }
291     }
292   }
293 }
294
295 /*
296  * Business methods
297  */
298 static xbt_dynar_t routing_floyd_get_route(int src_id,int dst_id) {
299
300   routing_floyd_t routing = (routing_floyd_t) used_routing;
301
302   int pred = dst_id;
303   int prev_pred = 0;
304
305   xbt_dynar_t link_list = xbt_dynar_new(routing->size_of_link, NULL);
306
307   do {
308     prev_pred = pred;
309     pred = FLOYD_PRED(src_id, pred);
310
311     if(pred == -1) // if no pred in route -> no route to host
312         break;
313
314     xbt_dynar_unshift(link_list, &FLOYD_LINK(pred,prev_pred));
315
316   } while(pred != src_id);
317
318   xbt_assert2(pred != -1, "no route from host %d to %d", src_id, dst_id);
319
320   return link_list;
321 }
322
323 static void routing_floyd_finalize_route(xbt_dynar_t route) {
324   xbt_dynar_free(&route);
325 }
326
327 static void routing_floyd_finalize(void) {
328   routing_floyd_t routing = (routing_floyd_t)used_routing;
329
330   if (routing) {
331     free(routing->link_table);
332     free(routing->cost_table);
333     free(routing->predecessor_table);
334     xbt_dict_free(&used_routing->host_id);
335     free(routing);
336     routing=NULL;
337   }
338 }
339
340 static void routing_model_floyd_create(size_t size_of_link,void *loopback) {
341   /* initialize our structure */
342   routing_floyd_t routing = xbt_new0(s_routing_floyd_t,1);
343   routing->generic_routing.name = "Floyd";
344   routing->generic_routing.host_count = 0;
345   routing->generic_routing.host_id = xbt_dict_new();
346   routing->generic_routing.get_route = routing_floyd_get_route;
347   routing->generic_routing.finalize = routing_floyd_finalize;
348   routing->generic_routing.finalize_route = routing_floyd_finalize_route;
349   routing->size_of_link = size_of_link;
350   routing->loopback = loopback;
351
352   /* Set it in position */
353   used_routing = (routing_t) routing;
354   
355   /* Setup the parsing callbacks we need */
356   surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
357   surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_floyd_parse_end);
358   surfxml_add_callback(STag_surfxml_route_cb_list, 
359       &routing_full_parse_Sroute_set_endpoints);
360   surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);
361   
362 }
363
364 /* ************************************************************************** */
365 /* ********** Dijkstra & Dijkstra Cached ROUTING **************************** */
366 typedef struct {
367   s_routing_t generic_routing;
368   xbt_graph_t route_graph;
369   xbt_set_t graph_node_map;
370   xbt_set_t route_cache;
371   int cached;
372   void *loopback;
373   size_t size_of_link;
374 } s_routing_dijkstra_t,*routing_dijkstra_t;
375
376
377 typedef struct graph_node_data {
378   int id; 
379   int graph_id; //used for caching internal id's
380 } s_graph_node_data_t, * graph_node_data_t;
381
382 typedef struct graph_node_map_element {
383   XBT_SET_HEADERS;
384
385   xbt_node_t node;
386 } s_graph_node_map_element_t, * graph_node_map_element_t;
387
388 typedef struct route_cache_element {
389   XBT_SET_HEADERS;
390
391   int * pred_arr;
392   int size;
393 } s_route_cache_element_t, * route_cache_element_t;     
394
395 /*
396  * Free functions
397  */
398 static void route_cache_elem_free(void *e) {
399   route_cache_element_t elm=(route_cache_element_t)e;
400
401   if (elm) {
402     free(elm->name);
403     free(elm->pred_arr);
404     free(elm);
405   }
406 }
407
408 static void graph_node_map_elem_free(void *e) {
409   graph_node_map_element_t elm = (graph_node_map_element_t)e;
410
411   if(elm) {
412     free(elm->name);
413     free(elm);
414   }
415 }
416
417 /*
418  * Utility functions
419 */
420 static xbt_node_t route_graph_new_node(int id, int graph_id) {
421   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
422
423   graph_node_data_t data = xbt_new0(struct graph_node_data, sizeof(struct graph_node_data));
424   data->id = id;
425   data->graph_id = graph_id;
426   xbt_node_t node = xbt_graph_new_node(routing->route_graph, data);
427
428   graph_node_map_element_t elm = xbt_new0(struct graph_node_map_element, sizeof(struct graph_node_map_element));
429   elm->name_len = 0;
430   elm->name = bprintf("%d",id);
431   elm->node = node;
432
433   xbt_set_add(routing->graph_node_map, (xbt_set_elm_t)elm, &graph_node_map_elem_free);
434
435   return node;
436 }
437
438 static graph_node_map_element_t graph_node_map_search(int id) {
439   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
440
441   char * id_str = bprintf("%d",id);
442   graph_node_map_element_t elm = (graph_node_map_element_t)xbt_set_get_by_name_or_null(routing->graph_node_map, id_str);
443   free(id_str);
444
445   return elm;
446 }
447
448 /*
449  * Parsing
450  */
451 static void route_new_dijkstra(int src_id, int dst_id, void* link) {
452   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
453
454   xbt_node_t src = NULL;
455   xbt_node_t dst = NULL;
456   char * src_id_str = bprintf("%d",src_id);
457   char * dst_id_str = bprintf("%d",dst_id);
458   graph_node_map_element_t src_elm = (graph_node_map_element_t)xbt_set_get_by_name_or_null(routing->graph_node_map, src_id_str);
459   graph_node_map_element_t dst_elm = (graph_node_map_element_t)xbt_set_get_by_name_or_null(routing->graph_node_map, dst_id_str);
460   free(src_id_str);
461   free(dst_id_str);
462
463   if(src_elm)
464     src = src_elm->node;
465
466   if(dst_elm)
467     dst = dst_elm->node;
468
469   //add nodes if they don't exist in the graph
470   if(src_id == dst_id && src == NULL && dst == NULL) {
471     src = route_graph_new_node(src_id, -1);
472     dst = src;
473   } else {
474     if(src == NULL) {
475       src = route_graph_new_node(src_id, -1);
476     }
477         
478     if(dst == NULL) {
479       dst = route_graph_new_node(dst_id, -1);
480     }
481   }
482
483   //add link as edge to graph
484   xbt_graph_new_edge(routing->route_graph, src, dst, link);
485   
486 }
487
488 static void add_loopback_dijkstra(void) {
489   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
490
491         xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
492         
493         xbt_node_t node = NULL;
494         unsigned int cursor2;
495         xbt_dynar_foreach(nodes, cursor2, node) {
496                 xbt_dynar_t out_edges = xbt_graph_node_get_outedges(node); 
497                 xbt_edge_t edge = NULL;
498                 unsigned int cursor;
499         
500                 int found = 0;
501                 xbt_dynar_foreach(out_edges, cursor, edge) {
502                         xbt_node_t other_node = xbt_graph_edge_get_target(edge);
503                         if(other_node == node) {
504                                 found = 1;
505                                 break;
506                         }
507                 }
508
509                 if(!found)
510                         xbt_graph_new_edge(routing->route_graph, node, node, &routing->loopback);
511         }
512 }
513
514 static void routing_dijkstra_parse_end(void) {
515   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
516   int nb_link = 0;
517   unsigned int cpt = 0;
518   xbt_dict_cursor_t cursor = NULL;
519   char *key, *data, *end;
520   const char *sep = "#";
521   xbt_dynar_t links, keys;
522   char *link_name = NULL;
523
524   /* Create the topology graph */
525   routing->route_graph = xbt_graph_new_graph(1, NULL);
526   routing->graph_node_map = xbt_set_new();
527   if(routing->cached)
528     routing->route_cache = xbt_set_new();
529
530
531   /* Put the routes in position */
532   xbt_dict_foreach(route_table, cursor, key, data) {
533     nb_link = 0;
534     links = (xbt_dynar_t) data;
535     keys = xbt_str_split_str(key, sep);
536
537     src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
538     dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
539     xbt_dynar_free(&keys);
540
541     DEBUG4("Handle %d %d (from %d hosts): %ld links",
542         src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
543
544     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);
545
546     xbt_dynar_foreach(links, cpt, link_name) {
547       void* link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
548       if (link)
549         route_new_dijkstra(src_id,dst_id,link);
550       else
551         THROW1(mismatch_error,0,"Link %s not found", link_name);
552     }
553     
554   }
555
556   /* Add the loopback if needed */
557   add_loopback_dijkstra();
558
559   /* initialize graph indexes in nodes after graph has been built */
560   xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
561
562   xbt_node_t node = NULL;
563   unsigned int cursor2;
564   xbt_dynar_foreach(nodes, cursor2, node) {
565     graph_node_data_t data = xbt_graph_node_get_data(node);
566     data->graph_id = cursor2;
567   }
568
569 }
570
571 /*
572  * Business methods
573  */
574 static xbt_dynar_t routing_dijkstra_get_route(int src_id,int dst_id) {
575
576   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
577   int * pred_arr = NULL;
578   
579   xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
580
581   /*Use the graph_node id mapping set to quickly find the nodes */
582   graph_node_map_element_t src_elm = graph_node_map_search(src_id);
583   graph_node_map_element_t dst_elm = graph_node_map_search(dst_id);
584   xbt_assert2(src_elm != NULL && dst_elm != NULL, "src %d or dst %d does not exist", src_id, dst_id);
585   int src_node_id = ((graph_node_data_t)xbt_graph_node_get_data(src_elm->node))->graph_id;
586   int dst_node_id = ((graph_node_data_t)xbt_graph_node_get_data(dst_elm->node))->graph_id;
587
588   route_cache_element_t elm = NULL;
589   if(routing->cached) {
590     /*check if there is a cached predecessor list avail */
591     char * src_id_str = bprintf("%d",src_id);
592     elm = (route_cache_element_t)xbt_set_get_by_name_or_null(routing->route_cache, src_id_str);
593     free(src_id_str);
594   }
595
596   if(elm) { //cached mode and cache hit
597     pred_arr = elm->pred_arr;
598   } else { //not cached mode or cache miss
599     double * cost_arr = NULL;
600     xbt_heap_t pqueue = NULL;
601     int i = 0;
602
603     int nr_nodes = xbt_dynar_length(nodes);
604     cost_arr = xbt_new0(double, nr_nodes); //link cost from src to other hosts
605     pred_arr = xbt_new0(int, nr_nodes); //predecessors in path from src
606     pqueue = xbt_heap_new(nr_nodes, free);
607
608     //initialize
609     cost_arr[src_node_id] = 0.0;
610
611     for(i = 0; i < nr_nodes; i++) {
612       if(i != src_node_id) {
613         cost_arr[i] = DBL_MAX;
614       }
615
616       pred_arr[i] = 0;
617
618       //initialize priority queue
619       int * nodeid = xbt_new0(int, 1);
620       *nodeid = i;
621       xbt_heap_push(pqueue, nodeid, cost_arr[i]);
622
623     }
624
625     // apply dijkstra using the indexes from the graph's node array
626     while(xbt_heap_size(pqueue) > 0) {
627       int * v_id = xbt_heap_pop(pqueue);
628       xbt_node_t v_node = xbt_dynar_get_as(nodes, *v_id, xbt_node_t);
629       xbt_dynar_t out_edges = xbt_graph_node_get_outedges(v_node); 
630       xbt_edge_t edge = NULL;
631       unsigned int cursor;
632
633       xbt_dynar_foreach(out_edges, cursor, edge) {
634         xbt_node_t u_node = xbt_graph_edge_get_target(edge);
635         graph_node_data_t data = xbt_graph_node_get_data(u_node);
636         int u_id = data->graph_id;
637         int cost_v_u = 1; //fixed link cost for now
638
639         if(cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
640           pred_arr[u_id] = *v_id;
641           cost_arr[u_id] = cost_v_u + cost_arr[*v_id];
642           int * nodeid = xbt_new0(int, 1);
643           *nodeid = u_id;
644           xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
645         }
646       }
647
648       //free item popped from pqueue
649       free(v_id);
650     }
651
652     free(cost_arr);
653     xbt_heap_free(pqueue);
654
655   }
656
657   //compose route path with links
658   xbt_dynar_t link_list = xbt_dynar_new(routing->size_of_link, NULL);
659
660   int v;
661   int size = 0;
662   for(v = dst_node_id; v != src_node_id; v = pred_arr[v]) {
663     xbt_node_t node_pred_v = xbt_dynar_get_as(nodes, pred_arr[v], xbt_node_t);
664     xbt_node_t node_v = xbt_dynar_get_as(nodes, v, xbt_node_t);
665     xbt_edge_t edge = xbt_graph_get_edge(routing->route_graph, node_pred_v, node_v);
666
667     xbt_assert2(edge != NULL, "no route between host %d and %d", src_id, dst_id);
668
669     void * link = xbt_graph_edge_get_data(edge);
670     xbt_dynar_unshift(link_list, &link);
671     size++;
672   }
673
674
675   if(routing->cached && elm == NULL) {
676     //add to predecessor list of the current src-host to cache
677     elm = xbt_new0(struct route_cache_element, sizeof(struct route_cache_element));
678     elm->name = bprintf("%d",src_id);
679     elm->name_len = 0;
680     elm->pred_arr = pred_arr;
681     elm->size = size;
682     xbt_set_add(routing->route_cache, (xbt_set_elm_t)elm, &route_cache_elem_free);
683   }
684
685   if(!routing->cached)
686     free(pred_arr);
687
688   return link_list;
689 }
690
691 static void routing_dijkstra_finalize_route(xbt_dynar_t route) {
692   xbt_dynar_free(&route);
693 }
694
695 static void routing_dijkstra_finalize(void) {
696   routing_dijkstra_t routing = (routing_dijkstra_t)used_routing;
697
698   if (routing) {
699     xbt_graph_free_graph(routing->route_graph, &free, NULL, &free);
700     xbt_set_free(&routing->graph_node_map);
701     if(routing->cached)
702       xbt_set_free(&routing->route_cache);
703     xbt_dict_free(&used_routing->host_id);
704     free(routing);
705     routing=NULL;
706   }
707 }
708
709 /*
710  *
711  */
712 static void routing_model_dijkstraboth_create(size_t size_of_link,void *loopback, int cached) {
713   /* initialize our structure */
714   routing_dijkstra_t routing = xbt_new0(s_routing_dijkstra_t,1);
715   routing->generic_routing.name = "Dijkstra";
716   routing->generic_routing.host_count = 0;
717   routing->generic_routing.get_route = routing_dijkstra_get_route;
718   routing->generic_routing.finalize = routing_dijkstra_finalize;
719   routing->generic_routing.finalize_route = routing_dijkstra_finalize_route;
720   routing->size_of_link = size_of_link;
721   routing->loopback = loopback;
722   routing->cached = cached;
723
724   /* Set it in position */
725   used_routing = (routing_t) routing;
726
727   /* Setup the parsing callbacks we need */
728   routing->generic_routing.host_id = xbt_dict_new();
729   surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
730   surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_dijkstra_parse_end);
731   surfxml_add_callback(STag_surfxml_route_cb_list,
732       &routing_full_parse_Sroute_set_endpoints);
733   surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);
734 }
735
736 static void routing_model_dijkstra_create(size_t size_of_link,void *loopback) {
737   routing_model_dijkstraboth_create(size_of_link, loopback, 0);
738 }
739
740 static void routing_model_dijkstracache_create(size_t size_of_link,void *loopback) {
741   routing_model_dijkstraboth_create(size_of_link, loopback, 1);
742 }
743
744 /* ************************************************** */
745 /* ********** NO ROUTING **************************** */
746
747 static void routing_none_finalize(void) {
748   if (used_routing) {
749     xbt_dict_free(&used_routing->host_id);
750     free(used_routing);
751     used_routing=NULL;
752   }
753 }
754
755 static void routing_model_none_create(size_t size_of_link,void *loopback) {
756   routing_t routing = xbt_new0(s_routing_t,1);
757   INFO0("Null routing");
758   routing->name = "none";
759   routing->host_count = 0;
760   routing->host_id = xbt_dict_new();
761   routing->get_route = NULL;
762   routing->finalize = routing_none_finalize;
763
764   /* Set it in position */
765   used_routing = (routing_t) routing;
766 }