Logo AND Algorithmique Numérique Distribuée

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