1 /* Copyright (c) 2014-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/TorusZone.hpp"
7 #include "simgrid/kernel/routing/NetPoint.hpp"
8 #include "simgrid/s4u/Host.hpp"
9 #include "src/surf/network_interface.hpp"
10 #include "src/surf/xml/platf_private.hpp"
12 #include <boost/algorithm/string/classification.hpp>
13 #include <boost/algorithm/string/split.hpp>
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_cluster_torus, surf_route_cluster, "Torus Routing part of surf");
24 void TorusZone::create_links_for_node(ClusterCreationArgs* cluster, int id, int rank, unsigned int position)
26 /* Create all links that exist in the torus. Each rank creates @a dimensions-1 links */
27 int dim_product = 1; // Needed to calculate the next neighbor_id
29 for (unsigned int j = 0; j < dimensions_.size(); j++) {
30 int current_dimension = dimensions_[j]; // which dimension are we currently in?
31 // we need to iterate over all dimensions and create all links there
32 // The other node the link connects
33 int neighbor_rank_id = ((rank / dim_product) % current_dimension == current_dimension - 1)
34 ? rank - (current_dimension - 1) * dim_product
36 // name of neighbor is not right for non contiguous cluster radicals (as id != rank in this case)
38 std::string(cluster->id) + "_link_from_" + std::to_string(id) + "_to_" + std::to_string(neighbor_rank_id);
39 const s4u::Link* linkup;
40 const s4u::Link* linkdown;
41 if (cluster->sharing_policy == s4u::Link::SharingPolicy::SPLITDUPLEX) {
42 linkup = create_link(link_id + "_UP", std::vector<double>{cluster->bw})->set_latency(cluster->lat)->seal();
43 linkdown = create_link(link_id + "_DOWN", std::vector<double>{cluster->bw})->set_latency(cluster->lat)->seal();
46 linkup = create_link(link_id, std::vector<double>{cluster->bw})->set_latency(cluster->lat)->seal();
50 * Add the link to its appropriate position.
51 * Note that position rankId*(xbt_dynar_length(dimensions)+has_loopback?+has_limiter?)
52 * holds the link "rankId->rankId"
54 add_private_link_at(position + j, {linkup->get_impl(), linkdown->get_impl()});
55 dim_product *= current_dimension;
59 std::vector<unsigned int> TorusZone::parse_topo_parameters(const std::string& topo_parameters)
61 std::vector<std::string> dimensions_str;
62 boost::split(dimensions_str, topo_parameters, boost::is_any_of(","));
63 std::vector<unsigned int> dimensions;
65 if (not dimensions_str.empty()) {
66 /* We are in a torus cluster
67 * Parse attribute dimensions="dim1,dim2,dim3,...,dimN" and save them into a vector.
68 * Additionally, we need to know how many ranks we have in total
70 std::transform(begin(dimensions_str), end(dimensions_str), std::back_inserter(dimensions), surf_parse_get_int);
75 void TorusZone::parse_specific_arguments(ClusterCreationArgs* cluster)
77 set_topology(TorusZone::parse_topo_parameters(cluster->topo_parameters));
80 void TorusZone::set_topology(const std::vector<unsigned int>& dimensions)
82 xbt_assert(not dimensions.empty(), "Torus dimensions cannot be empty");
83 dimensions_ = dimensions;
84 set_num_links_per_node(dimensions_.size());
87 void TorusZone::get_local_route(NetPoint* src, NetPoint* dst, RouteCreationArgs* route, double* lat)
89 XBT_VERB("torus getLocalRoute from '%s'[%u] to '%s'[%u]", src->get_cname(), src->id(), dst->get_cname(), dst->id());
91 if (dst->is_router() || src->is_router())
94 if (src->id() == dst->id() && has_loopback()) {
95 resource::LinkImpl* uplink = get_uplink_from(node_pos(src->id()));
97 route->link_list.push_back(uplink);
99 *lat += uplink->get_latency();
104 * Dimension based routing routes through each dimension consecutively
105 * TODO Change to dynamic assignment
109 * Arrays that hold the coordinates of the current node and the target; comparing the values at the i-th position of
110 * both arrays, we can easily assess whether we need to route into this dimension or not.
112 const unsigned int dsize = dimensions_.size();
113 std::vector<unsigned int> myCoords(dsize);
114 std::vector<unsigned int> targetCoords(dsize);
115 unsigned int dim_size_product = 1;
116 for (unsigned i = 0; i < dsize; i++) {
117 unsigned cur_dim_size = dimensions_[i];
118 myCoords[i] = (src->id() / dim_size_product) % cur_dim_size;
119 targetCoords[i] = (dst->id() / dim_size_product) % cur_dim_size;
120 dim_size_product *= cur_dim_size;
124 * linkOffset describes the offset where the link we want to use is stored(+1 is added because each node has a link
125 * from itself to itself, which can only be the case if src->m_id == dst->m_id -- see above for this special case)
127 int linkOffset = (dsize + 1) * src->id();
129 bool use_lnk_up = false; // Is this link of the form "cur -> next" or "next -> cur"? false means: next -> cur
130 unsigned int current_node = src->id();
131 while (current_node != dst->id()) {
132 unsigned int next_node = 0;
133 unsigned int dim_product = 1; // First, we will route in x-dimension
134 for (unsigned j = 0; j < dsize; j++) {
135 const unsigned cur_dim = dimensions_[j];
136 // current_node/dim_product = position in current dimension
137 if ((current_node / dim_product) % cur_dim != (dst->id() / dim_product) % cur_dim) {
138 if ((targetCoords[j] > myCoords[j] &&
139 targetCoords[j] <= myCoords[j] + cur_dim / 2) // Is the target node on the right, without the wrap-around?
141 (myCoords[j] > cur_dim / 2 && (myCoords[j] + cur_dim / 2) % cur_dim >=
142 targetCoords[j])) { // Or do we need to use the wrap around to reach it?
143 if ((current_node / dim_product) % cur_dim == cur_dim - 1)
144 next_node = (current_node + dim_product - dim_product * cur_dim);
146 next_node = (current_node + dim_product);
148 // HERE: We use *CURRENT* node for calculation (as opposed to next_node)
149 linkOffset = node_pos_with_loopback_limiter(current_node) + j;
151 assert(linkOffset >= 0);
152 } else { // Route to the left
153 if ((current_node / dim_product) % cur_dim == 0)
154 next_node = (current_node - dim_product + dim_product * cur_dim);
156 next_node = (current_node - dim_product);
158 // HERE: We use *next* node for calculation (as opposed to current_node!)
159 linkOffset = node_pos_with_loopback_limiter(next_node) + j;
162 assert(linkOffset >= 0);
164 XBT_DEBUG("torus_get_route_and_latency - current_node: %u, next_node: %u, linkOffset is %i", current_node,
165 next_node, linkOffset);
169 dim_product *= cur_dim;
172 if (has_limiter()) { // limiter for sender
173 route->link_list.push_back(get_uplink_from(node_pos_with_loopback(current_node)));
176 resource::LinkImpl* lnk;
178 lnk = get_uplink_from(linkOffset);
180 lnk = get_downlink_to(linkOffset);
182 route->link_list.push_back(lnk);
184 *lat += lnk->get_latency();
186 current_node = next_node;
188 // set gateways (if any)
189 route->gw_src = get_gateway(src->id());
190 route->gw_dst = get_gateway(dst->id());
193 /** @brief Auxiliary function to create hosts */
194 static std::pair<kernel::routing::NetPoint*, kernel::routing::NetPoint*>
195 create_torus_host(const kernel::routing::ClusterCreationArgs* cluster, s4u::NetZone* zone,
196 const std::vector<unsigned int>& /*coord*/, int id)
198 std::string host_id = std::string(cluster->prefix) + std::to_string(id) + cluster->suffix;
199 XBT_DEBUG("TorusCluster: creating host=%s speed=%f", host_id.c_str(), cluster->speeds.front());
200 const s4u::Host* host = zone->create_host(host_id, cluster->speeds)
201 ->set_core_count(cluster->core_amount)
202 ->set_properties(cluster->properties)
204 return std::make_pair(host->get_netpoint(), nullptr);
207 /** @brief Auxiliary function to create loopback links */
208 static s4u::Link* create_torus_loopback(const kernel::routing::ClusterCreationArgs* cluster, s4u::NetZone* zone,
209 const std::vector<unsigned int>& /*coord*/, int id)
211 std::string link_id = std::string(cluster->id) + "_link_" + std::to_string(id) + "_loopback";
212 XBT_DEBUG("TorusCluster: creating loopback link=%s bw=%f", link_id.c_str(), cluster->loopback_bw);
214 s4u::Link* loopback = zone->create_link(link_id, cluster->loopback_bw)
215 ->set_sharing_policy(simgrid::s4u::Link::SharingPolicy::FATPIPE)
216 ->set_latency(cluster->loopback_lat)
221 /** @brief Auxiliary function to create limiter links */
222 static s4u::Link* create_torus_limiter(const kernel::routing::ClusterCreationArgs* cluster, s4u::NetZone* zone,
223 const std::vector<unsigned int>& /*coord*/, int id)
225 std::string link_id = std::string(cluster->id) + "_link_" + std::to_string(id) + "_limiter";
226 XBT_DEBUG("TorusCluster: creating limiter link=%s bw=%f", link_id.c_str(), cluster->limiter_link);
228 s4u::Link* limiter = zone->create_link(link_id, cluster->limiter_link)->seal();
232 s4u::NetZone* create_torus_zone_with_hosts(const kernel::routing::ClusterCreationArgs* cluster,
233 const s4u::NetZone* parent)
235 using namespace std::placeholders;
236 auto set_host = std::bind(create_torus_host, cluster, _1, _2, _3);
237 std::function<s4u::TorusLinkCb> set_loopback{};
238 std::function<s4u::TorusLinkCb> set_limiter{};
240 if (cluster->loopback_bw > 0 || cluster->loopback_lat > 0) {
241 set_loopback = std::bind(create_torus_loopback, cluster, _1, _2, _3);
244 if (cluster->limiter_link > 0) {
245 set_loopback = std::bind(create_torus_limiter, cluster, _1, _2, _3);
248 return s4u::create_torus_zone(cluster->id, parent, TorusZone::parse_topo_parameters(cluster->topo_parameters),
249 cluster->bw, cluster->lat, cluster->sharing_policy, set_host, set_loopback,
253 } // namespace routing
254 } // namespace kernel
258 NetZone* create_torus_zone(const std::string& name, const NetZone* parent, const std::vector<unsigned int>& dimensions,
259 double bandwidth, double latency, Link::SharingPolicy sharing_policy,
260 const std::function<TorusNetPointCb>& set_netpoint,
261 const std::function<TorusLinkCb>& set_loopback,
262 const std::function<TorusLinkCb>& set_limiter)
264 int tot_elements = std::accumulate(dimensions.begin(), dimensions.end(), 1, std::multiplies<>());
265 if (dimensions.empty() || tot_elements <= 0)
266 throw std::invalid_argument("TorusZone: incorrect dimensions parameter, each value must be > 0");
268 throw std::invalid_argument("TorusZone: incorrect bandwidth for internode communication, bw=" +
269 std::to_string(bandwidth));
271 throw std::invalid_argument("TorusZone: incorrect latency for internode communication, lat=" +
272 std::to_string(latency));
274 // auxiliary function to get dims from index
275 auto index_to_dims = [&dimensions](int index) {
276 std::vector<unsigned int> dims_array(dimensions.size());
277 for (unsigned long i = dimensions.size() - 1; i != 0; --i) {
281 unsigned int value = index % dimensions[i];
282 dims_array[i] = value;
283 index = (index / dimensions[i]);
288 auto* zone = new kernel::routing::TorusZone(name);
289 zone->set_topology(dimensions);
291 zone->set_parent(parent->get_impl());
293 for (int i = 0; i < tot_elements; i++) {
294 kernel::routing::NetPoint* netpoint = nullptr;
295 kernel::routing::NetPoint* gw = nullptr;
296 auto dims = index_to_dims(i);
297 std::tie(netpoint, gw) = set_netpoint(zone->get_iface(), dims, i);
298 xbt_assert(netpoint, "TorusZone::set_netpoint(elem=%d): Invalid netpoint (nullptr)", i);
299 if (netpoint->is_netzone()) {
300 xbt_assert(gw && not gw->is_netzone(),
301 "TorusZone::set_netpoint(elem=%d): Netpoint (%s) is a netzone, but gateway (%s) is invalid", i,
302 netpoint->get_cname(), gw ? gw->get_cname() : "nullptr");
304 xbt_assert(not gw, "TorusZone: Netpoint (%s) isn't netzone, gateway must be nullptr", netpoint->get_cname());
307 zone->set_gateway(i, gw);
310 const Link* loopback = set_loopback(zone->get_iface(), dims, i);
311 xbt_assert(loopback, "TorusZone::set_loopback: Invalid loopback link (nullptr) for element %d", i);
312 zone->set_loopback();
313 zone->add_private_link_at(zone->node_pos(netpoint->id()), {loopback->get_impl(), loopback->get_impl()});
317 const Link* limiter = set_limiter(zone->get_iface(), dims, i);
318 xbt_assert(limiter, "TorusZone::set_limiter: Invalid limiter link (nullptr) for element %d", i);
320 zone->add_private_link_at(zone->node_pos_with_loopback(netpoint->id()),
321 {limiter->get_impl(), limiter->get_impl()});
324 kernel::routing::ClusterCreationArgs params;
326 params.bw = bandwidth;
327 params.lat = latency;
328 params.sharing_policy = sharing_policy;
329 zone->create_links_for_node(¶ms, netpoint->id(), i, zone->node_pos_with_loopback_limiter(netpoint->id()));
332 return zone->get_iface();
336 } // namespace simgrid