Logo AND Algorithmique Numérique Distribuée

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