1 /* Copyright (c) 2014-2016. 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. */
9 #include "src/kernel/routing/AsClusterFatTree.hpp"
10 #include "src/kernel/routing/NetCard.hpp"
11 #include "src/surf/network_interface.hpp"
15 #include <boost/algorithm/string/split.hpp>
16 #include <boost/algorithm/string/classification.hpp>
18 #include "src/surf/xml/platf.hpp" // FIXME: move that back to the parsing area
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_fat_tree, surf, "Routing for fat trees");
27 AsClusterFatTree::AsClusterFatTree(As* father, const char* name) : AsCluster(father, name)
29 XBT_DEBUG("Creating a new fat tree.");
32 AsClusterFatTree::~AsClusterFatTree() {
33 for (unsigned int i = 0 ; i < this->nodes_.size() ; i++) {
34 delete this->nodes_[i];
36 for (unsigned int i = 0 ; i < this->links_.size() ; i++) {
37 delete this->links_[i];
41 bool AsClusterFatTree::isInSubTree(FatTreeNode *root, FatTreeNode *node) {
42 XBT_DEBUG("Is %d(%u,%u) in the sub tree of %d(%u,%u) ?", node->id,
43 node->level, node->position, root->id, root->level, root->position);
44 if (root->level <= node->level) {
47 for (unsigned int i = 0 ; i < node->level ; i++) {
48 if(root->label[i] != node->label[i]) {
53 for (unsigned int i = root->level ; i < this->levels_ ; i++) {
54 if(root->label[i] != node->label[i]) {
61 void AsClusterFatTree::getLocalRoute(NetCard* src, NetCard* dst, sg_platf_route_cbarg_t into, double* latency)
64 if (dst->isRouter() || src->isRouter())
67 /* Let's find the source and the destination in our internal structure */
68 auto searchedNode = this->computeNodes_.find(src->id());
69 xbt_assert(searchedNode != this->computeNodes_.end(), "Could not find the source %s [%d] in the fat tree",
70 src->name().c_str(), src->id());
71 FatTreeNode* source = searchedNode->second;
73 searchedNode = this->computeNodes_.find(dst->id());
74 xbt_assert(searchedNode != this->computeNodes_.end(), "Could not find the destination %s [%d] in the fat tree",
75 dst->name().c_str(), dst->id());
76 FatTreeNode* destination = searchedNode->second;
78 XBT_VERB("Get route and latency from '%s' [%d] to '%s' [%d] in a fat tree", src->name().c_str(), src->id(),
79 dst->name().c_str(), 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->hasLoopback_) {
83 into->link_list->push_back(source->loopback);
85 *latency += source->loopback->latency();
89 FatTreeNode* currentNode = source;
92 while (!isInSubTree(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->upperLevelNodesNumber_[i];
98 int k = this->upperLevelNodesNumber_[currentNode->level];
100 into->link_list->push_back(currentNode->parents[d]->upLink);
103 *latency += currentNode->parents[d]->upLink->latency();
105 if (this->hasLimiter_)
106 into->link_list->push_back(currentNode->limiterLink);
107 currentNode = currentNode->parents[d]->upNode;
110 XBT_DEBUG("%d(%u,%u) is in the sub tree of %d(%u,%u).", destination->id,
111 destination->level, destination->position, currentNode->id,
112 currentNode->level, currentNode->position);
115 while (currentNode != destination) {
116 for(unsigned int i = 0 ; i < currentNode->children.size() ; i++) {
117 if (i % this->lowerLevelNodesNumber_[currentNode->level - 1] == destination->label[currentNode->level - 1]) {
118 into->link_list->push_back(currentNode->children[i]->downLink);
120 *latency += currentNode->children[i]->downLink->latency();
121 currentNode = currentNode->children[i]->downNode;
122 if (this->hasLimiter_)
123 into->link_list->push_back(currentNode->limiterLink);
124 XBT_DEBUG("%d(%u,%u) is accessible through %d(%u,%u)", destination->id,
125 destination->level, destination->position, currentNode->id,
126 currentNode->level, currentNode->position);
132 /* This function makes the assumption that parse_specific_arguments() and
133 * addNodes() have already been called
135 void AsClusterFatTree::seal(){
136 if(this->levels_ == 0) {
139 this->generateSwitches();
142 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
143 std::stringstream msgBuffer;
145 msgBuffer << "We are creating a fat tree of " << this->levels_ << " levels "
146 << "with " << this->nodesByLevel_[0] << " processing nodes";
147 for (unsigned int i = 1 ; i <= this->levels_ ; i++) {
148 msgBuffer << ", " << this->nodesByLevel_[i] << " switches at level " << i;
150 XBT_DEBUG("%s", msgBuffer.str().c_str());
152 msgBuffer << "Nodes are : ";
154 for (unsigned int i = 0 ; i < this->nodes_.size() ; i++) {
155 msgBuffer << this->nodes_[i]->id << "(" << this->nodes_[i]->level << ","
156 << this->nodes_[i]->position << ") ";
158 XBT_DEBUG("%s", msgBuffer.str().c_str());
162 this->generateLabels();
165 // Nodes are totally ordered, by level and then by position, in this->nodes
166 for (unsigned int i = 0 ; i < this->levels_ ; i++) {
167 for (unsigned int j = 0 ; j < this->nodesByLevel_[i] ; j++) {
168 this->connectNodeToParents(this->nodes_[k]);
173 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
174 std::stringstream msgBuffer;
175 msgBuffer << "Links are : ";
176 for (unsigned int i = 0 ; i < this->links_.size() ; i++) {
177 msgBuffer << "(" << this->links_[i]->upNode->id << ","
178 << this->links_[i]->downNode->id << ") ";
180 XBT_DEBUG("%s", msgBuffer.str().c_str());
186 int AsClusterFatTree::connectNodeToParents(FatTreeNode *node) {
187 std::vector<FatTreeNode*>::iterator currentParentNode = this->nodes_.begin();
188 int connectionsNumber = 0;
189 const int level = node->level;
190 XBT_DEBUG("We are connecting node %d(%u,%u) to his parents.",
191 node->id, node->level, node->position);
192 currentParentNode += this->getLevelPosition(level + 1);
193 for (unsigned int i = 0 ; i < this->nodesByLevel_[level + 1] ; i++ ) {
194 if(this->areRelated(*currentParentNode, node)) {
195 XBT_DEBUG("%d(%u,%u) and %d(%u,%u) are related,"
196 " with %u links between them.", node->id,
197 node->level, node->position, (*currentParentNode)->id,
198 (*currentParentNode)->level, (*currentParentNode)->position, this->lowerLevelPortsNumber_[level]);
199 for (unsigned int j = 0 ; j < this->lowerLevelPortsNumber_[level] ; j++) {
200 this->addLink(*currentParentNode, node->label[level] +
201 j * this->lowerLevelNodesNumber_[level], node,
202 (*currentParentNode)->label[level] +
203 j * this->upperLevelNodesNumber_[level]);
209 return connectionsNumber;
213 bool AsClusterFatTree::areRelated(FatTreeNode *parent, FatTreeNode *child) {
214 std::stringstream msgBuffer;
216 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
217 msgBuffer << "Are " << child->id << "(" << child->level << ","
218 << child->position << ") <";
220 for (unsigned int i = 0 ; i < this->levels_ ; i++) {
221 msgBuffer << child->label[i] << ",";
225 msgBuffer << " and " << parent->id << "(" << parent->level
226 << "," << parent->position << ") <";
227 for (unsigned int i = 0 ; i < this->levels_ ; i++) {
228 msgBuffer << parent->label[i] << ",";
231 msgBuffer << " related ? ";
232 XBT_DEBUG("%s", msgBuffer.str().c_str());
235 if (parent->level != child->level + 1) {
239 for (unsigned int i = 0 ; i < this->levels_; i++) {
240 if (parent->label[i] != child->label[i] && i + 1 != parent->level) {
247 void AsClusterFatTree::generateSwitches() {
248 XBT_DEBUG("Generating switches.");
249 this->nodesByLevel_.resize(this->levels_ + 1, 0);
250 unsigned int nodesRequired = 0;
252 // Take care of the number of nodes by level
253 this->nodesByLevel_[0] = 1;
254 for (unsigned int i = 0 ; i < this->levels_ ; i++)
255 this->nodesByLevel_[0] *= this->lowerLevelNodesNumber_[i];
257 if(this->nodesByLevel_[0] != this->nodes_.size()) {
258 surf_parse_error("The number of provided nodes does not fit with the wanted topology."
259 " Please check your platform description (We need %d nodes, we got %zu)",
260 this->nodesByLevel_[0], this->nodes_.size());
265 for (unsigned int i = 0 ; i < this->levels_ ; i++) {
266 int nodesInThisLevel = 1;
268 for (unsigned int j = 0 ; j <= i ; j++)
269 nodesInThisLevel *= this->upperLevelNodesNumber_[j];
271 for (unsigned int j = i+1 ; j < this->levels_ ; j++)
272 nodesInThisLevel *= this->lowerLevelNodesNumber_[j];
274 this->nodesByLevel_[i+1] = nodesInThisLevel;
275 nodesRequired += nodesInThisLevel;
279 // Create the switches
281 for (unsigned int i = 0 ; i < this->levels_ ; i++) {
282 for (unsigned int j = 0 ; j < this->nodesByLevel_[i + 1] ; j++) {
283 FatTreeNode* newNode = new FatTreeNode(this->cluster_, --k, i + 1, j);
284 XBT_DEBUG("We create the switch %d(%d,%d)", newNode->id, newNode->level, newNode->position);
285 newNode->children.resize(this->lowerLevelNodesNumber_[i] *
286 this->lowerLevelPortsNumber_[i]);
287 if (i != this->levels_ - 1) {
288 newNode->parents.resize(this->upperLevelNodesNumber_[i + 1] *
289 this->lowerLevelPortsNumber_[i + 1]);
291 newNode->label.resize(this->levels_);
292 this->nodes_.push_back(newNode);
297 void AsClusterFatTree::generateLabels() {
298 XBT_DEBUG("Generating labels.");
299 // TODO : check if nodesByLevel and nodes are filled
300 std::vector<int> maxLabel(this->levels_);
301 std::vector<int> currentLabel(this->levels_);
303 for (unsigned int i = 0 ; i <= this->levels_ ; i++) {
304 currentLabel.assign(this->levels_, 0);
305 for (unsigned int j = 0 ; j < this->levels_ ; j++) {
306 maxLabel[j] = j + 1 > i ?
307 this->lowerLevelNodesNumber_[j] : this->upperLevelNodesNumber_[j];
310 for (unsigned int j = 0 ; j < this->nodesByLevel_[i] ; j++) {
312 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug )) {
313 std::stringstream msgBuffer;
315 msgBuffer << "Assigning label <";
316 for (unsigned int l = 0 ; l < this->levels_ ; l++) {
317 msgBuffer << currentLabel[l] << ",";
319 msgBuffer << "> to " << k << " (" << i << "," << j <<")";
321 XBT_DEBUG("%s", msgBuffer.str().c_str());
323 this->nodes_[k]->label.assign(currentLabel.begin(), currentLabel.end());
325 bool remainder = true;
326 unsigned int pos = 0;
327 while (remainder && pos < this->levels_) {
329 if (currentLabel[pos] >= maxLabel[pos]) {
330 currentLabel[pos] = 0;
345 int AsClusterFatTree::getLevelPosition(const unsigned int level) {
346 xbt_assert(level <= this->levels_, "The impossible did happen. Yet again.");
347 int tempPosition = 0;
349 for (unsigned int i = 0 ; i < level ; i++)
350 tempPosition += this->nodesByLevel_[i];
355 void AsClusterFatTree::addProcessingNode(int id) {
356 using std::make_pair;
357 static int position = 0;
358 FatTreeNode* newNode;
359 newNode = new FatTreeNode(this->cluster_, id, 0, position++);
360 newNode->parents.resize(this->upperLevelNodesNumber_[0] *
361 this->lowerLevelPortsNumber_[0]);
362 newNode->label.resize(this->levels_);
363 this->computeNodes_.insert(make_pair(id,newNode));
364 this->nodes_.push_back(newNode);
367 void AsClusterFatTree::addLink(FatTreeNode *parent, unsigned int parentPort,
368 FatTreeNode *child, unsigned int childPort) {
369 FatTreeLink *newLink;
370 newLink = new FatTreeLink(this->cluster_, child, parent);
371 XBT_DEBUG("Creating a link between the parent (%d,%d,%u) and the child (%d,%d,%u)",
372 parent->level, parent->position, parentPort, child->level, child->position, childPort);
373 parent->children[parentPort] = newLink;
374 child->parents[childPort] = newLink;
376 this->links_.push_back(newLink);
379 void AsClusterFatTree::parse_specific_arguments(sg_platf_cluster_cbarg_t cluster) {
380 std::vector<std::string> parameters;
381 std::vector<std::string> tmp;
382 boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
384 // TODO : we have to check for zeros and negative numbers, or it might crash
385 if (parameters.size() != 4){
386 surf_parse_error("Fat trees are defined by the levels number and 3 vectors, see the documentation for more information");
389 // The first parts of topo_parameters should be the levels number
390 this->levels_ = xbt_str_parse_int(parameters[0].c_str(), "First parameter is not the amount of levels: %s");
392 // Then, a l-sized vector standing for the children number by level
393 boost::split(tmp, parameters[1], 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++){
399 this->lowerLevelNodesNumber_.push_back(xbt_str_parse_int(tmp[i].c_str(), "Invalid lower level node number: %s"));
402 // Then, a l-sized vector standing for the parents number by level
403 boost::split(tmp, parameters[2], boost::is_any_of(","));
404 if(tmp.size() != this->levels_) {
405 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
406 ", see the documentation for more information");
408 for(size_t i = 0 ; i < tmp.size() ; i++){
409 this->upperLevelNodesNumber_.push_back(xbt_str_parse_int(tmp[i].c_str(), "Invalid upper level node number: %s"));
412 // Finally, a l-sized vector standing for the ports number with the lower level
413 boost::split(tmp, parameters[3], boost::is_any_of(","));
414 if(tmp.size() != this->levels_) {
415 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
416 ", see the documentation for more information");
419 for(size_t i = 0 ; i < tmp.size() ; i++){
420 this->lowerLevelPortsNumber_.push_back(xbt_str_parse_int(tmp[i].c_str(), "Invalid lower level node number: %s"));
422 this->cluster_ = cluster;
426 void AsClusterFatTree::generateDotFile(const std::string& filename) const {
428 file.open(filename, std::ios::out | std::ios::trunc);
429 xbt_assert(file.is_open(), "Unable to open file %s", filename.c_str());
431 file << "graph AsClusterFatTree {\n";
432 for (unsigned int i = 0 ; i < this->nodes_.size() ; i++) {
433 file << this->nodes_[i]->id;
434 if(this->nodes_[i]->id < 0)
435 file << " [shape=circle];\n";
437 file << " [shape=hexagon];\n";
440 for (unsigned int i = 0 ; i < this->links_.size() ; i++ ) {
441 file << this->links_[i]->downNode->id
443 << this->links_[i]->upNode->id
450 FatTreeNode::FatTreeNode(sg_platf_cluster_cbarg_t cluster, int id, int level,
451 int position) : id(id), level(level),
453 s_sg_platf_link_cbarg_t linkTemplate;
454 if(cluster->limiter_link) {
455 memset(&linkTemplate, 0, sizeof(linkTemplate));
456 linkTemplate.bandwidth = cluster->limiter_link;
457 linkTemplate.latency = 0;
458 linkTemplate.policy = SURF_LINK_SHARED;
459 linkTemplate.id = bprintf("limiter_%d", id);
460 sg_platf_new_link(&linkTemplate);
461 this->limiterLink = Link::byName(linkTemplate.id);
462 free((void*)linkTemplate.id);
464 if(cluster->loopback_bw || cluster->loopback_lat) {
465 memset(&linkTemplate, 0, sizeof(linkTemplate));
466 linkTemplate.bandwidth = cluster->loopback_bw;
467 linkTemplate.latency = cluster->loopback_lat;
468 linkTemplate.policy = SURF_LINK_FATPIPE;
469 linkTemplate.id = bprintf("loopback_%d", id);
470 sg_platf_new_link(&linkTemplate);
471 this->loopback = Link::byName(linkTemplate.id);
472 free((void*)linkTemplate.id);
476 FatTreeLink::FatTreeLink(sg_platf_cluster_cbarg_t cluster,
477 FatTreeNode *downNode,
478 FatTreeNode *upNode) : upNode(upNode),
480 static int uniqueId = 0;
481 s_sg_platf_link_cbarg_t linkTemplate;
482 memset(&linkTemplate, 0, sizeof(linkTemplate));
483 linkTemplate.bandwidth = cluster->bw;
484 linkTemplate.latency = cluster->lat;
485 linkTemplate.policy = cluster->sharing_policy; // sthg to do with that ?
486 linkTemplate.id = bprintf("link_from_%d_to_%d_%d", downNode->id, upNode->id, uniqueId);
487 sg_platf_new_link(&linkTemplate);
490 if (cluster->sharing_policy == SURF_LINK_FULLDUPLEX) {
491 tmpID = std::string(linkTemplate.id) + "_UP";
492 link = Link::byName(tmpID.c_str());
493 this->upLink = link; // check link?
494 tmpID = std::string(linkTemplate.id) + "_DOWN";
495 link = Link::byName(tmpID.c_str());
496 this->downLink = link; // check link ?
499 link = Link::byName(linkTemplate.id);
501 this->downLink = link;
504 free((void*)linkTemplate.id);