Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fixed full routing support for routers, no routing complexity is added routers are...
[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");
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_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);
76 }
77
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);
83 }
84 static int src_id = -1;
85 static int dst_id = -1;
86 static void routing_full_parse_Sroute_set_endpoints(void)
87 {
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;
92 }
93 static void routing_full_parse_Eroute(void)
94 {
95   char *name;
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);
99     free(name);
100   }
101 }
102
103 /* Cluster tag functions */
104
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)
110 {
111   int AX_ptr = 0;
112
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);
118 }
119
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)
126 {
127   int AX_ptr = 0;
128
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);
135 }
136
137 static void routing_full_parse_Scluster(void)
138 {
139   static int AX_ptr = 0;
140
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;
150   char *backbone_name;
151
152   surfxml_bufferstack_push(1);
153
154   /* Make set */
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);
159
160   SURFXML_START_TAG(set);
161   SURFXML_END_TAG(set);
162
163   /* Make foreach */
164   SURFXML_BUFFER_SET(foreach_set_id, cluster_id);
165
166   SURFXML_START_TAG(foreach);
167
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;
171
172   SURFXML_START_TAG(host);
173   SURFXML_END_TAG(host);
174
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;
179
180   SURFXML_START_TAG(link);
181   SURFXML_END_TAG(link);
182
183   SURFXML_END_TAG(foreach);
184
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, "",
188                          "");
189   A_surfxml_link_state = A_surfxml_link_state_ON;
190   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
191
192   SURFXML_START_TAG(link);
193   SURFXML_END_TAG(link);
194
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;
200
201   SURFXML_START_TAG(route_c_multi);
202
203   SURFXML_BUFFER_SET(link_c_ctn_id, "$src");
204
205   SURFXML_START_TAG(link_c_ctn);
206   SURFXML_END_TAG(link_c_ctn);
207
208   SURFXML_END_TAG(route_c_multi);
209
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;
215
216   SURFXML_START_TAG(route_c_multi);
217
218   SURFXML_BUFFER_SET(link_c_ctn_id, backbone_name);
219
220   SURFXML_START_TAG(link_c_ctn);
221   SURFXML_END_TAG(link_c_ctn);
222
223   SURFXML_END_TAG(route_c_multi);
224
225   free(backbone_name);
226
227   /* Restore buff */
228   surfxml_bufferstack_pop(1);
229 }
230
231
232 static void routing_full_parse_end(void) {
233   routing_full_t routing = (routing_full_t) used_routing;
234   int nb_link = 0;
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;
241   int i,j;
242
243   int host_count = routing->generic_routing.host_count;
244
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);
250
251   /* Put the routes in position */
252   xbt_dict_foreach(route_table, cursor, key, data) {
253     nb_link = 0;
254     links = (xbt_dynar_t) data;
255     keys = xbt_str_split_str(key, sep);
256
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);
260
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);
265       if (link)
266         xbt_dynar_push(ROUTE_FULL(src_id,dst_id),&link);
267       else
268         THROW1(mismatch_error,0,"Link %s not found", link_name);
269     }
270   }
271
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);
276
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);
281 }
282
283 /*
284  * Business methods
285  */
286 static xbt_dynar_t routing_full_get_route(int src,int dst) {
287   return ROUTE_FULL(src,dst);
288 }
289
290 static void routing_full_finalize(void) {
291   routing_full_t routing = (routing_full_t)used_routing;
292   int i,j;
293
294   if (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);
300     free(routing);
301     routing=NULL;
302   }
303 }
304
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;
314
315   /* Set it in position */
316   used_routing = (routing_t) routing;
317
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);
327 }
328
329 /* ************************************************************************** */
330
331 static void routing_shortest_path_parse_Scluster(void)
332 {
333   static int AX_ptr = 0;
334
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;
342   char *backbone_name;
343
344   surfxml_bufferstack_push(1);
345
346   /* Make set */
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);
351
352   SURFXML_START_TAG(set);
353   SURFXML_END_TAG(set);
354
355   /* Make foreach */
356   SURFXML_BUFFER_SET(foreach_set_id, cluster_id);
357
358   SURFXML_START_TAG(foreach);
359
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;
363
364   SURFXML_START_TAG(host);
365   SURFXML_END_TAG(host);
366
367   SURFXML_END_TAG(foreach);
368
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, "",
372                          "");
373   A_surfxml_link_state = A_surfxml_link_state_ON;
374   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
375
376   SURFXML_START_TAG(link);
377   SURFXML_END_TAG(link);
378
379   free(backbone_name);
380
381   /* Restore buff */
382   surfxml_bufferstack_pop(1);
383 }
384
385 /* ************************************************************************** */
386 /* *************************** FLOYD ROUTING ********************************* */
387 typedef struct {
388   s_routing_t generic_routing;
389   int *predecessor_table;
390   void** link_table;
391   xbt_dynar_t last_route;
392   void *loopback;
393   size_t size_of_link;
394 } s_routing_floyd_t,*routing_floyd_t;
395
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]
399
400 static void routing_floyd_parse_end(void) {
401
402   routing_floyd_t routing = (routing_floyd_t) used_routing;
403   int nb_link = 0;
404   void* link_list = NULL;
405   double * cost_table;
406   xbt_dict_cursor_t cursor = NULL;
407   char *key,*data, *end;
408   const char *sep = "#";
409   xbt_dynar_t links, keys;
410
411   unsigned int i,j;
412
413   int host_count = routing->generic_routing.host_count;
414
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);
420
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;
426     }
427
428    /* Put the routes in position */
429   xbt_dict_foreach(route_table, cursor, key, data) {
430     nb_link = 0;
431     links = (xbt_dynar_t)data;
432     keys = xbt_str_split_str(key, sep);
433
434     
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);
438  
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);
442     
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);
445     if (link)
446       link_list = link;
447     else
448       THROW1(mismatch_error,0,"Link %s not found", link_name);
449     
450
451     FLOYD_LINK(src_id,dst_id) = link_list;
452     FLOYD_PRED(src_id, dst_id) = src_id;
453
454     //link cost
455     FLOYD_COST(src_id, dst_id) = 1; // assume 1 for now
456
457   }
458
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;
465     }
466
467
468   //Calculate path costs 
469   unsigned int a,b,c;
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);
477           }
478         }
479       }
480     }
481   }
482
483   //cleanup
484   free(cost_table);
485 }
486
487 /*
488  * Business methods
489  */
490 static xbt_dynar_t routing_floyd_get_route(int src_id,int dst_id) {
491
492   routing_floyd_t routing = (routing_floyd_t) used_routing;
493
494   int pred = dst_id;
495   int prev_pred = 0;
496
497   xbt_dynar_reset(routing->last_route);
498
499   do {
500     prev_pred = pred;
501     pred = FLOYD_PRED(src_id, pred);
502
503     if(pred == -1) // if no pred in route -> no route to host
504         break;
505
506     xbt_dynar_unshift(routing->last_route, &FLOYD_LINK(pred,prev_pred));
507
508   } while(pred != src_id);
509
510   xbt_assert2(pred != -1, "no route from host %d to %d", src_id, dst_id);
511
512   return routing->last_route;
513 }
514
515 static void routing_floyd_finalize(void) {
516   routing_floyd_t routing = (routing_floyd_t)used_routing;
517
518   if (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);
523     free(routing);
524     routing=NULL;
525   }
526 }
527
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;
538
539   /* Set it in position */
540   used_routing = (routing_t) routing;
541   
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);
549   
550 }
551
552 /* ************************************************************************** */
553 /* ********** Dijkstra & Dijkstra Cached ROUTING **************************** */
554 typedef struct {
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;
560   int cached;
561   void *loopback;
562   size_t size_of_link;
563 } s_routing_dijkstra_t,*routing_dijkstra_t;
564
565
566 typedef struct graph_node_data {
567   int id; 
568   int graph_id; //used for caching internal graph id's
569 } s_graph_node_data_t, * graph_node_data_t;
570
571 typedef struct graph_node_map_element {
572   xbt_node_t node;
573 } s_graph_node_map_element_t, * graph_node_map_element_t;
574
575 typedef struct route_cache_element {
576   int * pred_arr;
577   int size;
578 } s_route_cache_element_t, * route_cache_element_t;     
579
580 /*
581  * Free functions
582  */
583 static void route_cache_elem_free(void *e) {
584   route_cache_element_t elm=(route_cache_element_t)e;
585
586   if (elm) {
587     free(elm->pred_arr);
588     free(elm);
589   }
590 }
591
592 static void graph_node_map_elem_free(void *e) {
593   graph_node_map_element_t elm = (graph_node_map_element_t)e;
594
595   if(elm) {
596     free(elm);
597   }
598 }
599
600 /*
601  * Utility functions
602 */
603 static xbt_node_t route_graph_new_node(int id, int graph_id) {
604   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
605
606   graph_node_data_t data = xbt_new0(struct graph_node_data, sizeof(struct graph_node_data));
607   data->id = id;
608   data->graph_id = graph_id;
609   xbt_node_t node = xbt_graph_new_node(routing->route_graph, data);
610
611   graph_node_map_element_t elm = xbt_new0(struct graph_node_map_element, sizeof(struct graph_node_map_element));
612   elm->node = node;
613   xbt_dict_set_ext(routing->graph_node_map, (char*)(&id), sizeof(int), (xbt_set_elm_t)elm, &graph_node_map_elem_free);
614
615   return node;
616 }
617
618 static graph_node_map_element_t graph_node_map_search(int id) {
619   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
620
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));
622
623   return elm;
624 }
625
626 /*
627  * Parsing
628  */
629 static void route_new_dijkstra(int src_id, int dst_id, void* link) {
630   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
631
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));
636
637   if(src_elm)
638     src = src_elm->node;
639
640   if(dst_elm)
641     dst = dst_elm->node;
642
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);
646     dst = src;
647   } else {
648     if(src == NULL) {
649       src = route_graph_new_node(src_id, -1);
650     }
651         
652     if(dst == NULL) {
653       dst = route_graph_new_node(dst_id, -1);
654     }
655   }
656
657   //add link as edge to graph
658   xbt_graph_new_edge(routing->route_graph, src, dst, link);
659   
660 }
661
662 static void add_loopback_dijkstra(void) {
663   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
664
665         xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
666         
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;
672                 unsigned int cursor;
673         
674                 int found = 0;
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) {
678                                 found = 1;
679                                 break;
680                         }
681                 }
682
683                 if(!found)
684                         xbt_graph_new_edge(routing->route_graph, node, node, &routing->loopback);
685         }
686 }
687
688 static void routing_dijkstra_parse_end(void) {
689   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
690   int nb_link = 0;
691   xbt_dict_cursor_t cursor = NULL;
692   char *key, *data, *end;
693   const char *sep = "#";
694   xbt_dynar_t links, keys;
695
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);
700   if(routing->cached)
701     routing->route_cache = xbt_dict_new();
702
703
704   /* Put the routes in position */
705   xbt_dict_foreach(route_table, cursor, key, data) {
706     nb_link = 0;
707     links = (xbt_dynar_t) data;
708     keys = xbt_str_split_str(key, sep);
709
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);
713
714     DEBUG4("Handle %d %d (from %d hosts): %ld links",
715         src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
716
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);
718
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);
721     if (link)
722       route_new_dijkstra(src_id,dst_id,link);
723     else
724       THROW1(mismatch_error,0,"Link %s not found", link_name);
725     
726   }
727
728   /* Add the loopback if needed */
729   add_loopback_dijkstra();
730
731   /* initialize graph indexes in nodes after graph has been built */
732   xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
733
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;
739   }
740
741 }
742
743 /*
744  * Business methods
745  */
746 static xbt_dynar_t routing_dijkstra_get_route(int src_id,int dst_id) {
747
748   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
749   int * pred_arr = NULL;
750   
751   xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
752
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;
759
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));
764   }
765
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;
771     int i = 0;
772
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);
777
778     //initialize
779     cost_arr[src_node_id] = 0.0;
780
781     for(i = 0; i < nr_nodes; i++) {
782       if(i != src_node_id) {
783         cost_arr[i] = DBL_MAX;
784       }
785
786       pred_arr[i] = 0;
787
788       //initialize priority queue
789       int * nodeid = xbt_new0(int, 1);
790       *nodeid = i;
791       xbt_heap_push(pqueue, nodeid, cost_arr[i]);
792
793     }
794
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;
801       unsigned int cursor;
802
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
808
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);
813           *nodeid = u_id;
814           xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
815         }
816       }
817
818       //free item popped from pqueue
819       free(v_id);
820     }
821
822     free(cost_arr);
823     xbt_heap_free(pqueue);
824
825   }
826
827   //compose route path with links
828   xbt_dynar_reset(routing->last_route);
829
830   int v;
831   int size = 0;
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);
836
837     xbt_assert2(edge != NULL, "no route between host %d and %d", src_id, dst_id);
838
839     void * link = xbt_graph_edge_get_data(edge);
840     xbt_dynar_unshift(routing->last_route, &link);
841     size++;
842   }
843
844
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;
849     elm->size = size;
850     xbt_dict_set_ext(routing->route_cache, (char*)(&src_id), sizeof(int), (xbt_set_elm_t)elm, &route_cache_elem_free);
851   }
852
853   if(!routing->cached)
854     free(pred_arr);
855
856   return routing->last_route;
857 }
858
859
860 static void routing_dijkstra_finalize(void) {
861   routing_dijkstra_t routing = (routing_dijkstra_t)used_routing;
862
863   if (routing) {
864     xbt_graph_free_graph(routing->route_graph, &free, NULL, &free);
865     xbt_dict_free(&routing->graph_node_map);
866     if(routing->cached)
867       xbt_dict_free(&routing->route_cache);
868     xbt_dynar_free(&routing->last_route);
869     xbt_dict_free(&used_routing->host_id);
870     free(routing);
871     routing=NULL;
872   }
873 }
874
875 /*
876  *
877  */
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;
888
889   /* Set it in position */
890   used_routing = (routing_t) routing;
891
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);
900 }
901
902 static void routing_model_dijkstra_create(size_t size_of_link,void *loopback) {
903   routing_model_dijkstraboth_create(size_of_link, loopback, 0);
904 }
905
906 static void routing_model_dijkstracache_create(size_t size_of_link,void *loopback) {
907   routing_model_dijkstraboth_create(size_of_link, loopback, 1);
908 }
909
910 /* ************************************************** */
911 /* ********** NO ROUTING **************************** */
912
913 static void routing_none_finalize(void) {
914   if (used_routing) {
915     xbt_dict_free(&used_routing->host_id);
916     free(used_routing);
917     used_routing=NULL;
918   }
919 }
920
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;
929
930   /* Set it in position */
931   used_routing = (routing_t) routing;
932 }