Logo AND Algorithmique Numérique Distribuée

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