Logo AND Algorithmique Numérique Distribuée

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