1 /* Copyright (c) 2009-2016. 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/NetCard.hpp"
9 #include "src/surf/network_interface.hpp"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_dijkstra, surf, "Routing part of surf -- dijkstra routing logic");
15 static void route_cache_elem_free(void* e)
17 route_cache_element_t elm = (route_cache_element_t)e;
19 xbt_free(elm->pred_arr);
24 static void graph_node_map_elem_free(void* e)
26 graph_node_map_element_t elm = (graph_node_map_element_t)e;
30 static void graph_edge_data_free(void* e) // FIXME: useless code duplication
32 sg_platf_route_cbarg_t e_route = (sg_platf_route_cbarg_t)e;
34 delete e_route->link_list;
39 /* Utility functions */
44 void DijkstraZone::seal()
46 xbt_node_t node = nullptr;
47 unsigned int cursor2, cursor;
49 /* Create the topology graph */
51 routeGraph_ = xbt_graph_new_graph(1, nullptr);
53 graphNodeMap_ = xbt_dict_new_homogeneous(&graph_node_map_elem_free);
55 /* Add the loopback if needed */
56 if (surf_network_model->loopback_ && hierarchy_ == RoutingMode::base) {
57 xbt_dynar_foreach (xbt_graph_get_nodes(routeGraph_), cursor, node) {
58 xbt_edge_t edge = nullptr;
61 xbt_dynar_foreach (xbt_graph_node_get_outedges(node), cursor2, edge) {
62 if (xbt_graph_edge_get_target(edge) == node) {
69 sg_platf_route_cbarg_t e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
70 e_route->link_list = new std::vector<Link*>();
71 e_route->link_list->push_back(surf_network_model->loopback_);
72 xbt_graph_new_edge(routeGraph_, node, node, e_route);
77 /* initialize graph indexes in nodes after graph has been built */
78 xbt_dynar_t nodes = xbt_graph_get_nodes(routeGraph_);
80 xbt_dynar_foreach (nodes, cursor, node) {
81 graph_node_data_t data = (graph_node_data_t)xbt_graph_node_get_data(node);
82 data->graph_id = cursor;
86 xbt_node_t DijkstraZone::routeGraphNewNode(int id, int graph_id)
88 xbt_node_t node = nullptr;
89 graph_node_data_t data = nullptr;
90 graph_node_map_element_t elm = nullptr;
92 data = xbt_new0(struct graph_node_data, 1);
94 data->graph_id = graph_id;
95 node = xbt_graph_new_node(routeGraph_, data);
97 elm = xbt_new0(struct graph_node_map_element, 1);
99 xbt_dict_set_ext(graphNodeMap_, (char*)(&id), sizeof(int), (xbt_dictelm_t)elm, nullptr);
104 graph_node_map_element_t DijkstraZone::nodeMapSearch(int id)
106 return (graph_node_map_element_t)xbt_dict_get_or_null_ext(graphNodeMap_, (char*)(&id), sizeof(int));
111 void DijkstraZone::newRoute(int src_id, int dst_id, sg_platf_route_cbarg_t e_route)
113 XBT_DEBUG("Load Route from \"%d\" to \"%d\"", src_id, dst_id);
114 xbt_node_t src = nullptr;
115 xbt_node_t dst = nullptr;
117 graph_node_map_element_t src_elm = nodeMapSearch(src_id);
118 graph_node_map_element_t dst_elm = nodeMapSearch(dst_id);
126 /* add nodes if they don't exist in the graph */
127 if (src_id == dst_id && src == nullptr && dst == nullptr) {
128 src = this->routeGraphNewNode(src_id, -1);
131 if (src == nullptr) {
132 src = this->routeGraphNewNode(src_id, -1);
134 if (dst == nullptr) {
135 dst = this->routeGraphNewNode(dst_id, -1);
139 /* add link as edge to graph */
140 xbt_graph_new_edge(routeGraph_, src, dst, e_route);
143 void DijkstraZone::getLocalRoute(NetCard* src, NetCard* dst, sg_platf_route_cbarg_t route, double* lat)
145 getRouteCheckParams(src, dst);
146 int src_id = src->id();
147 int dst_id = dst->id();
149 int* pred_arr = nullptr;
151 xbt_dynar_t nodes = xbt_graph_get_nodes(routeGraph_);
153 /* Use the graph_node id mapping set to quickly find the nodes */
154 graph_node_map_element_t src_elm = nodeMapSearch(src_id);
155 graph_node_map_element_t dst_elm = nodeMapSearch(dst_id);
157 int src_node_id = ((graph_node_data_t)xbt_graph_node_get_data(src_elm->node))->graph_id;
158 int dst_node_id = ((graph_node_data_t)xbt_graph_node_get_data(dst_elm->node))->graph_id;
160 /* if the src and dst are the same */
161 if (src_node_id == dst_node_id) {
163 xbt_node_t node_s_v = xbt_dynar_get_as(nodes, src_node_id, xbt_node_t);
164 xbt_node_t node_e_v = xbt_dynar_get_as(nodes, dst_node_id, xbt_node_t);
165 xbt_edge_t edge = xbt_graph_get_edge(routeGraph_, node_s_v, node_e_v);
168 THROWF(arg_error, 0, "No route from '%s' to '%s'", src->name().c_str(), dst->name().c_str());
170 sg_platf_route_cbarg_t e_route = (sg_platf_route_cbarg_t)xbt_graph_edge_get_data(edge);
172 for (auto link : *e_route->link_list) {
173 route->link_list->insert(route->link_list->begin(), link);
175 *lat += static_cast<Link*>(link)->latency();
179 route_cache_element_t elm = nullptr;
180 if (routeCache_) { /* cache mode */
181 elm = (route_cache_element_t)xbt_dict_get_or_null_ext(routeCache_, (char*)(&src_id), sizeof(int));
184 if (elm) { /* cached mode and cache hit */
185 pred_arr = elm->pred_arr;
186 } else { /* not cached mode, or cache miss */
188 int nr_nodes = xbt_dynar_length(nodes);
189 double* cost_arr = xbt_new0(double, nr_nodes); /* link cost from src to other hosts */
190 pred_arr = xbt_new0(int, nr_nodes); /* predecessors in path from src */
191 xbt_heap_t pqueue = xbt_heap_new(nr_nodes, xbt_free_f);
194 cost_arr[src_node_id] = 0.0;
196 for (int i = 0; i < nr_nodes; i++) {
197 if (i != src_node_id) {
198 cost_arr[i] = DBL_MAX;
203 /* initialize priority queue */
204 int* nodeid = xbt_new0(int, 1);
206 xbt_heap_push(pqueue, nodeid, cost_arr[i]);
209 /* apply dijkstra using the indexes from the graph's node array */
210 while (xbt_heap_size(pqueue) > 0) {
211 int* v_id = (int*)xbt_heap_pop(pqueue);
212 xbt_node_t v_node = xbt_dynar_get_as(nodes, *v_id, xbt_node_t);
213 xbt_edge_t edge = nullptr;
216 xbt_dynar_foreach (xbt_graph_node_get_outedges(v_node), cursor, edge) {
217 xbt_node_t u_node = xbt_graph_edge_get_target(edge);
218 graph_node_data_t data = (graph_node_data_t)xbt_graph_node_get_data(u_node);
219 int u_id = data->graph_id;
220 sg_platf_route_cbarg_t tmp_e_route = (sg_platf_route_cbarg_t)xbt_graph_edge_get_data(edge);
221 int cost_v_u = tmp_e_route->link_list->size(); /* count of links, old model assume 1 */
223 if (cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
224 pred_arr[u_id] = *v_id;
225 cost_arr[u_id] = cost_v_u + cost_arr[*v_id];
226 int* nodeid = xbt_new0(int, 1);
228 xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
232 /* free item popped from pqueue */
237 xbt_heap_free(pqueue);
240 /* compose route path with links */
241 NetCard *gw_src = nullptr, *gw_dst, *prev_gw_src, *first_gw = nullptr;
242 NetCard *gw_dst_net_elm = nullptr, *prev_gw_src_net_elm = nullptr;
244 for (int v = dst_node_id; v != src_node_id; v = pred_arr[v]) {
245 xbt_node_t node_pred_v = xbt_dynar_get_as(nodes, pred_arr[v], xbt_node_t);
246 xbt_node_t node_v = xbt_dynar_get_as(nodes, v, xbt_node_t);
247 xbt_edge_t edge = xbt_graph_get_edge(routeGraph_, node_pred_v, node_v);
250 THROWF(arg_error, 0, "No route from '%s' to '%s'", src->name().c_str(), dst->name().c_str());
252 prev_gw_src = gw_src;
254 sg_platf_route_cbarg_t e_route = (sg_platf_route_cbarg_t)xbt_graph_edge_get_data(edge);
255 gw_src = e_route->gw_src;
256 gw_dst = e_route->gw_dst;
258 if (v == dst_node_id)
261 if (hierarchy_ == RoutingMode::recursive && v != dst_node_id &&
262 strcmp(gw_dst->name().c_str(), prev_gw_src->name().c_str())) {
263 std::vector<Link*>* e_route_as_to_as = new std::vector<Link*>();
265 getGlobalRoute(gw_dst_net_elm, prev_gw_src_net_elm, e_route_as_to_as, nullptr);
266 auto pos = route->link_list->begin();
267 for (auto link : *e_route_as_to_as) {
268 route->link_list->insert(pos, link);
270 *lat += link->latency();
275 for (auto link : *e_route->link_list) {
276 route->link_list->insert(route->link_list->begin(), link);
278 *lat += static_cast<Link*>(link)->latency();
283 if (hierarchy_ == RoutingMode::recursive) {
284 route->gw_src = gw_src;
285 route->gw_dst = first_gw;
288 if (routeCache_ && elm == nullptr) {
289 /* add to predecessor list of the current src-host to cache */
290 elm = xbt_new0(struct route_cache_element, 1);
291 elm->pred_arr = pred_arr;
293 xbt_dict_set_ext(routeCache_, (char*)(&src_id), sizeof(int), (xbt_dictelm_t)elm, nullptr);
300 DijkstraZone::~DijkstraZone()
302 xbt_graph_free_graph(routeGraph_, &xbt_free_f, &graph_edge_data_free, &xbt_free_f);
303 xbt_dict_free(&graphNodeMap_);
304 xbt_dict_free(&routeCache_);
307 /* Creation routing model functions */
309 DijkstraZone::DijkstraZone(NetZone* father, const char* name, bool cached) : RoutedZone(father, name)
312 routeCache_ = xbt_dict_new_homogeneous(&route_cache_elem_free);
315 void DijkstraZone::addRoute(sg_platf_route_cbarg_t route)
317 NetCard* src = route->src;
318 NetCard* dst = route->dst;
319 const char* srcName = src->name().c_str();
320 const char* dstName = dst->name().c_str();
322 addRouteCheckParams(route);
324 /* Create the topology graph */
326 routeGraph_ = xbt_graph_new_graph(1, nullptr);
328 graphNodeMap_ = xbt_dict_new_homogeneous(&graph_node_map_elem_free);
330 /* we don't check whether the route already exist, because the algorithm may find another path through some other
333 /* Add the route to the base */
334 sg_platf_route_cbarg_t e_route = newExtendedRoute(hierarchy_, route, 1);
335 newRoute(src->id(), dst->id(), e_route);
338 if (route->symmetrical == true) {
339 if (!route->gw_dst && !route->gw_src)
340 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dstName, srcName);
342 XBT_DEBUG("Load ASroute from %s@%s to %s@%s", dstName, route->gw_dst->name().c_str(), srcName,
343 route->gw_src->name().c_str());
345 xbt_dynar_t nodes = xbt_graph_get_nodes(routeGraph_);
346 xbt_node_t node_s_v = xbt_dynar_get_as(nodes, src->id(), xbt_node_t);
347 xbt_node_t node_e_v = xbt_dynar_get_as(nodes, dst->id(), xbt_node_t);
348 xbt_edge_t edge = xbt_graph_get_edge(routeGraph_, node_e_v, node_s_v);
351 THROWF(arg_error, 0, "Route from %s@%s to %s@%s already exists", dstName, route->gw_dst->name().c_str(), srcName,
352 route->gw_src->name().c_str());
354 if (route->gw_dst && route->gw_src) {
355 NetCard* gw_tmp = route->gw_src;
356 route->gw_src = route->gw_dst;
357 route->gw_dst = gw_tmp;
359 sg_platf_route_cbarg_t link_route_back = newExtendedRoute(hierarchy_, route, 0);
360 newRoute(dst->id(), src->id(), link_route_back);