Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Added a gtnets_jitter_seed parameter enabling jitted repeatable experiments, activate...
[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 */
171   SURFXML_BUFFER_SET(set_id, cluster_id);
172   SURFXML_BUFFER_SET(set_prefix, cluster_prefix);
173   SURFXML_BUFFER_SET(set_suffix, cluster_suffix);
174   SURFXML_BUFFER_SET(set_radical, cluster_radical);
175
176   SURFXML_START_TAG(set);
177   SURFXML_END_TAG(set);
178
179   /* Make foreach */
180   SURFXML_BUFFER_SET(foreach_set_id, cluster_id);
181
182   SURFXML_START_TAG(foreach);
183
184   /* Make host for the foreach */
185   routing_full_parse_change_cpu_data("$1", cluster_power, "1.0", "", "");
186   A_surfxml_host_state = A_surfxml_host_state_ON;
187
188   SURFXML_START_TAG(host);
189   SURFXML_END_TAG(host);
190
191   /* Make link for the foreach */
192   routing_full_parse_change_link_data("$1", cluster_bw, "", cluster_lat, "", "");
193   A_surfxml_link_state = A_surfxml_link_state_ON;
194   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
195
196   SURFXML_START_TAG(link);
197   SURFXML_END_TAG(link);
198
199   SURFXML_END_TAG(foreach);
200
201   /* Make backbone link */
202   backbone_name = bprintf("%s_bb", cluster_id);
203   routing_full_parse_change_link_data(backbone_name, cluster_bb_bw, "", cluster_bb_lat, "",
204                          "");
205   A_surfxml_link_state = A_surfxml_link_state_ON;
206   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
207
208   SURFXML_START_TAG(link);
209   SURFXML_END_TAG(link);
210
211   /* Make route multi with the outside world, i.e. cluster->$* */
212   SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
213   SURFXML_BUFFER_SET(route_c_multi_dst, "$*");
214   A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
215   A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_OVERRIDE;
216
217   SURFXML_START_TAG(route_c_multi);
218
219   SURFXML_BUFFER_SET(link_c_ctn_id, "$src");
220
221   SURFXML_START_TAG(link_c_ctn);
222   SURFXML_END_TAG(link_c_ctn);
223
224   SURFXML_END_TAG(route_c_multi);
225
226   /* Make route multi between cluster hosts, i.e. cluster->cluster */
227   SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
228   SURFXML_BUFFER_SET(route_c_multi_dst, cluster_id);
229   A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_POSTPEND;
230   A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
231
232   SURFXML_START_TAG(route_c_multi);
233
234   SURFXML_BUFFER_SET(link_c_ctn_id, backbone_name);
235
236   SURFXML_START_TAG(link_c_ctn);
237   SURFXML_END_TAG(link_c_ctn);
238
239   SURFXML_END_TAG(route_c_multi);
240
241   free(backbone_name);
242
243   /* Restore buff */
244   surfxml_bufferstack_pop(1);
245 }
246
247
248 static void routing_full_parse_end(void) {
249   routing_full_t routing = (routing_full_t) used_routing;
250   int nb_link = 0;
251   unsigned int cpt = 0;
252   xbt_dict_cursor_t cursor = NULL;
253   char *key, *data, *end;
254   const char *sep = "#";
255   xbt_dynar_t links, keys;
256   char *link_name = NULL;
257   int i,j;
258
259   int host_count = routing->generic_routing.host_count;
260
261   /* Create the routing table */
262   routing->routing_table = xbt_new0(xbt_dynar_t, host_count * host_count);
263   for (i=0;i<host_count;i++)
264     for (j=0;j<host_count;j++)
265       ROUTE_FULL(i,j) = xbt_dynar_new(routing->size_of_link,NULL);
266
267   /* Put the routes in position */
268   xbt_dict_foreach(route_table, cursor, key, data) {
269     nb_link = 0;
270     links = (xbt_dynar_t) data;
271     keys = xbt_str_split_str(key, sep);
272
273     src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
274     dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
275     xbt_dynar_free(&keys);
276
277     if(xbt_dynar_length(links) == 1){
278       s_onelink_t new_link = (s_onelink_t) xbt_malloc0(sizeof(s_onelink));
279       new_link->src_id = src_id;
280       new_link->dst_id = dst_id;
281       link_name = xbt_dynar_getfirst_as(links, char*);
282       new_link->link_ptr = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
283       DEBUG3("Adding onelink route from (#%d) to (#%d), link_name %s",src_id, dst_id, link_name);
284       xbt_dict_set(onelink_routes, link_name, (void *)new_link, onelink_route_elem_free);
285     }
286
287     if(ISROUTER(src_id) || ISROUTER(dst_id)) {
288                                 DEBUG2("There is route with a router here: (%d ,%d)",src_id,dst_id);
289                                 /* Check there is only one link in the route and store the information */
290                                 continue;
291     }
292
293     DEBUG4("Handle %d %d (from %d hosts): %ld links",
294         src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
295     xbt_dynar_foreach(links, cpt, link_name) {
296       void* link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
297       if (link)
298         xbt_dynar_push(ROUTE_FULL(src_id,dst_id),&link);
299       else
300         THROW1(mismatch_error,0,"Link %s not found", link_name);
301     }
302   }
303
304   /* Add the loopback if needed */
305   for (i = 0; i < host_count; i++)
306     if (!xbt_dynar_length(ROUTE_FULL(i, i)))
307       xbt_dynar_push(ROUTE_FULL(i,i),&routing->loopback);
308
309   /* Shrink the dynar routes (save unused slots) */
310   for (i=0;i<host_count;i++)
311     for (j=0;j<host_count;j++)
312       xbt_dynar_shrink(ROUTE_FULL(i,j),0);
313 }
314
315 /*
316  * Business methods
317  */
318 static xbt_dynar_t routing_full_get_route(int src,int dst) {
319   xbt_assert0(!(ISROUTER(src) || ISROUTER(dst)), "Ask for route \"from\" or \"to\" a router node");
320   return ROUTE_FULL(src,dst);
321 }
322
323 static xbt_dict_t routing_full_get_onelink_routes(void){
324   return onelink_routes;
325 }
326
327 static int routing_full_is_router(int id){
328         return ISROUTER(id);
329 }
330
331 static void routing_full_finalize(void) {
332   routing_full_t routing = (routing_full_t)used_routing;
333   int i,j;
334
335   if (routing) {
336     for (i = 0; i < used_routing->host_count; i++)
337       for (j = 0; j < used_routing->host_count; j++)
338         xbt_dynar_free(&ROUTE_FULL(i, j));
339     free(routing->routing_table);
340     xbt_dict_free(&used_routing->host_id);
341     free(routing);
342     routing=NULL;
343   }
344 }
345
346 static void routing_model_full_create(size_t size_of_link,void *loopback) {
347   /* initialize our structure */
348   routing_full_t routing = xbt_new0(s_routing_full_t,1);
349   routing->generic_routing.name = "Full";
350   routing->generic_routing.host_count = 0;
351   routing->generic_routing.get_route = routing_full_get_route;
352   routing->generic_routing.get_onelink_routes = routing_full_get_onelink_routes;
353   routing->generic_routing.is_router = routing_full_is_router;
354   routing->generic_routing.finalize = routing_full_finalize;
355
356   routing->size_of_link = size_of_link;
357   routing->loopback = loopback;
358
359   /* Set it in position */
360   used_routing = (routing_t) routing;
361
362   /* Set the dict for onehop routes */
363   onelink_routes =  xbt_dict_new();
364
365   /* Setup the parsing callbacks we need */
366   routing->generic_routing.host_id = xbt_dict_new();
367   surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
368   surfxml_add_callback(STag_surfxml_router_cb_list, &routing_full_parse_Srouter);
369   surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_full_parse_end);
370   surfxml_add_callback(STag_surfxml_route_cb_list,
371       &routing_full_parse_Sroute_set_endpoints);
372   surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);
373   surfxml_add_callback(STag_surfxml_cluster_cb_list, &routing_full_parse_Scluster);
374 }
375
376 /* ************************************************************************** */
377
378 static void routing_shortest_path_parse_Scluster(void)
379 {
380   static int AX_ptr = 0;
381
382   char *cluster_id = A_surfxml_cluster_id;
383   char *cluster_prefix = A_surfxml_cluster_prefix;
384   char *cluster_suffix = A_surfxml_cluster_suffix;
385   char *cluster_radical = A_surfxml_cluster_radical;
386   char *cluster_power = A_surfxml_cluster_power;
387   char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
388   char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
389   char *backbone_name;
390
391   surfxml_bufferstack_push(1);
392
393   /* Make set */
394   SURFXML_BUFFER_SET(set_id, cluster_id);
395   SURFXML_BUFFER_SET(set_prefix, cluster_prefix);
396   SURFXML_BUFFER_SET(set_suffix, cluster_suffix);
397   SURFXML_BUFFER_SET(set_radical, cluster_radical);
398
399   SURFXML_START_TAG(set);
400   SURFXML_END_TAG(set);
401
402   /* Make foreach */
403   SURFXML_BUFFER_SET(foreach_set_id, cluster_id);
404
405   SURFXML_START_TAG(foreach);
406
407   /* Make host for the foreach */
408   routing_full_parse_change_cpu_data("$1", cluster_power, "1.0", "", "");
409   A_surfxml_host_state = A_surfxml_host_state_ON;
410
411   SURFXML_START_TAG(host);
412   SURFXML_END_TAG(host);
413
414   SURFXML_END_TAG(foreach);
415
416   /* Make backbone link */
417   backbone_name = bprintf("%s_bb", cluster_id);
418   routing_full_parse_change_link_data(backbone_name, cluster_bb_bw, "", cluster_bb_lat, "",
419                          "");
420   A_surfxml_link_state = A_surfxml_link_state_ON;
421   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
422
423   SURFXML_START_TAG(link);
424   SURFXML_END_TAG(link);
425
426   free(backbone_name);
427
428   /* Restore buff */
429   surfxml_bufferstack_pop(1);
430 }
431
432 /* ************************************************************************** */
433 /* *************************** FLOYD ROUTING ********************************* */
434 typedef struct {
435   s_routing_t generic_routing;
436   int *predecessor_table;
437   void** link_table;
438   xbt_dynar_t last_route;
439   void *loopback;
440   size_t size_of_link;
441 } s_routing_floyd_t,*routing_floyd_t;
442
443 #define FLOYD_COST(i,j) cost_table[(i)+(j)*(used_routing)->host_count]
444 #define FLOYD_PRED(i,j) ((routing_floyd_t)used_routing)->predecessor_table[(i)+(j)*(used_routing)->host_count]
445 #define FLOYD_LINK(i,j) ((routing_floyd_t)used_routing)->link_table[(i)+(j)*(used_routing)->host_count]
446
447 static void routing_floyd_parse_end(void) {
448
449   routing_floyd_t routing = (routing_floyd_t) used_routing;
450   int nb_link = 0;
451   void* link_list = NULL;
452   double * cost_table;
453   xbt_dict_cursor_t cursor = NULL;
454   char *key,*data, *end;
455   const char *sep = "#";
456   xbt_dynar_t links, keys;
457
458   unsigned int i,j;
459
460   int host_count = routing->generic_routing.host_count;
461
462   /* Create Cost, Predecessor and Link tables */
463   cost_table = xbt_new0(double, host_count * host_count); //link cost from host to host
464   routing->predecessor_table = xbt_new0(int, host_count*host_count); //predecessor host numbers
465   routing->link_table = xbt_new0(void*,host_count*host_count); //actual link between src and dst
466   routing->last_route = xbt_dynar_new(routing->size_of_link, NULL);
467
468   /* Initialize costs and predecessors*/
469   for(i = 0; i<host_count;i++)
470     for(j = 0; j<host_count;j++) {
471         FLOYD_COST(i,j) = DBL_MAX;
472         FLOYD_PRED(i,j) = -1;
473     }
474
475    /* Put the routes in position */
476   xbt_dict_foreach(route_table, cursor, key, data) {
477     nb_link = 0;
478     links = (xbt_dynar_t)data;
479     keys = xbt_str_split_str(key, sep);
480
481     
482     src_id = strtol(xbt_dynar_get_as(keys, 0, char*), &end, 16);
483     dst_id = strtol(xbt_dynar_get_as(keys, 1, char*), &end, 16);
484     xbt_dynar_free(&keys);
485  
486     DEBUG4("Handle %d %d (from %d hosts): %ld links",
487         src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
488     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);
489     
490     char * link_name = xbt_dynar_getfirst_as(links, char*);
491     void * link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
492     if (link)
493       link_list = link;
494     else
495       THROW1(mismatch_error,0,"Link %s not found", link_name);
496     
497
498     FLOYD_LINK(src_id,dst_id) = link_list;
499     FLOYD_PRED(src_id, dst_id) = src_id;
500
501     //link cost
502     FLOYD_COST(src_id, dst_id) = 1; // assume 1 for now
503
504   }
505
506     /* Add the loopback if needed */
507   for (i = 0; i < host_count; i++)
508     if (!FLOYD_PRED(i, i)) {
509       FLOYD_PRED(i, i) = i;
510       FLOYD_COST(i, i) = 1;
511       FLOYD_LINK(i, i) = routing->loopback;
512     }
513
514
515   //Calculate path costs 
516   unsigned int a,b,c;
517   for(c=0;c<host_count;c++) {
518     for(a=0;a<host_count;a++) {
519       for(b=0;b<host_count;b++) {
520         if(FLOYD_COST(a,c) < DBL_MAX && FLOYD_COST(c,b) < DBL_MAX) {
521           if(FLOYD_COST(a,b) == DBL_MAX || (FLOYD_COST(a,c)+FLOYD_COST(c,b) < FLOYD_COST(a,b))) {
522             FLOYD_COST(a,b) = FLOYD_COST(a,c)+FLOYD_COST(c,b);
523             FLOYD_PRED(a,b) = FLOYD_PRED(c,b);
524           }
525         }
526       }
527     }
528   }
529
530   //cleanup
531   free(cost_table);
532 }
533
534 /*
535  * Business methods
536  */
537 static xbt_dynar_t routing_floyd_get_route(int src_id,int dst_id) {
538
539   routing_floyd_t routing = (routing_floyd_t) used_routing;
540
541   int pred = dst_id;
542   int prev_pred = 0;
543
544   xbt_dynar_reset(routing->last_route);
545
546   do {
547     prev_pred = pred;
548     pred = FLOYD_PRED(src_id, pred);
549
550     if(pred == -1) // if no pred in route -> no route to host
551         break;
552
553     xbt_dynar_unshift(routing->last_route, &FLOYD_LINK(pred,prev_pred));
554
555   } while(pred != src_id);
556
557   xbt_assert2(pred != -1, "no route from host %d to %d", src_id, dst_id);
558
559   return routing->last_route;
560 }
561
562 static void routing_floyd_finalize(void) {
563   routing_floyd_t routing = (routing_floyd_t)used_routing;
564
565   if (routing) {
566     free(routing->link_table);
567     free(routing->predecessor_table);
568     xbt_dynar_free(&routing->last_route);
569     xbt_dict_free(&used_routing->host_id);
570     free(routing);
571     routing=NULL;
572   }
573 }
574
575 static xbt_dict_t routing_floyd_get_onelink_routes(void){
576   xbt_assert0(0,"The get_onelink_routes feature is not supported in routing model Floyd");
577 }
578
579 static int routing_floyd_is_router(int id){
580   xbt_assert0(0,"The get_is_router feature is not supported in routing model Floyd");
581 }
582
583 static void routing_model_floyd_create(size_t size_of_link,void *loopback) {
584   /* initialize our structure */
585   routing_floyd_t routing = xbt_new0(s_routing_floyd_t,1);
586   routing->generic_routing.name = "Floyd";
587   routing->generic_routing.host_count = 0;
588   routing->generic_routing.host_id = xbt_dict_new();
589   routing->generic_routing.get_route = routing_floyd_get_route;
590   routing->generic_routing.get_onelink_routes = routing_floyd_get_onelink_routes;
591   routing->generic_routing.is_router = routing_floyd_is_router;
592   routing->generic_routing.finalize = routing_floyd_finalize;
593   routing->size_of_link = size_of_link;
594   routing->loopback = loopback;
595
596   /* Set it in position */
597   used_routing = (routing_t) routing;
598   
599   /* Setup the parsing callbacks we need */
600   surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
601   surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_floyd_parse_end);
602   surfxml_add_callback(STag_surfxml_route_cb_list, 
603       &routing_full_parse_Sroute_set_endpoints);
604   surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);
605   surfxml_add_callback(STag_surfxml_cluster_cb_list, &routing_shortest_path_parse_Scluster);
606   
607 }
608
609 /* ************************************************************************** */
610 /* ********** Dijkstra & Dijkstra Cached ROUTING **************************** */
611 typedef struct {
612   s_routing_t generic_routing;
613   xbt_graph_t route_graph;
614   xbt_dict_t graph_node_map;
615   xbt_dict_t route_cache;
616   xbt_dynar_t last_route;
617   int cached;
618   void *loopback;
619   size_t size_of_link;
620 } s_routing_dijkstra_t,*routing_dijkstra_t;
621
622
623 typedef struct graph_node_data {
624   int id; 
625   int graph_id; //used for caching internal graph id's
626 } s_graph_node_data_t, * graph_node_data_t;
627
628 typedef struct graph_node_map_element {
629   xbt_node_t node;
630 } s_graph_node_map_element_t, * graph_node_map_element_t;
631
632 typedef struct route_cache_element {
633   int * pred_arr;
634   int size;
635 } s_route_cache_element_t, * route_cache_element_t;     
636
637 /*
638  * Free functions
639  */
640 static void route_cache_elem_free(void *e) {
641   route_cache_element_t elm=(route_cache_element_t)e;
642
643   if (elm) {
644     free(elm->pred_arr);
645     free(elm);
646   }
647 }
648
649 static void graph_node_map_elem_free(void *e) {
650   graph_node_map_element_t elm = (graph_node_map_element_t)e;
651
652   if(elm) {
653     free(elm);
654   }
655 }
656
657 /*
658  * Utility functions
659 */
660 static xbt_node_t route_graph_new_node(int id, int graph_id) {
661   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
662
663   graph_node_data_t data = xbt_new0(struct graph_node_data, sizeof(struct graph_node_data));
664   data->id = id;
665   data->graph_id = graph_id;
666   xbt_node_t node = xbt_graph_new_node(routing->route_graph, data);
667
668   graph_node_map_element_t elm = xbt_new0(struct graph_node_map_element, sizeof(struct graph_node_map_element));
669   elm->node = node;
670   xbt_dict_set_ext(routing->graph_node_map, (char*)(&id), sizeof(int), (xbt_set_elm_t)elm, &graph_node_map_elem_free);
671
672   return node;
673 }
674
675 static graph_node_map_element_t graph_node_map_search(int id) {
676   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
677
678   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));
679
680   return elm;
681 }
682
683 /*
684  * Parsing
685  */
686 static void route_new_dijkstra(int src_id, int dst_id, void* link) {
687   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
688
689   xbt_node_t src = NULL;
690   xbt_node_t dst = NULL;
691   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));
692   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));
693
694   if(src_elm)
695     src = src_elm->node;
696
697   if(dst_elm)
698     dst = dst_elm->node;
699
700   //add nodes if they don't exist in the graph
701   if(src_id == dst_id && src == NULL && dst == NULL) {
702     src = route_graph_new_node(src_id, -1);
703     dst = src;
704   } else {
705     if(src == NULL) {
706       src = route_graph_new_node(src_id, -1);
707     }
708         
709     if(dst == NULL) {
710       dst = route_graph_new_node(dst_id, -1);
711     }
712   }
713
714   //add link as edge to graph
715   xbt_graph_new_edge(routing->route_graph, src, dst, link);
716   
717 }
718
719 static void add_loopback_dijkstra(void) {
720   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
721
722         xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
723         
724         xbt_node_t node = NULL;
725         unsigned int cursor2;
726         xbt_dynar_foreach(nodes, cursor2, node) {
727                 xbt_dynar_t out_edges = xbt_graph_node_get_outedges(node); 
728                 xbt_edge_t edge = NULL;
729                 unsigned int cursor;
730         
731                 int found = 0;
732                 xbt_dynar_foreach(out_edges, cursor, edge) {
733                         xbt_node_t other_node = xbt_graph_edge_get_target(edge);
734                         if(other_node == node) {
735                                 found = 1;
736                                 break;
737                         }
738                 }
739
740                 if(!found)
741                         xbt_graph_new_edge(routing->route_graph, node, node, &routing->loopback);
742         }
743 }
744
745 static void routing_dijkstra_parse_end(void) {
746   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
747   int nb_link = 0;
748   xbt_dict_cursor_t cursor = NULL;
749   char *key, *data, *end;
750   const char *sep = "#";
751   xbt_dynar_t links, keys;
752
753   /* Create the topology graph */
754   routing->route_graph = xbt_graph_new_graph(1, NULL);
755   routing->graph_node_map = xbt_dict_new();
756   routing->last_route = xbt_dynar_new(routing->size_of_link, NULL);
757   if(routing->cached)
758     routing->route_cache = xbt_dict_new();
759
760
761   /* Put the routes in position */
762   xbt_dict_foreach(route_table, cursor, key, data) {
763     nb_link = 0;
764     links = (xbt_dynar_t) data;
765     keys = xbt_str_split_str(key, sep);
766
767     src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
768     dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
769     xbt_dynar_free(&keys);
770
771     DEBUG4("Handle %d %d (from %d hosts): %ld links",
772         src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
773
774     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);
775
776     char* link_name = xbt_dynar_getfirst_as(links, char*);
777     void* link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
778     if (link)
779       route_new_dijkstra(src_id,dst_id,link);
780     else
781       THROW1(mismatch_error,0,"Link %s not found", link_name);
782     
783   }
784
785   /* Add the loopback if needed */
786   add_loopback_dijkstra();
787
788   /* initialize graph indexes in nodes after graph has been built */
789   xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
790
791   xbt_node_t node = NULL;
792   unsigned int cursor2;
793   xbt_dynar_foreach(nodes, cursor2, node) {
794     graph_node_data_t data = xbt_graph_node_get_data(node);
795     data->graph_id = cursor2;
796   }
797
798 }
799
800 /*
801  * Business methods
802  */
803 static xbt_dynar_t routing_dijkstra_get_route(int src_id,int dst_id) {
804
805   routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
806   int * pred_arr = NULL;
807   
808   xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
809
810   /*Use the graph_node id mapping set to quickly find the nodes */
811   graph_node_map_element_t src_elm = graph_node_map_search(src_id);
812   graph_node_map_element_t dst_elm = graph_node_map_search(dst_id);
813   xbt_assert2(src_elm != NULL && dst_elm != NULL, "src %d or dst %d does not exist", src_id, dst_id);
814   int src_node_id = ((graph_node_data_t)xbt_graph_node_get_data(src_elm->node))->graph_id;
815   int dst_node_id = ((graph_node_data_t)xbt_graph_node_get_data(dst_elm->node))->graph_id;
816
817   route_cache_element_t elm = NULL;
818   if(routing->cached) {
819     /*check if there is a cached predecessor list avail */
820     elm = (route_cache_element_t)xbt_dict_get_or_null_ext(routing->route_cache, (char*)(&src_id), sizeof(int));
821   }
822
823   if(elm) { //cached mode and cache hit
824     pred_arr = elm->pred_arr;
825   } else { //not cached mode or cache miss
826     double * cost_arr = NULL;
827     xbt_heap_t pqueue = NULL;
828     int i = 0;
829
830     int nr_nodes = xbt_dynar_length(nodes);
831     cost_arr = xbt_new0(double, nr_nodes); //link cost from src to other hosts
832     pred_arr = xbt_new0(int, nr_nodes); //predecessors in path from src
833     pqueue = xbt_heap_new(nr_nodes, free);
834
835     //initialize
836     cost_arr[src_node_id] = 0.0;
837
838     for(i = 0; i < nr_nodes; i++) {
839       if(i != src_node_id) {
840         cost_arr[i] = DBL_MAX;
841       }
842
843       pred_arr[i] = 0;
844
845       //initialize priority queue
846       int * nodeid = xbt_new0(int, 1);
847       *nodeid = i;
848       xbt_heap_push(pqueue, nodeid, cost_arr[i]);
849
850     }
851
852     // apply dijkstra using the indexes from the graph's node array
853     while(xbt_heap_size(pqueue) > 0) {
854       int * v_id = xbt_heap_pop(pqueue);
855       xbt_node_t v_node = xbt_dynar_get_as(nodes, *v_id, xbt_node_t);
856       xbt_dynar_t out_edges = xbt_graph_node_get_outedges(v_node); 
857       xbt_edge_t edge = NULL;
858       unsigned int cursor;
859
860       xbt_dynar_foreach(out_edges, cursor, edge) {
861         xbt_node_t u_node = xbt_graph_edge_get_target(edge);
862         graph_node_data_t data = xbt_graph_node_get_data(u_node);
863         int u_id = data->graph_id;
864         int cost_v_u = 1; //fixed link cost for now
865
866         if(cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
867           pred_arr[u_id] = *v_id;
868           cost_arr[u_id] = cost_v_u + cost_arr[*v_id];
869           int * nodeid = xbt_new0(int, 1);
870           *nodeid = u_id;
871           xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
872         }
873       }
874
875       //free item popped from pqueue
876       free(v_id);
877     }
878
879     free(cost_arr);
880     xbt_heap_free(pqueue);
881
882   }
883
884   //compose route path with links
885   xbt_dynar_reset(routing->last_route);
886
887   int v;
888   int size = 0;
889   for(v = dst_node_id; v != src_node_id; v = pred_arr[v]) {
890     xbt_node_t node_pred_v = xbt_dynar_get_as(nodes, pred_arr[v], xbt_node_t);
891     xbt_node_t node_v = xbt_dynar_get_as(nodes, v, xbt_node_t);
892     xbt_edge_t edge = xbt_graph_get_edge(routing->route_graph, node_pred_v, node_v);
893
894     xbt_assert2(edge != NULL, "no route between host %d and %d", src_id, dst_id);
895
896     void * link = xbt_graph_edge_get_data(edge);
897     xbt_dynar_unshift(routing->last_route, &link);
898     size++;
899   }
900
901
902   if(routing->cached && elm == NULL) {
903     //add to predecessor list of the current src-host to cache
904     elm = xbt_new0(struct route_cache_element, sizeof(struct route_cache_element));
905     elm->pred_arr = pred_arr;
906     elm->size = size;
907     xbt_dict_set_ext(routing->route_cache, (char*)(&src_id), sizeof(int), (xbt_set_elm_t)elm, &route_cache_elem_free);
908   }
909
910   if(!routing->cached)
911     free(pred_arr);
912
913   return routing->last_route;
914 }
915
916
917 static void routing_dijkstra_finalize(void) {
918   routing_dijkstra_t routing = (routing_dijkstra_t)used_routing;
919
920   if (routing) {
921     xbt_graph_free_graph(routing->route_graph, &free, NULL, &free);
922     xbt_dict_free(&routing->graph_node_map);
923     if(routing->cached)
924       xbt_dict_free(&routing->route_cache);
925     xbt_dynar_free(&routing->last_route);
926     xbt_dict_free(&used_routing->host_id);
927     free(routing);
928     routing=NULL;
929   }
930 }
931
932 static xbt_dict_t routing_dijkstraboth_get_onelink_routes(void){
933   xbt_assert0(0,"The get_onelink_routes feature is not supported in routing model dijkstraboth");
934 }
935
936 static int routing_dijkstraboth_is_router(int id){
937   xbt_assert0(0,"The get_is_router feature is not supported in routing model dijkstraboth");
938 }
939
940 /*
941  *
942  */
943 static void routing_model_dijkstraboth_create(size_t size_of_link,void *loopback, int cached) {
944   /* initialize our structure */
945   routing_dijkstra_t routing = xbt_new0(s_routing_dijkstra_t,1);
946   routing->generic_routing.name = "Dijkstra";
947   routing->generic_routing.host_count = 0;
948   routing->generic_routing.get_route = routing_dijkstra_get_route;
949   routing->generic_routing.get_onelink_routes = routing_dijkstraboth_get_onelink_routes;
950   routing->generic_routing.is_router = routing_dijkstraboth_is_router;
951   routing->generic_routing.finalize = routing_dijkstra_finalize;
952   routing->size_of_link = size_of_link;
953   routing->loopback = loopback;
954   routing->cached = cached;
955
956   /* Set it in position */
957   used_routing = (routing_t) routing;
958
959   /* Setup the parsing callbacks we need */
960   routing->generic_routing.host_id = xbt_dict_new();
961   surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
962   surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_dijkstra_parse_end);
963   surfxml_add_callback(STag_surfxml_route_cb_list,
964       &routing_full_parse_Sroute_set_endpoints);
965   surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);
966   surfxml_add_callback(STag_surfxml_cluster_cb_list, &routing_shortest_path_parse_Scluster);
967 }
968
969 static void routing_model_dijkstra_create(size_t size_of_link,void *loopback) {
970   routing_model_dijkstraboth_create(size_of_link, loopback, 0);
971 }
972
973 static void routing_model_dijkstracache_create(size_t size_of_link,void *loopback) {
974   routing_model_dijkstraboth_create(size_of_link, loopback, 1);
975 }
976
977 /* ************************************************** */
978 /* ********** NO ROUTING **************************** */
979
980
981 static void routing_none_finalize(void) {
982   if (used_routing) {
983     xbt_dict_free(&used_routing->host_id);
984     free(used_routing);
985     used_routing=NULL;
986   }
987 }
988
989 static void routing_model_none_create(size_t size_of_link,void *loopback) {
990   routing_t routing = xbt_new0(s_routing_t,1);
991   INFO0("Null routing");
992   routing->name = "none";
993   routing->host_count = 0;
994   routing->host_id = xbt_dict_new();
995   routing->get_onelink_routes = NULL;
996   routing->is_router = NULL;
997   routing->get_route = NULL;
998
999   routing->finalize = routing_none_finalize;
1000
1001   /* Set it in position */
1002   used_routing = (routing_t) routing;
1003 }