Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
refactor link characteristics management across Clustered Zones
[simgrid.git] / src / kernel / routing / ClusterZone.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/ClusterZone.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" // FIXME: RouteCreationArgs and friends
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_cluster, surf, "Routing part of surf");
13
14 /* This routing is specifically setup to represent clusters, aka homogeneous sets of machines
15  * Note that a router is created, easing the interconnection with the rest of the world. */
16
17 namespace simgrid {
18 namespace kernel {
19 namespace routing {
20 ClusterZone::ClusterZone(const std::string& name) : NetZoneImpl(name) {}
21
22 void ClusterZone::set_loopback()
23 {
24   if (not has_loopback_) {
25     num_links_per_node_++;
26     has_loopback_ = true;
27   }
28 }
29
30 void ClusterZone::set_limiter()
31 {
32   if (not has_limiter_) {
33     num_links_per_node_++;
34     has_limiter_ = true;
35   }
36 }
37
38 void ClusterZone::set_link_characteristics(double bw, double lat, s4u::Link::SharingPolicy sharing_policy)
39 {
40   link_sharing_policy_ = sharing_policy;
41   link_bw_             = bw;
42   link_lat_            = lat;
43 }
44
45 void ClusterZone::add_private_link_at(unsigned int position, std::pair<resource::LinkImpl*, resource::LinkImpl*> link)
46 {
47   private_links_.insert({position, link});
48 }
49
50 void ClusterZone::get_local_route(NetPoint* src, NetPoint* dst, RouteCreationArgs* route, double* lat)
51 {
52   XBT_VERB("cluster getLocalRoute from '%s'[%u] to '%s'[%u]", src->get_cname(), src->id(), dst->get_cname(), dst->id());
53   xbt_assert(not private_links_.empty(),
54              "Cluster routing: no links attached to the source node - did you use host_link tag?");
55
56   if ((src->id() == dst->id()) && has_loopback_) {
57     if (src->is_router()) {
58       XBT_WARN("Routing from a cluster private router to itself is meaningless");
59     } else {
60       std::pair<resource::LinkImpl*, resource::LinkImpl*> info = private_links_.at(node_pos(src->id()));
61       route->link_list.push_back(info.first);
62       if (lat)
63         *lat += info.first->get_latency();
64     }
65     return;
66   }
67
68   if (not src->is_router()) { // No private link for the private router
69     if (has_limiter_) {       // limiter for sender
70       std::pair<resource::LinkImpl*, resource::LinkImpl*> info = private_links_.at(node_pos_with_loopback(src->id()));
71       route->link_list.push_back(info.first);
72     }
73
74     std::pair<resource::LinkImpl*, resource::LinkImpl*> info =
75         private_links_.at(node_pos_with_loopback_limiter(src->id()));
76     if (info.first) { // link up
77       route->link_list.push_back(info.first);
78       if (lat)
79         *lat += info.first->get_latency();
80     }
81   }
82
83   if (backbone_) {
84     route->link_list.push_back(backbone_);
85     if (lat)
86       *lat += backbone_->get_latency();
87   }
88
89   if (not dst->is_router()) { // No specific link for router
90     std::pair<resource::LinkImpl*, resource::LinkImpl*> info =
91         private_links_.at(node_pos_with_loopback_limiter(dst->id()));
92
93     if (info.second) { // link down
94       route->link_list.push_back(info.second);
95       if (lat)
96         *lat += info.second->get_latency();
97     }
98     if (has_limiter_) { // limiter for receiver
99       info = private_links_.at(node_pos_with_loopback(dst->id()));
100       route->link_list.push_back(info.first);
101     }
102   }
103 }
104
105 void ClusterZone::get_graph(const s_xbt_graph_t* graph, std::map<std::string, xbt_node_t, std::less<>>* nodes,
106                             std::map<std::string, xbt_edge_t, std::less<>>* edges)
107 {
108   xbt_assert(router_,
109              "Malformed cluster. This may be because your platform file is a hypergraph while it must be a graph.");
110
111   /* create the router */
112   xbt_node_t routerNode = new_xbt_graph_node(graph, router_->get_cname(), nodes);
113
114   xbt_node_t backboneNode = nullptr;
115   if (backbone_) {
116     backboneNode = new_xbt_graph_node(graph, backbone_->get_cname(), nodes);
117     new_xbt_graph_edge(graph, routerNode, backboneNode, edges);
118   }
119
120   for (auto const& src : get_vertices()) {
121     if (not src->is_router()) {
122       xbt_node_t previous = new_xbt_graph_node(graph, src->get_cname(), nodes);
123
124       std::pair<resource::LinkImpl*, resource::LinkImpl*> info = private_links_.at(src->id());
125
126       if (info.first) { // link up
127         xbt_node_t current = new_xbt_graph_node(graph, info.first->get_cname(), nodes);
128         new_xbt_graph_edge(graph, previous, current, edges);
129
130         if (backbone_) {
131           new_xbt_graph_edge(graph, current, backboneNode, edges);
132         } else {
133           new_xbt_graph_edge(graph, current, routerNode, edges);
134         }
135       }
136
137       if (info.second) { // link down
138         xbt_node_t current = new_xbt_graph_node(graph, info.second->get_cname(), nodes);
139         new_xbt_graph_edge(graph, previous, current, edges);
140
141         if (backbone_) {
142           new_xbt_graph_edge(graph, current, backboneNode, edges);
143         } else {
144           new_xbt_graph_edge(graph, current, routerNode, edges);
145         }
146       }
147     }
148   }
149 }
150
151 void ClusterZone::create_links_for_node(const ClusterCreationArgs* cluster, int id, int /*rank*/, unsigned int position)
152 {
153   std::string link_id = cluster->id + "_link_" + std::to_string(id);
154
155   const s4u::Link* linkUp;
156   const s4u::Link* linkDown;
157   if (cluster->sharing_policy == simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX) {
158     linkUp   = create_link(link_id + "_UP", std::vector<double>{cluster->bw})->set_latency(cluster->lat)->seal();
159     linkDown = create_link(link_id + "_DOWN", std::vector<double>{cluster->bw})->set_latency(cluster->lat)->seal();
160   } else {
161     linkUp   = create_link(link_id, std::vector<double>{cluster->bw})->set_latency(cluster->lat)->seal();
162     linkDown = linkUp;
163   }
164   private_links_.insert({position, {linkUp->get_impl(), linkDown->get_impl()}});
165 }
166
167 void ClusterZone::set_gateway(unsigned int position, NetPoint* gateway)
168 {
169   xbt_assert(not gateway || not gateway->is_netzone(), "ClusterZone: gateway cannot be another netzone %s",
170              gateway->get_cname());
171   gateways_[position] = gateway;
172 }
173
174 NetPoint* ClusterZone::get_gateway(unsigned int position)
175 {
176   NetPoint* res = nullptr;
177   auto it       = gateways_.find(position);
178   if (it != gateways_.end()) {
179     res = it->second;
180   }
181   return res;
182 }
183
184 void ClusterZone::fill_leaf_from_cb(unsigned int position, const std::vector<unsigned int>& dimensions,
185                                     const s4u::ClusterCallbacks& set_callbacks, NetPoint** node_netpoint,
186                                     s4u::Link** lb_link, s4u::Link** limiter_link)
187 {
188   xbt_assert(node_netpoint, "Invalid node_netpoint parameter");
189   xbt_assert(lb_link, "Invalid lb_link parameter");
190   xbt_assert(limiter_link, "Invalid limiter_link paramater");
191   *lb_link      = nullptr;
192   *limiter_link = nullptr;
193
194   // auxiliary function to get dims from index
195   auto index_to_dims = [&dimensions](int index) {
196     std::vector<unsigned int> dims_array(dimensions.size());
197     for (unsigned long i = dimensions.size() - 1; i != 0; --i) {
198       if (index <= 0) {
199         break;
200       }
201       unsigned int value = index % dimensions[i];
202       dims_array[i]      = value;
203       index              = (index / dimensions[i]);
204     }
205     return dims_array;
206   };
207
208   kernel::routing::NetPoint* netpoint = nullptr;
209   kernel::routing::NetPoint* gw       = nullptr;
210   auto dims                           = index_to_dims(position);
211   std::tie(netpoint, gw)              = set_callbacks.netpoint(get_iface(), dims, position);
212   xbt_assert(netpoint, "set_netpoint(elem=%u): Invalid netpoint (nullptr)", position);
213   if (netpoint->is_netzone()) {
214     xbt_assert(gw && not gw->is_netzone(),
215                "set_netpoint(elem=%u): Netpoint (%s) is a netzone, but gateway (%s) is invalid", position,
216                netpoint->get_cname(), gw ? gw->get_cname() : "nullptr");
217   } else {
218     xbt_assert(not gw, "set_netpoint: Netpoint (%s) isn't netzone, gateway must be nullptr", netpoint->get_cname());
219   }
220   // setting gateway
221   set_gateway(position, gw);
222
223   if (set_callbacks.loopback) {
224     s4u::Link* loopback = set_callbacks.loopback(get_iface(), dims, position);
225     xbt_assert(loopback, "set_loopback: Invalid loopback link (nullptr) for element %u", position);
226     set_loopback();
227     add_private_link_at(node_pos(netpoint->id()), {loopback->get_impl(), loopback->get_impl()});
228     *lb_link = loopback;
229   }
230
231   if (set_callbacks.limiter) {
232     s4u::Link* limiter = set_callbacks.limiter(get_iface(), dims, position);
233     xbt_assert(limiter, "set_limiter: Invalid limiter link (nullptr) for element %u", position);
234     set_limiter();
235     add_private_link_at(node_pos_with_loopback(netpoint->id()), {limiter->get_impl(), limiter->get_impl()});
236     *limiter_link = limiter;
237   }
238   *node_netpoint = netpoint;
239 }
240
241 } // namespace routing
242 } // namespace kernel
243 } // namespace simgrid