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 bool FatTreeZone::is_in_sub_tree(const FatTreeNode* root, const FatTreeNode* node) const
27 XBT_DEBUG("Is %d(%u,%u) in the sub tree of %d(%u,%u) ?", node->id, node->level, node->position, root->id, root->level,
29 if (root->level <= node->level) {
32 for (unsigned int i = 0; i < node->level; i++) {
33 if (root->label[i] != node->label[i]) {
38 for (unsigned int i = root->level; i < this->levels_; i++) {
39 if (root->label[i] != node->label[i]) {
46 void FatTreeZone::get_local_route(NetPoint* src, NetPoint* dst, Route* into, double* latency)
48 if (dst->is_router() || src->is_router())
51 /* Let's find the source and the destination in our internal structure */
52 auto searchedNode = this->compute_nodes_.find(src->id());
53 xbt_assert(searchedNode != this->compute_nodes_.end(), "Could not find the source %s [%u] in the fat tree",
54 src->get_cname(), src->id());
55 const FatTreeNode* source = searchedNode->second.get();
57 searchedNode = this->compute_nodes_.find(dst->id());
58 xbt_assert(searchedNode != this->compute_nodes_.end(), "Could not find the destination %s [%u] in the fat tree",
59 dst->get_cname(), dst->id());
60 const FatTreeNode* destination = searchedNode->second.get();
62 XBT_VERB("Get route and latency from '%s' [%u] to '%s' [%u] in a fat tree", src->get_cname(), src->id(),
63 dst->get_cname(), dst->id());
65 /* In case destination is the source, and there is a loopback, let's use it instead of going up to a switch */
66 if (source->id == destination->id && has_loopback()) {
67 into->link_list_.push_back(source->loopback_);
69 *latency += source->loopback_->get_latency();
73 const FatTreeNode* currentNode = source;
76 while (not is_in_sub_tree(currentNode, destination)) {
77 int d = destination->position; // as in d-mod-k
79 for (unsigned int i = 0; i < currentNode->level; i++)
80 d /= this->num_parents_per_node_[i];
82 int k = this->num_parents_per_node_[currentNode->level];
85 if (currentNode->limiter_link_)
86 into->link_list_.push_back(currentNode->limiter_link_);
88 into->link_list_.push_back(currentNode->parents[d]->up_link_);
91 *latency += currentNode->parents[d]->up_link_->get_latency();
93 currentNode = currentNode->parents[d]->up_node_;
96 XBT_DEBUG("%d(%u,%u) is in the sub tree of %d(%u,%u).", destination->id, destination->level, destination->position,
97 currentNode->id, currentNode->level, currentNode->position);
100 while (currentNode != destination) {
101 for (unsigned int i = 0; i < currentNode->children.size(); i++) {
102 if (i % this->num_children_per_node_[currentNode->level - 1] == destination->label[currentNode->level - 1]) {
104 into->link_list_.push_back(currentNode->children[i]->down_link_);
106 if (currentNode->limiter_link_)
107 into->link_list_.push_back(currentNode->limiter_link_);
110 *latency += currentNode->children[i]->down_link_->get_latency();
111 currentNode = currentNode->children[i]->down_node_;
112 XBT_DEBUG("%d(%u,%u) is accessible through %d(%u,%u)", destination->id, destination->level,
113 destination->position, currentNode->id, currentNode->level, currentNode->position);
117 if (currentNode->limiter_link_) { // limiter for receiver/destination
118 into->link_list_.push_back(currentNode->limiter_link_);
120 // set gateways (if any)
121 into->gw_src_ = get_gateway(src->id());
122 into->gw_dst_ = get_gateway(dst->id());
125 void FatTreeZone::build_upper_levels(const s4u::ClusterCallbacks& set_callbacks)
127 generate_switches(set_callbacks);
131 // Nodes are totally ordered, by level and then by position, in this->nodes
132 for (unsigned int i = 0; i < levels_; i++) {
133 for (unsigned int j = 0; j < nodes_by_level_[i]; j++) {
134 connect_node_to_parents(nodes_[k].get());
140 void FatTreeZone::do_seal()
142 if (this->levels_ == 0) {
145 if (not XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
149 /* for debugging purpose only, Fat-Tree is already build when seal is called */
150 std::stringstream msgBuffer;
152 msgBuffer << "We are creating a fat tree of " << this->levels_ << " levels "
153 << "with " << this->nodes_by_level_[0] << " processing nodes";
154 for (unsigned int i = 1; i <= this->levels_; i++) {
155 msgBuffer << ", " << this->nodes_by_level_[i] << " switches at level " << i;
157 XBT_DEBUG("%s", msgBuffer.str().c_str());
159 msgBuffer << "Nodes are : ";
161 for (auto const& node : this->nodes_) {
162 msgBuffer << node->id << "(" << node->level << "," << node->position << ") ";
164 XBT_DEBUG("%s", msgBuffer.str().c_str());
167 msgBuffer << "Links are : ";
168 for (auto const& link : this->links_) {
169 msgBuffer << "(" << link->up_node_->id << "," << link->down_node_->id << ") ";
171 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->get(), 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->get(), 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(const s4u::ClusterCallbacks& set_callbacks)
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 /* get limiter for this router */
259 auto get_limiter = [this, &set_callbacks](unsigned int i, unsigned int j, int id) -> resource::LinkImpl* {
260 kernel::resource::LinkImpl* limiter = nullptr;
261 if (set_callbacks.limiter) {
262 const auto* s4u_link = set_callbacks.limiter(get_iface(), {i + 1, j}, id);
264 limiter = s4u_link->get_impl();
269 // Create the switches
271 for (unsigned int i = 0; i < this->levels_; i++) {
272 for (unsigned int j = 0; j < this->nodes_by_level_[i + 1]; j++) {
274 auto newNode = std::make_shared<FatTreeNode>(id, i + 1, j, get_limiter(i, j, id), nullptr);
275 XBT_DEBUG("We create the switch %d(%u,%u)", newNode->id, newNode->level, newNode->position);
276 newNode->children.resize(this->num_children_per_node_[i] * this->num_port_lower_level_[i]);
277 if (i != this->levels_ - 1) {
278 newNode->parents.resize(this->num_parents_per_node_[i + 1] * this->num_port_lower_level_[i + 1]);
280 newNode->label.resize(this->levels_);
281 this->nodes_.emplace_back(newNode);
286 void FatTreeZone::generate_labels()
288 XBT_DEBUG("Generating labels.");
289 // TODO : check if nodesByLevel and nodes are filled
290 std::vector<int> maxLabel(this->levels_);
291 std::vector<int> currentLabel(this->levels_);
293 for (unsigned int i = 0; i <= this->levels_; i++) {
294 currentLabel.assign(this->levels_, 0);
295 for (unsigned int j = 0; j < this->levels_; j++) {
296 maxLabel[j] = j + 1 > i ? this->num_children_per_node_[j] : this->num_parents_per_node_[j];
299 for (unsigned int j = 0; j < this->nodes_by_level_[i]; j++) {
300 if (XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
301 std::stringstream msgBuffer;
303 msgBuffer << "Assigning label <";
304 for (unsigned int l = 0; l < this->levels_; l++) {
305 msgBuffer << currentLabel[l] << ",";
307 msgBuffer << "> to " << k << " (" << i << "," << j << ")";
309 XBT_DEBUG("%s", msgBuffer.str().c_str());
311 this->nodes_[k]->label.assign(currentLabel.begin(), currentLabel.end());
313 bool remainder = true;
314 unsigned int pos = 0;
315 while (remainder && pos < this->levels_) {
317 if (currentLabel[pos] >= maxLabel[pos]) {
318 currentLabel[pos] = 0;
331 int FatTreeZone::get_level_position(const unsigned int level)
333 xbt_assert(level <= this->levels_, "The impossible did happen. Yet again.");
334 int tempPosition = 0;
336 for (unsigned int i = 0; i < level; i++)
337 tempPosition += this->nodes_by_level_[i];
342 void FatTreeZone::add_processing_node(int id, resource::LinkImpl* limiter, resource::LinkImpl* loopback)
344 using std::make_pair;
345 static int position = 0;
346 auto newNode = std::make_shared<FatTreeNode>(id, 0, position++, limiter, loopback);
347 newNode->parents.resize(this->num_parents_per_node_[0] * this->num_port_lower_level_[0]);
348 newNode->label.resize(this->levels_);
349 this->compute_nodes_.insert(make_pair(id, newNode));
350 this->nodes_.emplace_back(newNode);
353 void FatTreeZone::add_link(FatTreeNode* parent, unsigned int parentPort, FatTreeNode* child, unsigned int childPort)
355 static int uniqueId = 0;
356 const s4u::Link* linkup;
357 const s4u::Link* linkdown;
359 "link_from_" + std::to_string(child->id) + "_" + std::to_string(parent->id) + "_" + std::to_string(uniqueId);
361 if (get_link_sharing_policy() == s4u::Link::SharingPolicy::SPLITDUPLEX) {
363 create_link(id + "_UP", std::vector<double>{get_link_bandwidth()})->set_latency(get_link_latency())->seal();
365 create_link(id + "_DOWN", std::vector<double>{get_link_bandwidth()})->set_latency(get_link_latency())->seal();
367 linkup = create_link(id, std::vector<double>{get_link_bandwidth()})->set_latency(get_link_latency())->seal();
372 auto newLink = std::make_shared<FatTreeLink>(child, parent, linkup->get_impl(), linkdown->get_impl());
373 XBT_DEBUG("Creating a link between the parent (%u,%u,%u) and the child (%u,%u,%u)", parent->level, parent->position,
374 parentPort, child->level, child->position, childPort);
375 parent->children[parentPort] = newLink;
376 child->parents[childPort] = newLink;
378 this->links_.emplace_back(newLink);
381 void FatTreeZone::check_topology(unsigned int n_levels, const std::vector<unsigned int>& down_links,
382 const std::vector<unsigned int>& up_links, const std::vector<unsigned int>& link_count)
385 /* check number of levels */
387 throw std::invalid_argument("FatTreeZone: invalid number of levels, must be > 0");
389 auto check_vector = [&n_levels](const std::vector<unsigned int>& vector, const std::string& var_name) {
390 if (vector.size() != n_levels)
391 throw std::invalid_argument("FatTreeZone: invalid " + var_name + " parameter, vector has " +
392 std::to_string(vector.size()) + " elements, must have " + std::to_string(n_levels));
394 auto check_zero = [](unsigned int i) { return i == 0; };
395 if (std::any_of(vector.begin(), vector.end(), check_zero))
396 throw std::invalid_argument("FatTreeZone: invalid " + var_name + " parameter, all values must be greater than 0");
399 /* check remaining vectors */
400 check_vector(down_links, "down links");
401 check_vector(up_links, "up links");
402 check_vector(link_count, "link count");
405 void FatTreeZone::set_topology(unsigned int n_levels, const std::vector<unsigned int>& down_links,
406 const std::vector<unsigned int>& up_links, const std::vector<unsigned int>& link_count)
409 num_children_per_node_ = down_links;
410 num_parents_per_node_ = up_links;
411 num_port_lower_level_ = link_count;
414 s4u::FatTreeParams FatTreeZone::parse_topo_parameters(const std::string& topo_parameters)
416 std::vector<std::string> parameters;
417 std::vector<std::string> tmp;
418 unsigned int n_lev = 0;
419 std::vector<unsigned int> down;
420 std::vector<unsigned int> up;
421 std::vector<unsigned int> count;
422 boost::split(parameters, topo_parameters, boost::is_any_of(";"));
425 parameters.size() == 4,
426 "Fat trees are defined by the levels number and 3 vectors, see the documentation for more information.");
428 // The first parts of topo_parameters should be the levels number
430 n_lev = std::stoi(parameters[0]);
431 } catch (const std::invalid_argument&) {
432 surf_parse_error(std::string("First parameter is not the amount of levels: ") + parameters[0]);
435 // Then, a l-sized vector standing for the children number by level
436 boost::split(tmp, parameters[1], boost::is_any_of(","));
437 surf_parse_assert(tmp.size() == n_lev, std::string("You specified ") + std::to_string(n_lev) +
438 " levels but the child count vector (the first one) contains " +
439 std::to_string(tmp.size()) + " levels.");
441 for (std::string const& level : tmp) {
443 down.push_back(std::stoi(level));
444 } catch (const std::invalid_argument&) {
445 surf_parse_error(std::string("Invalid child count: ") + level);
449 // Then, a l-sized vector standing for the parents number by level
450 boost::split(tmp, parameters[2], boost::is_any_of(","));
451 surf_parse_assert(tmp.size() == n_lev, std::string("You specified ") + std::to_string(n_lev) +
452 " levels but the parent count vector (the second one) contains " +
453 std::to_string(tmp.size()) + " levels.");
454 for (std::string const& parent : tmp) {
456 up.push_back(std::stoi(parent));
457 } catch (const std::invalid_argument&) {
458 surf_parse_error(std::string("Invalid parent count: ") + parent);
462 // Finally, a l-sized vector standing for the ports number with the lower level
463 boost::split(tmp, parameters[3], boost::is_any_of(","));
464 surf_parse_assert(tmp.size() == n_lev, std::string("You specified ") + std::to_string(n_lev) +
465 " levels but the port count vector (the third one) contains " +
466 std::to_string(tmp.size()) + " levels.");
467 for (std::string const& port : tmp) {
469 count.push_back(std::stoi(port));
470 } catch (const std::invalid_argument&) {
471 throw std::invalid_argument(std::string("Invalid lower level port number:") + port);
474 return s4u::FatTreeParams(n_lev, down, up, count);
477 void FatTreeZone::generate_dot_file(const std::string& filename) const
480 file.open(filename, std::ios::out | std::ios::trunc);
481 xbt_assert(file.is_open(), "Unable to open file %s", filename.c_str());
483 file << "graph AsClusterFatTree {\n";
484 for (auto const& node : this->nodes_) {
487 file << " [shape=circle];\n";
489 file << " [shape=hexagon];\n";
492 for (auto const& link : this->links_) {
493 file << link->down_node_->id << " -- " << link->up_node_->id << ";\n";
498 } // namespace routing
499 } // namespace kernel
502 FatTreeParams::FatTreeParams(unsigned int n_levels, const std::vector<unsigned int>& down_links,
503 const std::vector<unsigned int>& up_links, const std::vector<unsigned int>& links_number)
504 : levels(n_levels), down(down_links), up(up_links), number(links_number)
506 kernel::routing::FatTreeZone::check_topology(levels, down, up, number);
509 NetZone* create_fatTree_zone(const std::string& name, const NetZone* parent, const FatTreeParams& params,
510 const ClusterCallbacks& set_callbacks, double bandwidth, double latency,
511 Link::SharingPolicy sharing_policy)
515 throw std::invalid_argument("FatTreeZone: incorrect bandwidth for internode communication, bw=" +
516 std::to_string(bandwidth));
518 throw std::invalid_argument("FatTreeZone: incorrect latency for internode communication, lat=" +
519 std::to_string(latency));
522 auto* zone = new kernel::routing::FatTreeZone(name);
523 zone->set_topology(params.levels, params.down, params.up, params.number);
525 zone->set_parent(parent->get_impl());
526 zone->set_link_characteristics(bandwidth, latency, sharing_policy);
529 unsigned int tot_elements = std::accumulate(params.down.begin(), params.down.end(), 1, std::multiplies<>());
530 for (unsigned int i = 0; i < tot_elements; i++) {
531 kernel::routing::NetPoint* netpoint;
534 /* coordinates are based on 2 indexes: number of levels and id */
535 zone->fill_leaf_from_cb(i, {params.levels + 1, tot_elements}, set_callbacks, &netpoint, &loopback, &limiter);
536 zone->add_processing_node(i, limiter ? limiter->get_impl() : nullptr, loopback ? loopback->get_impl() : nullptr);
538 zone->build_upper_levels(set_callbacks);
540 return zone->get_iface();
544 } // namespace simgrid