Logo AND Algorithmique Numérique Distribuée

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