Logo AND Algorithmique Numérique Distribuée

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