Logo AND Algorithmique Numérique Distribuée

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