Logo AND Algorithmique Numérique Distribuée

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