Logo AND Algorithmique Numérique Distribuée

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