1 /* Copyright (c) 2014-2017. 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 "src/kernel/routing/FatTreeZone.hpp"
11 #include "src/kernel/routing/NetPoint.hpp"
12 #include "src/surf/network_interface.hpp"
14 #include <boost/algorithm/string/classification.hpp>
15 #include <boost/algorithm/string/split.hpp>
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_fat_tree, surf, "Routing for fat trees");
23 FatTreeZone::FatTreeZone(NetZone* father, const char* name) : ClusterZone(father, name)
25 XBT_DEBUG("Creating a new fat tree.");
28 FatTreeZone::~FatTreeZone()
30 for (unsigned int i = 0; i < this->nodes_.size(); i++) {
31 delete this->nodes_[i];
33 for (unsigned int i = 0; i < this->links_.size(); i++) {
34 delete this->links_[i];
38 bool FatTreeZone::isInSubTree(FatTreeNode* root, FatTreeNode* node)
40 XBT_DEBUG("Is %d(%u,%u) in the sub tree of %d(%u,%u) ?", node->id, node->level, node->position, root->id, root->level,
42 if (root->level <= node->level) {
45 for (unsigned int i = 0; i < node->level; i++) {
46 if (root->label[i] != node->label[i]) {
51 for (unsigned int i = root->level; i < this->levels_; i++) {
52 if (root->label[i] != node->label[i]) {
59 void FatTreeZone::getLocalRoute(NetPoint* src, NetPoint* dst, sg_platf_route_cbarg_t into, double* latency)
62 if (dst->isRouter() || src->isRouter())
65 /* Let's find the source and the destination in our internal structure */
66 auto searchedNode = this->computeNodes_.find(src->id());
67 xbt_assert(searchedNode != this->computeNodes_.end(), "Could not find the source %s [%u] in the fat tree",
68 src->name().c_str(), src->id());
69 FatTreeNode* source = searchedNode->second;
71 searchedNode = this->computeNodes_.find(dst->id());
72 xbt_assert(searchedNode != this->computeNodes_.end(), "Could not find the destination %s [%u] in the fat tree",
73 dst->name().c_str(), dst->id());
74 FatTreeNode* destination = searchedNode->second;
76 XBT_VERB("Get route and latency from '%s' [%u] to '%s' [%u] in a fat tree", src->name().c_str(), src->id(),
77 dst->name().c_str(), dst->id());
79 /* In case destination is the source, and there is a loopback, let's use it instead of going up to a switch */
80 if (source->id == destination->id && this->hasLoopback_) {
81 into->link_list->push_back(source->loopback);
83 *latency += source->loopback->latency();
87 FatTreeNode* currentNode = source;
90 while (not isInSubTree(currentNode, destination)) {
91 int d = destination->position; // as in d-mod-k
93 for (unsigned int i = 0; i < currentNode->level; i++)
94 d /= this->upperLevelNodesNumber_[i];
96 int k = this->upperLevelNodesNumber_[currentNode->level];
98 into->link_list->push_back(currentNode->parents[d]->upLink);
101 *latency += currentNode->parents[d]->upLink->latency();
103 if (this->hasLimiter_)
104 into->link_list->push_back(currentNode->limiterLink);
105 currentNode = currentNode->parents[d]->upNode;
108 XBT_DEBUG("%d(%u,%u) is in the sub tree of %d(%u,%u).", destination->id, destination->level, destination->position,
109 currentNode->id, currentNode->level, currentNode->position);
112 while (currentNode != destination) {
113 for (unsigned int i = 0; i < currentNode->children.size(); i++) {
114 if (i % this->lowerLevelNodesNumber_[currentNode->level - 1] == destination->label[currentNode->level - 1]) {
115 into->link_list->push_back(currentNode->children[i]->downLink);
117 *latency += currentNode->children[i]->downLink->latency();
118 currentNode = currentNode->children[i]->downNode;
119 if (this->hasLimiter_)
120 into->link_list->push_back(currentNode->limiterLink);
121 XBT_DEBUG("%d(%u,%u) is accessible through %d(%u,%u)", destination->id, destination->level,
122 destination->position, currentNode->id, currentNode->level, currentNode->position);
128 /* This function makes the assumption that parse_specific_arguments() and
129 * addNodes() have already been called
131 void FatTreeZone::seal()
133 if (this->levels_ == 0) {
136 this->generateSwitches();
138 if (XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
139 std::stringstream msgBuffer;
141 msgBuffer << "We are creating a fat tree of " << this->levels_ << " levels "
142 << "with " << this->nodesByLevel_[0] << " processing nodes";
143 for (unsigned int i = 1; i <= this->levels_; i++) {
144 msgBuffer << ", " << this->nodesByLevel_[i] << " switches at level " << i;
146 XBT_DEBUG("%s", msgBuffer.str().c_str());
148 msgBuffer << "Nodes are : ";
150 for (unsigned int i = 0; i < this->nodes_.size(); i++) {
151 msgBuffer << this->nodes_[i]->id << "(" << this->nodes_[i]->level << "," << this->nodes_[i]->position << ") ";
153 XBT_DEBUG("%s", msgBuffer.str().c_str());
156 this->generateLabels();
159 // Nodes are totally ordered, by level and then by position, in this->nodes
160 for (unsigned int i = 0; i < this->levels_; i++) {
161 for (unsigned int j = 0; j < this->nodesByLevel_[i]; j++) {
162 this->connectNodeToParents(this->nodes_[k]);
167 if (XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
168 std::stringstream msgBuffer;
169 msgBuffer << "Links are : ";
170 for (unsigned int i = 0; i < this->links_.size(); i++) {
171 msgBuffer << "(" << this->links_[i]->upNode->id << "," << this->links_[i]->downNode->id << ") ";
173 XBT_DEBUG("%s", msgBuffer.str().c_str());
177 int FatTreeZone::connectNodeToParents(FatTreeNode* node)
179 std::vector<FatTreeNode*>::iterator currentParentNode = this->nodes_.begin();
180 int connectionsNumber = 0;
181 const int level = node->level;
182 XBT_DEBUG("We are connecting node %d(%u,%u) to his parents.", node->id, node->level, node->position);
183 currentParentNode += this->getLevelPosition(level + 1);
184 for (unsigned int i = 0; i < this->nodesByLevel_[level + 1]; i++) {
185 if (this->areRelated(*currentParentNode, node)) {
186 XBT_DEBUG("%d(%u,%u) and %d(%u,%u) are related,"
187 " with %u links between them.",
188 node->id, node->level, node->position, (*currentParentNode)->id, (*currentParentNode)->level,
189 (*currentParentNode)->position, this->lowerLevelPortsNumber_[level]);
190 for (unsigned int j = 0; j < this->lowerLevelPortsNumber_[level]; j++) {
191 this->addLink(*currentParentNode, node->label[level] + j * this->lowerLevelNodesNumber_[level], node,
192 (*currentParentNode)->label[level] + j * this->upperLevelNodesNumber_[level]);
198 return connectionsNumber;
201 bool FatTreeZone::areRelated(FatTreeNode* parent, FatTreeNode* child)
203 std::stringstream msgBuffer;
205 if (XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
206 msgBuffer << "Are " << child->id << "(" << child->level << "," << child->position << ") <";
208 for (unsigned int i = 0; i < this->levels_; i++) {
209 msgBuffer << child->label[i] << ",";
213 msgBuffer << " and " << parent->id << "(" << parent->level << "," << parent->position << ") <";
214 for (unsigned int i = 0; i < this->levels_; i++) {
215 msgBuffer << parent->label[i] << ",";
218 msgBuffer << " related ? ";
219 XBT_DEBUG("%s", msgBuffer.str().c_str());
221 if (parent->level != child->level + 1) {
225 for (unsigned int i = 0; i < this->levels_; i++) {
226 if (parent->label[i] != child->label[i] && i + 1 != parent->level) {
233 void FatTreeZone::generateSwitches()
235 XBT_DEBUG("Generating switches.");
236 this->nodesByLevel_.resize(this->levels_ + 1, 0);
238 // Take care of the number of nodes by level
239 this->nodesByLevel_[0] = 1;
240 for (unsigned int i = 0; i < this->levels_; i++)
241 this->nodesByLevel_[0] *= this->lowerLevelNodesNumber_[i];
243 if (this->nodesByLevel_[0] != this->nodes_.size()) {
244 surf_parse_error("The number of provided nodes does not fit with the wanted topology."
245 " Please check your platform description (We need %u nodes, we got %zu)",
246 this->nodesByLevel_[0], this->nodes_.size());
250 for (unsigned int i = 0; i < this->levels_; i++) {
251 int nodesInThisLevel = 1;
253 for (unsigned int j = 0; j <= i; j++)
254 nodesInThisLevel *= this->upperLevelNodesNumber_[j];
256 for (unsigned int j = i + 1; j < this->levels_; j++)
257 nodesInThisLevel *= this->lowerLevelNodesNumber_[j];
259 this->nodesByLevel_[i + 1] = nodesInThisLevel;
262 // Create the switches
264 for (unsigned int i = 0; i < this->levels_; i++) {
265 for (unsigned int j = 0; j < this->nodesByLevel_[i + 1]; j++) {
266 FatTreeNode* newNode = new FatTreeNode(this->cluster_, --k, i + 1, j);
267 XBT_DEBUG("We create the switch %d(%u,%u)", newNode->id, newNode->level, newNode->position);
268 newNode->children.resize(this->lowerLevelNodesNumber_[i] * this->lowerLevelPortsNumber_[i]);
269 if (i != this->levels_ - 1) {
270 newNode->parents.resize(this->upperLevelNodesNumber_[i + 1] * this->lowerLevelPortsNumber_[i + 1]);
272 newNode->label.resize(this->levels_);
273 this->nodes_.push_back(newNode);
278 void FatTreeZone::generateLabels()
280 XBT_DEBUG("Generating labels.");
281 // TODO : check if nodesByLevel and nodes are filled
282 std::vector<int> maxLabel(this->levels_);
283 std::vector<int> currentLabel(this->levels_);
285 for (unsigned int i = 0; i <= this->levels_; i++) {
286 currentLabel.assign(this->levels_, 0);
287 for (unsigned int j = 0; j < this->levels_; j++) {
288 maxLabel[j] = j + 1 > i ? this->lowerLevelNodesNumber_[j] : this->upperLevelNodesNumber_[j];
291 for (unsigned int j = 0; j < this->nodesByLevel_[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::getLevelPosition(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->nodesByLevel_[i];
335 void FatTreeZone::addProcessingNode(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->upperLevelNodesNumber_[0] * this->lowerLevelPortsNumber_[0]);
342 newNode->label.resize(this->levels_);
343 this->computeNodes_.insert(make_pair(id, newNode));
344 this->nodes_.push_back(newNode);
347 void FatTreeZone::addLink(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(sg_platf_cluster_cbarg_t 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
366 if (parameters.size() != 4) {
368 "Fat trees are defined by the levels number and 3 vectors, see the documentation for more information");
371 // The first parts of topo_parameters should be the levels number
373 this->levels_ = std::stoi(parameters[0]);
374 } catch (std::invalid_argument& ia) {
375 throw std::invalid_argument(std::string("First parameter is not the amount of levels:") + parameters[0]);
378 // Then, a l-sized vector standing for the children number by level
379 boost::split(tmp, parameters[1], boost::is_any_of(","));
380 if (tmp.size() != this->levels_) {
381 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
382 ", see the documentation for more information");
384 for (size_t i = 0; i < tmp.size(); i++) {
386 this->lowerLevelNodesNumber_.push_back(std::stoi(tmp[i]));
387 } catch (std::invalid_argument& ia) {
388 throw std::invalid_argument(std::string("Invalid lower level node number:") + tmp[i]);
392 // Then, a l-sized vector standing for the parents number by level
393 boost::split(tmp, parameters[2], boost::is_any_of(","));
394 if (tmp.size() != this->levels_) {
395 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
396 ", see the documentation for more information");
398 for (size_t i = 0; i < tmp.size(); i++) {
400 this->upperLevelNodesNumber_.push_back(std::stoi(tmp[i]));
401 } catch (std::invalid_argument& ia) {
402 throw std::invalid_argument(std::string("Invalid upper level node number:") + tmp[i]);
406 // Finally, a l-sized vector standing for the ports number with the lower level
407 boost::split(tmp, parameters[3], boost::is_any_of(","));
408 if (tmp.size() != this->levels_) {
409 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
410 ", see the documentation for more information");
412 for (size_t i = 0; i < tmp.size(); i++) {
414 this->lowerLevelPortsNumber_.push_back(std::stoi(tmp[i]));
415 } catch (std::invalid_argument& ia) {
416 throw std::invalid_argument(std::string("Invalid lower level port number:") + tmp[i]);
419 this->cluster_ = cluster;
422 void FatTreeZone::generateDotFile(const std::string& filename) const
425 file.open(filename, std::ios::out | std::ios::trunc);
426 xbt_assert(file.is_open(), "Unable to open file %s", filename.c_str());
428 file << "graph AsClusterFatTree {\n";
429 for (unsigned int i = 0; i < this->nodes_.size(); i++) {
430 file << this->nodes_[i]->id;
431 if (this->nodes_[i]->id < 0)
432 file << " [shape=circle];\n";
434 file << " [shape=hexagon];\n";
437 for (unsigned int i = 0; i < this->links_.size(); i++) {
438 file << this->links_[i]->downNode->id << " -- " << this->links_[i]->upNode->id << ";\n";
444 FatTreeNode::FatTreeNode(sg_platf_cluster_cbarg_t cluster, int id, int level, int position)
445 : id(id), level(level), position(position)
447 LinkCreationArgs linkTemplate;
448 if (cluster->limiter_link) {
449 linkTemplate.bandwidth = cluster->limiter_link;
450 linkTemplate.latency = 0;
451 linkTemplate.policy = SURF_LINK_SHARED;
452 linkTemplate.id = "limiter_"+std::to_string(id);
453 sg_platf_new_link(&linkTemplate);
454 this->limiterLink = surf::LinkImpl::byName(linkTemplate.id.c_str());
456 if (cluster->loopback_bw || cluster->loopback_lat) {
457 linkTemplate.bandwidth = cluster->loopback_bw;
458 linkTemplate.latency = cluster->loopback_lat;
459 linkTemplate.policy = SURF_LINK_FATPIPE;
460 linkTemplate.id = "loopback_"+ std::to_string(id);
461 sg_platf_new_link(&linkTemplate);
462 this->loopback = surf::LinkImpl::byName(linkTemplate.id.c_str());
466 FatTreeLink::FatTreeLink(sg_platf_cluster_cbarg_t cluster, FatTreeNode* downNode, FatTreeNode* upNode)
467 : upNode(upNode), downNode(downNode)
469 static int uniqueId = 0;
470 LinkCreationArgs linkTemplate;
471 linkTemplate.bandwidth = cluster->bw;
472 linkTemplate.latency = cluster->lat;
473 linkTemplate.policy = cluster->sharing_policy; // sthg to do with that ?
475 "link_from_" + std::to_string(downNode->id) + "_" + std::to_string(upNode->id) + "_" + std::to_string(uniqueId);
476 sg_platf_new_link(&linkTemplate);
478 if (cluster->sharing_policy == SURF_LINK_FULLDUPLEX) {
479 std::string tmpID = std::string(linkTemplate.id) + "_UP";
480 this->upLink = surf::LinkImpl::byName(tmpID.c_str()); // check link?
481 tmpID = std::string(linkTemplate.id) + "_DOWN";
482 this->downLink = surf::LinkImpl::byName(tmpID.c_str()); // check link ?
484 this->upLink = surf::LinkImpl::byName(linkTemplate.id.c_str());
485 this->downLink = this->upLink;