Logo AND Algorithmique Numérique Distribuée

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