Logo AND Algorithmique Numérique Distribuée

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