Logo AND Algorithmique Numérique Distribuée

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