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. */
11 #include "simgrid/kernel/routing/FatTreeZone.hpp"
12 #include "simgrid/kernel/routing/NetPoint.hpp"
13 #include "src/surf/network_interface.hpp"
14 #include "src/surf/xml/platf_private.hpp"
16 #include <boost/algorithm/string/classification.hpp>
17 #include <boost/algorithm/string/split.hpp>
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_fat_tree, surf, "Routing for fat trees");
25 FatTreeZone::~FatTreeZone()
27 for (FatTreeNode const* node : this->nodes_)
29 for (FatTreeLink const* link : this->links_)
33 bool FatTreeZone::is_in_sub_tree(FatTreeNode* root, FatTreeNode* node) const
35 XBT_DEBUG("Is %d(%u,%u) in the sub tree of %d(%u,%u) ?", node->id, node->level, node->position, root->id, root->level,
37 if (root->level <= node->level) {
40 for (unsigned int i = 0; i < node->level; i++) {
41 if (root->label[i] != node->label[i]) {
46 for (unsigned int i = root->level; i < this->levels_; i++) {
47 if (root->label[i] != node->label[i]) {
54 void FatTreeZone::get_local_route(NetPoint* src, NetPoint* dst, RouteCreationArgs* into, double* latency)
56 if (dst->is_router() || src->is_router())
59 /* Let's find the source and the destination in our internal structure */
60 auto searchedNode = this->compute_nodes_.find(src->id());
61 xbt_assert(searchedNode != this->compute_nodes_.end(), "Could not find the source %s [%u] in the fat tree",
62 src->get_cname(), src->id());
63 FatTreeNode* source = searchedNode->second;
65 searchedNode = this->compute_nodes_.find(dst->id());
66 xbt_assert(searchedNode != this->compute_nodes_.end(), "Could not find the destination %s [%u] in the fat tree",
67 dst->get_cname(), dst->id());
68 FatTreeNode* destination = searchedNode->second;
70 XBT_VERB("Get route and latency from '%s' [%u] to '%s' [%u] in a fat tree", src->get_cname(), src->id(),
71 dst->get_cname(), dst->id());
73 /* In case destination is the source, and there is a loopback, let's use it instead of going up to a switch */
74 if (source->id == destination->id && has_loopback()) {
75 into->link_list.push_back(source->loopback_);
77 *latency += source->loopback_->get_latency();
81 FatTreeNode* currentNode = source;
84 while (not is_in_sub_tree(currentNode, destination)) {
85 int d = destination->position; // as in d-mod-k
87 for (unsigned int i = 0; i < currentNode->level; i++)
88 d /= this->num_parents_per_node_[i];
90 int k = this->num_parents_per_node_[currentNode->level];
92 into->link_list.push_back(currentNode->parents[d]->up_link_);
95 *latency += currentNode->parents[d]->up_link_->get_latency();
97 if (currentNode->limiter_link_)
98 into->link_list.push_back(currentNode->limiter_link_);
99 currentNode = currentNode->parents[d]->up_node_;
102 XBT_DEBUG("%d(%u,%u) is in the sub tree of %d(%u,%u).", destination->id, destination->level, destination->position,
103 currentNode->id, currentNode->level, currentNode->position);
106 while (currentNode != destination) {
107 for (unsigned int i = 0; i < currentNode->children.size(); i++) {
108 if (i % this->num_children_per_node_[currentNode->level - 1] == destination->label[currentNode->level - 1]) {
109 into->link_list.push_back(currentNode->children[i]->down_link_);
111 *latency += currentNode->children[i]->down_link_->get_latency();
112 currentNode = currentNode->children[i]->down_node_;
113 if (currentNode->limiter_link_)
114 into->link_list.push_back(currentNode->limiter_link_);
115 XBT_DEBUG("%d(%u,%u) is accessible through %d(%u,%u)", destination->id, destination->level,
116 destination->position, currentNode->id, currentNode->level, currentNode->position);
120 // set gateways (if any)
121 into->gw_src = get_gateway(src->id());
122 into->gw_dst = get_gateway(dst->id());
125 /* This function makes the assumption that parse_specific_arguments() and
126 * addNodes() have already been called
128 void FatTreeZone::do_seal()
130 if (this->levels_ == 0) {
133 this->generate_switches();
135 if (XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
136 std::stringstream msgBuffer;
138 msgBuffer << "We are creating a fat tree of " << this->levels_ << " levels "
139 << "with " << this->nodes_by_level_[0] << " processing nodes";
140 for (unsigned int i = 1; i <= this->levels_; i++) {
141 msgBuffer << ", " << this->nodes_by_level_[i] << " switches at level " << i;
143 XBT_DEBUG("%s", msgBuffer.str().c_str());
145 msgBuffer << "Nodes are : ";
147 for (FatTreeNode const* node : this->nodes_) {
148 msgBuffer << node->id << "(" << node->level << "," << node->position << ") ";
150 XBT_DEBUG("%s", msgBuffer.str().c_str());
153 this->generate_labels();
156 // Nodes are totally ordered, by level and then by position, in this->nodes
157 for (unsigned int i = 0; i < this->levels_; i++) {
158 for (unsigned int j = 0; j < this->nodes_by_level_[i]; j++) {
159 this->connect_node_to_parents(this->nodes_[k]);
164 if (XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
165 std::stringstream msgBuffer;
166 msgBuffer << "Links are : ";
167 for (FatTreeLink const* link : this->links_) {
168 msgBuffer << "(" << link->up_node_->id << "," << link->down_node_->id << ") ";
170 XBT_DEBUG("%s", msgBuffer.str().c_str());
174 int FatTreeZone::connect_node_to_parents(FatTreeNode* node)
176 auto currentParentNode = this->nodes_.begin();
177 int connectionsNumber = 0;
178 const int level = node->level;
179 XBT_DEBUG("We are connecting node %d(%u,%u) to his parents.", node->id, node->level, node->position);
180 currentParentNode += this->get_level_position(level + 1);
181 for (unsigned int i = 0; i < this->nodes_by_level_[level + 1]; i++) {
182 if (this->are_related(*currentParentNode, node)) {
183 XBT_DEBUG("%d(%u,%u) and %d(%u,%u) are related,"
184 " with %u links between them.",
185 node->id, node->level, node->position, (*currentParentNode)->id, (*currentParentNode)->level,
186 (*currentParentNode)->position, this->num_port_lower_level_[level]);
187 for (unsigned int j = 0; j < this->num_port_lower_level_[level]; j++) {
188 this->add_link(*currentParentNode, node->label[level] + j * this->num_children_per_node_[level], node,
189 (*currentParentNode)->label[level] + j * this->num_parents_per_node_[level]);
195 return connectionsNumber;
198 bool FatTreeZone::are_related(FatTreeNode* parent, FatTreeNode* child) const
200 std::stringstream msgBuffer;
202 if (XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
203 msgBuffer << "Are " << child->id << "(" << child->level << "," << child->position << ") <";
205 for (unsigned int i = 0; i < this->levels_; i++) {
206 msgBuffer << child->label[i] << ",";
210 msgBuffer << " and " << parent->id << "(" << parent->level << "," << parent->position << ") <";
211 for (unsigned int i = 0; i < this->levels_; i++) {
212 msgBuffer << parent->label[i] << ",";
215 msgBuffer << " related ? ";
216 XBT_DEBUG("%s", msgBuffer.str().c_str());
218 if (parent->level != child->level + 1) {
222 for (unsigned int i = 0; i < this->levels_; i++) {
223 if (parent->label[i] != child->label[i] && i + 1 != parent->level) {
230 void FatTreeZone::generate_switches()
232 XBT_DEBUG("Generating switches.");
233 this->nodes_by_level_.resize(this->levels_ + 1, 0);
235 // Take care of the number of nodes by level
236 this->nodes_by_level_[0] = 1;
237 for (unsigned int i = 0; i < this->levels_; i++)
238 this->nodes_by_level_[0] *= this->num_children_per_node_[i];
240 if (this->nodes_by_level_[0] != this->nodes_.size()) {
241 surf_parse_error(std::string("The number of provided nodes does not fit with the wanted topology.") +
242 " Please check your platform description (We need " + std::to_string(this->nodes_by_level_[0]) +
243 "nodes, we got " + std::to_string(this->nodes_.size()));
246 for (unsigned int i = 0; i < this->levels_; i++) {
247 int nodesInThisLevel = 1;
249 for (unsigned int j = 0; j <= i; j++)
250 nodesInThisLevel *= this->num_parents_per_node_[j];
252 for (unsigned int j = i + 1; j < this->levels_; j++)
253 nodesInThisLevel *= this->num_children_per_node_[j];
255 this->nodes_by_level_[i + 1] = nodesInThisLevel;
258 // Create the switches
260 for (unsigned int i = 0; i < this->levels_; i++) {
261 for (unsigned int j = 0; j < this->nodes_by_level_[i + 1]; j++) {
262 auto* newNode = new FatTreeNode(--k, i + 1, j, nullptr, nullptr);
263 XBT_DEBUG("We create the switch %d(%u,%u)", newNode->id, newNode->level, newNode->position);
264 newNode->children.resize(this->num_children_per_node_[i] * this->num_port_lower_level_[i]);
265 if (i != this->levels_ - 1) {
266 newNode->parents.resize(this->num_parents_per_node_[i + 1] * this->num_port_lower_level_[i + 1]);
268 newNode->label.resize(this->levels_);
269 this->nodes_.push_back(newNode);
274 void FatTreeZone::generate_labels()
276 XBT_DEBUG("Generating labels.");
277 // TODO : check if nodesByLevel and nodes are filled
278 std::vector<int> maxLabel(this->levels_);
279 std::vector<int> currentLabel(this->levels_);
281 for (unsigned int i = 0; i <= this->levels_; i++) {
282 currentLabel.assign(this->levels_, 0);
283 for (unsigned int j = 0; j < this->levels_; j++) {
284 maxLabel[j] = j + 1 > i ? this->num_children_per_node_[j] : this->num_parents_per_node_[j];
287 for (unsigned int j = 0; j < this->nodes_by_level_[i]; j++) {
288 if (XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
289 std::stringstream msgBuffer;
291 msgBuffer << "Assigning label <";
292 for (unsigned int l = 0; l < this->levels_; l++) {
293 msgBuffer << currentLabel[l] << ",";
295 msgBuffer << "> to " << k << " (" << i << "," << j << ")";
297 XBT_DEBUG("%s", msgBuffer.str().c_str());
299 this->nodes_[k]->label.assign(currentLabel.begin(), currentLabel.end());
301 bool remainder = true;
302 unsigned int pos = 0;
303 while (remainder && pos < this->levels_) {
305 if (currentLabel[pos] >= maxLabel[pos]) {
306 currentLabel[pos] = 0;
319 int FatTreeZone::get_level_position(const unsigned int level)
321 xbt_assert(level <= this->levels_, "The impossible did happen. Yet again.");
322 int tempPosition = 0;
324 for (unsigned int i = 0; i < level; i++)
325 tempPosition += this->nodes_by_level_[i];
330 void FatTreeZone::add_processing_node(int id, resource::LinkImpl* limiter, resource::LinkImpl* loopback)
332 using std::make_pair;
333 static int position = 0;
334 auto* newNode = new FatTreeNode(id, 0, position++, limiter, loopback);
335 newNode->parents.resize(this->num_parents_per_node_[0] * this->num_port_lower_level_[0]);
336 newNode->label.resize(this->levels_);
337 this->compute_nodes_.insert(make_pair(id, newNode));
338 this->nodes_.push_back(newNode);
341 void FatTreeZone::add_link(FatTreeNode* parent, unsigned int parentPort, FatTreeNode* child, unsigned int childPort)
343 static int uniqueId = 0;
344 const s4u::Link* linkup;
345 const s4u::Link* linkdown;
347 "link_from_" + std::to_string(child->id) + "_" + std::to_string(parent->id) + "_" + std::to_string(uniqueId);
349 if (get_link_sharing_policy() == s4u::Link::SharingPolicy::SPLITDUPLEX) {
351 create_link(id + "_UP", std::vector<double>{get_link_bandwidth()})->set_latency(get_link_latency())->seal();
353 create_link(id + "_DOWN", std::vector<double>{get_link_bandwidth()})->set_latency(get_link_latency())->seal();
355 linkup = create_link(id, std::vector<double>{get_link_bandwidth()})->set_latency(get_link_latency())->seal();
360 auto* newLink = new FatTreeLink(child, parent, linkup->get_impl(), linkdown->get_impl());
361 XBT_DEBUG("Creating a link between the parent (%u,%u,%u) and the child (%u,%u,%u)", parent->level, parent->position,
362 parentPort, child->level, child->position, childPort);
363 parent->children[parentPort] = newLink;
364 child->parents[childPort] = newLink;
366 this->links_.push_back(newLink);
369 void FatTreeZone::check_topology(unsigned int n_levels, const std::vector<unsigned int>& down_links,
370 const std::vector<unsigned int>& up_links, const std::vector<unsigned int>& link_count)
373 /* check number of levels */
375 throw std::invalid_argument("FatTreeZone: invalid number of levels, must be > 0");
377 auto check_vector = [&n_levels](const std::vector<unsigned int>& vector, const std::string& var_name) {
378 if (vector.size() != n_levels)
379 throw std::invalid_argument("FatTreeZone: invalid " + var_name + " parameter, vector has " +
380 std::to_string(vector.size()) + " elements, must have " + std::to_string(n_levels));
382 auto check_zero = [](unsigned int i) { return i == 0; };
383 if (std::any_of(vector.begin(), vector.end(), check_zero))
384 throw std::invalid_argument("FatTreeZone: invalid " + var_name + " parameter, all values must be greater than 0");
387 /* check remaining vectors */
388 check_vector(down_links, "down links");
389 check_vector(up_links, "up links");
390 check_vector(link_count, "link count");
393 void FatTreeZone::set_topology(unsigned int n_levels, const std::vector<unsigned int>& down_links,
394 const std::vector<unsigned int>& up_links, const std::vector<unsigned int>& link_count)
397 num_children_per_node_ = down_links;
398 num_parents_per_node_ = up_links;
399 num_port_lower_level_ = link_count;
402 s4u::FatTreeParams FatTreeZone::parse_topo_parameters(const std::string& topo_parameters)
404 std::vector<std::string> parameters;
405 std::vector<std::string> tmp;
406 unsigned int n_lev = 0;
407 std::vector<unsigned int> down;
408 std::vector<unsigned int> up;
409 std::vector<unsigned int> count;
410 boost::split(parameters, topo_parameters, boost::is_any_of(";"));
413 parameters.size() == 4,
414 "Fat trees are defined by the levels number and 3 vectors, see the documentation for more information.");
416 // The first parts of topo_parameters should be the levels number
418 n_lev = std::stoi(parameters[0]);
419 } catch (const std::invalid_argument&) {
420 surf_parse_error(std::string("First parameter is not the amount of levels: ") + parameters[0]);
423 // Then, a l-sized vector standing for the children number by level
424 boost::split(tmp, parameters[1], boost::is_any_of(","));
425 surf_parse_assert(tmp.size() == n_lev, std::string("You specified ") + std::to_string(n_lev) +
426 " levels but the child count vector (the first one) contains " +
427 std::to_string(tmp.size()) + " levels.");
429 for (std::string const& level : tmp) {
431 down.push_back(std::stoi(level));
432 } catch (const std::invalid_argument&) {
433 surf_parse_error(std::string("Invalid child count: ") + level);
437 // Then, a l-sized vector standing for the parents number by level
438 boost::split(tmp, parameters[2], boost::is_any_of(","));
439 surf_parse_assert(tmp.size() == n_lev, std::string("You specified ") + std::to_string(n_lev) +
440 " levels but the parent count vector (the second one) contains " +
441 std::to_string(tmp.size()) + " levels.");
442 for (std::string const& parent : tmp) {
444 up.push_back(std::stoi(parent));
445 } catch (const std::invalid_argument&) {
446 surf_parse_error(std::string("Invalid parent count: ") + parent);
450 // Finally, a l-sized vector standing for the ports number with the lower level
451 boost::split(tmp, parameters[3], boost::is_any_of(","));
452 surf_parse_assert(tmp.size() == n_lev, std::string("You specified ") + std::to_string(n_lev) +
453 " levels but the port count vector (the third one) contains " +
454 std::to_string(tmp.size()) + " levels.");
455 for (std::string const& port : tmp) {
457 count.push_back(std::stoi(port));
458 } catch (const std::invalid_argument&) {
459 throw std::invalid_argument(std::string("Invalid lower level port number:") + port);
462 return s4u::FatTreeParams(n_lev, down, up, count);
465 void FatTreeZone::generate_dot_file(const std::string& filename) const
468 file.open(filename, std::ios::out | std::ios::trunc);
469 xbt_assert(file.is_open(), "Unable to open file %s", filename.c_str());
471 file << "graph AsClusterFatTree {\n";
472 for (FatTreeNode const* node : this->nodes_) {
475 file << " [shape=circle];\n";
477 file << " [shape=hexagon];\n";
480 for (FatTreeLink const* link : this->links_) {
481 file << link->down_node_->id << " -- " << link->up_node_->id << ";\n";
486 } // namespace routing
487 } // namespace kernel
490 FatTreeParams::FatTreeParams(unsigned int n_levels, const std::vector<unsigned int>& down_links,
491 const std::vector<unsigned int>& up_links, const std::vector<unsigned int>& links_number)
492 : levels(n_levels), down(down_links), up(up_links), number(links_number)
494 kernel::routing::FatTreeZone::check_topology(levels, down, up, number);
497 NetZone* create_fatTree_zone(const std::string& name, const NetZone* parent, const FatTreeParams& params,
498 const ClusterCallbacks& set_callbacks, double bandwidth, double latency,
499 Link::SharingPolicy sharing_policy)
503 throw std::invalid_argument("FatTreeZone: incorrect bandwidth for internode communication, bw=" +
504 std::to_string(bandwidth));
506 throw std::invalid_argument("FatTreeZone: incorrect latency for internode communication, lat=" +
507 std::to_string(latency));
510 auto* zone = new kernel::routing::FatTreeZone(name);
511 zone->set_topology(params.levels, params.down, params.up, params.number);
513 zone->set_parent(parent->get_impl());
514 zone->set_link_characteristics(bandwidth, latency, sharing_policy);
517 int tot_elements = std::accumulate(params.down.begin(), params.down.end(), 1, std::multiplies<>());
518 for (int i = 0; i < tot_elements; i++) {
519 kernel::routing::NetPoint* netpoint;
522 zone->fill_leaf_from_cb(i, params.down, set_callbacks, &netpoint, &loopback, &limiter);
523 zone->add_processing_node(i, limiter ? limiter->get_impl() : nullptr, loopback ? loopback->get_impl() : nullptr);
526 return zone->get_iface();
530 } // namespace simgrid