1 /* Copyright (c) 2009-2017. The SimGrid Team. All rights reserved. */
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. */
6 #include "src/kernel/routing/DijkstraZone.hpp"
7 #include "src/kernel/routing/NetPoint.hpp"
8 #include "src/surf/network_interface.hpp"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_dijkstra, surf, "Routing part of surf -- dijkstra routing logic");
16 static void graph_node_data_free(void* n)
18 graph_node_data_t data = static_cast<graph_node_data_t>(n);
22 static void graph_edge_data_free(void* e) // FIXME: useless code duplication
24 sg_platf_route_cbarg_t e_route = static_cast<sg_platf_route_cbarg_t>(e);
26 delete e_route->link_list;
31 /* Utility functions */
36 void DijkstraZone::seal()
39 xbt_node_t node = nullptr;
41 /* Create the topology graph */
43 routeGraph_ = xbt_graph_new_graph(1, nullptr);
45 /* Add the loopback if needed */
46 if (surf_network_model->loopback_ && hierarchy_ == RoutingMode::base) {
47 xbt_dynar_foreach (xbt_graph_get_nodes(routeGraph_), cursor, node) {
50 xbt_edge_t edge = nullptr;
52 xbt_dynar_foreach (xbt_graph_node_get_outedges(node), cursor2, edge) {
53 if (xbt_graph_edge_get_target(edge) == node) {
60 sg_platf_route_cbarg_t e_route = new s_sg_platf_route_cbarg_t;
61 e_route->link_list = new std::vector<surf::LinkImpl*>();
62 e_route->link_list->push_back(surf_network_model->loopback_);
63 xbt_graph_new_edge(routeGraph_, node, node, e_route);
68 /* initialize graph indexes in nodes after graph has been built */
69 xbt_dynar_t nodes = xbt_graph_get_nodes(routeGraph_);
71 xbt_dynar_foreach (nodes, cursor, node) {
72 graph_node_data_t data = static_cast<graph_node_data_t>(xbt_graph_node_get_data(node));
73 data->graph_id = cursor;
77 xbt_node_t DijkstraZone::routeGraphNewNode(int id, int graph_id)
79 graph_node_data_t data = new s_graph_node_data_t;
81 data->graph_id = graph_id;
83 xbt_node_t node = xbt_graph_new_node(routeGraph_, data);
84 graphNodeMap_.emplace(id, node);
89 xbt_node_t DijkstraZone::nodeMapSearch(int id)
91 auto ret = graphNodeMap_.find(id);
92 return ret == graphNodeMap_.end() ? nullptr : ret->second;
97 void DijkstraZone::newRoute(int src_id, int dst_id, sg_platf_route_cbarg_t e_route)
99 XBT_DEBUG("Load Route from \"%d\" to \"%d\"", src_id, dst_id);
100 xbt_node_t src = nullptr;
101 xbt_node_t dst = nullptr;
103 xbt_node_t src_elm = nodeMapSearch(src_id);
104 xbt_node_t dst_elm = nodeMapSearch(dst_id);
112 /* add nodes if they don't exist in the graph */
113 if (src_id == dst_id && src == nullptr && dst == nullptr) {
114 src = this->routeGraphNewNode(src_id, -1);
117 if (src == nullptr) {
118 src = this->routeGraphNewNode(src_id, -1);
120 if (dst == nullptr) {
121 dst = this->routeGraphNewNode(dst_id, -1);
125 /* add link as edge to graph */
126 xbt_graph_new_edge(routeGraph_, src, dst, e_route);
129 void DijkstraZone::getLocalRoute(NetPoint* src, NetPoint* dst, sg_platf_route_cbarg_t route, double* lat)
131 getRouteCheckParams(src, dst);
132 int src_id = src->id();
133 int dst_id = dst->id();
135 int* pred_arr = nullptr;
137 xbt_dynar_t nodes = xbt_graph_get_nodes(routeGraph_);
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);
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;
146 /* if the src and dst are the same */
147 if (src_node_id == dst_node_id) {
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);
154 THROWF(arg_error, 0, "No route from '%s' to '%s'", src->name().c_str(), dst->name().c_str());
156 sg_platf_route_cbarg_t e_route = static_cast<sg_platf_route_cbarg_t>(xbt_graph_edge_get_data(edge));
158 for (auto const& link : *e_route->link_list) {
159 route->link_list->insert(route->link_list->begin(), link);
161 *lat += static_cast<surf::LinkImpl*>(link)->latency();
165 route_cache_element_t elm = nullptr;
166 if (not routeCache_.empty()) { /* cache mode */
167 auto it = routeCache_.find(src_id);
168 elm = (it == routeCache_.end()) ? nullptr : it->second;
171 if (elm) { /* cached mode and cache hit */
172 pred_arr = elm->pred_arr;
173 } else { /* not cached mode, or cache miss */
175 int nr_nodes = xbt_dynar_length(nodes);
176 double* cost_arr = new double[nr_nodes]; /* link cost from src to other hosts */
177 pred_arr = new int[nr_nodes]; /* predecessors in path from src */
178 xbt_heap_t pqueue = xbt_heap_new(nr_nodes, nullptr);
181 cost_arr[src_node_id] = 0.0;
183 for (int i = 0; i < nr_nodes; i++) {
184 if (i != src_node_id) {
185 cost_arr[i] = DBL_MAX;
190 /* initialize priority queue */
191 int* nodeid = new int(i);
192 xbt_heap_push(pqueue, nodeid, cost_arr[i]);
195 /* apply dijkstra using the indexes from the graph's node array */
196 while (xbt_heap_size(pqueue) > 0) {
197 int* v_id = static_cast<int*>(xbt_heap_pop(pqueue));
198 xbt_node_t v_node = xbt_dynar_get_as(nodes, *v_id, xbt_node_t);
199 xbt_edge_t edge = nullptr;
202 xbt_dynar_foreach (xbt_graph_node_get_outedges(v_node), cursor, edge) {
203 xbt_node_t u_node = xbt_graph_edge_get_target(edge);
204 graph_node_data_t data = static_cast<graph_node_data_t>(xbt_graph_node_get_data(u_node));
205 int u_id = data->graph_id;
206 sg_platf_route_cbarg_t tmp_e_route = static_cast<sg_platf_route_cbarg_t>(xbt_graph_edge_get_data(edge));
207 int cost_v_u = tmp_e_route->link_list->size(); /* count of links, old model assume 1 */
209 if (cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
210 pred_arr[u_id] = *v_id;
211 cost_arr[u_id] = cost_v_u + cost_arr[*v_id];
212 int* nodeid = new int(u_id);
213 xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
217 /* free item popped from pqueue */
222 xbt_heap_free(pqueue);
225 /* compose route path with links */
226 NetPoint* gw_src = nullptr;
228 NetPoint* first_gw = nullptr;
230 for (int v = dst_node_id; v != src_node_id; v = pred_arr[v]) {
231 xbt_node_t node_pred_v = xbt_dynar_get_as(nodes, pred_arr[v], xbt_node_t);
232 xbt_node_t node_v = xbt_dynar_get_as(nodes, v, xbt_node_t);
233 xbt_edge_t edge = xbt_graph_get_edge(routeGraph_, node_pred_v, node_v);
236 THROWF(arg_error, 0, "No route from '%s' to '%s'", src->name().c_str(), dst->name().c_str());
238 sg_platf_route_cbarg_t e_route = static_cast<sg_platf_route_cbarg_t>(xbt_graph_edge_get_data(edge));
240 NetPoint* prev_gw_src = gw_src;
241 gw_src = e_route->gw_src;
242 gw_dst = e_route->gw_dst;
244 if (v == dst_node_id)
247 if (hierarchy_ == RoutingMode::recursive && v != dst_node_id &&
248 strcmp(gw_dst->name().c_str(), prev_gw_src->name().c_str())) {
249 std::vector<surf::LinkImpl*> e_route_as_to_as;
251 NetPoint* gw_dst_net_elm = nullptr;
252 NetPoint* prev_gw_src_net_elm = nullptr;
253 getGlobalRoute(gw_dst_net_elm, prev_gw_src_net_elm, &e_route_as_to_as, nullptr);
254 auto pos = route->link_list->begin();
255 for (auto const& link : e_route_as_to_as) {
256 route->link_list->insert(pos, link);
258 *lat += link->latency();
263 for (auto const& link : *e_route->link_list) {
264 route->link_list->insert(route->link_list->begin(), link);
266 *lat += static_cast<surf::LinkImpl*>(link)->latency();
271 if (hierarchy_ == RoutingMode::recursive) {
272 route->gw_src = gw_src;
273 route->gw_dst = first_gw;
276 if (not routeCache_.empty() && elm == nullptr) {
277 /* add to predecessor list of the current src-host to cache */
278 elm = new s_route_cache_element_t;
279 elm->pred_arr = pred_arr;
281 routeCache_.insert({src_id, elm});
284 if (routeCache_.empty())
288 DijkstraZone::~DijkstraZone()
290 xbt_graph_free_graph(routeGraph_, &graph_node_data_free, &graph_edge_data_free, nullptr);
293 /* Creation routing model functions */
295 DijkstraZone::DijkstraZone(NetZone* father, std::string name, bool cached) : RoutedZone(father, name)
299 void DijkstraZone::addRoute(sg_platf_route_cbarg_t route)
301 NetPoint* src = route->src;
302 NetPoint* dst = route->dst;
303 const char* srcName = src->name().c_str();
304 const char* dstName = dst->name().c_str();
306 addRouteCheckParams(route);
308 /* Create the topology graph */
310 routeGraph_ = xbt_graph_new_graph(1, nullptr);
312 /* we don't check whether the route already exist, because the algorithm may find another path through some other
315 /* Add the route to the base */
316 sg_platf_route_cbarg_t e_route = newExtendedRoute(hierarchy_, route, 1);
317 newRoute(src->id(), dst->id(), e_route);
320 if (route->symmetrical == true) {
322 xbt_dynar_t nodes = xbt_graph_get_nodes(routeGraph_);
323 xbt_node_t node_s_v = xbt_dynar_get_as(nodes, src->id(), xbt_node_t);
324 xbt_node_t node_e_v = xbt_dynar_get_as(nodes, dst->id(), xbt_node_t);
325 xbt_edge_t edge = xbt_graph_get_edge(routeGraph_, node_e_v, node_s_v);
327 if (not route->gw_dst || not route->gw_src){
328 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dstName, srcName);
330 THROWF(arg_error, 0, "Route from %s to %s already exists", dstName, srcName);
332 XBT_DEBUG("Load NetzoneRoute from %s@%s to %s@%s", dstName, route->gw_dst->name().c_str(), srcName,
333 route->gw_src->name().c_str());
335 THROWF(arg_error, 0, "Route from %s@%s to %s@%s already exists", dstName, route->gw_dst->name().c_str(), srcName,
336 route->gw_src->name().c_str());
339 if (route->gw_dst && route->gw_src) {
340 NetPoint* gw_tmp = route->gw_src;
341 route->gw_src = route->gw_dst;
342 route->gw_dst = gw_tmp;
344 sg_platf_route_cbarg_t link_route_back = newExtendedRoute(hierarchy_, route, 0);
345 newRoute(dst->id(), src->id(), link_route_back);