Logo AND Algorithmique Numérique Distribuée

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