Logo AND Algorithmique Numérique Distribuée

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