Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
442c965cfcb069bede47a7097322998b337be651
[simgrid.git] / src / surf / surf_routing_dijkstra.c
1 /* Copyright (c) 2009, 2010, 2011. 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 "surf_routing_private.h"
8
9 /* Global vars */
10 extern routing_global_t global_routing;
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_dijkstra, surf, "Routing part of surf -- dijkstra routing logic");
13
14 typedef struct {
15   s_as_t generic_routing;
16   xbt_graph_t route_graph;      /* xbt_graph */
17   xbt_dict_t graph_node_map;    /* map */
18   xbt_dict_t route_cache;       /* use in cache mode */
19   int cached;
20 } s_as_dijkstra_t, *as_dijkstra_t;
21
22
23 typedef struct graph_node_data {
24   int id;
25   int graph_id;                 /* used for caching internal graph id's */
26 } s_graph_node_data_t, *graph_node_data_t;
27
28 typedef struct graph_node_map_element {
29   xbt_node_t node;
30 } s_graph_node_map_element_t, *graph_node_map_element_t;
31
32 typedef struct route_cache_element {
33   int *pred_arr;
34   int size;
35 } s_route_cache_element_t, *route_cache_element_t;
36
37 /* Free functions */
38
39 static void route_cache_elem_free(void *e)
40 {
41   route_cache_element_t elm = (route_cache_element_t) e;
42   if (elm) {
43     xbt_free(elm->pred_arr);
44     xbt_free(elm);
45   }
46 }
47
48 static void graph_node_map_elem_free(void *e)
49 {
50   graph_node_map_element_t elm = (graph_node_map_element_t) e;
51   xbt_free(elm);
52 }
53
54 static void graph_edge_data_free(void *e)
55 {
56   route_extended_t e_route = (route_extended_t) e;
57   if (e_route) {
58     xbt_dynar_free(&(e_route->generic_route.link_list));
59     xbt_free(e_route->src_gateway);
60     xbt_free(e_route->dst_gateway);
61     xbt_free(e_route);
62   }
63 }
64
65 /* Utility functions */
66
67 static xbt_node_t route_graph_new_node(as_dijkstra_t as,
68                                        int id, int graph_id)
69 {
70   xbt_node_t node = NULL;
71   graph_node_data_t data = NULL;
72   graph_node_map_element_t elm = NULL;
73
74   data = xbt_new0(struct graph_node_data, 1);
75   data->id = id;
76   data->graph_id = graph_id;
77   node = xbt_graph_new_node(as->route_graph, data);
78
79   elm = xbt_new0(struct graph_node_map_element, 1);
80   elm->node = node;
81   xbt_dict_set_ext(as->graph_node_map, (char *) (&id), sizeof(int),
82                    (xbt_set_elm_t) elm, &graph_node_map_elem_free);
83
84   return node;
85 }
86
87 static graph_node_map_element_t
88 graph_node_map_search(as_dijkstra_t as, int id)
89 {
90   graph_node_map_element_t elm = (graph_node_map_element_t)
91       xbt_dict_get_or_null_ext(as->graph_node_map,
92                                (char *) (&id),
93                                sizeof(int));
94   return elm;
95 }
96
97 /* Parsing */
98
99 static void route_new_dijkstra(as_dijkstra_t as, int src_id,
100                                int dst_id, route_extended_t e_route)
101 {
102   XBT_DEBUG("Load Route from \"%d\" to \"%d\"", src_id, dst_id);
103   xbt_node_t src = NULL;
104   xbt_node_t dst = NULL;
105
106   graph_node_map_element_t src_elm = (graph_node_map_element_t)
107       xbt_dict_get_or_null_ext(as->graph_node_map,
108                                (char *) (&src_id),
109                                sizeof(int));
110   graph_node_map_element_t dst_elm = (graph_node_map_element_t)
111       xbt_dict_get_or_null_ext(as->graph_node_map,
112                                (char *) (&dst_id),
113                                sizeof(int));
114
115
116   if (src_elm)
117     src = src_elm->node;
118
119   if (dst_elm)
120     dst = dst_elm->node;
121
122   /* add nodes if they don't exist in the graph */
123   if (src_id == dst_id && src == NULL && dst == NULL) {
124     src = route_graph_new_node(as, src_id, -1);
125     dst = src;
126   } else {
127     if (src == NULL) {
128       src = route_graph_new_node(as, src_id, -1);
129     }
130     if (dst == NULL) {
131       dst = route_graph_new_node(as, dst_id, -1);
132     }
133   }
134
135   /* add link as edge to graph */
136   xbt_graph_new_edge(as->route_graph, src, dst, e_route);
137 }
138
139 static void add_loopback_dijkstra(as_dijkstra_t as) {
140   xbt_dynar_t nodes = xbt_graph_get_nodes(as->route_graph);
141
142   xbt_node_t node = NULL;
143   unsigned int cursor2;
144   xbt_dynar_foreach(nodes, cursor2, node) {
145     xbt_dynar_t out_edges = xbt_graph_node_get_outedges(node);
146     xbt_edge_t edge = NULL;
147     unsigned int cursor;
148
149     int found = 0;
150     xbt_dynar_foreach(out_edges, cursor, edge) {
151       xbt_node_t other_node = xbt_graph_edge_get_target(edge);
152       if (other_node == node) {
153         found = 1;
154         break;
155       }
156     }
157
158     if (!found) {
159       route_extended_t e_route = xbt_new0(s_route_extended_t, 1);
160       e_route->src_gateway = NULL;
161       e_route->dst_gateway = NULL;
162       e_route->generic_route.link_list =
163           xbt_dynar_new(global_routing->size_of_link, NULL);
164       xbt_dynar_push(e_route->generic_route.link_list,
165                      &global_routing->loopback);
166       xbt_graph_new_edge(as->route_graph, node, node, e_route);
167     }
168   }
169 }
170
171 static route_extended_t dijkstra_get_route(AS_t as_generic,
172         const char *src,
173         const char *dst);
174
175 static xbt_dynar_t dijkstra_get_onelink_routes(AS_t as)
176 {
177  // xbt_die("\"dijkstra_get_onelink_routes\" function not implemented yet");
178           xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
179
180           //size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
181           xbt_dict_cursor_t c1 = NULL, c2 = NULL;
182           char *k1, *d1, *k2, *d2;
183           xbt_dict_foreach(as->to_index, c1, k1, d1) {
184             xbt_dict_foreach(as->to_index, c2, k2, d2) {
185               route_extended_t route = dijkstra_get_route(as, k1, k2);
186               if (route) {
187                 if (xbt_dynar_length(route->generic_route.link_list) == 1) {
188                   void *link =
189                       *(void **) xbt_dynar_get_ptr(route->generic_route.link_list,
190                                                    0);
191                   onelink_t onelink = xbt_new0(s_onelink_t, 1);
192                   onelink->link_ptr = link;
193                   if (as->hierarchy == SURF_ROUTING_BASE) {
194                     onelink->src = xbt_strdup(k1);
195                     onelink->dst = xbt_strdup(k2);
196                   } else if (as->hierarchy ==
197                              SURF_ROUTING_RECURSIVE) {
198                     onelink->src = xbt_strdup(route->src_gateway);
199                     onelink->dst = xbt_strdup(route->dst_gateway);
200                   }
201                   xbt_dynar_push(ret, &onelink);
202                 }
203               }
204             }
205           }
206           return ret;
207 }
208
209 static route_extended_t dijkstra_get_route(AS_t asg,
210                                            const char *src,
211                                            const char *dst)
212 {
213   xbt_assert(asg && src
214               && dst,
215               "Invalid params for \"get_route\" function at AS \"%s\"",
216               asg->name);
217
218   /* set utils vars */
219   as_dijkstra_t as = (as_dijkstra_t) asg;
220
221   generic_src_dst_check(asg, src, dst);
222   int *src_id = xbt_dict_get_or_null(asg->to_index, src);
223   int *dst_id = xbt_dict_get_or_null(asg->to_index, dst);
224   xbt_assert(src_id
225               && dst_id,
226               "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",
227               src, dst);
228
229   /* create a result route */
230   route_extended_t new_e_route = xbt_new0(s_route_extended_t, 1);
231   new_e_route->generic_route.link_list =
232       xbt_dynar_new(global_routing->size_of_link, NULL);
233   new_e_route->src_gateway = NULL;
234   new_e_route->dst_gateway = NULL;
235
236   int *pred_arr = NULL;
237   int src_node_id = 0;
238   int dst_node_id = 0;
239   int *nodeid = NULL;
240   int v;
241   route_extended_t e_route;
242   int size = 0;
243   unsigned int cpt;
244   void *link;
245   xbt_dynar_t links = NULL;
246   route_cache_element_t elm = NULL;
247   xbt_dynar_t nodes = xbt_graph_get_nodes(as->route_graph);
248
249   /* Use the graph_node id mapping set to quickly find the nodes */
250   graph_node_map_element_t src_elm =
251       graph_node_map_search(as, *src_id);
252   graph_node_map_element_t dst_elm =
253       graph_node_map_search(as, *dst_id);
254   xbt_assert(src_elm != NULL
255               && dst_elm != NULL, "src %d or dst %d does not exist",
256               *src_id, *dst_id);
257   src_node_id = ((graph_node_data_t)
258                  xbt_graph_node_get_data(src_elm->node))->graph_id;
259   dst_node_id = ((graph_node_data_t)
260                  xbt_graph_node_get_data(dst_elm->node))->graph_id;
261
262   /* if the src and dst are the same *//* fixed, missing in the previous version */
263   if (src_node_id == dst_node_id) {
264
265     xbt_node_t node_s_v = xbt_dynar_get_as(nodes, src_node_id, xbt_node_t);
266     xbt_node_t node_e_v = xbt_dynar_get_as(nodes, dst_node_id, xbt_node_t);
267     xbt_edge_t edge =
268         xbt_graph_get_edge(as->route_graph, node_s_v, node_e_v);
269
270     xbt_assert(edge != NULL, "no route between host %d and %d", *src_id,
271                 *dst_id);
272
273     e_route = (route_extended_t) xbt_graph_edge_get_data(edge);
274
275     links = e_route->generic_route.link_list;
276     xbt_dynar_foreach(links, cpt, link) {
277       xbt_dynar_unshift(new_e_route->generic_route.link_list, &link);
278     }
279
280     return new_e_route;
281   }
282
283   if (as->cached) {
284     /*check if there is a cached predecessor list avail */
285     elm = (route_cache_element_t)
286         xbt_dict_get_or_null_ext(as->route_cache, (char *) (&src_id),
287                                  sizeof(int));
288   }
289
290   if (elm) {                    /* cached mode and cache hit */
291     pred_arr = elm->pred_arr;
292   } else {                      /* not cached mode or cache miss */
293     double *cost_arr = NULL;
294     xbt_heap_t pqueue = NULL;
295     int i = 0;
296
297     int nr_nodes = xbt_dynar_length(nodes);
298     cost_arr = xbt_new0(double, nr_nodes);      /* link cost from src to other hosts */
299     pred_arr = xbt_new0(int, nr_nodes); /* predecessors in path from src */
300     pqueue = xbt_heap_new(nr_nodes, xbt_free);
301
302     /* initialize */
303     cost_arr[src_node_id] = 0.0;
304
305     for (i = 0; i < nr_nodes; i++) {
306       if (i != src_node_id) {
307         cost_arr[i] = DBL_MAX;
308       }
309
310       pred_arr[i] = 0;
311
312       /* initialize priority queue */
313       nodeid = xbt_new0(int, 1);
314       *nodeid = i;
315       xbt_heap_push(pqueue, nodeid, cost_arr[i]);
316
317     }
318
319     /* apply dijkstra using the indexes from the graph's node array */
320     while (xbt_heap_size(pqueue) > 0) {
321       int *v_id = xbt_heap_pop(pqueue);
322       xbt_node_t v_node = xbt_dynar_get_as(nodes, *v_id, xbt_node_t);
323       xbt_dynar_t out_edges = xbt_graph_node_get_outedges(v_node);
324       xbt_edge_t edge = NULL;
325       unsigned int cursor;
326
327       xbt_dynar_foreach(out_edges, cursor, edge) {
328         xbt_node_t u_node = xbt_graph_edge_get_target(edge);
329         graph_node_data_t data = xbt_graph_node_get_data(u_node);
330         int u_id = data->graph_id;
331         route_extended_t tmp_e_route =
332             (route_extended_t) xbt_graph_edge_get_data(edge);
333         int cost_v_u = (tmp_e_route->generic_route.link_list)->used;    /* count of links, old model assume 1 */
334
335         if (cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
336           pred_arr[u_id] = *v_id;
337           cost_arr[u_id] = cost_v_u + cost_arr[*v_id];
338           nodeid = xbt_new0(int, 1);
339           *nodeid = u_id;
340           xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
341         }
342       }
343
344       /* free item popped from pqueue */
345       xbt_free(v_id);
346     }
347
348     xbt_free(cost_arr);
349     xbt_heap_free(pqueue);
350   }
351
352   /* compose route path with links */
353   char *gw_src = NULL, *gw_dst = NULL, *prev_gw_src, *first_gw = NULL;
354
355   for (v = dst_node_id; v != src_node_id; v = pred_arr[v]) {
356     xbt_node_t node_pred_v =
357         xbt_dynar_get_as(nodes, pred_arr[v], xbt_node_t);
358     xbt_node_t node_v = xbt_dynar_get_as(nodes, v, xbt_node_t);
359     xbt_edge_t edge =
360         xbt_graph_get_edge(as->route_graph, node_pred_v, node_v);
361
362     xbt_assert(edge != NULL, "no route between host %d and %d", *src_id,
363                 *dst_id);
364
365     prev_gw_src = gw_src;
366
367     e_route = (route_extended_t) xbt_graph_edge_get_data(edge);
368     gw_src = e_route->src_gateway;
369     gw_dst = e_route->dst_gateway;
370
371     if (v == dst_node_id)
372       first_gw = gw_dst;
373
374     if (asg->hierarchy == SURF_ROUTING_RECURSIVE && v != dst_node_id
375         && strcmp(gw_dst, prev_gw_src)) {
376       xbt_dynar_t e_route_as_to_as =
377           (*(global_routing->get_route)) (gw_dst, prev_gw_src);
378       xbt_assert(e_route_as_to_as, "no route between \"%s\" and \"%s\"",
379                   gw_dst, prev_gw_src);
380       links = e_route_as_to_as;
381       int pos = 0;
382       xbt_dynar_foreach(links, cpt, link) {
383         xbt_dynar_insert_at(new_e_route->generic_route.link_list, pos,
384                             &link);
385         pos++;
386       }
387     }
388
389     links = e_route->generic_route.link_list;
390     xbt_dynar_foreach(links, cpt, link) {
391       xbt_dynar_unshift(new_e_route->generic_route.link_list, &link);
392     }
393     size++;
394   }
395
396   if (asg->hierarchy == SURF_ROUTING_RECURSIVE) {
397     new_e_route->src_gateway = xbt_strdup(gw_src);
398     new_e_route->dst_gateway = xbt_strdup(first_gw);
399   }
400
401   if (as->cached && elm == NULL) {
402     /* add to predecessor list of the current src-host to cache */
403     elm = xbt_new0(struct route_cache_element, 1);
404     elm->pred_arr = pred_arr;
405     elm->size = size;
406     xbt_dict_set_ext(as->route_cache, (char *) (&src_id), sizeof(int),
407                      (xbt_set_elm_t) elm, &route_cache_elem_free);
408   }
409
410   if (!as->cached)
411     xbt_free(pred_arr);
412
413   return new_e_route;
414 }
415
416 static void dijkstra_finalize(AS_t asg)
417 {
418   as_dijkstra_t as = (as_dijkstra_t) asg;
419
420   if (as) {
421     xbt_graph_free_graph(as->route_graph, &xbt_free,
422                          &graph_edge_data_free, &xbt_free);
423     xbt_dict_free(&as->graph_node_map);
424     if (as->cached)
425       xbt_dict_free(&as->route_cache);
426     /* Delete bypass dict */
427     xbt_dict_free(&as->generic_routing.bypassRoutes);
428     /* Delete index dict */
429     xbt_dict_free(&(as->generic_routing.to_index));
430     /* Delete structure */
431     xbt_free(as);
432   }
433 }
434
435 /* Creation routing model functions */
436
437 AS_t model_dijkstra_both_create(int cached)
438 {
439   as_dijkstra_t new_component = (as_dijkstra_t)
440       routmod_generic_create(sizeof(s_as_dijkstra_t));
441
442   new_component->generic_routing.parse_route = model_dijkstra_both_parse_route;
443   new_component->generic_routing.parse_ASroute = model_dijkstra_both_parse_route;
444   new_component->generic_routing.get_route = dijkstra_get_route;
445   new_component->generic_routing.get_onelink_routes =
446       dijkstra_get_onelink_routes;
447   new_component->generic_routing.finalize = dijkstra_finalize;
448   new_component->cached = cached;
449
450   return (AS_t)new_component;
451 }
452
453 AS_t model_dijkstra_create(void)
454 {
455   return model_dijkstra_both_create(0);
456 }
457
458 AS_t model_dijkstracache_create(void)
459 {
460   return model_dijkstra_both_create(1);
461 }
462
463 void model_dijkstra_both_end(AS_t as)
464 {
465   as_dijkstra_t THIS = (as_dijkstra_t) as;
466
467   xbt_node_t node = NULL;
468   unsigned int cursor2;
469   xbt_dynar_t nodes = NULL;
470
471   /* Create the topology graph */
472   if(!THIS->route_graph)
473   THIS->route_graph = xbt_graph_new_graph(1, NULL);
474   if(!THIS->graph_node_map)
475   THIS->graph_node_map = xbt_dict_new();
476
477   if (THIS->cached && !THIS->route_cache)
478   THIS->route_cache = xbt_dict_new();
479
480   /* Add the loopback if needed */
481   if (as->hierarchy == SURF_ROUTING_BASE)
482     add_loopback_dijkstra(THIS);
483
484   /* initialize graph indexes in nodes after graph has been built */
485   nodes = xbt_graph_get_nodes(THIS->route_graph);
486
487   xbt_dynar_foreach(nodes, cursor2, node) {
488     graph_node_data_t data = xbt_graph_node_get_data(node);
489     data->graph_id = cursor2;
490   }
491
492 }
493 void model_dijkstra_both_parse_route (AS_t asg, const char *src,
494                      const char *dst, route_extended_t route)
495 {
496         as_dijkstra_t as = (as_dijkstra_t) asg;
497         int *src_id, *dst_id;
498         src_id = xbt_dict_get_or_null(asg->to_index, src);
499         dst_id = xbt_dict_get_or_null(asg->to_index, dst);
500
501         xbt_assert(src_id, "Network elements %s not found", src);
502         xbt_assert(dst_id, "Network elements %s not found", dst);
503
504     /* Create the topology graph */
505         if(!as->route_graph)
506         as->route_graph = xbt_graph_new_graph(1, NULL);
507         if(!as->graph_node_map)
508         as->graph_node_map = xbt_dict_new();
509
510         if (as->cached && !as->route_cache)
511         as->route_cache = xbt_dict_new();
512
513         if( A_surfxml_route_symmetrical == A_surfxml_route_symmetrical_YES
514                 || A_surfxml_ASroute_symmetrical == A_surfxml_ASroute_symmetrical_YES )
515                 xbt_die("Route symmetrical not supported on model dijkstra");
516
517         if(!route->dst_gateway && !route->src_gateway)
518           XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
519         else{
520           XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
521                          route->src_gateway, dst, route->dst_gateway);
522           if(global_routing->get_network_element_type((const char*)route->dst_gateway) == SURF_NETWORK_ELEMENT_NULL)
523                   xbt_die("The dst_gateway '%s' does not exist!",route->dst_gateway);
524           if(global_routing->get_network_element_type((const char*)route->src_gateway) == SURF_NETWORK_ELEMENT_NULL)
525                   xbt_die("The src_gateway '%s' does not exist!",route->src_gateway);
526         }
527
528         route_extended_t e_route =
529                 generic_new_extended_route(asg->hierarchy, route, 1);
530         route_new_dijkstra(as, *src_id, *dst_id, e_route);
531 }