Logo AND Algorithmique Numérique Distribuée

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