Logo AND Algorithmique Numérique Distribuée

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