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 "src/surf/network_interface.hpp"
9 #include "src/surf/xml/platf_private.hpp"
11 #include <boost/algorithm/string/classification.hpp>
12 #include <boost/algorithm/string/split.hpp>
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_cluster_torus, surf_route_cluster, "Torus Routing part of surf");
21 TorusZone::TorusZone(const std::string& name) : ClusterZone(name) {}
23 void TorusZone::create_links_for_node(ClusterCreationArgs* cluster, int id, int rank, unsigned int position)
25 /* Create all links that exist in the torus. Each rank creates @a dimensions-1 links */
26 int dim_product = 1; // Needed to calculate the next neighbor_id
28 for (unsigned int j = 0; j < dimensions_.size(); j++) {
29 LinkCreationArgs link;
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);
40 link.bandwidths.push_back(cluster->bw);
41 link.latency = cluster->lat;
42 link.policy = cluster->sharing_policy;
43 sg_platf_new_link(&link);
44 resource::LinkImpl* linkUp;
45 resource::LinkImpl* linkDown;
46 if (link.policy == s4u::Link::SharingPolicy::SPLITDUPLEX) {
47 linkUp = s4u::Link::by_name(link_id + "_UP")->get_impl();
48 linkDown = s4u::Link::by_name(link_id + "_DOWN")->get_impl();
50 linkUp = s4u::Link::by_name(link_id)->get_impl();
54 * Add the link to its appropriate position.
55 * Note that position rankId*(xbt_dynar_length(dimensions)+has_loopback?+has_limiter?)
56 * holds the link "rankId->rankId"
58 add_private_link_at(position + j, {linkUp, linkDown});
59 dim_product *= current_dimension;
63 void TorusZone::parse_specific_arguments(ClusterCreationArgs* cluster)
65 std::vector<std::string> dimensions;
66 boost::split(dimensions, cluster->topo_parameters, boost::is_any_of(","));
68 if (not dimensions.empty()) {
69 /* We are in a torus cluster
70 * Parse attribute dimensions="dim1,dim2,dim3,...,dimN" and save them into a vector.
71 * Additionally, we need to know how many ranks we have in total
73 for (auto const& group : dimensions)
74 dimensions_.push_back(surf_parse_get_int(group));
76 set_num_links_per_node(dimensions_.size());
80 void TorusZone::get_local_route(NetPoint* src, NetPoint* dst, RouteCreationArgs* route, double* lat)
82 XBT_VERB("torus getLocalRoute from '%s'[%u] to '%s'[%u]", src->get_cname(), src->id(), dst->get_cname(), dst->id());
84 if (dst->is_router() || src->is_router())
87 if (src->id() == dst->id() && has_loopback()) {
88 resource::LinkImpl* uplink = get_uplink_from(node_pos(src->id()));
90 route->link_list.push_back(uplink);
92 *lat += uplink->get_latency();
97 * Dimension based routing routes through each dimension consecutively
98 * TODO Change to dynamic assignment
102 * Arrays that hold the coordinates of the current node and the target; comparing the values at the i-th position of
103 * both arrays, we can easily assess whether we need to route into this dimension or not.
105 const unsigned int dsize = dimensions_.size();
106 std::vector<unsigned int> myCoords(dsize);
107 std::vector<unsigned int> targetCoords(dsize);
108 unsigned int dim_size_product = 1;
109 for (unsigned i = 0; i < dsize; i++) {
110 unsigned cur_dim_size = dimensions_[i];
111 myCoords[i] = (src->id() / dim_size_product) % cur_dim_size;
112 targetCoords[i] = (dst->id() / dim_size_product) % cur_dim_size;
113 dim_size_product *= cur_dim_size;
117 * linkOffset describes the offset where the link we want to use is stored(+1 is added because each node has a link
118 * from itself to itself, which can only be the case if src->m_id == dst->m_id -- see above for this special case)
120 int nodeOffset = (dsize + 1) * src->id();
122 int linkOffset = nodeOffset;
123 bool use_lnk_up = false; // Is this link of the form "cur -> next" or "next -> cur"? false means: next -> cur
124 unsigned int current_node = src->id();
125 while (current_node != dst->id()) {
126 unsigned int next_node = 0;
127 unsigned int dim_product = 1; // First, we will route in x-dimension
128 for (unsigned j = 0; j < dsize; j++) {
129 const unsigned cur_dim = dimensions_[j];
130 // current_node/dim_product = position in current dimension
131 if ((current_node / dim_product) % cur_dim != (dst->id() / dim_product) % cur_dim) {
132 if ((targetCoords[j] > myCoords[j] &&
133 targetCoords[j] <= myCoords[j] + cur_dim / 2) // Is the target node on the right, without the wrap-around?
135 (myCoords[j] > cur_dim / 2 && (myCoords[j] + cur_dim / 2) % cur_dim >=
136 targetCoords[j])) { // Or do we need to use the wrap around to reach it?
137 if ((current_node / dim_product) % cur_dim == cur_dim - 1)
138 next_node = (current_node + dim_product - dim_product * cur_dim);
140 next_node = (current_node + dim_product);
142 // HERE: We use *CURRENT* node for calculation (as opposed to next_node)
143 nodeOffset = node_pos(current_node);
144 linkOffset = node_pos_with_loopback_limiter(current_node) + j;
146 assert(linkOffset >= 0);
147 } else { // Route to the left
148 if ((current_node / dim_product) % cur_dim == 0)
149 next_node = (current_node - dim_product + dim_product * cur_dim);
151 next_node = (current_node - dim_product);
153 // HERE: We use *next* node for calculation (as opposed to current_node!)
154 nodeOffset = node_pos(next_node);
155 linkOffset = node_pos_with_loopback_limiter(next_node) + j;
158 assert(linkOffset >= 0);
160 XBT_DEBUG("torus_get_route_and_latency - current_node: %u, next_node: %u, linkOffset is %i", current_node,
161 next_node, linkOffset);
165 dim_product *= cur_dim;
168 if (has_limiter()) { // limiter for sender
169 route->link_list.push_back(get_uplink_from(node_pos_with_loopback(nodeOffset)));
172 resource::LinkImpl* lnk;
174 lnk = get_uplink_from(linkOffset);
176 lnk = get_downlink_to(linkOffset);
178 route->link_list.push_back(lnk);
180 *lat += lnk->get_latency();
182 current_node = next_node;
185 } // namespace routing
186 } // namespace kernel
187 } // namespace simgrid