Logo AND Algorithmique Numérique Distribuée

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