Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid into no_simix_global
[simgrid.git] / src / kernel / routing / StarZone.cpp
1 /* Copyright (c) 2009-2021. 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 "simgrid/kernel/routing/StarZone.hpp"
7 #include "simgrid/kernel/routing/NetPoint.hpp"
8 #include "simgrid/kernel/routing/RoutedZone.hpp"
9 #include "src/surf/network_interface.hpp"
10 #include "xbt/string.hpp"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_star, surf, "Routing part of surf");
13
14 namespace simgrid {
15 namespace kernel {
16 namespace routing {
17 StarZone::StarZone(const std::string& name) : ClusterZone(name) {}
18
19 void StarZone::add_links_to_route(const std::vector<resource::LinkImpl*>& links, Route* route, double* latency,
20                                   std::unordered_set<resource::LinkImpl*>& added_links) const
21 {
22   for (auto* link : links) {
23     /* do not add duplicated links in route->link_list_ */
24     if (not added_links.insert(link).second)
25       continue;
26     add_link_latency(route->link_list_, link, latency);
27   }
28 }
29
30 void StarZone::get_local_route(const NetPoint* src, const NetPoint* dst, Route* route, double* latency)
31 {
32   XBT_VERB("StarZone getLocalRoute from '%s'[%lu] to '%s'[%lu]", src->get_cname(), src->id(), dst->get_cname(),
33            dst->id());
34
35   const auto& src_route = routes_.at(src->id());
36   const auto& dst_route = routes_.at(dst->id());
37   std::unordered_set<resource::LinkImpl*> added_links;
38   /* loopback */
39   if (src == dst && src_route.has_loopback()) {
40     add_links_to_route(src_route.loopback, route, latency, added_links);
41     return;
42   }
43
44   xbt_assert(src_route.has_links_up(),
45              "StarZone routing (%s - %s): no link UP from source node. Did you use add_route() to set it?",
46              src->get_cname(), dst->get_cname());
47   xbt_assert(dst_route.has_links_down(),
48              "StarZone routing (%s - %s): no link DOWN to destination node. Did you use add_route() to set it?",
49              src->get_cname(), dst->get_cname());
50
51   /* going UP */
52   add_links_to_route(src_route.links_up, route, latency, added_links);
53
54   /* going DOWN */
55   add_links_to_route(dst_route.links_down, route, latency, added_links);
56   /* gateways */
57   route->gw_src_ = src_route.gateway;
58   route->gw_dst_ = dst_route.gateway;
59 }
60
61 void StarZone::get_graph(const s_xbt_graph_t* graph, std::map<std::string, xbt_node_t, std::less<>>* nodes,
62                          std::map<std::string, xbt_edge_t, std::less<>>* edges)
63 {
64   xbt_node_t star_node = new_xbt_graph_node(graph, get_cname(), nodes);
65
66   for (auto const& src : get_vertices()) {
67     /* going up */
68     xbt_node_t src_node = new_xbt_graph_node(graph, src->get_cname(), nodes);
69     xbt_node_t previous = src_node;
70     for (auto const* link : routes_[src->id()].links_up) {
71       xbt_node_t current = new_xbt_graph_node(graph, link->get_cname(), nodes);
72       new_xbt_graph_edge(graph, previous, current, edges);
73       previous = current;
74     }
75     new_xbt_graph_edge(graph, previous, star_node, edges);
76     /* going down */
77     previous = star_node;
78     for (auto const* link : routes_[src->id()].links_down) {
79       xbt_node_t current = new_xbt_graph_node(graph, link->get_cname(), nodes);
80       new_xbt_graph_edge(graph, current, previous, edges);
81       previous = current;
82     }
83     new_xbt_graph_edge(graph, src_node, previous, edges);
84   }
85 }
86
87 void StarZone::check_add_route_param(const NetPoint* src, const NetPoint* dst, const NetPoint* gw_src,
88                                      const NetPoint* gw_dst, bool symmetrical) const
89 {
90   const char* src_name = src ? src->get_cname() : "nullptr";
91   const char* dst_name = dst ? dst->get_cname() : "nullptr";
92
93   if ((not src && not dst) || (dst && src && src != dst))
94     throw std::invalid_argument(xbt::string_printf(
95         "Cannot add route from %s to %s. In a StarZone, route must be:  i) from source netpoint to everyone, ii) from "
96         "everyone to a single netpoint or iii) loopback, same source and destination",
97         src_name, dst_name));
98
99   if (symmetrical && not src)
100     throw std::invalid_argument(xbt::string_printf("Cannot add route from %s to %s. In a StarZone, symmetrical routes "
101                                                    "must be set from source to everyone (not the contrary)",
102                                                    src_name, dst_name));
103
104   if (src && src->is_netzone()) {
105     if (not gw_src)
106       throw std::invalid_argument(xbt::string_printf(
107           "StarZone::add_route(): source %s is a netzone but gw_src isn't configured", src->get_cname()));
108     if (gw_src->is_netzone())
109       throw std::invalid_argument(
110           xbt::string_printf("StarZone::add_route(): src(%s) is a netzone, gw_src(%s) cannot be a netzone",
111                              src->get_cname(), gw_src->get_cname()));
112
113     const auto* netzone_src = get_netzone_recursive(src);
114     if (not netzone_src->is_component_recursive(gw_src))
115       throw std::invalid_argument(xbt::string_printf(
116           "Invalid NetzoneRoute from %s@%s to %s: gw_src %s belongs to %s, not to %s.", src_name, gw_src->get_cname(),
117           dst_name, gw_src->get_cname(), gw_src->get_englobing_zone()->get_cname(), src_name));
118   }
119
120   if (dst && dst->is_netzone()) {
121     if (not gw_dst)
122       throw std::invalid_argument(xbt::string_printf(
123           "StarZone::add_route(): destination %s is a netzone but gw_dst isn't configured", dst->get_cname()));
124     if (gw_dst->is_netzone())
125       throw std::invalid_argument(
126           xbt::string_printf("StarZone::add_route(): dst(%s) is a netzone, gw_dst(%s) cannot be a netzone",
127                              dst->get_cname(), gw_dst->get_cname()));
128
129     const auto* netzone_dst = get_netzone_recursive(dst);
130     if (not netzone_dst->is_component_recursive(gw_dst))
131       throw std::invalid_argument(xbt::string_printf(
132           "Invalid NetzoneRoute from %s@%s to %s: gw_dst %s belongs to %s, not to %s.", dst_name, gw_dst->get_cname(),
133           src_name, gw_dst->get_cname(), gw_dst->get_englobing_zone()->get_cname(), dst_name));
134   }
135 }
136
137 void StarZone::add_route(NetPoint* src, NetPoint* dst, NetPoint* gw_src, NetPoint* gw_dst,
138                          const std::vector<s4u::LinkInRoute>& link_list, bool symmetrical)
139 {
140   check_add_route_param(src, dst, gw_src, gw_dst, symmetrical);
141
142   /* loopback */
143   if (src == dst) {
144     routes_[src->id()].loopback = get_link_list_impl(link_list, false);
145   } else {
146     /* src to everyone */
147     if (src) {
148       auto& route        = routes_[src->id()];
149       route.links_up     = get_link_list_impl(link_list, false);
150       route.gateway      = gw_src;
151       route.links_up_set = true;
152       if (symmetrical) {
153         auto links_down = get_link_list_impl(link_list, true);
154         /* reverse it for down/symmetrical links */
155         route.links_down.assign(links_down.rbegin(), links_down.rend());
156         route.links_down_set = true;
157       }
158     }
159     /* dst to everyone */
160     if (dst) {
161       auto& route          = routes_[dst->id()];
162       route.links_down     = get_link_list_impl(link_list, false);
163       route.gateway        = gw_dst;
164       route.links_down_set = true;
165     }
166   }
167 }
168
169 void StarZone::do_seal()
170 {
171   /* add default empty links if nothing was configured by user */
172   for (auto const& node : get_vertices()) {
173     auto route = routes_.emplace(node->id(), StarRoute());
174     if (route.second) {
175       route.first->second.links_down_set = true;
176       route.first->second.links_up_set   = true;
177     }
178   }
179 }
180
181 } // namespace routing
182 } // namespace kernel
183
184 namespace s4u {
185 NetZone* create_star_zone(const std::string& name)
186 {
187   return (new kernel::routing::StarZone(name))->get_iface();
188 }
189 } // namespace s4u
190
191 } // namespace simgrid