1 /* Copyright (c) 2009-2021. The SimGrid Team. All rights reserved. */
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. */
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
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_cluster, surf, "Routing part of surf");
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. */
20 ClusterZone::ClusterZone(const std::string& name) : NetZoneImpl(name) {}
22 void ClusterZone::set_loopback()
24 if (not has_loopback_) {
25 num_links_per_node_++;
30 void ClusterZone::set_limiter()
32 if (not has_limiter_) {
33 num_links_per_node_++;
38 void ClusterZone::set_link_characteristics(double bw, double lat, s4u::Link::SharingPolicy sharing_policy)
40 link_sharing_policy_ = sharing_policy;
45 void ClusterZone::add_private_link_at(unsigned int position, std::pair<resource::LinkImpl*, resource::LinkImpl*> link)
47 private_links_.insert({position, link});
50 void ClusterZone::get_local_route(NetPoint* src, NetPoint* dst, RouteCreationArgs* route, double* lat)
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?");
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");
60 std::pair<resource::LinkImpl*, resource::LinkImpl*> info = private_links_.at(node_pos(src->id()));
61 route->link_list.push_back(info.first);
63 *lat += info.first->get_latency();
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);
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);
79 *lat += info.first->get_latency();
84 route->link_list.push_back(backbone_);
86 *lat += backbone_->get_latency();
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()));
93 if (info.second) { // link down
94 route->link_list.push_back(info.second);
96 *lat += info.second->get_latency();
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);
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)
109 "Malformed cluster. This may be because your platform file is a hypergraph while it must be a graph.");
111 /* create the router */
112 xbt_node_t routerNode = new_xbt_graph_node(graph, router_->get_cname(), nodes);
114 xbt_node_t backboneNode = nullptr;
116 backboneNode = new_xbt_graph_node(graph, backbone_->get_cname(), nodes);
117 new_xbt_graph_edge(graph, routerNode, backboneNode, edges);
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);
124 std::pair<resource::LinkImpl*, resource::LinkImpl*> info = private_links_.at(src->id());
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);
131 new_xbt_graph_edge(graph, current, backboneNode, edges);
133 new_xbt_graph_edge(graph, current, routerNode, edges);
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);
142 new_xbt_graph_edge(graph, current, backboneNode, edges);
144 new_xbt_graph_edge(graph, current, routerNode, edges);
151 void ClusterZone::create_links_for_node(const ClusterCreationArgs* cluster, int id, int /*rank*/, unsigned int position)
153 std::string link_id = cluster->id + "_link_" + std::to_string(id);
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();
161 linkUp = create_link(link_id, std::vector<double>{cluster->bw})->set_latency(cluster->lat)->seal();
164 private_links_.insert({position, {linkUp->get_impl(), linkDown->get_impl()}});
167 void ClusterZone::set_gateway(unsigned int position, NetPoint* gateway)
169 xbt_assert(not gateway || not gateway->is_netzone(), "ClusterZone: gateway cannot be another netzone %s",
170 gateway->get_cname());
171 gateways_[position] = gateway;
174 NetPoint* ClusterZone::get_gateway(unsigned int position)
176 NetPoint* res = nullptr;
177 auto it = gateways_.find(position);
178 if (it != gateways_.end()) {
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)
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");
192 *limiter_link = nullptr;
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) {
201 unsigned int value = index % dimensions[i];
202 dims_array[i] = value;
203 index = (index / dimensions[i]);
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");
218 xbt_assert(not gw, "set_netpoint: Netpoint (%s) isn't netzone, gateway must be nullptr", netpoint->get_cname());
221 set_gateway(position, gw);
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);
227 add_private_link_at(node_pos(netpoint->id()), {loopback->get_impl(), loopback->get_impl()});
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);
235 add_private_link_at(node_pos_with_loopback(netpoint->id()), {limiter->get_impl(), limiter->get_impl()});
236 *limiter_link = limiter;
238 *node_netpoint = netpoint;
241 } // namespace routing
242 } // namespace kernel
243 } // namespace simgrid