Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2c65d60bbe8e42e05f24352fc295ce262e8cc6f8
[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) // FIXME: useless code dupplication
55 {
56   route_t e_route = (route_t) e;
57   if (e_route) {
58     xbt_dynar_free(&(e_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_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_t e_route = xbt_new0(s_route_t, 1);
160       e_route->link_list = xbt_dynar_new(global_routing->size_of_link, NULL);
161       xbt_dynar_push(e_route->link_list,
162                      &global_routing->loopback);
163       xbt_graph_new_edge(as->route_graph, node, node, e_route);
164     }
165   }
166 }
167
168 static void dijkstra_get_route(AS_t as_generic,
169         const char *src, const char *dst, route_t route);
170
171 static xbt_dynar_t dijkstra_get_onelink_routes(AS_t as)
172 {
173  // xbt_die("\"dijkstra_get_onelink_routes\" function not implemented yet");
174           xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
175
176           //size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
177           xbt_dict_cursor_t c1 = NULL, c2 = NULL;
178           char *k1, *d1, *k2, *d2;
179           xbt_dict_foreach(as->to_index, c1, k1, d1) {
180             xbt_dict_foreach(as->to_index, c2, k2, d2) {
181               route_t route = xbt_new0(s_route_t,1);
182               route->link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
183               dijkstra_get_route(as, k1, k2,route);
184
185               if (xbt_dynar_length(route->link_list) == 1) {
186                 void *link =
187                     *(void **) xbt_dynar_get_ptr(route->link_list, 0);
188                 onelink_t onelink = xbt_new0(s_onelink_t, 1);
189                 onelink->link_ptr = link;
190                 if (as->hierarchy == SURF_ROUTING_BASE) {
191                   onelink->src = xbt_strdup(k1);
192                   onelink->dst = xbt_strdup(k2);
193                 } else if (as->hierarchy ==
194                     SURF_ROUTING_RECURSIVE) {
195                   onelink->src = xbt_strdup(route->src_gateway);
196                   onelink->dst = xbt_strdup(route->dst_gateway);
197                 }
198                 xbt_dynar_push(ret, &onelink);
199               }
200             }
201           }
202           return ret;
203 }
204
205 static void dijkstra_get_route(AS_t asg,
206                                const char *src, const char *dst,
207                                route_t route)
208 {
209   xbt_assert(asg && src
210               && dst,
211               "Invalid params for \"get_route\" function at AS \"%s\"",
212               asg->name);
213
214   /* set utils vars */
215   as_dijkstra_t as = (as_dijkstra_t) asg;
216
217   generic_src_dst_check(asg, src, dst);
218   int *src_id = xbt_dict_get_or_null(asg->to_index, src);
219   int *dst_id = xbt_dict_get_or_null(asg->to_index, dst);
220   xbt_assert(src_id
221               && dst_id,
222               "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",
223               src, dst);
224
225   int *pred_arr = NULL;
226   int src_node_id = 0;
227   int dst_node_id = 0;
228   int *nodeid = NULL;
229   int v;
230   route_t e_route;
231   int size = 0;
232   unsigned int cpt;
233   void *link;
234   xbt_dynar_t links = NULL;
235   route_cache_element_t elm = NULL;
236   xbt_dynar_t nodes = xbt_graph_get_nodes(as->route_graph);
237
238   /* Use the graph_node id mapping set to quickly find the nodes */
239   graph_node_map_element_t src_elm =
240       graph_node_map_search(as, *src_id);
241   graph_node_map_element_t dst_elm =
242       graph_node_map_search(as, *dst_id);
243   xbt_assert(src_elm != NULL
244               && dst_elm != NULL, "src %d or dst %d does not exist",
245               *src_id, *dst_id);
246   src_node_id = ((graph_node_data_t)
247                  xbt_graph_node_get_data(src_elm->node))->graph_id;
248   dst_node_id = ((graph_node_data_t)
249                  xbt_graph_node_get_data(dst_elm->node))->graph_id;
250
251   /* if the src and dst are the same *//* fixed, missing in the previous version */
252   if (src_node_id == dst_node_id) {
253
254     xbt_node_t node_s_v = xbt_dynar_get_as(nodes, src_node_id, xbt_node_t);
255     xbt_node_t node_e_v = xbt_dynar_get_as(nodes, dst_node_id, xbt_node_t);
256     xbt_edge_t edge =
257         xbt_graph_get_edge(as->route_graph, node_s_v, node_e_v);
258
259     xbt_assert(edge != NULL, "no route between host %d and %d", *src_id,
260                 *dst_id);
261
262     e_route = (route_t) xbt_graph_edge_get_data(edge);
263
264     links = e_route->link_list;
265     xbt_dynar_foreach(links, cpt, link) {
266       xbt_dynar_unshift(route->link_list, &link);
267     }
268
269   }
270
271   if (as->cached) {
272     /*check if there is a cached predecessor list avail */
273     elm = (route_cache_element_t)
274         xbt_dict_get_or_null_ext(as->route_cache, (char *) (&src_id),
275                                  sizeof(int));
276   }
277
278   if (elm) {                    /* cached mode and cache hit */
279     pred_arr = elm->pred_arr;
280   } else {                      /* not cached mode or cache miss */
281     double *cost_arr = NULL;
282     xbt_heap_t pqueue = NULL;
283     int i = 0;
284
285     int nr_nodes = xbt_dynar_length(nodes);
286     cost_arr = xbt_new0(double, nr_nodes);      /* link cost from src to other hosts */
287     pred_arr = xbt_new0(int, nr_nodes); /* predecessors in path from src */
288     pqueue = xbt_heap_new(nr_nodes, xbt_free);
289
290     /* initialize */
291     cost_arr[src_node_id] = 0.0;
292
293     for (i = 0; i < nr_nodes; i++) {
294       if (i != src_node_id) {
295         cost_arr[i] = DBL_MAX;
296       }
297
298       pred_arr[i] = 0;
299
300       /* initialize priority queue */
301       nodeid = xbt_new0(int, 1);
302       *nodeid = i;
303       xbt_heap_push(pqueue, nodeid, cost_arr[i]);
304
305     }
306
307     /* apply dijkstra using the indexes from the graph's node array */
308     while (xbt_heap_size(pqueue) > 0) {
309       int *v_id = xbt_heap_pop(pqueue);
310       xbt_node_t v_node = xbt_dynar_get_as(nodes, *v_id, xbt_node_t);
311       xbt_dynar_t out_edges = xbt_graph_node_get_outedges(v_node);
312       xbt_edge_t edge = NULL;
313       unsigned int cursor;
314
315       xbt_dynar_foreach(out_edges, cursor, edge) {
316         xbt_node_t u_node = xbt_graph_edge_get_target(edge);
317         graph_node_data_t data = xbt_graph_node_get_data(u_node);
318         int u_id = data->graph_id;
319         route_t tmp_e_route = (route_t) xbt_graph_edge_get_data(edge);
320         int cost_v_u = (tmp_e_route->link_list)->used;    /* count of links, old model assume 1 */
321
322         if (cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
323           pred_arr[u_id] = *v_id;
324           cost_arr[u_id] = cost_v_u + cost_arr[*v_id];
325           nodeid = xbt_new0(int, 1);
326           *nodeid = u_id;
327           xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
328         }
329       }
330
331       /* free item popped from pqueue */
332       xbt_free(v_id);
333     }
334
335     xbt_free(cost_arr);
336     xbt_heap_free(pqueue);
337   }
338
339   /* compose route path with links */
340   char *gw_src = NULL, *gw_dst = NULL, *prev_gw_src, *first_gw = NULL;
341
342   for (v = dst_node_id; v != src_node_id; v = pred_arr[v]) {
343     xbt_node_t node_pred_v =
344         xbt_dynar_get_as(nodes, pred_arr[v], xbt_node_t);
345     xbt_node_t node_v = xbt_dynar_get_as(nodes, v, xbt_node_t);
346     xbt_edge_t edge =
347         xbt_graph_get_edge(as->route_graph, node_pred_v, node_v);
348
349     xbt_assert(edge != NULL, "no route between host %d and %d", *src_id,
350                 *dst_id);
351
352     prev_gw_src = gw_src;
353
354     e_route = (route_t) xbt_graph_edge_get_data(edge);
355     gw_src = e_route->src_gateway;
356     gw_dst = e_route->dst_gateway;
357
358     if (v == dst_node_id)
359       first_gw = gw_dst;
360
361     if (asg->hierarchy == SURF_ROUTING_RECURSIVE && v != dst_node_id
362         && strcmp(gw_dst, prev_gw_src)) {
363       xbt_dynar_t e_route_as_to_as=NULL;
364       routing_get_route_and_latency(gw_dst, prev_gw_src,&e_route_as_to_as,NULL);
365       xbt_assert(e_route_as_to_as, "no route between \"%s\" and \"%s\"",
366                   gw_dst, prev_gw_src);
367       links = e_route_as_to_as;
368       int pos = 0;
369       xbt_dynar_foreach(links, cpt, link) {
370         xbt_dynar_insert_at(route->link_list, pos, &link);
371         pos++;
372       }
373     }
374
375     links = e_route->link_list;
376     xbt_dynar_foreach(links, cpt, link) {
377       xbt_dynar_unshift(route->link_list, &link);
378     }
379     size++;
380   }
381
382   if (asg->hierarchy == SURF_ROUTING_RECURSIVE) {
383     route->src_gateway = xbt_strdup(gw_src);
384     route->dst_gateway = xbt_strdup(first_gw);
385   }
386
387   if (as->cached && elm == NULL) {
388     /* add to predecessor list of the current src-host to cache */
389     elm = xbt_new0(struct route_cache_element, 1);
390     elm->pred_arr = pred_arr;
391     elm->size = size;
392     xbt_dict_set_ext(as->route_cache, (char *) (&src_id), sizeof(int),
393                      (xbt_set_elm_t) elm, &route_cache_elem_free);
394   }
395
396   if (!as->cached)
397     xbt_free(pred_arr);
398 }
399
400 static void dijkstra_finalize(AS_t asg)
401 {
402   as_dijkstra_t as = (as_dijkstra_t) asg;
403
404   xbt_graph_free_graph(as->route_graph, &xbt_free,
405       &graph_edge_data_free, &xbt_free);
406   xbt_dict_free(&as->graph_node_map);
407   if (as->cached)
408     xbt_dict_free(&as->route_cache);
409
410   model_generic_finalize(asg);
411 }
412
413 /* Creation routing model functions */
414
415 AS_t model_dijkstra_both_create(int cached)
416 {
417   as_dijkstra_t new_component = (as_dijkstra_t)
418       model_generic_create_sized(sizeof(s_as_dijkstra_t));
419
420   new_component->generic_routing.parse_route = model_dijkstra_both_parse_route;
421   new_component->generic_routing.parse_ASroute = model_dijkstra_both_parse_route;
422   new_component->generic_routing.get_route = dijkstra_get_route;
423   new_component->generic_routing.get_onelink_routes =
424       dijkstra_get_onelink_routes;
425   new_component->generic_routing.finalize = dijkstra_finalize;
426   new_component->cached = cached;
427
428   return (AS_t)new_component;
429 }
430
431 AS_t model_dijkstra_create(void)
432 {
433   return model_dijkstra_both_create(0);
434 }
435
436 AS_t model_dijkstracache_create(void)
437 {
438   return model_dijkstra_both_create(1);
439 }
440
441 void model_dijkstra_both_end(AS_t as)
442 {
443   as_dijkstra_t THIS = (as_dijkstra_t) as;
444
445   xbt_node_t node = NULL;
446   unsigned int cursor2;
447   xbt_dynar_t nodes = NULL;
448
449   /* Create the topology graph */
450   if(!THIS->route_graph)
451   THIS->route_graph = xbt_graph_new_graph(1, NULL);
452   if(!THIS->graph_node_map)
453   THIS->graph_node_map = xbt_dict_new();
454
455   if (THIS->cached && !THIS->route_cache)
456   THIS->route_cache = xbt_dict_new();
457
458   /* Add the loopback if needed */
459   if (as->hierarchy == SURF_ROUTING_BASE)
460     add_loopback_dijkstra(THIS);
461
462   /* initialize graph indexes in nodes after graph has been built */
463   nodes = xbt_graph_get_nodes(THIS->route_graph);
464
465   xbt_dynar_foreach(nodes, cursor2, node) {
466     graph_node_data_t data = xbt_graph_node_get_data(node);
467     data->graph_id = cursor2;
468   }
469
470 }
471 void model_dijkstra_both_parse_route (AS_t asg, const char *src,
472                      const char *dst, route_t route)
473 {
474         as_dijkstra_t as = (as_dijkstra_t) asg;
475         int *src_id, *dst_id;
476         src_id = xbt_dict_get_or_null(asg->to_index, src);
477         dst_id = xbt_dict_get_or_null(asg->to_index, dst);
478
479         xbt_assert(src_id, "Network elements %s not found", src);
480         xbt_assert(dst_id, "Network elements %s not found", dst);
481
482     /* Create the topology graph */
483         if(!as->route_graph)
484         as->route_graph = xbt_graph_new_graph(1, NULL);
485         if(!as->graph_node_map)
486         as->graph_node_map = xbt_dict_new();
487
488         if (as->cached && !as->route_cache)
489         as->route_cache = xbt_dict_new();
490
491         if( A_surfxml_route_symmetrical == A_surfxml_route_symmetrical_YES
492                 || A_surfxml_ASroute_symmetrical == A_surfxml_ASroute_symmetrical_YES )
493                 xbt_die("Route symmetrical not supported on model dijkstra");
494
495         if(!route->dst_gateway && !route->src_gateway)
496           XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
497         else{
498           XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
499                          route->src_gateway, dst, route->dst_gateway);
500           if(routing_get_network_element_type((const char*)route->dst_gateway) == SURF_NETWORK_ELEMENT_NULL)
501                   xbt_die("The dst_gateway '%s' does not exist!",route->dst_gateway);
502           if(routing_get_network_element_type((const char*)route->src_gateway) == SURF_NETWORK_ELEMENT_NULL)
503                   xbt_die("The src_gateway '%s' does not exist!",route->src_gateway);
504         }
505
506         route_t e_route = generic_new_extended_route(asg->hierarchy, route, 1);
507         route_new_dijkstra(as, *src_id, *dst_id, e_route);
508 }