1 /* Copyright (c) 2014-2020. 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. */
10 #include "simgrid/kernel/routing/FatTreeZone.hpp"
11 #include "simgrid/kernel/routing/NetPoint.hpp"
12 #include "src/surf/network_interface.hpp"
13 #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(NetZoneImpl* father, const std::string& name, resource::NetworkModel* netmodel)
26 : ClusterZone(father, name, netmodel)
28 XBT_DEBUG("Creating a new fat tree.");
31 FatTreeZone::~FatTreeZone()
33 for (FatTreeNode const* node : this->nodes_) {
36 for (FatTreeLink const* link : this->links_) {
41 bool FatTreeZone::is_in_sub_tree(FatTreeNode* root, FatTreeNode* node) const
43 XBT_DEBUG("Is %d(%u,%u) in the sub tree of %d(%u,%u) ?", node->id, node->level, node->position, root->id, root->level,
45 if (root->level <= node->level) {
48 for (unsigned int i = 0; i < node->level; i++) {
49 if (root->label[i] != node->label[i]) {
54 for (unsigned int i = root->level; i < this->levels_; i++) {
55 if (root->label[i] != node->label[i]) {
62 void FatTreeZone::get_local_route(NetPoint* src, NetPoint* dst, RouteCreationArgs* into, double* latency)
64 if (dst->is_router() || src->is_router())
67 /* Let's find the source and the destination in our internal structure */
68 auto searchedNode = this->compute_nodes_.find(src->id());
69 xbt_assert(searchedNode != this->compute_nodes_.end(), "Could not find the source %s [%u] in the fat tree",
70 src->get_cname(), src->id());
71 FatTreeNode* source = searchedNode->second;
73 searchedNode = this->compute_nodes_.find(dst->id());
74 xbt_assert(searchedNode != this->compute_nodes_.end(), "Could not find the destination %s [%u] in the fat tree",
75 dst->get_cname(), dst->id());
76 FatTreeNode* destination = searchedNode->second;
78 XBT_VERB("Get route and latency from '%s' [%u] to '%s' [%u] in a fat tree", src->get_cname(), src->id(),
79 dst->get_cname(), dst->id());
81 /* In case destination is the source, and there is a loopback, let's use it instead of going up to a switch */
82 if (source->id == destination->id && this->has_loopback_) {
83 into->link_list.push_back(source->loopback);
85 *latency += source->loopback->get_latency();
89 FatTreeNode* currentNode = source;
92 while (not is_in_sub_tree(currentNode, destination)) {
93 int d = destination->position; // as in d-mod-k
95 for (unsigned int i = 0; i < currentNode->level; i++)
96 d /= this->num_parents_per_node_[i];
98 int k = this->num_parents_per_node_[currentNode->level];
100 into->link_list.push_back(currentNode->parents[d]->up_link_);
103 *latency += currentNode->parents[d]->up_link_->get_latency();
105 if (this->has_limiter_)
106 into->link_list.push_back(currentNode->limiter_link_);
107 currentNode = currentNode->parents[d]->up_node_;
110 XBT_DEBUG("%d(%u,%u) is in the sub tree of %d(%u,%u).", destination->id, destination->level, destination->position,
111 currentNode->id, currentNode->level, currentNode->position);
114 while (currentNode != destination) {
115 for (unsigned int i = 0; i < currentNode->children.size(); i++) {
116 if (i % this->num_children_per_node_[currentNode->level - 1] == destination->label[currentNode->level - 1]) {
117 into->link_list.push_back(currentNode->children[i]->down_link_);
119 *latency += currentNode->children[i]->down_link_->get_latency();
120 currentNode = currentNode->children[i]->down_node_;
121 if (this->has_limiter_)
122 into->link_list.push_back(currentNode->limiter_link_);
123 XBT_DEBUG("%d(%u,%u) is accessible through %d(%u,%u)", destination->id, destination->level,
124 destination->position, currentNode->id, currentNode->level, currentNode->position);
130 /* This function makes the assumption that parse_specific_arguments() and
131 * addNodes() have already been called
133 void FatTreeZone::seal()
135 if (this->levels_ == 0) {
138 this->generate_switches();
140 if (XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
141 std::stringstream msgBuffer;
143 msgBuffer << "We are creating a fat tree of " << this->levels_ << " levels "
144 << "with " << this->nodes_by_level_[0] << " processing nodes";
145 for (unsigned int i = 1; i <= this->levels_; i++) {
146 msgBuffer << ", " << this->nodes_by_level_[i] << " switches at level " << i;
148 XBT_DEBUG("%s", msgBuffer.str().c_str());
150 msgBuffer << "Nodes are : ";
152 for (FatTreeNode const* node : this->nodes_) {
153 msgBuffer << node->id << "(" << node->level << "," << node->position << ") ";
155 XBT_DEBUG("%s", msgBuffer.str().c_str());
158 this->generate_labels();
161 // Nodes are totally ordered, by level and then by position, in this->nodes
162 for (unsigned int i = 0; i < this->levels_; i++) {
163 for (unsigned int j = 0; j < this->nodes_by_level_[i]; j++) {
164 this->connect_node_to_parents(this->nodes_[k]);
169 if (XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
170 std::stringstream msgBuffer;
171 msgBuffer << "Links are : ";
172 for (FatTreeLink const* link : this->links_) {
173 msgBuffer << "(" << link->up_node_->id << "," << link->down_node_->id << ") ";
175 XBT_DEBUG("%s", msgBuffer.str().c_str());
179 int FatTreeZone::connect_node_to_parents(FatTreeNode* node)
181 auto currentParentNode = this->nodes_.begin();
182 int connectionsNumber = 0;
183 const int level = node->level;
184 XBT_DEBUG("We are connecting node %d(%u,%u) to his parents.", node->id, node->level, node->position);
185 currentParentNode += this->get_level_position(level + 1);
186 for (unsigned int i = 0; i < this->nodes_by_level_[level + 1]; i++) {
187 if (this->are_related(*currentParentNode, node)) {
188 XBT_DEBUG("%d(%u,%u) and %d(%u,%u) are related,"
189 " with %u links between them.",
190 node->id, node->level, node->position, (*currentParentNode)->id, (*currentParentNode)->level,
191 (*currentParentNode)->position, this->num_port_lower_level_[level]);
192 for (unsigned int j = 0; j < this->num_port_lower_level_[level]; j++) {
193 this->add_link(*currentParentNode, node->label[level] + j * this->num_children_per_node_[level], node,
194 (*currentParentNode)->label[level] + j * this->num_parents_per_node_[level]);
200 return connectionsNumber;
203 bool FatTreeZone::are_related(FatTreeNode* parent, FatTreeNode* child) const
205 std::stringstream msgBuffer;
207 if (XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
208 msgBuffer << "Are " << child->id << "(" << child->level << "," << child->position << ") <";
210 for (unsigned int i = 0; i < this->levels_; i++) {
211 msgBuffer << child->label[i] << ",";
215 msgBuffer << " and " << parent->id << "(" << parent->level << "," << parent->position << ") <";
216 for (unsigned int i = 0; i < this->levels_; i++) {
217 msgBuffer << parent->label[i] << ",";
220 msgBuffer << " related ? ";
221 XBT_DEBUG("%s", msgBuffer.str().c_str());
223 if (parent->level != child->level + 1) {
227 for (unsigned int i = 0; i < this->levels_; i++) {
228 if (parent->label[i] != child->label[i] && i + 1 != parent->level) {
235 void FatTreeZone::generate_switches()
237 XBT_DEBUG("Generating switches.");
238 this->nodes_by_level_.resize(this->levels_ + 1, 0);
240 // Take care of the number of nodes by level
241 this->nodes_by_level_[0] = 1;
242 for (unsigned int i = 0; i < this->levels_; i++)
243 this->nodes_by_level_[0] *= this->num_children_per_node_[i];
245 if (this->nodes_by_level_[0] != this->nodes_.size()) {
246 surf_parse_error(std::string("The number of provided nodes does not fit with the wanted topology.") +
247 " Please check your platform description (We need " + std::to_string(this->nodes_by_level_[0]) +
248 "nodes, we got " + std::to_string(this->nodes_.size()));
251 for (unsigned int i = 0; i < this->levels_; i++) {
252 int nodesInThisLevel = 1;
254 for (unsigned int j = 0; j <= i; j++)
255 nodesInThisLevel *= this->num_parents_per_node_[j];
257 for (unsigned int j = i + 1; j < this->levels_; j++)
258 nodesInThisLevel *= this->num_children_per_node_[j];
260 this->nodes_by_level_[i + 1] = nodesInThisLevel;
263 // Create the switches
265 for (unsigned int i = 0; i < this->levels_; i++) {
266 for (unsigned int j = 0; j < this->nodes_by_level_[i + 1]; j++) {
267 auto* newNode = new FatTreeNode(this->cluster_, --k, i + 1, j);
268 XBT_DEBUG("We create the switch %d(%u,%u)", newNode->id, newNode->level, newNode->position);
269 newNode->children.resize(this->num_children_per_node_[i] * this->num_port_lower_level_[i]);
270 if (i != this->levels_ - 1) {
271 newNode->parents.resize(this->num_parents_per_node_[i + 1] * this->num_port_lower_level_[i + 1]);
273 newNode->label.resize(this->levels_);
274 this->nodes_.push_back(newNode);
279 void FatTreeZone::generate_labels()
281 XBT_DEBUG("Generating labels.");
282 // TODO : check if nodesByLevel and nodes are filled
283 std::vector<int> maxLabel(this->levels_);
284 std::vector<int> currentLabel(this->levels_);
286 for (unsigned int i = 0; i <= this->levels_; i++) {
287 currentLabel.assign(this->levels_, 0);
288 for (unsigned int j = 0; j < this->levels_; j++) {
289 maxLabel[j] = j + 1 > i ? this->num_children_per_node_[j] : this->num_parents_per_node_[j];
292 for (unsigned int j = 0; j < this->nodes_by_level_[i]; j++) {
293 if (XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
294 std::stringstream msgBuffer;
296 msgBuffer << "Assigning label <";
297 for (unsigned int l = 0; l < this->levels_; l++) {
298 msgBuffer << currentLabel[l] << ",";
300 msgBuffer << "> to " << k << " (" << i << "," << j << ")";
302 XBT_DEBUG("%s", msgBuffer.str().c_str());
304 this->nodes_[k]->label.assign(currentLabel.begin(), currentLabel.end());
306 bool remainder = true;
307 unsigned int pos = 0;
308 while (remainder && pos < this->levels_) {
310 if (currentLabel[pos] >= maxLabel[pos]) {
311 currentLabel[pos] = 0;
324 int FatTreeZone::get_level_position(const unsigned int level)
326 xbt_assert(level <= this->levels_, "The impossible did happen. Yet again.");
327 int tempPosition = 0;
329 for (unsigned int i = 0; i < level; i++)
330 tempPosition += this->nodes_by_level_[i];
335 void FatTreeZone::add_processing_node(int id)
337 using std::make_pair;
338 static int position = 0;
339 FatTreeNode* newNode;
340 newNode = new FatTreeNode(this->cluster_, id, 0, position++);
341 newNode->parents.resize(this->num_parents_per_node_[0] * this->num_port_lower_level_[0]);
342 newNode->label.resize(this->levels_);
343 this->compute_nodes_.insert(make_pair(id, newNode));
344 this->nodes_.push_back(newNode);
347 void FatTreeZone::add_link(FatTreeNode* parent, unsigned int parentPort, FatTreeNode* child, unsigned int childPort)
349 FatTreeLink* newLink;
350 newLink = new FatTreeLink(this->cluster_, child, parent);
351 XBT_DEBUG("Creating a link between the parent (%u,%u,%u) and the child (%u,%u,%u)", parent->level, parent->position,
352 parentPort, child->level, child->position, childPort);
353 parent->children[parentPort] = newLink;
354 child->parents[childPort] = newLink;
356 this->links_.push_back(newLink);
359 void FatTreeZone::parse_specific_arguments(ClusterCreationArgs* cluster)
361 std::vector<std::string> parameters;
362 std::vector<std::string> tmp;
363 boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
365 // TODO : we have to check for zeros and negative numbers, or it might crash
367 parameters.size() == 4,
368 "Fat trees are defined by the levels number and 3 vectors, see the documentation for more information.");
370 // The first parts of topo_parameters should be the levels number
372 this->levels_ = std::stoi(parameters[0]);
373 } catch (const std::invalid_argument&) {
374 surf_parse_error(std::string("First parameter is not the amount of levels: ") + parameters[0]);
377 // Then, a l-sized vector standing for the children number by level
378 boost::split(tmp, parameters[1], boost::is_any_of(","));
379 surf_parse_assert(tmp.size() == this->levels_, std::string("You specified ") + std::to_string(this->levels_) +
380 " levels but the child count vector (the first one) contains " +
381 std::to_string(tmp.size()) + " levels.");
383 for (std::string const& level : tmp) {
385 this->num_children_per_node_.push_back(std::stoi(level));
386 } catch (const std::invalid_argument&) {
387 surf_parse_error(std::string("Invalid child count: ") + level);
391 // Then, a l-sized vector standing for the parents number by level
392 boost::split(tmp, parameters[2], boost::is_any_of(","));
393 surf_parse_assert(tmp.size() == this->levels_, std::string("You specified ") + std::to_string(this->levels_) +
394 " levels but the parent count vector (the second one) contains " +
395 std::to_string(tmp.size()) + " levels.");
396 for (std::string const& parent : tmp) {
398 this->num_parents_per_node_.push_back(std::stoi(parent));
399 } catch (const std::invalid_argument&) {
400 surf_parse_error(std::string("Invalid parent count: ") + parent);
404 // Finally, a l-sized vector standing for the ports number with the lower level
405 boost::split(tmp, parameters[3], boost::is_any_of(","));
406 surf_parse_assert(tmp.size() == this->levels_, std::string("You specified ") + std::to_string(this->levels_) +
407 " levels but the port count vector (the third one) contains " +
408 std::to_string(tmp.size()) + " levels.");
409 for (std::string const& port : tmp) {
411 this->num_port_lower_level_.push_back(std::stoi(port));
412 } catch (const std::invalid_argument&) {
413 throw std::invalid_argument(std::string("Invalid lower level port number:") + port);
416 this->cluster_ = cluster;
419 void FatTreeZone::generate_dot_file(const std::string& filename) const
422 file.open(filename, std::ios::out | std::ios::trunc);
423 xbt_assert(file.is_open(), "Unable to open file %s", filename.c_str());
425 file << "graph AsClusterFatTree {\n";
426 for (FatTreeNode const* node : this->nodes_) {
429 file << " [shape=circle];\n";
431 file << " [shape=hexagon];\n";
434 for (FatTreeLink const* link : this->links_) {
435 file << link->down_node_->id << " -- " << link->up_node_->id << ";\n";
441 FatTreeNode::FatTreeNode(const ClusterCreationArgs* cluster, int id, int level, int position)
442 : id(id), level(level), position(position)
444 LinkCreationArgs linkTemplate;
445 if (cluster->limiter_link != 0.0) {
446 linkTemplate.bandwidths.push_back(cluster->limiter_link);
447 linkTemplate.latency = 0;
448 linkTemplate.policy = s4u::Link::SharingPolicy::SHARED;
449 linkTemplate.id = "limiter_"+std::to_string(id);
450 sg_platf_new_link(&linkTemplate);
451 this->limiter_link_ = s4u::Link::by_name(linkTemplate.id)->get_impl();
453 if (cluster->loopback_bw != 0.0 || cluster->loopback_lat != 0.0) {
454 linkTemplate.bandwidths.push_back(cluster->loopback_bw);
455 linkTemplate.latency = cluster->loopback_lat;
456 linkTemplate.policy = s4u::Link::SharingPolicy::FATPIPE;
457 linkTemplate.id = "loopback_"+ std::to_string(id);
458 sg_platf_new_link(&linkTemplate);
459 this->loopback = s4u::Link::by_name(linkTemplate.id)->get_impl();
463 FatTreeLink::FatTreeLink(const ClusterCreationArgs* cluster, FatTreeNode* downNode, FatTreeNode* upNode)
464 : up_node_(upNode), down_node_(downNode)
466 static int uniqueId = 0;
467 LinkCreationArgs linkTemplate;
468 linkTemplate.bandwidths.push_back(cluster->bw);
469 linkTemplate.latency = cluster->lat;
470 linkTemplate.policy = cluster->sharing_policy; // sthg to do with that ?
472 "link_from_" + std::to_string(downNode->id) + "_" + std::to_string(upNode->id) + "_" + std::to_string(uniqueId);
473 sg_platf_new_link(&linkTemplate);
475 if (cluster->sharing_policy == s4u::Link::SharingPolicy::SPLITDUPLEX) {
476 this->up_link_ = s4u::Link::by_name(linkTemplate.id + "_UP")->get_impl(); // check link?
477 this->down_link_ = s4u::Link::by_name(linkTemplate.id + "_DOWN")->get_impl(); // check link ?
479 this->up_link_ = s4u::Link::by_name(linkTemplate.id)->get_impl();
480 this->down_link_ = this->up_link_;
484 } // namespace routing
485 } // namespace kernel
486 } // namespace simgrid