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"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_cluster, surf, "Routing part of surf");
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. */
19 ClusterZone::ClusterZone(const std::string& name) : NetZoneImpl(name) {}
21 void ClusterZone::set_loopback()
23 if (not has_loopback_) {
24 num_links_per_node_++;
29 void ClusterZone::set_limiter()
31 if (not has_limiter_) {
32 num_links_per_node_++;
37 void ClusterZone::set_link_characteristics(double bw, double lat, s4u::Link::SharingPolicy sharing_policy)
39 link_sharing_policy_ = sharing_policy;
44 void ClusterZone::add_private_link_at(unsigned int position, std::pair<resource::LinkImpl*, resource::LinkImpl*> link)
46 private_links_.insert({position, link});
49 void ClusterZone::get_local_route(NetPoint* src, NetPoint* dst, Route* route, double* lat)
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?");
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");
59 std::pair<resource::LinkImpl*, resource::LinkImpl*> info = private_links_.at(node_pos(src->id()));
60 route->link_list_.push_back(info.first);
62 *lat += info.first->get_latency();
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);
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);
78 *lat += info.first->get_latency();
83 route->link_list_.push_back(backbone_);
85 *lat += backbone_->get_latency();
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()));
92 if (info.second) { // link down
93 route->link_list_.push_back(info.second);
95 *lat += info.second->get_latency();
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);
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)
108 "Malformed cluster. This may be because your platform file is a hypergraph while it must be a graph.");
110 /* create the router */
111 xbt_node_t routerNode = new_xbt_graph_node(graph, router_->get_cname(), nodes);
113 xbt_node_t backboneNode = nullptr;
115 backboneNode = new_xbt_graph_node(graph, backbone_->get_cname(), nodes);
116 new_xbt_graph_edge(graph, routerNode, backboneNode, edges);
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);
123 std::pair<resource::LinkImpl*, resource::LinkImpl*> info = private_links_.at(src->id());
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);
130 new_xbt_graph_edge(graph, current, backboneNode, edges);
132 new_xbt_graph_edge(graph, current, routerNode, edges);
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);
141 new_xbt_graph_edge(graph, current, backboneNode, edges);
143 new_xbt_graph_edge(graph, current, routerNode, edges);
150 void ClusterZone::create_links(int id, int rank)
152 std::string link_id = get_name() + "_link_" + std::to_string(id);
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();
160 linkUp = create_link(link_id, std::vector<double>{link_bw_})->set_latency(link_lat_)->seal();
163 private_links_.insert({node_pos_with_loopback_limiter(rank), {linkUp->get_impl(), linkDown->get_impl()}});
166 void ClusterZone::set_gateway(unsigned int position, NetPoint* gateway)
168 xbt_assert(not gateway || not gateway->is_netzone(), "ClusterZone: gateway cannot be another netzone %s",
169 gateway->get_cname());
170 gateways_[position] = gateway;
173 NetPoint* ClusterZone::get_gateway(unsigned int position)
175 NetPoint* res = nullptr;
176 auto it = gateways_.find(position);
177 if (it != gateways_.end()) {
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)
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");
191 *limiter_link = nullptr;
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) {
200 unsigned int value = index % dimensions[i];
201 dims_array[i] = value;
202 index = (index / dimensions[i]);
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");
217 xbt_assert(not gw, "set_netpoint: Netpoint (%s) isn't netzone, gateway must be nullptr", netpoint->get_cname());
220 set_gateway(position, gw);
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);
226 add_private_link_at(node_pos(netpoint->id()), {loopback->get_impl(), loopback->get_impl()});
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);
234 add_private_link_at(node_pos_with_loopback(netpoint->id()), {limiter->get_impl(), limiter->get_impl()});
235 *limiter_link = limiter;
237 *node_netpoint = netpoint;
240 } // namespace routing
241 } // namespace kernel
242 } // namespace simgrid