Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
00356453a3a0f0c06bb86d5930700b9b2faa11b6
[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 #define HOST2ROUTER(id) ((id)+(2<<29))
68 #define ROUTER2HOST(id) ((id)-(2>>20))
69 #define ROUTER(id) ((id)>=(2<<29))
70
71 /*
72  * Parsing
73  */
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);
79 }
80
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);
87 }
88
89 static int src_id = -1;
90 static int dst_id = -1;
91 static void routing_full_parse_Sroute_set_endpoints(void)
92 {
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;
97 }
98 static void routing_full_parse_Eroute(void)
99 {
100   char *name;
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);
104     free(name);
105   }
106 }
107
108 /* Cluster tag functions */
109
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)
115 {
116   int AX_ptr = 0;
117
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);
123 }
124
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)
131 {
132   int AX_ptr = 0;
133
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);
140 }
141
142 static void routing_full_parse_Scluster(void)
143 {
144   static int AX_ptr = 0;
145
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;
155   char *backbone_name;
156
157   surfxml_bufferstack_push(1);
158
159   /* Make set */
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);
164
165   SURFXML_START_TAG(set);
166   SURFXML_END_TAG(set);
167
168   /* Make foreach */
169   SURFXML_BUFFER_SET(foreach_set_id, cluster_id);
170
171   SURFXML_START_TAG(foreach);
172
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;
176
177   SURFXML_START_TAG(host);
178   SURFXML_END_TAG(host);
179
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;
184
185   SURFXML_START_TAG(link);
186   SURFXML_END_TAG(link);
187
188   SURFXML_END_TAG(foreach);
189
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, "",
193                          "");
194   A_surfxml_link_state = A_surfxml_link_state_ON;
195   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
196
197   SURFXML_START_TAG(link);
198   SURFXML_END_TAG(link);
199
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;
205
206   SURFXML_START_TAG(route_c_multi);
207
208   SURFXML_BUFFER_SET(link_c_ctn_id, "$src");
209
210   SURFXML_START_TAG(link_c_ctn);
211   SURFXML_END_TAG(link_c_ctn);
212
213   SURFXML_END_TAG(route_c_multi);
214
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;
220
221   SURFXML_START_TAG(route_c_multi);
222
223   SURFXML_BUFFER_SET(link_c_ctn_id, backbone_name);
224
225   SURFXML_START_TAG(link_c_ctn);
226   SURFXML_END_TAG(link_c_ctn);
227
228   SURFXML_END_TAG(route_c_multi);
229
230   free(backbone_name);
231
232   /* Restore buff */
233   surfxml_bufferstack_pop(1);
234 }
235
236
237 static void routing_full_parse_end(void) {
238   routing_full_t routing = (routing_full_t) used_routing;
239   int nb_link = 0;
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;
246   int i,j;
247
248   int host_count = routing->generic_routing.host_count;
249
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);
255
256   /* Put the routes in position */
257   xbt_dict_foreach(route_table, cursor, key, data) {
258     nb_link = 0;
259     links = (xbt_dynar_t) data;
260     keys = xbt_str_split_str(key, sep);
261
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);
265
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 */
269                                 continue;
270     }
271
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);
276       if (link)
277         xbt_dynar_push(ROUTE_FULL(src_id,dst_id),&link);
278       else
279         THROW1(mismatch_error,0,"Link %s not found", link_name);
280     }
281   }
282
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);
287
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);
292 }
293
294 /*
295  * Business methods
296  */
297 static xbt_dynar_t routing_full_get_route(int src,int dst) {
298   return ROUTE_FULL(src,dst);
299 }
300
301 static void routing_full_finalize(void) {
302   routing_full_t routing = (routing_full_t)used_routing;
303   int i,j;
304
305   if (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);
311     free(routing);
312     routing=NULL;
313   }
314 }
315
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;
325
326   /* Set it in position */
327   used_routing = (routing_t) routing;
328
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);
338 }
339
340 /* ************************************************************************** */
341
342 static void routing_shortest_path_parse_Scluster(void)
343 {
344   static int AX_ptr = 0;
345
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;
353   char *backbone_name;
354
355   surfxml_bufferstack_push(1);
356
357   /* Make set */
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);
362
363   SURFXML_START_TAG(set);
364   SURFXML_END_TAG(set);
365
366   /* Make foreach */
367   SURFXML_BUFFER_SET(foreach_set_id, cluster_id);
368
369   SURFXML_START_TAG(foreach);
370
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;
374
375   SURFXML_START_TAG(host);
376   SURFXML_END_TAG(host);
377
378   SURFXML_END_TAG(foreach);
379
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, "",
383                          "");
384   A_surfxml_link_state = A_surfxml_link_state_ON;
385   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
386
387   SURFXML_START_TAG(link);
388   SURFXML_END_TAG(link);
389
390   free(backbone_name);
391
392   /* Restore buff */
393   surfxml_bufferstack_pop(1);
394 }
395
396 /* ************************************************************************** */
397 /* *************************** FLOYD ROUTING ********************************* */
398 typedef struct {
399   s_routing_t generic_routing;
400   int *predecessor_table;
401   void** link_table;
402   xbt_dynar_t last_route;
403   void *loopback;
404   size_t size_of_link;
405 } s_routing_floyd_t,*routing_floyd_t;
406
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]
410
411 static void routing_floyd_parse_end(void) {
412
413   routing_floyd_t routing = (routing_floyd_t) used_routing;
414   int nb_link = 0;
415   void* link_list = NULL;
416   double * cost_table;
417   xbt_dict_cursor_t cursor = NULL;
418   char *key,*data, *end;
419   const char *sep = "#";
420   xbt_dynar_t links, keys;
421
422   unsigned int i,j;
423
424   int host_count = routing->generic_routing.host_count;
425
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);
431
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;
437     }
438
439    /* Put the routes in position */
440   xbt_dict_foreach(route_table, cursor, key, data) {
441     nb_link = 0;
442     links = (xbt_dynar_t)data;
443     keys = xbt_str_split_str(key, sep);
444
445     
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);
449  
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);
453     
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);
456     if (link)
457       link_list = link;
458     else
459       THROW1(mismatch_error,0,"Link %s not found", link_name);
460     
461
462     FLOYD_LINK(src_id,dst_id) = link_list;
463     FLOYD_PRED(src_id, dst_id) = src_id;
464
465     //link cost
466     FLOYD_COST(src_id, dst_id) = 1; // assume 1 for now
467
468   }
469
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;
476     }
477
478
479   //Calculate path costs 
480   unsigned int a,b,c;
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);
488           }
489         }
490       }
491     }
492   }
493
494   //cleanup
495   free(cost_table);
496 }
497
498 /*
499  * Business methods
500  */
501 static xbt_dynar_t routing_floyd_get_route(int src_id,int dst_id) {
502
503   routing_floyd_t routing = (routing_floyd_t) used_routing;
504
505   int pred = dst_id;
506   int prev_pred = 0;
507
508   xbt_dynar_reset(routing->last_route);
509
510   do {
511     prev_pred = pred;
512     pred = FLOYD_PRED(src_id, pred);
513
514     if(pred == -1) // if no pred in route -> no route to host
515         break;
516
517     xbt_dynar_unshift(routing->last_route, &FLOYD_LINK(pred,prev_pred));
518
519   } while(pred != src_id);
520
521   xbt_assert2(pred != -1, "no route from host %d to %d", src_id, dst_id);
522
523   return routing->last_route;
524 }
525
526 static void routing_floyd_finalize(void) {
527   routing_floyd_t routing = (routing_floyd_t)used_routing;
528
529   if (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);
534     free(routing);
535     routing=NULL;
536   }
537 }
538
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;
549
550   /* Set it in position */
551   used_routing = (routing_t) routing;
552   
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);
560   
561 }
562
563 /* ************************************************************************** */
564 /* ********** Dijkstra & Dijkstra Cached ROUTING **************************** */
565 typedef struct {
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;
571   int cached;
572   void *loopback;
573   size_t size_of_link;
574 } s_routing_dijkstra_t,*routing_dijkstra_t;
575
576
577 typedef struct graph_node_data {
578   int id; 
579   int graph_id; //used for caching internal graph id's
580 } s_graph_node_data_t, * graph_node_data_t;
581
582 typedef struct graph_node_map_element {
583   xbt_node_t node;
584 } s_graph_node_map_element_t, * graph_node_map_element_t;
585
586 typedef struct route_cache_element {
587   int * pred_arr;
588   int size;
589 } s_route_cache_element_t, * route_cache_element_t;     
590
591 /*
592  * Free functions
593  */
594 static void route_cache_elem_free(void *e) {
595   route_cache_element_t elm=(route_cache_element_t)e;
596
597   if (elm) {
598     free(elm->pred_arr);
599     free(elm);
600   }
601 }
602
603 static void graph_node_map_elem_free(void *e) {
604   graph_node_map_element_t elm = (graph_node_map_element_t)e;
605
606   if(elm) {
607     free(elm);
608   }
609 }
610
611 /*
612  * Utility functions
613 */
614 static xbt_node_t route_graph_new_node(int id, int graph_id) {
615   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
616
617   graph_node_data_t data = xbt_new0(struct graph_node_data, sizeof(struct graph_node_data));
618   data->id = id;
619   data->graph_id = graph_id;
620   xbt_node_t node = xbt_graph_new_node(routing->route_graph, data);
621
622   graph_node_map_element_t elm = xbt_new0(struct graph_node_map_element, sizeof(struct graph_node_map_element));
623   elm->node = node;
624   xbt_dict_set_ext(routing->graph_node_map, (char*)(&id), sizeof(int), (xbt_set_elm_t)elm, &graph_node_map_elem_free);
625
626   return node;
627 }
628
629 static graph_node_map_element_t graph_node_map_search(int id) {
630   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
631
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));
633
634   return elm;
635 }
636
637 /*
638  * Parsing
639  */
640 static void route_new_dijkstra(int src_id, int dst_id, void* link) {
641   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
642
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));
647
648   if(src_elm)
649     src = src_elm->node;
650
651   if(dst_elm)
652     dst = dst_elm->node;
653
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);
657     dst = src;
658   } else {
659     if(src == NULL) {
660       src = route_graph_new_node(src_id, -1);
661     }
662         
663     if(dst == NULL) {
664       dst = route_graph_new_node(dst_id, -1);
665     }
666   }
667
668   //add link as edge to graph
669   xbt_graph_new_edge(routing->route_graph, src, dst, link);
670   
671 }
672
673 static void add_loopback_dijkstra(void) {
674   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
675
676         xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
677         
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;
683                 unsigned int cursor;
684         
685                 int found = 0;
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) {
689                                 found = 1;
690                                 break;
691                         }
692                 }
693
694                 if(!found)
695                         xbt_graph_new_edge(routing->route_graph, node, node, &routing->loopback);
696         }
697 }
698
699 static void routing_dijkstra_parse_end(void) {
700   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
701   int nb_link = 0;
702   xbt_dict_cursor_t cursor = NULL;
703   char *key, *data, *end;
704   const char *sep = "#";
705   xbt_dynar_t links, keys;
706
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);
711   if(routing->cached)
712     routing->route_cache = xbt_dict_new();
713
714
715   /* Put the routes in position */
716   xbt_dict_foreach(route_table, cursor, key, data) {
717     nb_link = 0;
718     links = (xbt_dynar_t) data;
719     keys = xbt_str_split_str(key, sep);
720
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);
724
725     DEBUG4("Handle %d %d (from %d hosts): %ld links",
726         src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
727
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);
729
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);
732     if (link)
733       route_new_dijkstra(src_id,dst_id,link);
734     else
735       THROW1(mismatch_error,0,"Link %s not found", link_name);
736     
737   }
738
739   /* Add the loopback if needed */
740   add_loopback_dijkstra();
741
742   /* initialize graph indexes in nodes after graph has been built */
743   xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
744
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;
750   }
751
752 }
753
754 /*
755  * Business methods
756  */
757 static xbt_dynar_t routing_dijkstra_get_route(int src_id,int dst_id) {
758
759   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
760   int * pred_arr = NULL;
761   
762   xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
763
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;
770
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));
775   }
776
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;
782     int i = 0;
783
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);
788
789     //initialize
790     cost_arr[src_node_id] = 0.0;
791
792     for(i = 0; i < nr_nodes; i++) {
793       if(i != src_node_id) {
794         cost_arr[i] = DBL_MAX;
795       }
796
797       pred_arr[i] = 0;
798
799       //initialize priority queue
800       int * nodeid = xbt_new0(int, 1);
801       *nodeid = i;
802       xbt_heap_push(pqueue, nodeid, cost_arr[i]);
803
804     }
805
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;
812       unsigned int cursor;
813
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
819
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);
824           *nodeid = u_id;
825           xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
826         }
827       }
828
829       //free item popped from pqueue
830       free(v_id);
831     }
832
833     free(cost_arr);
834     xbt_heap_free(pqueue);
835
836   }
837
838   //compose route path with links
839   xbt_dynar_reset(routing->last_route);
840
841   int v;
842   int size = 0;
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);
847
848     xbt_assert2(edge != NULL, "no route between host %d and %d", src_id, dst_id);
849
850     void * link = xbt_graph_edge_get_data(edge);
851     xbt_dynar_unshift(routing->last_route, &link);
852     size++;
853   }
854
855
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;
860     elm->size = size;
861     xbt_dict_set_ext(routing->route_cache, (char*)(&src_id), sizeof(int), (xbt_set_elm_t)elm, &route_cache_elem_free);
862   }
863
864   if(!routing->cached)
865     free(pred_arr);
866
867   return routing->last_route;
868 }
869
870
871 static void routing_dijkstra_finalize(void) {
872   routing_dijkstra_t routing = (routing_dijkstra_t)used_routing;
873
874   if (routing) {
875     xbt_graph_free_graph(routing->route_graph, &free, NULL, &free);
876     xbt_dict_free(&routing->graph_node_map);
877     if(routing->cached)
878       xbt_dict_free(&routing->route_cache);
879     xbt_dynar_free(&routing->last_route);
880     xbt_dict_free(&used_routing->host_id);
881     free(routing);
882     routing=NULL;
883   }
884 }
885
886 /*
887  *
888  */
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;
899
900   /* Set it in position */
901   used_routing = (routing_t) routing;
902
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);
911 }
912
913 static void routing_model_dijkstra_create(size_t size_of_link,void *loopback) {
914   routing_model_dijkstraboth_create(size_of_link, loopback, 0);
915 }
916
917 static void routing_model_dijkstracache_create(size_t size_of_link,void *loopback) {
918   routing_model_dijkstraboth_create(size_of_link, loopback, 1);
919 }
920
921 /* ************************************************** */
922 /* ********** NO ROUTING **************************** */
923
924 static void routing_none_finalize(void) {
925   if (used_routing) {
926     xbt_dict_free(&used_routing->host_id);
927     free(used_routing);
928     used_routing=NULL;
929   }
930 }
931
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;
940
941   /* Set it in position */
942   used_routing = (routing_t) routing;
943 }