Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Allow to set latency_factor, bandwidth_factor and weight_S parameters of the network...
[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 xbt_dict_t onelink_routes = NULL;
18
19 /* Prototypes of each model */
20 static void routing_model_full_create(size_t size_of_link,void *loopback);
21 static void routing_model_floyd_create(size_t size_of_link, void*loopback);
22 static void routing_model_dijkstra_create(size_t size_of_link, void*loopback);
23 static void routing_model_dijkstracache_create(size_t size_of_link, void*loopback);
24 static void routing_model_none_create(size_t size_of_link, void*loopback);
25
26 /* Definition of each model */
27 struct model_type {
28   const char *name;
29   const char *desc;
30   void (*fun)(size_t,void*);
31 };
32 struct model_type models[] =
33 { {"Full", "Full routing data (fast, large memory requirements, fully expressive)", routing_model_full_create },
34   {"Floyd", "Floyd routing data (slow initialization, fast lookup, lesser memory requirements, shortest path routing only)", routing_model_floyd_create },
35   {"Dijkstra", "Dijkstra routing data (fast initialization, slow lookup, small memory requirements, shortest path routing only)", routing_model_dijkstra_create },
36   {"DijkstraCache", "Dijkstra routing data (fast initialization, fast lookup, small memory requirements, shortest path routing only)", routing_model_dijkstracache_create },
37   {"none", "No routing (usable with Constant network only)", routing_model_none_create },
38     {NULL,NULL,NULL}};
39
40
41 void routing_model_create(size_t size_of_links, void* loopback) {
42
43   char * wanted=xbt_cfg_get_string(_surf_cfg_set,"routing");
44   int cpt;
45   for (cpt=0;models[cpt].name;cpt++) {
46     if (!strcmp(wanted,models[cpt].name)) {
47       (*(models[cpt].fun))(size_of_links,loopback);
48       return;
49     }
50   }
51   fprintf(stderr,"Routing model %s not found. Existing models:\n",wanted);
52   for (cpt=0;models[cpt].name;cpt++)
53     if (!strcmp(wanted,models[cpt].name))
54       fprintf(stderr,"   %s: %s\n",models[cpt].name,models[cpt].desc);
55   exit(1);
56 }
57
58 /* ************************************************************************** */
59 /* *************************** FULL ROUTING ********************************* */
60 typedef struct {
61   s_routing_t generic_routing;
62   xbt_dynar_t *routing_table;
63   void *loopback;
64   size_t size_of_link;
65 } s_routing_full_t,*routing_full_t;
66
67 #define ROUTE_FULL(i,j) ((routing_full_t)used_routing)->routing_table[(i)+(j)*(used_routing)->host_count]
68 #define HOST2ROUTER(id) ((id)+(2<<29))
69 #define ROUTER2HOST(id) ((id)-(2>>29))
70 #define ISROUTER(id) ((id)>=(2<<29))
71
72 /*
73  * Free the onelink routes
74  */
75 static void onelink_route_elem_free(void *e) {
76   s_onelink_t tmp = (s_onelink_t)e;
77   if(tmp) {
78     free(tmp);
79   }
80 }
81
82 /*
83  * Parsing
84  */
85 static void routing_full_parse_Shost(void) {
86   int *val = xbt_malloc(sizeof(int));
87   DEBUG2("Seen host %s (#%d)",A_surfxml_host_id,used_routing->host_count);
88   *val = used_routing->host_count++;
89   xbt_dict_set(used_routing->host_id,A_surfxml_host_id,val,xbt_free);
90 }
91
92 static void routing_full_parse_Srouter(void) {
93         int *val = xbt_malloc(sizeof(int));
94   DEBUG3("Seen router %s (%d -> #%d)",A_surfxml_router_id,used_routing->router_count,
95                 HOST2ROUTER(used_routing->router_count));
96   *val = HOST2ROUTER(used_routing->router_count++);
97   xbt_dict_set(used_routing->host_id,A_surfxml_router_id,val,xbt_free);
98 }
99
100 static int src_id = -1;
101 static int dst_id = -1;
102 static void routing_full_parse_Sroute_set_endpoints(void)
103 {
104   src_id = *(int*)xbt_dict_get(used_routing->host_id,A_surfxml_route_src);
105   dst_id = *(int*)xbt_dict_get(used_routing->host_id,A_surfxml_route_dst);
106   DEBUG4("Route %s %d -> %s %d",A_surfxml_route_src,src_id,A_surfxml_route_dst,dst_id);
107   route_action = A_surfxml_route_action;
108 }
109 static void routing_full_parse_Eroute(void)
110 {
111   char *name;
112   if (src_id != -1 && dst_id != -1) {
113     name = bprintf("%x#%x", src_id, dst_id);
114     manage_route(route_table, name, route_action, 0);
115     free(name);
116   }
117 }
118
119 /* Cluster tag functions */
120
121 static void routing_full_parse_change_cpu_data(const char *hostName,
122                                   const char *surfxml_host_power,
123                                   const char *surfxml_host_availability,
124                                   const char *surfxml_host_availability_file,
125                                   const char *surfxml_host_state_file)
126 {
127   int AX_ptr = 0;
128
129   SURFXML_BUFFER_SET(host_id, hostName);
130   SURFXML_BUFFER_SET(host_power, surfxml_host_power /*hostPower */ );
131   SURFXML_BUFFER_SET(host_availability, surfxml_host_availability);
132   SURFXML_BUFFER_SET(host_availability_file, surfxml_host_availability_file);
133   SURFXML_BUFFER_SET(host_state_file, surfxml_host_state_file);
134 }
135
136 static void routing_full_parse_change_link_data(const char *linkName,
137                                    const char *surfxml_link_bandwidth,
138                                    const char *surfxml_link_bandwidth_file,
139                                    const char *surfxml_link_latency,
140                                    const char *surfxml_link_latency_file,
141                                    const char *surfxml_link_state_file)
142 {
143   int AX_ptr = 0;
144
145   SURFXML_BUFFER_SET(link_id, linkName);
146   SURFXML_BUFFER_SET(link_bandwidth, surfxml_link_bandwidth);
147   SURFXML_BUFFER_SET(link_bandwidth_file, surfxml_link_bandwidth_file);
148   SURFXML_BUFFER_SET(link_latency, surfxml_link_latency);
149   SURFXML_BUFFER_SET(link_latency_file, surfxml_link_latency_file);
150   SURFXML_BUFFER_SET(link_state_file, surfxml_link_state_file);
151 }
152
153 static void routing_full_parse_Scluster(void)
154 {
155   static int AX_ptr = 0;
156
157   char *cluster_id = A_surfxml_cluster_id;
158   char *cluster_prefix = A_surfxml_cluster_prefix;
159   char *cluster_suffix = A_surfxml_cluster_suffix;
160   char *cluster_radical = A_surfxml_cluster_radical;
161   char *cluster_power = A_surfxml_cluster_power;
162   char *cluster_bw = A_surfxml_cluster_bw;
163   char *cluster_lat = A_surfxml_cluster_lat;
164   char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
165   char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
166   char *backbone_name;
167
168   surfxml_bufferstack_push(1);
169
170   /* Make set a set to parse the prefix/suffix/radical into a neat list of names */
171   DEBUG4("Make <set id='%s' prefix='%s' suffix='%s' radical='%s'>",
172       cluster_id,cluster_prefix,cluster_suffix,cluster_radical);
173   SURFXML_BUFFER_SET(set_id, cluster_id);
174   SURFXML_BUFFER_SET(set_prefix, cluster_prefix);
175   SURFXML_BUFFER_SET(set_suffix, cluster_suffix);
176   SURFXML_BUFFER_SET(set_radical, cluster_radical);
177
178   SURFXML_START_TAG(set);
179   SURFXML_END_TAG(set);
180
181   xbt_dynar_t names = xbt_dict_get(set_list,cluster_id);
182
183   unsigned int it1,it2;
184   char *name1,*name2;
185   xbt_dynar_foreach(names,it1,name1) {
186     /* create the host */
187     routing_full_parse_change_cpu_data(name1, cluster_power, "1.0", "", "");
188     A_surfxml_host_state = A_surfxml_host_state_ON;
189
190     SURFXML_START_TAG(host);
191     SURFXML_END_TAG(host);
192
193     /* Here comes the link */
194     routing_full_parse_change_link_data(name1, cluster_bw, "", cluster_lat, "", "");
195     A_surfxml_link_state = A_surfxml_link_state_ON;
196     A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
197
198     SURFXML_START_TAG(link);
199     SURFXML_END_TAG(link);
200   }
201
202   /* Make backbone link */
203   backbone_name = bprintf("%s_bb", cluster_id);
204   routing_full_parse_change_link_data(backbone_name, cluster_bb_bw, "", cluster_bb_lat, "",
205                          "");
206   A_surfxml_link_state = A_surfxml_link_state_ON;
207   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
208
209   SURFXML_START_TAG(link);
210   SURFXML_END_TAG(link);
211
212   /* And now the internal routes */
213   xbt_dynar_foreach(names,it1,name1) {
214     xbt_dynar_foreach(names,it2,name2) {
215       if (strcmp(name1,name2)) {
216         A_surfxml_route_action = A_surfxml_route_action_POSTPEND;
217         SURFXML_BUFFER_SET(route_src,name1);
218         SURFXML_BUFFER_SET(route_dst,name2);
219         SURFXML_START_TAG(route); {
220           /* FIXME: name1 link is added by error about 20 lines below, so don't add it here
221           SURFXML_BUFFER_SET(link_c_ctn_id, name1);
222           SURFXML_START_TAG(link_c_ctn);
223           SURFXML_END_TAG(link_c_ctn);
224            */
225           SURFXML_BUFFER_SET(link_c_ctn_id, backbone_name);
226           SURFXML_START_TAG(link_c_ctn);
227           SURFXML_END_TAG(link_c_ctn);
228
229           SURFXML_BUFFER_SET(link_c_ctn_id, name2);
230           SURFXML_START_TAG(link_c_ctn);
231           SURFXML_END_TAG(link_c_ctn);
232
233         } SURFXML_END_TAG(route);
234       }
235     }
236   }
237
238   /* Make route multi with the outside world, i.e. cluster->$* */
239
240   /* FIXME
241    * This also adds an elements to the routes within the cluster,
242    * and I guess it's wrong, but since this element is commented out in the above
243    * code creating the internal routes, we're good.
244    * To fix it, I'd say that we need a way to understand "$*-${cluster_id}" as "whole world, but the guys in that cluster"
245    * But for that, we need to install a real expression parser for src/dst attributes
246    *
247    * FIXME
248    * This also adds a dumb element (the private link) in place of the loopback. Then, since
249    * the loopback is added only if no link to self already exist, this fails.
250    * That's really dumb.
251    *
252    * FIXME
253    * It seems to me that it does not add the backbone to the path to outside world...
254    */
255   SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
256   SURFXML_BUFFER_SET(route_c_multi_dst, "$*");
257   A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
258   A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_PREPEND;
259
260   SURFXML_START_TAG(route_c_multi);
261
262   SURFXML_BUFFER_SET(link_c_ctn_id, "$src");
263
264   SURFXML_START_TAG(link_c_ctn);
265   SURFXML_END_TAG(link_c_ctn);
266
267   SURFXML_END_TAG(route_c_multi);
268
269   free(backbone_name);
270
271   /* Restore buff */
272   surfxml_bufferstack_pop(1);
273 }
274
275
276 static void routing_full_parse_end(void) {
277   routing_full_t routing = (routing_full_t) used_routing;
278   int nb_link = 0;
279   unsigned int cpt = 0;
280   xbt_dict_cursor_t cursor = NULL;
281   char *key, *data, *end;
282   const char *sep = "#";
283   xbt_dynar_t links, keys;
284   char *link_name = NULL;
285   int i,j;
286
287   int host_count = routing->generic_routing.host_count;
288
289   /* Create the routing table */
290   routing->routing_table = xbt_new0(xbt_dynar_t, host_count * host_count);
291   for (i=0;i<host_count;i++)
292     for (j=0;j<host_count;j++)
293       ROUTE_FULL(i,j) = xbt_dynar_new(routing->size_of_link,NULL);
294
295   /* Put the routes in position */
296   xbt_dict_foreach(route_table, cursor, key, data) {
297     nb_link = 0;
298     links = (xbt_dynar_t) data;
299     keys = xbt_str_split_str(key, sep);
300
301     src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
302     dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
303     xbt_dynar_free(&keys);
304
305     if(xbt_dynar_length(links) == 1){
306       s_onelink_t new_link = (s_onelink_t) xbt_malloc0(sizeof(s_onelink));
307       new_link->src_id = src_id;
308       new_link->dst_id = dst_id;
309       link_name = xbt_dynar_getfirst_as(links, char*);
310       new_link->link_ptr = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
311       DEBUG3("Adding onelink route from (#%d) to (#%d), link_name %s",src_id, dst_id, link_name);
312       xbt_dict_set(onelink_routes, link_name, (void *)new_link, onelink_route_elem_free);
313     }
314
315     if(ISROUTER(src_id) || ISROUTER(dst_id)) {
316                                 DEBUG2("There is route with a router here: (%d ,%d)",src_id,dst_id);
317                                 /* Check there is only one link in the route and store the information */
318                                 continue;
319     }
320
321     DEBUG4("Handle %d %d (from %d hosts): %ld links",
322         src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
323     xbt_dynar_foreach(links, cpt, link_name) {
324       void* link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
325       if (link)
326         xbt_dynar_push(ROUTE_FULL(src_id,dst_id),&link);
327       else
328         THROW1(mismatch_error,0,"Link %s not found", link_name);
329     }
330   }
331
332   /* Add the loopback if needed */
333   for (i = 0; i < host_count; i++)
334     if (!xbt_dynar_length(ROUTE_FULL(i, i)))
335       xbt_dynar_push(ROUTE_FULL(i,i),&routing->loopback);
336
337   /* Shrink the dynar routes (save unused slots) */
338   for (i=0;i<host_count;i++)
339     for (j=0;j<host_count;j++)
340       xbt_dynar_shrink(ROUTE_FULL(i,j),0);
341 }
342
343 /*
344  * Business methods
345  */
346 static xbt_dynar_t routing_full_get_route(int src,int dst) {
347   xbt_assert0(!(ISROUTER(src) || ISROUTER(dst)), "Ask for route \"from\" or \"to\" a router node");
348   return ROUTE_FULL(src,dst);
349 }
350
351 static xbt_dict_t routing_full_get_onelink_routes(void){
352   return onelink_routes;
353 }
354
355 static int routing_full_is_router(int id){
356         return ISROUTER(id);
357 }
358
359 static void routing_full_finalize(void) {
360   routing_full_t routing = (routing_full_t)used_routing;
361   int i,j;
362
363   if (routing) {
364     for (i = 0; i < used_routing->host_count; i++)
365       for (j = 0; j < used_routing->host_count; j++)
366         xbt_dynar_free(&ROUTE_FULL(i, j));
367     free(routing->routing_table);
368     xbt_dict_free(&used_routing->host_id);
369     xbt_dict_free(&onelink_routes);
370     free(routing);
371     routing=NULL;
372   }
373 }
374
375 static void routing_model_full_create(size_t size_of_link,void *loopback) {
376   /* initialize our structure */
377   routing_full_t routing = xbt_new0(s_routing_full_t,1);
378   routing->generic_routing.name = "Full";
379   routing->generic_routing.host_count = 0;
380   routing->generic_routing.get_route = routing_full_get_route;
381   routing->generic_routing.get_onelink_routes = routing_full_get_onelink_routes;
382   routing->generic_routing.is_router = routing_full_is_router;
383   routing->generic_routing.finalize = routing_full_finalize;
384
385   routing->size_of_link = size_of_link;
386   routing->loopback = loopback;
387
388   /* Set it in position */
389   used_routing = (routing_t) routing;
390
391   /* Set the dict for onehop routes */
392   onelink_routes =  xbt_dict_new();
393
394   /* Setup the parsing callbacks we need */
395   routing->generic_routing.host_id = xbt_dict_new();
396   surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
397   surfxml_add_callback(STag_surfxml_router_cb_list, &routing_full_parse_Srouter);
398   surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_full_parse_end);
399   surfxml_add_callback(STag_surfxml_route_cb_list,
400       &routing_full_parse_Sroute_set_endpoints);
401   surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);
402   surfxml_add_callback(STag_surfxml_cluster_cb_list, &routing_full_parse_Scluster);
403 }
404
405 /* ************************************************************************** */
406
407 static void routing_shortest_path_parse_Scluster(void)
408 {
409   static int AX_ptr = 0;
410
411   char *cluster_id = A_surfxml_cluster_id;
412   char *cluster_prefix = A_surfxml_cluster_prefix;
413   char *cluster_suffix = A_surfxml_cluster_suffix;
414   char *cluster_radical = A_surfxml_cluster_radical;
415   char *cluster_power = A_surfxml_cluster_power;
416   char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
417   char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
418   char *backbone_name;
419
420   surfxml_bufferstack_push(1);
421
422   /* Make set */
423   SURFXML_BUFFER_SET(set_id, cluster_id);
424   SURFXML_BUFFER_SET(set_prefix, cluster_prefix);
425   SURFXML_BUFFER_SET(set_suffix, cluster_suffix);
426   SURFXML_BUFFER_SET(set_radical, cluster_radical);
427
428   SURFXML_START_TAG(set);
429   SURFXML_END_TAG(set);
430
431   /* Make foreach */
432   SURFXML_BUFFER_SET(foreach_set_id, cluster_id);
433
434   SURFXML_START_TAG(foreach);
435
436   /* Make host for the foreach */
437   routing_full_parse_change_cpu_data("$1", cluster_power, "1.0", "", "");
438   A_surfxml_host_state = A_surfxml_host_state_ON;
439
440   SURFXML_START_TAG(host);
441   SURFXML_END_TAG(host);
442
443   SURFXML_END_TAG(foreach);
444
445   /* Make backbone link */
446   backbone_name = bprintf("%s_bb", cluster_id);
447   routing_full_parse_change_link_data(backbone_name, cluster_bb_bw, "", cluster_bb_lat, "",
448                          "");
449   A_surfxml_link_state = A_surfxml_link_state_ON;
450   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
451
452   SURFXML_START_TAG(link);
453   SURFXML_END_TAG(link);
454
455   free(backbone_name);
456
457   /* Restore buff */
458   surfxml_bufferstack_pop(1);
459 }
460
461 /* ************************************************************************** */
462 /* *************************** FLOYD ROUTING ********************************* */
463 typedef struct {
464   s_routing_t generic_routing;
465   int *predecessor_table;
466   void** link_table;
467   xbt_dynar_t last_route;
468   void *loopback;
469   size_t size_of_link;
470 } s_routing_floyd_t,*routing_floyd_t;
471
472 #define FLOYD_COST(i,j) cost_table[(i)+(j)*(used_routing)->host_count]
473 #define FLOYD_PRED(i,j) ((routing_floyd_t)used_routing)->predecessor_table[(i)+(j)*(used_routing)->host_count]
474 #define FLOYD_LINK(i,j) ((routing_floyd_t)used_routing)->link_table[(i)+(j)*(used_routing)->host_count]
475
476 static void routing_floyd_parse_end(void) {
477
478   routing_floyd_t routing = (routing_floyd_t) used_routing;
479   int nb_link = 0;
480   void* link_list = NULL;
481   double * cost_table;
482   xbt_dict_cursor_t cursor = NULL;
483   char *key,*data, *end;
484   const char *sep = "#";
485   xbt_dynar_t links, keys;
486
487   unsigned int i,j;
488
489   int host_count = routing->generic_routing.host_count;
490
491   /* Create Cost, Predecessor and Link tables */
492   cost_table = xbt_new0(double, host_count * host_count); //link cost from host to host
493   routing->predecessor_table = xbt_new0(int, host_count*host_count); //predecessor host numbers
494   routing->link_table = xbt_new0(void*,host_count*host_count); //actual link between src and dst
495   routing->last_route = xbt_dynar_new(routing->size_of_link, NULL);
496
497   /* Initialize costs and predecessors*/
498   for(i = 0; i<host_count;i++)
499     for(j = 0; j<host_count;j++) {
500         FLOYD_COST(i,j) = DBL_MAX;
501         FLOYD_PRED(i,j) = -1;
502     }
503
504    /* Put the routes in position */
505   xbt_dict_foreach(route_table, cursor, key, data) {
506     nb_link = 0;
507     links = (xbt_dynar_t)data;
508     keys = xbt_str_split_str(key, sep);
509
510     
511     src_id = strtol(xbt_dynar_get_as(keys, 0, char*), &end, 16);
512     dst_id = strtol(xbt_dynar_get_as(keys, 1, char*), &end, 16);
513     xbt_dynar_free(&keys);
514  
515     DEBUG4("Handle %d %d (from %d hosts): %ld links",
516         src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
517     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);
518     
519     char * link_name = xbt_dynar_getfirst_as(links, char*);
520     void * link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
521     if (link)
522       link_list = link;
523     else
524       THROW1(mismatch_error,0,"Link %s not found", link_name);
525     
526
527     FLOYD_LINK(src_id,dst_id) = link_list;
528     FLOYD_PRED(src_id, dst_id) = src_id;
529
530     //link cost
531     FLOYD_COST(src_id, dst_id) = 1; // assume 1 for now
532
533   }
534
535     /* Add the loopback if needed */
536   for (i = 0; i < host_count; i++)
537     if (!FLOYD_PRED(i, i)) {
538       FLOYD_PRED(i, i) = i;
539       FLOYD_COST(i, i) = 1;
540       FLOYD_LINK(i, i) = routing->loopback;
541     }
542
543
544   //Calculate path costs 
545   unsigned int a,b,c;
546   for(c=0;c<host_count;c++) {
547     for(a=0;a<host_count;a++) {
548       for(b=0;b<host_count;b++) {
549         if(FLOYD_COST(a,c) < DBL_MAX && FLOYD_COST(c,b) < DBL_MAX) {
550           if(FLOYD_COST(a,b) == DBL_MAX || (FLOYD_COST(a,c)+FLOYD_COST(c,b) < FLOYD_COST(a,b))) {
551             FLOYD_COST(a,b) = FLOYD_COST(a,c)+FLOYD_COST(c,b);
552             FLOYD_PRED(a,b) = FLOYD_PRED(c,b);
553           }
554         }
555       }
556     }
557   }
558
559   //cleanup
560   free(cost_table);
561 }
562
563 /*
564  * Business methods
565  */
566 static xbt_dynar_t routing_floyd_get_route(int src_id,int dst_id) {
567
568   routing_floyd_t routing = (routing_floyd_t) used_routing;
569
570   int pred = dst_id;
571   int prev_pred = 0;
572
573   xbt_dynar_reset(routing->last_route);
574
575   do {
576     prev_pred = pred;
577     pred = FLOYD_PRED(src_id, pred);
578
579     if(pred == -1) // if no pred in route -> no route to host
580         break;
581
582     xbt_dynar_unshift(routing->last_route, &FLOYD_LINK(pred,prev_pred));
583
584   } while(pred != src_id);
585
586   xbt_assert2(pred != -1, "no route from host %d to %d", src_id, dst_id);
587
588   return routing->last_route;
589 }
590
591 static void routing_floyd_finalize(void) {
592   routing_floyd_t routing = (routing_floyd_t)used_routing;
593
594   if (routing) {
595     free(routing->link_table);
596     free(routing->predecessor_table);
597     xbt_dynar_free(&routing->last_route);
598     xbt_dict_free(&used_routing->host_id);
599     free(routing);
600     routing=NULL;
601   }
602 }
603
604 static xbt_dict_t routing_floyd_get_onelink_routes(void){
605   xbt_assert0(0,"The get_onelink_routes feature is not supported in routing model Floyd");
606 }
607
608 static int routing_floyd_is_router(int id){
609   xbt_assert0(0,"The get_is_router feature is not supported in routing model Floyd");
610 }
611
612 static void routing_model_floyd_create(size_t size_of_link,void *loopback) {
613   /* initialize our structure */
614   routing_floyd_t routing = xbt_new0(s_routing_floyd_t,1);
615   routing->generic_routing.name = "Floyd";
616   routing->generic_routing.host_count = 0;
617   routing->generic_routing.host_id = xbt_dict_new();
618   routing->generic_routing.get_route = routing_floyd_get_route;
619   routing->generic_routing.get_onelink_routes = routing_floyd_get_onelink_routes;
620   routing->generic_routing.is_router = routing_floyd_is_router;
621   routing->generic_routing.finalize = routing_floyd_finalize;
622   routing->size_of_link = size_of_link;
623   routing->loopback = loopback;
624
625   /* Set it in position */
626   used_routing = (routing_t) routing;
627   
628   /* Setup the parsing callbacks we need */
629   surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
630   surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_floyd_parse_end);
631   surfxml_add_callback(STag_surfxml_route_cb_list, 
632       &routing_full_parse_Sroute_set_endpoints);
633   surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);
634   surfxml_add_callback(STag_surfxml_cluster_cb_list, &routing_shortest_path_parse_Scluster);
635   
636 }
637
638 /* ************************************************************************** */
639 /* ********** Dijkstra & Dijkstra Cached ROUTING **************************** */
640 typedef struct {
641   s_routing_t generic_routing;
642   xbt_graph_t route_graph;
643   xbt_dict_t graph_node_map;
644   xbt_dict_t route_cache;
645   xbt_dynar_t last_route;
646   int cached;
647   void *loopback;
648   size_t size_of_link;
649 } s_routing_dijkstra_t,*routing_dijkstra_t;
650
651
652 typedef struct graph_node_data {
653   int id; 
654   int graph_id; //used for caching internal graph id's
655 } s_graph_node_data_t, * graph_node_data_t;
656
657 typedef struct graph_node_map_element {
658   xbt_node_t node;
659 } s_graph_node_map_element_t, * graph_node_map_element_t;
660
661 typedef struct route_cache_element {
662   int * pred_arr;
663   int size;
664 } s_route_cache_element_t, * route_cache_element_t;     
665
666 /*
667  * Free functions
668  */
669 static void route_cache_elem_free(void *e) {
670   route_cache_element_t elm=(route_cache_element_t)e;
671
672   if (elm) {
673     free(elm->pred_arr);
674     free(elm);
675   }
676 }
677
678 static void graph_node_map_elem_free(void *e) {
679   graph_node_map_element_t elm = (graph_node_map_element_t)e;
680
681   if(elm) {
682     free(elm);
683   }
684 }
685
686 /*
687  * Utility functions
688 */
689 static xbt_node_t route_graph_new_node(int id, int graph_id) {
690   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
691
692   graph_node_data_t data = xbt_new0(struct graph_node_data, sizeof(struct graph_node_data));
693   data->id = id;
694   data->graph_id = graph_id;
695   xbt_node_t node = xbt_graph_new_node(routing->route_graph, data);
696
697   graph_node_map_element_t elm = xbt_new0(struct graph_node_map_element, sizeof(struct graph_node_map_element));
698   elm->node = node;
699   xbt_dict_set_ext(routing->graph_node_map, (char*)(&id), sizeof(int), (xbt_set_elm_t)elm, &graph_node_map_elem_free);
700
701   return node;
702 }
703
704 static graph_node_map_element_t graph_node_map_search(int id) {
705   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
706
707   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));
708
709   return elm;
710 }
711
712 /*
713  * Parsing
714  */
715 static void route_new_dijkstra(int src_id, int dst_id, void* link) {
716   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
717
718   xbt_node_t src = NULL;
719   xbt_node_t dst = NULL;
720   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));
721   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));
722
723   if(src_elm)
724     src = src_elm->node;
725
726   if(dst_elm)
727     dst = dst_elm->node;
728
729   //add nodes if they don't exist in the graph
730   if(src_id == dst_id && src == NULL && dst == NULL) {
731     src = route_graph_new_node(src_id, -1);
732     dst = src;
733   } else {
734     if(src == NULL) {
735       src = route_graph_new_node(src_id, -1);
736     }
737         
738     if(dst == NULL) {
739       dst = route_graph_new_node(dst_id, -1);
740     }
741   }
742
743   //add link as edge to graph
744   xbt_graph_new_edge(routing->route_graph, src, dst, link);
745   
746 }
747
748 static void add_loopback_dijkstra(void) {
749   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
750
751         xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
752         
753         xbt_node_t node = NULL;
754         unsigned int cursor2;
755         xbt_dynar_foreach(nodes, cursor2, node) {
756                 xbt_dynar_t out_edges = xbt_graph_node_get_outedges(node); 
757                 xbt_edge_t edge = NULL;
758                 unsigned int cursor;
759         
760                 int found = 0;
761                 xbt_dynar_foreach(out_edges, cursor, edge) {
762                         xbt_node_t other_node = xbt_graph_edge_get_target(edge);
763                         if(other_node == node) {
764                                 found = 1;
765                                 break;
766                         }
767                 }
768
769                 if(!found)
770                         xbt_graph_new_edge(routing->route_graph, node, node, &routing->loopback);
771         }
772 }
773
774 static void routing_dijkstra_parse_end(void) {
775   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
776   int nb_link = 0;
777   xbt_dict_cursor_t cursor = NULL;
778   char *key, *data, *end;
779   const char *sep = "#";
780   xbt_dynar_t links, keys;
781
782   /* Create the topology graph */
783   routing->route_graph = xbt_graph_new_graph(1, NULL);
784   routing->graph_node_map = xbt_dict_new();
785   routing->last_route = xbt_dynar_new(routing->size_of_link, NULL);
786   if(routing->cached)
787     routing->route_cache = xbt_dict_new();
788
789
790   /* Put the routes in position */
791   xbt_dict_foreach(route_table, cursor, key, data) {
792     nb_link = 0;
793     links = (xbt_dynar_t) data;
794     keys = xbt_str_split_str(key, sep);
795
796     src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
797     dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
798     xbt_dynar_free(&keys);
799
800     DEBUG4("Handle %d %d (from %d hosts): %ld links",
801         src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
802
803     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);
804
805     char* link_name = xbt_dynar_getfirst_as(links, char*);
806     void* link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
807     if (link)
808       route_new_dijkstra(src_id,dst_id,link);
809     else
810       THROW1(mismatch_error,0,"Link %s not found", link_name);
811     
812   }
813
814   /* Add the loopback if needed */
815   add_loopback_dijkstra();
816
817   /* initialize graph indexes in nodes after graph has been built */
818   xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
819
820   xbt_node_t node = NULL;
821   unsigned int cursor2;
822   xbt_dynar_foreach(nodes, cursor2, node) {
823     graph_node_data_t data = xbt_graph_node_get_data(node);
824     data->graph_id = cursor2;
825   }
826
827 }
828
829 /*
830  * Business methods
831  */
832 static xbt_dynar_t routing_dijkstra_get_route(int src_id,int dst_id) {
833
834   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
835   int * pred_arr = NULL;
836   
837   xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
838
839   /*Use the graph_node id mapping set to quickly find the nodes */
840   graph_node_map_element_t src_elm = graph_node_map_search(src_id);
841   graph_node_map_element_t dst_elm = graph_node_map_search(dst_id);
842   xbt_assert2(src_elm != NULL && dst_elm != NULL, "src %d or dst %d does not exist", src_id, dst_id);
843   int src_node_id = ((graph_node_data_t)xbt_graph_node_get_data(src_elm->node))->graph_id;
844   int dst_node_id = ((graph_node_data_t)xbt_graph_node_get_data(dst_elm->node))->graph_id;
845
846   route_cache_element_t elm = NULL;
847   if(routing->cached) {
848     /*check if there is a cached predecessor list avail */
849     elm = (route_cache_element_t)xbt_dict_get_or_null_ext(routing->route_cache, (char*)(&src_id), sizeof(int));
850   }
851
852   if(elm) { //cached mode and cache hit
853     pred_arr = elm->pred_arr;
854   } else { //not cached mode or cache miss
855     double * cost_arr = NULL;
856     xbt_heap_t pqueue = NULL;
857     int i = 0;
858
859     int nr_nodes = xbt_dynar_length(nodes);
860     cost_arr = xbt_new0(double, nr_nodes); //link cost from src to other hosts
861     pred_arr = xbt_new0(int, nr_nodes); //predecessors in path from src
862     pqueue = xbt_heap_new(nr_nodes, free);
863
864     //initialize
865     cost_arr[src_node_id] = 0.0;
866
867     for(i = 0; i < nr_nodes; i++) {
868       if(i != src_node_id) {
869         cost_arr[i] = DBL_MAX;
870       }
871
872       pred_arr[i] = 0;
873
874       //initialize priority queue
875       int * nodeid = xbt_new0(int, 1);
876       *nodeid = i;
877       xbt_heap_push(pqueue, nodeid, cost_arr[i]);
878
879     }
880
881     // apply dijkstra using the indexes from the graph's node array
882     while(xbt_heap_size(pqueue) > 0) {
883       int * v_id = xbt_heap_pop(pqueue);
884       xbt_node_t v_node = xbt_dynar_get_as(nodes, *v_id, xbt_node_t);
885       xbt_dynar_t out_edges = xbt_graph_node_get_outedges(v_node); 
886       xbt_edge_t edge = NULL;
887       unsigned int cursor;
888
889       xbt_dynar_foreach(out_edges, cursor, edge) {
890         xbt_node_t u_node = xbt_graph_edge_get_target(edge);
891         graph_node_data_t data = xbt_graph_node_get_data(u_node);
892         int u_id = data->graph_id;
893         int cost_v_u = 1; //fixed link cost for now
894
895         if(cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
896           pred_arr[u_id] = *v_id;
897           cost_arr[u_id] = cost_v_u + cost_arr[*v_id];
898           int * nodeid = xbt_new0(int, 1);
899           *nodeid = u_id;
900           xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
901         }
902       }
903
904       //free item popped from pqueue
905       free(v_id);
906     }
907
908     free(cost_arr);
909     xbt_heap_free(pqueue);
910
911   }
912
913   //compose route path with links
914   xbt_dynar_reset(routing->last_route);
915
916   int v;
917   int size = 0;
918   for(v = dst_node_id; v != src_node_id; v = pred_arr[v]) {
919     xbt_node_t node_pred_v = xbt_dynar_get_as(nodes, pred_arr[v], xbt_node_t);
920     xbt_node_t node_v = xbt_dynar_get_as(nodes, v, xbt_node_t);
921     xbt_edge_t edge = xbt_graph_get_edge(routing->route_graph, node_pred_v, node_v);
922
923     xbt_assert2(edge != NULL, "no route between host %d and %d", src_id, dst_id);
924
925     void * link = xbt_graph_edge_get_data(edge);
926     xbt_dynar_unshift(routing->last_route, &link);
927     size++;
928   }
929
930
931   if(routing->cached && elm == NULL) {
932     //add to predecessor list of the current src-host to cache
933     elm = xbt_new0(struct route_cache_element, sizeof(struct route_cache_element));
934     elm->pred_arr = pred_arr;
935     elm->size = size;
936     xbt_dict_set_ext(routing->route_cache, (char*)(&src_id), sizeof(int), (xbt_set_elm_t)elm, &route_cache_elem_free);
937   }
938
939   if(!routing->cached)
940     free(pred_arr);
941
942   return routing->last_route;
943 }
944
945
946 static void routing_dijkstra_finalize(void) {
947   routing_dijkstra_t routing = (routing_dijkstra_t)used_routing;
948
949   if (routing) {
950     xbt_graph_free_graph(routing->route_graph, &free, NULL, &free);
951     xbt_dict_free(&routing->graph_node_map);
952     if(routing->cached)
953       xbt_dict_free(&routing->route_cache);
954     xbt_dynar_free(&routing->last_route);
955     xbt_dict_free(&used_routing->host_id);
956     free(routing);
957     routing=NULL;
958   }
959 }
960
961 static xbt_dict_t routing_dijkstraboth_get_onelink_routes(void){
962   xbt_assert0(0,"The get_onelink_routes feature is not supported in routing model dijkstraboth");
963 }
964
965 static int routing_dijkstraboth_is_router(int id){
966   xbt_assert0(0,"The get_is_router feature is not supported in routing model dijkstraboth");
967 }
968
969 /*
970  *
971  */
972 static void routing_model_dijkstraboth_create(size_t size_of_link,void *loopback, int cached) {
973   /* initialize our structure */
974   routing_dijkstra_t routing = xbt_new0(s_routing_dijkstra_t,1);
975   routing->generic_routing.name = "Dijkstra";
976   routing->generic_routing.host_count = 0;
977   routing->generic_routing.get_route = routing_dijkstra_get_route;
978   routing->generic_routing.get_onelink_routes = routing_dijkstraboth_get_onelink_routes;
979   routing->generic_routing.is_router = routing_dijkstraboth_is_router;
980   routing->generic_routing.finalize = routing_dijkstra_finalize;
981   routing->size_of_link = size_of_link;
982   routing->loopback = loopback;
983   routing->cached = cached;
984
985   /* Set it in position */
986   used_routing = (routing_t) routing;
987
988   /* Setup the parsing callbacks we need */
989   routing->generic_routing.host_id = xbt_dict_new();
990   surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
991   surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_dijkstra_parse_end);
992   surfxml_add_callback(STag_surfxml_route_cb_list,
993       &routing_full_parse_Sroute_set_endpoints);
994   surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);
995   surfxml_add_callback(STag_surfxml_cluster_cb_list, &routing_shortest_path_parse_Scluster);
996 }
997
998 static void routing_model_dijkstra_create(size_t size_of_link,void *loopback) {
999   routing_model_dijkstraboth_create(size_of_link, loopback, 0);
1000 }
1001
1002 static void routing_model_dijkstracache_create(size_t size_of_link,void *loopback) {
1003   routing_model_dijkstraboth_create(size_of_link, loopback, 1);
1004 }
1005
1006 /* ************************************************** */
1007 /* ********** NO ROUTING **************************** */
1008
1009
1010 static void routing_none_finalize(void) {
1011   if (used_routing) {
1012     xbt_dict_free(&used_routing->host_id);
1013     free(used_routing);
1014     used_routing=NULL;
1015   }
1016 }
1017
1018 static void routing_model_none_create(size_t size_of_link,void *loopback) {
1019   routing_t routing = xbt_new0(s_routing_t,1);
1020   INFO0("Null routing");
1021   routing->name = "none";
1022   routing->host_count = 0;
1023   routing->host_id = xbt_dict_new();
1024   routing->get_onelink_routes = NULL;
1025   routing->is_router = NULL;
1026   routing->get_route = NULL;
1027
1028   routing->finalize = routing_none_finalize;
1029
1030   /* Set it in position */
1031   used_routing = (routing_t) routing;
1032 }