Logo AND Algorithmique Numérique Distribuée

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