1 /* Copyright (c) 2014-2018. 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");
18 inline void rankId_to_coords(int rankId, std::vector<unsigned int> dimensions, unsigned int (*coords)[4])
20 unsigned int dim_size_product = 1;
22 for (auto const& cur_dim_size : dimensions) {
23 (*coords)[i] = (rankId / dim_size_product) % cur_dim_size;
24 dim_size_product *= cur_dim_size;
32 TorusZone::TorusZone(NetZone* father, std::string name) : ClusterZone(father, name)
36 void TorusZone::create_links_for_node(ClusterCreationArgs* cluster, int id, int rank, unsigned int position)
38 /* Create all links that exist in the torus. Each rank creates @a dimensions-1 links */
39 int dim_product = 1; // Needed to calculate the next neighbor_id
41 for (unsigned int j = 0; j < dimensions_.size(); j++) {
42 LinkCreationArgs link;
43 int current_dimension = dimensions_.at(j); // which dimension are we currently in?
44 // we need to iterate over all dimensions and create all links there
45 // The other node the link connects
46 int neighbor_rank_id = ((static_cast<int>(rank) / dim_product) % current_dimension == current_dimension - 1)
47 ? rank - (current_dimension - 1) * dim_product
49 // name of neighbor is not right for non contiguous cluster radicals (as id != rank in this case)
51 std::string(cluster->id) + "_link_from_" + std::to_string(id) + "_to_" + std::to_string(neighbor_rank_id);
53 link.bandwidth = cluster->bw;
54 link.latency = cluster->lat;
55 link.policy = cluster->sharing_policy;
56 sg_platf_new_link(&link);
57 surf::LinkImpl* linkUp;
58 surf::LinkImpl* linkDown;
59 if (link.policy == SURF_LINK_SPLITDUPLEX) {
60 std::string tmp_link = link_id + "_UP";
61 linkUp = surf::LinkImpl::byName(tmp_link);
62 tmp_link = link_id + "_DOWN";
63 linkDown = surf::LinkImpl::byName(tmp_link);
65 linkUp = surf::LinkImpl::byName(link_id);
69 * Add the link to its appropriate position.
70 * Note that position rankId*(xbt_dynar_length(dimensions)+has_loopback?+has_limiter?)
71 * holds the link "rankId->rankId"
73 private_links_.insert({position + j, {linkUp, linkDown}});
74 dim_product *= current_dimension;
79 void TorusZone::parse_specific_arguments(ClusterCreationArgs* cluster)
81 std::vector<std::string> dimensions;
82 boost::split(dimensions, cluster->topo_parameters, boost::is_any_of(","));
84 if (not dimensions.empty()) {
85 /* We are in a torus cluster
86 * Parse attribute dimensions="dim1,dim2,dim3,...,dimN" and save them into a vector.
87 * Additionally, we need to know how many ranks we have in total
89 for (auto const& group : dimensions)
90 dimensions_.push_back(surf_parse_get_int(group));
92 num_links_per_node_ = dimensions_.size();
96 void TorusZone::getLocalRoute(NetPoint* src, NetPoint* dst, RouteCreationArgs* route, double* lat)
99 XBT_VERB("torus getLocalRoute from '%s'[%u] to '%s'[%u]", src->getCname(), src->id(), dst->getCname(), dst->id());
101 if (dst->isRouter() || src->isRouter())
104 if (src->id() == dst->id() && has_loopback_) {
105 std::pair<surf::LinkImpl*, surf::LinkImpl*> info = private_links_.at(src->id() * num_links_per_node_);
107 route->link_list.push_back(info.first);
109 *lat += info.first->latency();
114 * Dimension based routing routes through each dimension consecutively
115 * TODO Change to dynamic assignment
117 unsigned int current_node = src->id();
118 unsigned int next_node = 0;
120 * Arrays that hold the coordinates of the current node andthe target; comparing the values at the i-th position of
121 * both arrays, we can easily assess whether we need to route into this dimension or not.
123 unsigned int myCoords[4];
124 rankId_to_coords(src->id(), dimensions_, &myCoords);
125 unsigned int targetCoords[4];
126 rankId_to_coords(dst->id(), dimensions_, &targetCoords);
128 * linkOffset describes the offset where the link we want to use is stored(+1 is added because each node has a link
129 * from itself to itself, which can only be the case if src->m_id == dst->m_id -- see above for this special case)
131 int nodeOffset = (dimensions_.size() + 1) * src->id();
133 int linkOffset = nodeOffset;
134 bool use_lnk_up = false; // Is this link of the form "cur -> next" or "next -> cur"?
135 // false means: next -> cur
136 while (current_node != dst->id()) {
137 unsigned int dim_product = 1; // First, we will route in x-dimension
139 for (auto const& cur_dim : dimensions_) {
140 // current_node/dim_product = position in current dimension
141 if ((current_node / dim_product) % cur_dim != (dst->id() / dim_product) % cur_dim) {
143 if ((targetCoords[j] > myCoords[j] &&
144 targetCoords[j] <= myCoords[j] + cur_dim / 2) // Is the target node on the right, without the wrap-around?
145 || (myCoords[j] > cur_dim / 2 &&
146 (myCoords[j] + cur_dim / 2) % cur_dim >=
147 targetCoords[j])) { // Or do we need to use the wrap around to reach it?
148 if ((current_node / dim_product) % cur_dim == cur_dim - 1)
149 next_node = (current_node + dim_product - dim_product * cur_dim);
151 next_node = (current_node + dim_product);
153 // HERE: We use *CURRENT* node for calculation (as opposed to next_node)
154 nodeOffset = current_node * (num_links_per_node_);
155 linkOffset = nodeOffset + (has_loopback_ ? 1 : 0) + (has_limiter_ ? 1 : 0) + j;
157 assert(linkOffset >= 0);
158 } else { // Route to the left
159 if ((current_node / dim_product) % cur_dim == 0)
160 next_node = (current_node - dim_product + dim_product * cur_dim);
162 next_node = (current_node - dim_product);
164 // HERE: We use *next* node for calculation (as opposed to current_node!)
165 nodeOffset = next_node * (num_links_per_node_);
166 linkOffset = nodeOffset + j + (has_loopback_ ? 1 : 0) + (has_limiter_ ? 1 : 0);
169 assert(linkOffset >= 0);
171 XBT_DEBUG("torus_get_route_and_latency - current_node: %u, next_node: %u, linkOffset is %i", current_node,
172 next_node, linkOffset);
177 dim_product *= cur_dim;
180 std::pair<surf::LinkImpl*, surf::LinkImpl*> info;
182 if (has_limiter_) { // limiter for sender
183 info = private_links_.at(nodeOffset + (has_loopback_ ? 1 : 0));
184 route->link_list.push_back(info.first);
187 info = private_links_.at(linkOffset);
189 if (use_lnk_up == false) {
190 route->link_list.push_back(info.second);
192 *lat += info.second->latency();
194 route->link_list.push_back(info.first);
196 *lat += info.first->latency();
198 current_node = next_node;