Logo AND Algorithmique Numérique Distribuée

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