Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
convert S4U to my current coding convention
[simgrid.git] / src / surf / surf_routing_RoutedGraph.cpp
1 /* Copyright (c) 2009-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "surf_routing_RoutedGraph.hpp"
8
9 #include <cstdlib>
10
11 #include <algorithm>
12
13 #include "xbt/dict.h"
14 #include "xbt/log.h"
15 #include "xbt/sysdep.h"
16 #include "xbt/dynar.h"
17 #include "xbt/graph.h"
18
19 #include "network_interface.hpp"
20
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_routing_generic, surf_route, "Generic implementation of the surf routing");
22
23 void routing_route_free(sg_platf_route_cbarg_t route)
24 {
25   if (route) {
26     xbt_dynar_free(&route->link_list);
27     xbt_free(route);
28   }
29 }
30
31 namespace simgrid {
32 namespace surf {
33   
34 AsRoutedGraph::AsRoutedGraph(const char*name)
35   : As(name)
36 {
37 }
38
39 AsRoutedGraph::~AsRoutedGraph()
40 {
41 }
42
43
44 }
45 }
46
47 /* ************************************************************************** */
48 /* *********************** GENERIC BUSINESS METHODS ************************* */
49
50 static const char *instr_node_name(xbt_node_t node)
51 {
52   void *data = xbt_graph_node_get_data(node);
53   char *str = (char *) data;
54   return str;
55 }
56
57 xbt_node_t new_xbt_graph_node(xbt_graph_t graph, const char *name,
58                               xbt_dict_t nodes)
59 {
60   xbt_node_t ret = (xbt_node_t) xbt_dict_get_or_null(nodes, name);
61   if (ret)
62     return ret;
63
64   ret = xbt_graph_new_node(graph, xbt_strdup(name));
65   xbt_dict_set(nodes, name, ret, NULL);
66   return ret;
67 }
68
69 xbt_edge_t new_xbt_graph_edge(xbt_graph_t graph, xbt_node_t s, xbt_node_t d,
70                               xbt_dict_t edges)
71 {
72   xbt_edge_t ret;
73
74   const char *sn = instr_node_name(s);
75   const char *dn = instr_node_name(d);
76   int len = strlen(sn) + strlen(dn) + 1;
77   char *name = (char *) xbt_malloc(len * sizeof(char));
78
79
80   snprintf(name, len, "%s%s", sn, dn);
81   ret = (xbt_edge_t) xbt_dict_get_or_null(edges, name);
82   if (ret == NULL) {
83     snprintf(name, len, "%s%s", dn, sn);
84     ret = (xbt_edge_t) xbt_dict_get_or_null(edges, name);
85   }
86
87   if (ret == NULL) {
88     ret = xbt_graph_new_edge(graph, s, d, NULL);
89     xbt_dict_set(edges, name, ret, NULL);
90   }
91   free(name);
92   return ret;
93 }
94
95 namespace simgrid {
96 namespace surf {
97
98 void AsRoutedGraph::getGraph(xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges)
99 {
100   int src, dst;
101   int table_size = xbt_dynar_length(vertices_);
102
103
104   for (src = 0; src < table_size; src++) {
105     NetCard *my_src =
106         xbt_dynar_get_as(vertices_, src, NetCard*);
107     for (dst = 0; dst < table_size; dst++) {
108       if (src == dst)
109         continue;
110       NetCard *my_dst =
111           xbt_dynar_get_as(vertices_, dst, NetCard*);
112
113       sg_platf_route_cbarg_t route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
114       route->link_list = xbt_dynar_new(sizeof(Link*), NULL);
115
116       getRouteAndLatency(my_src, my_dst, route, NULL);
117
118       XBT_DEBUG ("get_route_and_latency %s -> %s", my_src->name(), my_dst->name());
119
120       unsigned int cpt;
121       void *link;
122
123       xbt_node_t current, previous;
124       const char *previous_name, *current_name;
125
126       if (route->gw_src) {
127         previous = new_xbt_graph_node(graph, route->gw_src->name(), nodes);
128         previous_name = route->gw_src->name();
129       } else {
130         previous = new_xbt_graph_node(graph, my_src->name(), nodes);
131         previous_name = my_src->name();
132       }
133
134       xbt_dynar_foreach(route->link_list, cpt, link) {
135         const char *link_name = static_cast<simgrid::surf::Resource*>(
136           link)->getName();
137         current = new_xbt_graph_node(graph, link_name, nodes);
138         current_name = link_name;
139         new_xbt_graph_edge(graph, previous, current, edges);
140         XBT_DEBUG ("  %s -> %s", previous_name, current_name);
141         previous = current;
142         previous_name = current_name;
143       }
144
145       if (route->gw_dst) {
146         current = new_xbt_graph_node(graph, route->gw_dst->name(), nodes);
147         current_name = route->gw_dst->name();
148       } else {
149         current = new_xbt_graph_node(graph, my_dst->name(), nodes);
150         current_name = my_dst->name();
151       }
152       new_xbt_graph_edge(graph, previous, current, edges);
153       XBT_DEBUG ("  %s -> %s", previous_name, current_name);
154
155       xbt_dynar_free (&(route->link_list));
156       xbt_free (route);
157     }
158   }
159 }
160
161
162 /* ************************************************************************** */
163 /* ************************* GENERIC AUX FUNCTIONS ************************** */
164 /* change a route containing link names into a route containing link entities */
165 sg_platf_route_cbarg_t AsRoutedGraph::newExtendedRoute(e_surf_routing_hierarchy_t hierarchy,
166       sg_platf_route_cbarg_t routearg, int change_order) {
167
168   sg_platf_route_cbarg_t result;
169   char *link_name;
170   unsigned int cpt;
171
172   result = xbt_new0(s_sg_platf_route_cbarg_t, 1);
173   result->link_list = xbt_dynar_new(sizeof(Link*), NULL);
174
175   xbt_assert(hierarchy == SURF_ROUTING_BASE
176       || hierarchy == SURF_ROUTING_RECURSIVE,
177       "The hierarchy of this AS is neither BASIC nor RECURSIVE, I'm lost here.");
178
179   if (hierarchy == SURF_ROUTING_RECURSIVE) {
180
181     xbt_assert(routearg->gw_src && routearg->gw_dst, "NULL is obviously a deficient gateway");
182
183     /* remember not erase the gateway names */
184     result->gw_src = routearg->gw_src;
185     result->gw_dst = routearg->gw_dst;
186   }
187
188   xbt_dynar_foreach(routearg->link_list, cpt, link_name) {
189
190     Link *link = Link::byName(link_name);
191     if (link) {
192       if (change_order)
193         xbt_dynar_push(result->link_list, &link);
194       else
195         xbt_dynar_unshift(result->link_list, &link);
196     } else
197       THROWF(mismatch_error, 0, "Link '%s' not found", link_name);
198   }
199
200   return result;
201 }
202
203 void AsRoutedGraph::getRouteCheckParams(NetCard *src, NetCard *dst)
204 {
205   xbt_assert(src,"Cannot find a route from NULL to %s", dst->name());
206   xbt_assert(dst,"Cannot find a route from %s to NULL", src->name());
207
208   As *src_as = src->containingAS();
209   As *dst_as = dst->containingAS();
210
211   xbt_assert(src_as == dst_as, "Internal error: %s@%s and %s@%s are not in the same AS as expected. Please report that bug.",
212         src->name(), src_as->name_, dst->name(), dst_as->name_);
213
214   xbt_assert(this == dst_as,
215       "Internal error: route destination %s@%s is not in AS %s as expected (route source: %s@%s). Please report that bug.",
216         src->name(), dst->name(),  src_as->name_, dst_as->name_,  name_);
217 }
218 void AsRoutedGraph::addRouteCheckParams(sg_platf_route_cbarg_t route) {
219   const char *srcName = route->src;
220   const char *dstName = route->dst;
221   NetCard *src = sg_netcard_by_name_or_null(srcName);
222   NetCard *dst = sg_netcard_by_name_or_null(dstName);
223
224   if(!route->gw_dst && !route->gw_src) {
225     XBT_DEBUG("Load Route from \"%s\" to \"%s\"", srcName, dstName);
226     xbt_assert(src, "Cannot add a route from %s to %s: %s does not exist.", srcName, dstName, srcName);
227     xbt_assert(dst, "Cannot add a route from %s to %s: %s does not exist.", srcName, dstName, dstName);
228     xbt_assert(!xbt_dynar_is_empty(route->link_list), "Empty route (between %s and %s) forbidden.", srcName, dstName);
229     xbt_assert(src->getRcType()==SURF_NETWORK_ELEMENT_HOST || src->getRcType()==SURF_NETWORK_ELEMENT_ROUTER,
230         "When defining a route, src must be an host or a router but '%s' is not. Did you meant to have an ASroute?", srcName);
231     xbt_assert(dst->getRcType()==SURF_NETWORK_ELEMENT_HOST || dst->getRcType()==SURF_NETWORK_ELEMENT_ROUTER,
232         "When defining a route, dst must be an host or a router but '%s' is not. Did you meant to have an ASroute?", dstName);
233   } else {
234     XBT_DEBUG("Load ASroute from %s@%s to %s@%s", srcName, route->gw_src->name(), dstName, route->gw_dst->name());
235     xbt_assert(src->getRcType()==SURF_NETWORK_ELEMENT_AS,
236         "When defining an ASroute, src must be an AS but '%s' is not", srcName);
237     xbt_assert(dst->getRcType()==SURF_NETWORK_ELEMENT_AS,
238         "When defining an ASroute, dst must be an AS but '%s' is not", dstName);
239
240     xbt_assert(route->gw_src->getRcType()==SURF_NETWORK_ELEMENT_HOST || route->gw_src->getRcType()==SURF_NETWORK_ELEMENT_ROUTER,
241         "When defining an ASroute, gw_src must be an host or a router but '%s' is not.", srcName);
242     xbt_assert(route->gw_dst->getRcType()==SURF_NETWORK_ELEMENT_HOST || route->gw_dst->getRcType()==SURF_NETWORK_ELEMENT_ROUTER,
243         "When defining an ASroute, gw_dst must be an host or a router but '%s' is not.", dstName);
244
245     xbt_assert(route->gw_src != route->gw_dst, "Cannot define an ASroute from '%s' to itself", route->gw_src->name());
246
247     xbt_assert(src, "Cannot add a route from %s@%s to %s@%s: %s does not exist.",
248         srcName,route->gw_src->name(), dstName,route->gw_dst->name(), srcName);
249     xbt_assert(dst, "Cannot add a route from %s@%s to %s@%s: %s does not exist.",
250         srcName,route->gw_src->name(), dstName,route->gw_dst->name(), dstName);
251     xbt_assert(!xbt_dynar_is_empty(route->link_list), "Empty route (between %s@%s and %s@%s) forbidden.",
252         srcName,route->gw_src->name(), dstName,route->gw_dst->name());
253   }
254 }
255
256 }
257 }