Logo AND Algorithmique Numérique Distribuée

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