Logo AND Algorithmique Numérique Distribuée

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