Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
insufficient cleanups in Dijkstra routing
[simgrid.git] / src / surf / surf_routing_dijkstra.cpp
1 /* Copyright (c) 2009-2015. 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 "src/surf/surf_routing_private.hpp"
8 #include "src/surf/surf_routing_dijkstra.hpp"
9 #include "src/surf/network_interface.hpp"
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_dijkstra, surf, "Routing part of surf -- dijkstra routing logic");
12
13 /* Free functions */
14
15 static void route_cache_elem_free(void *e)
16 {
17   route_cache_element_t elm = (route_cache_element_t) e;
18   if (elm) {
19     xbt_free(elm->pred_arr);
20     xbt_free(elm);
21   }
22 }
23
24 static void graph_node_map_elem_free(void *e)
25 {
26   graph_node_map_element_t elm = (graph_node_map_element_t) e;
27   xbt_free(elm);
28 }
29
30 static void graph_edge_data_free(void *e) // FIXME: useless code duplication
31 {
32   sg_platf_route_cbarg_t e_route = (sg_platf_route_cbarg_t) e;
33   if (e_route) {
34     xbt_dynar_free(&(e_route->link_list));
35     xbt_free(e_route);
36   }
37 }
38
39 /* Utility functions */
40
41 namespace simgrid {
42 namespace surf {
43 void AsDijkstra::Seal()
44 {
45   /* Create the topology graph */
46   if(!p_routeGraph)
47     p_routeGraph = xbt_graph_new_graph(1, NULL);
48   if(!p_graphNodeMap)
49     p_graphNodeMap = xbt_dict_new_homogeneous(&graph_node_map_elem_free);
50
51   if (m_cached && !p_routeCache)
52     p_routeCache = xbt_dict_new_homogeneous(&route_cache_elem_free);
53
54   /* Add the loopback if needed */
55   if (routing_platf->p_loopback && hierarchy_ == SURF_ROUTING_BASE)
56     addLoopback();
57
58   /* initialize graph indexes in nodes after graph has been built */
59   xbt_dynar_t nodes = xbt_graph_get_nodes(p_routeGraph);
60
61   xbt_node_t node = NULL;
62   unsigned int cursor2;
63   xbt_dynar_foreach(nodes, cursor2, node) {
64     graph_node_data_t data = (graph_node_data_t) xbt_graph_node_get_data(node);
65     data->graph_id = cursor2;
66   }
67 }
68
69 xbt_node_t AsDijkstra::routeGraphNewNode(int id, int graph_id)
70 {
71   xbt_node_t node = NULL;
72   graph_node_data_t data = NULL;
73   graph_node_map_element_t elm = NULL;
74
75   data = xbt_new0(struct graph_node_data, 1);
76   data->id = id;
77   data->graph_id = graph_id;
78   node = xbt_graph_new_node(p_routeGraph, data);
79
80   elm = xbt_new0(struct graph_node_map_element, 1);
81   elm->node = node;
82   xbt_dict_set_ext(p_graphNodeMap, (char *) (&id), sizeof(int),
83       (xbt_dictelm_t) elm, NULL);
84
85   return node;
86 }
87
88 graph_node_map_element_t AsDijkstra::nodeMapSearch(int id)
89 {
90   graph_node_map_element_t elm = (graph_node_map_element_t)
91           xbt_dict_get_or_null_ext(p_graphNodeMap,
92               (char *) (&id),
93               sizeof(int));
94   return elm;
95 }
96
97 /* Parsing */
98
99 void AsDijkstra::newRoute(int src_id, int dst_id, sg_platf_route_cbarg_t e_route)
100 {
101   XBT_DEBUG("Load Route from \"%d\" to \"%d\"", src_id, dst_id);
102   xbt_node_t src = NULL;
103   xbt_node_t dst = NULL;
104
105   graph_node_map_element_t src_elm = (graph_node_map_element_t)
106           xbt_dict_get_or_null_ext(p_graphNodeMap,
107               (char *) (&src_id),
108               sizeof(int));
109   graph_node_map_element_t dst_elm = (graph_node_map_element_t)
110           xbt_dict_get_or_null_ext(p_graphNodeMap,
111               (char *) (&dst_id),
112               sizeof(int));
113
114
115   if (src_elm)
116     src = src_elm->node;
117
118   if (dst_elm)
119     dst = dst_elm->node;
120
121   /* add nodes if they don't exist in the graph */
122   if (src_id == dst_id && src == NULL && dst == NULL) {
123     src = this->routeGraphNewNode(src_id, -1);
124     dst = src;
125   } else {
126     if (src == NULL) {
127       src = this->routeGraphNewNode(src_id, -1);
128     }
129     if (dst == NULL) {
130       dst = this->routeGraphNewNode(dst_id, -1);
131     }
132   }
133
134   /* add link as edge to graph */
135   xbt_graph_new_edge(p_routeGraph, src, dst, e_route);
136 }
137
138 void AsDijkstra::addLoopback() {
139   xbt_dynar_t nodes = xbt_graph_get_nodes(p_routeGraph);
140
141   xbt_node_t node = NULL;
142   unsigned int cursor2;
143   xbt_dynar_foreach(nodes, cursor2, node) {
144     xbt_dynar_t out_edges = xbt_graph_node_get_outedges(node);
145     xbt_edge_t edge = NULL;
146     unsigned int cursor;
147
148     int found = 0;
149     xbt_dynar_foreach(out_edges, cursor, edge) {
150       xbt_node_t other_node = xbt_graph_edge_get_target(edge);
151       if (other_node == node) {
152         found = 1;
153         break;
154       }
155     }
156
157     if (!found) {
158       sg_platf_route_cbarg_t e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
159       e_route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
160       xbt_dynar_push(e_route->link_list, &routing_platf->p_loopback);
161       xbt_graph_new_edge(p_routeGraph, node, node, e_route);
162     }
163   }
164 }
165
166 xbt_dynar_t AsDijkstra::getOneLinkRoutes()
167 {
168   xbt_dynar_t ret = xbt_dynar_new(sizeof(Onelink*), xbt_free_f);
169   sg_platf_route_cbarg_t route = xbt_new0(s_sg_platf_route_cbarg_t,1);
170   route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t),NULL);
171
172   int src,dst;
173   NetCard *src_elm, *dst_elm;
174   int table_size = (int)xbt_dynar_length(vertices_);
175   for(src=0; src < table_size; src++) {
176     for(dst=0; dst< table_size; dst++) {
177       xbt_dynar_reset(route->link_list);
178       src_elm = xbt_dynar_get_as(vertices_, src, NetCard*);
179       dst_elm = xbt_dynar_get_as(vertices_, dst, NetCard*);
180       this->getRouteAndLatency(src_elm, dst_elm,route, NULL);
181
182       if (xbt_dynar_length(route->link_list) == 1) {
183         void *link = *(void **) xbt_dynar_get_ptr(route->link_list, 0);
184         Onelink *onelink;
185         if (hierarchy_ == SURF_ROUTING_BASE)
186           onelink = new Onelink(link, src_elm, dst_elm);
187         else if (hierarchy_ == SURF_ROUTING_RECURSIVE)
188           onelink = new Onelink(link, route->gw_src, route->gw_dst);
189         else
190           onelink = new Onelink(link, NULL, NULL);
191         xbt_dynar_push(ret, &onelink);
192       }
193     }
194   }
195   return ret;
196 }
197
198 void AsDijkstra::getRouteAndLatency(NetCard *src, NetCard *dst, sg_platf_route_cbarg_t route, double *lat)
199 {
200
201   /* set utils vars */
202
203   srcDstCheck(src, dst);
204   int *src_id = src->getIdPtr();
205   int *dst_id = dst->getIdPtr();
206
207   if (!src_id || !dst_id)
208     THROWF(arg_error,0,"No route from '%s' to '%s'",src->name(),dst->name());
209
210   int *pred_arr = NULL;
211   sg_platf_route_cbarg_t e_route;
212   int size = 0;
213   unsigned int cpt;
214   void *link;
215   xbt_dynar_t nodes = xbt_graph_get_nodes(p_routeGraph);
216
217   /* Use the graph_node id mapping set to quickly find the nodes */
218   graph_node_map_element_t src_elm = nodeMapSearch(*src_id);
219   graph_node_map_element_t dst_elm = nodeMapSearch(*dst_id);
220
221   int src_node_id = ((graph_node_data_t) xbt_graph_node_get_data(src_elm->node))->graph_id;
222   int dst_node_id = ((graph_node_data_t) xbt_graph_node_get_data(dst_elm->node))->graph_id;
223
224   /* if the src and dst are the same */
225   if (src_node_id == dst_node_id) {
226
227     xbt_node_t node_s_v = xbt_dynar_get_as(nodes, src_node_id, xbt_node_t);
228     xbt_node_t node_e_v = xbt_dynar_get_as(nodes, dst_node_id, xbt_node_t);
229     xbt_edge_t edge = xbt_graph_get_edge(p_routeGraph, node_s_v, node_e_v);
230
231     if (edge == NULL)
232       THROWF(arg_error, 0, "No route from '%s' to '%s'", src->name(), dst->name());
233
234     e_route = (sg_platf_route_cbarg_t) xbt_graph_edge_get_data(edge);
235
236     xbt_dynar_foreach(e_route->link_list, cpt, link) {
237       xbt_dynar_unshift(route->link_list, &link);
238       if (lat)
239         *lat += static_cast<Link*>(link)->getLatency();
240     }
241
242   }
243
244   route_cache_element_t elm = NULL;
245   if (m_cached) {  /* cache mode  */
246     elm = (route_cache_element_t)
247             xbt_dict_get_or_null_ext(p_routeCache, (char *) (&src_id), sizeof(int));
248   }
249
250   if (elm) {                    /* cached mode and cache hit */
251     pred_arr = elm->pred_arr;
252   } else {                      /* not cached mode, or cache miss */
253
254     int nr_nodes = xbt_dynar_length(nodes);
255     double * cost_arr = xbt_new0(double, nr_nodes);      /* link cost from src to other hosts */
256     pred_arr = xbt_new0(int, nr_nodes); /* predecessors in path from src */
257     xbt_heap_t pqueue = xbt_heap_new(nr_nodes, xbt_free_f);
258
259     /* initialize */
260     cost_arr[src_node_id] = 0.0;
261
262     for (int i = 0; i < nr_nodes; i++) {
263       if (i != src_node_id) {
264         cost_arr[i] = DBL_MAX;
265       }
266
267       pred_arr[i] = 0;
268
269       /* initialize priority queue */
270       int *nodeid = xbt_new0(int, 1);
271       *nodeid = i;
272       xbt_heap_push(pqueue, nodeid, cost_arr[i]);
273
274     }
275
276     /* apply dijkstra using the indexes from the graph's node array */
277     while (xbt_heap_size(pqueue) > 0) {
278       int *v_id = (int *) xbt_heap_pop(pqueue);
279       xbt_node_t v_node = xbt_dynar_get_as(nodes, *v_id, xbt_node_t);
280       xbt_edge_t edge = NULL;
281       unsigned int cursor;
282
283       xbt_dynar_foreach(xbt_graph_node_get_outedges(v_node), cursor, edge) {
284         xbt_node_t u_node = xbt_graph_edge_get_target(edge);
285         graph_node_data_t data = (graph_node_data_t) xbt_graph_node_get_data(u_node);
286         int u_id = data->graph_id;
287         sg_platf_route_cbarg_t tmp_e_route = (sg_platf_route_cbarg_t) xbt_graph_edge_get_data(edge);
288         int cost_v_u = (tmp_e_route->link_list)->used;    /* count of links, old model assume 1 */
289
290         if (cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
291           pred_arr[u_id] = *v_id;
292           cost_arr[u_id] = cost_v_u + cost_arr[*v_id];
293           int *nodeid = xbt_new0(int, 1);
294           *nodeid = u_id;
295           xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
296         }
297       }
298
299       /* free item popped from pqueue */
300       xbt_free(v_id);
301     }
302
303     xbt_free(cost_arr);
304     xbt_heap_free(pqueue);
305   }
306
307   /* compose route path with links */
308   NetCard *gw_src = NULL, *gw_dst, *prev_gw_src, *first_gw = NULL;
309   NetCard *gw_dst_net_elm = NULL, *prev_gw_src_net_elm = NULL;
310
311   for (int v = dst_node_id; v != src_node_id; v = pred_arr[v]) {
312     xbt_node_t node_pred_v = xbt_dynar_get_as(nodes, pred_arr[v], xbt_node_t);
313     xbt_node_t node_v = xbt_dynar_get_as(nodes, v, xbt_node_t);
314     xbt_edge_t edge = xbt_graph_get_edge(p_routeGraph, node_pred_v, node_v);
315
316     if (edge == NULL)
317       THROWF(arg_error, 0, "No route from '%s' to '%s'", src->name(), dst->name());
318
319     prev_gw_src = gw_src;
320
321     e_route = (sg_platf_route_cbarg_t) xbt_graph_edge_get_data(edge);
322     gw_src = e_route->gw_src;
323     gw_dst = e_route->gw_dst;
324
325     if (v == dst_node_id)
326       first_gw = gw_dst;
327
328     if (hierarchy_ == SURF_ROUTING_RECURSIVE && v != dst_node_id
329         && strcmp(gw_dst->name(), prev_gw_src->name())) {
330       xbt_dynar_t e_route_as_to_as=NULL;
331
332       routing_platf->getRouteAndLatency(gw_dst_net_elm, prev_gw_src_net_elm, &e_route_as_to_as, NULL);
333       if (edge == NULL)
334         THROWF(arg_error,0,"No route from '%s' to '%s'", src->name(), dst->name());
335       int pos = 0;
336       xbt_dynar_foreach(e_route_as_to_as, cpt, link) {
337         xbt_dynar_insert_at(route->link_list, pos, &link);
338         if (lat)
339           *lat += static_cast<Link*>(link)->getLatency();
340         pos++;
341       }
342     }
343
344     xbt_dynar_foreach(e_route->link_list, cpt, link) {
345       xbt_dynar_unshift(route->link_list, &link);
346       if (lat)
347         *lat += static_cast<Link*>(link)->getLatency();
348     }
349     size++;
350   }
351
352   if (hierarchy_ == SURF_ROUTING_RECURSIVE) {
353     route->gw_src = gw_src;
354     route->gw_dst = first_gw;
355   }
356
357   if (m_cached && elm == NULL) {
358     /* add to predecessor list of the current src-host to cache */
359     elm = xbt_new0(struct route_cache_element, 1);
360     elm->pred_arr = pred_arr;
361     elm->size = size;
362     xbt_dict_set_ext(p_routeCache, (char *) (&src_id), sizeof(int), (xbt_dictelm_t) elm, NULL);
363   }
364
365   if (!m_cached)
366     xbt_free(pred_arr);
367 }
368
369 AsDijkstra::~AsDijkstra()
370 {
371   xbt_graph_free_graph(p_routeGraph, &xbt_free_f,  &graph_edge_data_free, &xbt_free_f);
372   xbt_dict_free(&p_graphNodeMap);
373   xbt_dict_free(&p_routeCache);
374 }
375
376 /* Creation routing model functions */
377
378 AsDijkstra::AsDijkstra(const char*name, bool cached)
379   : AsGeneric(name)
380   , m_cached(cached)
381 {
382   p_routeGraph = NULL;
383   p_graphNodeMap = NULL;
384   p_routeCache = NULL;
385 }
386
387 void AsDijkstra::end()
388 {
389   xbt_node_t node = NULL;
390   unsigned int cursor2;
391   xbt_dynar_t nodes = NULL;
392
393   /* Create the topology graph */
394   if(!p_routeGraph)
395   p_routeGraph = xbt_graph_new_graph(1, NULL);
396   if(!p_graphNodeMap)
397     p_graphNodeMap = xbt_dict_new_homogeneous(&graph_node_map_elem_free);
398
399   if (m_cached && !p_routeCache)
400     p_routeCache = xbt_dict_new_homogeneous(&route_cache_elem_free);
401
402   /* Add the loopback if needed */
403   if (routing_platf->p_loopback && hierarchy_ == SURF_ROUTING_BASE)
404     addLoopback();
405
406   /* initialize graph indexes in nodes after graph has been built */
407   nodes = xbt_graph_get_nodes(p_routeGraph);
408
409   xbt_dynar_foreach(nodes, cursor2, node) {
410     graph_node_data_t data = (graph_node_data_t) xbt_graph_node_get_data(node);
411     data->graph_id = cursor2;
412   }
413
414 }
415
416 void AsDijkstra::parseRoute(sg_platf_route_cbarg_t route)
417 {
418   char *src = (char*)(route->src);
419   char *dst = (char*)(route->dst);
420
421   int as_route = 0;
422   if(!route->gw_dst && !route->gw_src)
423     XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
424   else{
425     XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
426         route->gw_src->name(), dst, route->gw_dst->name());
427     as_route = 1;
428     if(route->gw_dst->getRcType() == SURF_NETWORK_ELEMENT_NULL)
429       surf_parse_error("The gw_dst '%s' does not exist!",route->gw_dst->name());
430     if(route->gw_src->getRcType() == SURF_NETWORK_ELEMENT_NULL)
431       surf_parse_error("The gw_src '%s' does not exist!",route->gw_src->name());
432   }
433
434   NetCard *src_net_elm, *dst_net_elm;
435
436   src_net_elm = sg_netcard_by_name_or_null(src);
437   dst_net_elm = sg_netcard_by_name_or_null(dst);
438
439   xbt_assert(src_net_elm, "Network elements %s not found", src);
440   xbt_assert(dst_net_elm, "Network elements %s not found", dst);
441
442   /* Create the topology graph */
443   if(!p_routeGraph)
444     p_routeGraph = xbt_graph_new_graph(1, NULL);
445   if(!p_graphNodeMap)
446     p_graphNodeMap = xbt_dict_new_homogeneous(&graph_node_map_elem_free);
447
448   if (m_cached && !p_routeCache)
449     p_routeCache = xbt_dict_new_homogeneous(&route_cache_elem_free);
450
451   sg_platf_route_cbarg_t e_route = newExtendedRoute(hierarchy_, route, 1);
452   newRoute(src_net_elm->id(), dst_net_elm->id(), e_route);
453
454   // Symmetrical YES
455   if ( (route->symmetrical == TRUE && as_route == 0)
456       || (route->symmetrical == TRUE && as_route == 1)
457   )
458   {
459     if(!route->gw_dst && !route->gw_src)
460       XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
461     else
462       XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
463           route->gw_dst->name(), src, route->gw_src->name());
464
465     xbt_dynar_t nodes = xbt_graph_get_nodes(p_routeGraph);
466     xbt_node_t node_s_v = xbt_dynar_get_as(nodes, src_net_elm->id(), xbt_node_t);
467     xbt_node_t node_e_v = xbt_dynar_get_as(nodes, dst_net_elm->id(), xbt_node_t);
468     xbt_edge_t edge =
469         xbt_graph_get_edge(p_routeGraph, node_e_v, node_s_v);
470
471     if (edge)
472       THROWF(arg_error,0,"(AS)Route from '%s' to '%s' already exists",src,dst);
473
474     if (route->gw_dst && route->gw_src) {
475       NetCard *gw_tmp;
476       gw_tmp = route->gw_src;
477       route->gw_src = route->gw_dst;
478       route->gw_dst = gw_tmp;
479     }
480     sg_platf_route_cbarg_t link_route_back = newExtendedRoute(hierarchy_, route, 0);
481     newRoute(dst_net_elm->id(), src_net_elm->id(), link_route_back);
482   }
483   xbt_dynar_free(&route->link_list);
484 }
485
486 }
487 }