Logo AND Algorithmique Numérique Distribuée

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