Logo AND Algorithmique Numérique Distribuée

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