Logo AND Algorithmique Numérique Distribuée

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