9 #include "src/surf/surf_routing_cluster_fat_tree.hpp"
12 #include <boost/algorithm/string/split.hpp>
13 #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");
26 AsClusterFatTree::AsClusterFatTree(const char*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::getRouteAndLatency(NetCard *src,
63 sg_platf_route_cbarg_t into,
65 FatTreeNode *source, *destination, *currentNode;
67 std::map<int, FatTreeNode*>::const_iterator tempIter;
69 if (dst->isRouter() || src->isRouter())
72 /* Let's find the source and the destination in our internal structure */
73 tempIter = this->computeNodes_.find(src->id());
76 if (tempIter == this->computeNodes_.end()) {
77 xbt_die("Could not find the source %s [%d] in the fat tree", src->name(),
80 source = tempIter->second;
81 tempIter = this->computeNodes_.find(dst->id());
82 if (tempIter == this->computeNodes_.end()) {
83 xbt_die("Could not find the destination %s [%d] in the fat tree",
84 dst->name(), dst->id());
88 destination = tempIter->second;
90 XBT_VERB("Get route and latency from '%s' [%d] to '%s' [%d] in a fat tree",
91 src->name(), src->id(), dst->name(), dst->id());
93 /* In case destination is the source, and there is a loopback, let's get
94 through it instead of going up to a switch*/
95 if(source->id == destination->id && this->has_loopback_) {
96 into->link_list->push_back(source->loopback);
98 *latency += source->loopback->getLatency();
103 currentNode = source;
106 while (!isInSubTree(currentNode, destination)) {
107 int d, k; // as in d-mod-k
108 d = destination->position;
110 for (unsigned int i = 0 ; i < currentNode->level ; i++) {
111 d /= this->upperLevelNodesNumber_[i];
113 k = this->upperLevelNodesNumber_[currentNode->level];
115 into->link_list->push_back(currentNode->parents[d]->upLink);
118 *latency += currentNode->parents[d]->upLink->getLatency();
121 if (this->has_limiter_) {
122 into->link_list->push_back(currentNode->limiterLink);
124 currentNode = currentNode->parents[d]->upNode;
127 XBT_DEBUG("%d(%u,%u) is in the sub tree of %d(%u,%u).", destination->id,
128 destination->level, destination->position, currentNode->id,
129 currentNode->level, currentNode->position);
132 while(currentNode != destination) {
133 for(unsigned int i = 0 ; i < currentNode->children.size() ; i++) {
134 if(i % this->lowerLevelNodesNumber_[currentNode->level - 1] ==
135 destination->label[currentNode->level - 1]) {
136 into->link_list->push_back(currentNode->children[i]->downLink);
138 *latency += currentNode->children[i]->downLink->getLatency();
140 currentNode = currentNode->children[i]->downNode;
141 if (this->has_limiter_) {
142 into->link_list->push_back(currentNode->limiterLink);
144 XBT_DEBUG("%d(%u,%u) is accessible through %d(%u,%u)", destination->id,
145 destination->level, destination->position, currentNode->id,
146 currentNode->level, currentNode->position);
152 /* This function makes the assumption that parse_specific_arguments() and
153 * addNodes() have already been called
155 void AsClusterFatTree::create_links(){
156 if(this->levels_ == 0) {
159 this->generateSwitches();
162 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
163 std::stringstream msgBuffer;
165 msgBuffer << "We are creating a fat tree of " << this->levels_ << " levels "
166 << "with " << this->nodesByLevel_[0] << " processing nodes";
167 for (unsigned int i = 1 ; i <= this->levels_ ; i++) {
168 msgBuffer << ", " << this->nodesByLevel_[i] << " switches at level " << i;
170 XBT_DEBUG("%s", msgBuffer.str().c_str());
172 msgBuffer << "Nodes are : ";
174 for (unsigned int i = 0 ; i < this->nodes_.size() ; i++) {
175 msgBuffer << this->nodes_[i]->id << "(" << this->nodes_[i]->level << ","
176 << this->nodes_[i]->position << ") ";
178 XBT_DEBUG("%s", msgBuffer.str().c_str());
182 this->generateLabels();
185 // Nodes are totally ordered, by level and then by position, in this->nodes
186 for (unsigned int i = 0 ; i < this->levels_ ; i++) {
187 for (unsigned int j = 0 ; j < this->nodesByLevel_[i] ; j++) {
188 this->connectNodeToParents(this->nodes_[k]);
193 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
194 std::stringstream msgBuffer;
195 msgBuffer << "Links are : ";
196 for (unsigned int i = 0 ; i < this->links_.size() ; i++) {
197 msgBuffer << "(" << this->links_[i]->upNode->id << ","
198 << this->links_[i]->downNode->id << ") ";
200 XBT_DEBUG("%s", msgBuffer.str().c_str());
206 int AsClusterFatTree::connectNodeToParents(FatTreeNode *node) {
207 std::vector<FatTreeNode*>::iterator currentParentNode = this->nodes_.begin();
208 int connectionsNumber = 0;
209 const int level = node->level;
210 XBT_DEBUG("We are connecting node %d(%u,%u) to his parents.",
211 node->id, node->level, node->position);
212 currentParentNode += this->getLevelPosition(level + 1);
213 for (unsigned int i = 0 ; i < this->nodesByLevel_[level + 1] ; i++ ) {
214 if(this->areRelated(*currentParentNode, node)) {
215 XBT_DEBUG("%d(%u,%u) and %d(%u,%u) are related,"
216 " with %u links between them.", node->id,
217 node->level, node->position, (*currentParentNode)->id,
218 (*currentParentNode)->level, (*currentParentNode)->position, this->lowerLevelPortsNumber_[level]);
219 for (unsigned int j = 0 ; j < this->lowerLevelPortsNumber_[level] ; j++) {
220 this->addLink(*currentParentNode, node->label[level] +
221 j * this->lowerLevelNodesNumber_[level], node,
222 (*currentParentNode)->label[level] +
223 j * this->upperLevelNodesNumber_[level]);
229 return connectionsNumber;
233 bool AsClusterFatTree::areRelated(FatTreeNode *parent, FatTreeNode *child) {
234 std::stringstream msgBuffer;
236 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
237 msgBuffer << "Are " << child->id << "(" << child->level << ","
238 << child->position << ") <";
240 for (unsigned int i = 0 ; i < this->levels_ ; i++) {
241 msgBuffer << child->label[i] << ",";
245 msgBuffer << " and " << parent->id << "(" << parent->level
246 << "," << parent->position << ") <";
247 for (unsigned int i = 0 ; i < this->levels_ ; i++) {
248 msgBuffer << parent->label[i] << ",";
251 msgBuffer << " related ? ";
252 XBT_DEBUG("%s", msgBuffer.str().c_str());
255 if (parent->level != child->level + 1) {
259 for (unsigned int i = 0 ; i < this->levels_; i++) {
260 if (parent->label[i] != child->label[i] && i + 1 != parent->level) {
267 void AsClusterFatTree::generateSwitches() {
268 XBT_DEBUG("Generating switches.");
269 this->nodesByLevel_.resize(this->levels_ + 1, 0);
270 unsigned int nodesRequired = 0;
272 // Take care of the number of nodes by level
273 this->nodesByLevel_[0] = 1;
274 for (unsigned int i = 0 ; i < this->levels_ ; i++)
275 this->nodesByLevel_[0] *= this->lowerLevelNodesNumber_[i];
277 if(this->nodesByLevel_[0] != this->nodes_.size()) {
278 surf_parse_error("The number of provided nodes does not fit with the wanted topology."
279 " Please check your platform description (We need %d nodes, we got %zu)",
280 this->nodesByLevel_[0], this->nodes_.size());
285 for (unsigned int i = 0 ; i < this->levels_ ; i++) {
286 int nodesInThisLevel = 1;
288 for (unsigned int j = 0 ; j <= i ; j++)
289 nodesInThisLevel *= this->upperLevelNodesNumber_[j];
291 for (unsigned int j = i+1 ; j < this->levels_ ; j++)
292 nodesInThisLevel *= this->lowerLevelNodesNumber_[j];
294 this->nodesByLevel_[i+1] = nodesInThisLevel;
295 nodesRequired += nodesInThisLevel;
299 // Create the switches
301 for (unsigned int i = 0 ; i < this->levels_ ; i++) {
302 for (unsigned int j = 0 ; j < this->nodesByLevel_[i + 1] ; j++) {
303 FatTreeNode* newNode = new FatTreeNode(this->cluster_, --k, i + 1, j);
304 XBT_DEBUG("We create the switch %d(%d,%d)", newNode->id, newNode->level, newNode->position);
305 newNode->children.resize(this->lowerLevelNodesNumber_[i] *
306 this->lowerLevelPortsNumber_[i]);
307 if (i != this->levels_ - 1) {
308 newNode->parents.resize(this->upperLevelNodesNumber_[i + 1] *
309 this->lowerLevelPortsNumber_[i + 1]);
311 newNode->label.resize(this->levels_);
312 this->nodes_.push_back(newNode);
317 void AsClusterFatTree::generateLabels() {
318 XBT_DEBUG("Generating labels.");
319 // TODO : check if nodesByLevel and nodes are filled
320 std::vector<int> maxLabel(this->levels_);
321 std::vector<int> currentLabel(this->levels_);
323 for (unsigned int i = 0 ; i <= this->levels_ ; i++) {
324 currentLabel.assign(this->levels_, 0);
325 for (unsigned int j = 0 ; j < this->levels_ ; j++) {
326 maxLabel[j] = j + 1 > i ?
327 this->lowerLevelNodesNumber_[j] : this->upperLevelNodesNumber_[j];
330 for (unsigned int j = 0 ; j < this->nodesByLevel_[i] ; j++) {
332 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug )) {
333 std::stringstream msgBuffer;
335 msgBuffer << "Assigning label <";
336 for (unsigned int l = 0 ; l < this->levels_ ; l++) {
337 msgBuffer << currentLabel[l] << ",";
339 msgBuffer << "> to " << k << " (" << i << "," << j <<")";
341 XBT_DEBUG("%s", msgBuffer.str().c_str());
343 this->nodes_[k]->label.assign(currentLabel.begin(), currentLabel.end());
345 bool remainder = true;
346 unsigned int pos = 0;
347 while (remainder && pos < this->levels_) {
349 if (currentLabel[pos] >= maxLabel[pos]) {
350 currentLabel[pos] = 0;
365 int AsClusterFatTree::getLevelPosition(const unsigned int level) {
366 xbt_assert(level <= this->levels_, "The impossible did happen. Yet again.");
367 int tempPosition = 0;
369 for (unsigned int i = 0 ; i < level ; i++)
370 tempPosition += this->nodesByLevel_[i];
375 void AsClusterFatTree::addProcessingNode(int id) {
376 using std::make_pair;
377 static int position = 0;
378 FatTreeNode* newNode;
379 newNode = new FatTreeNode(this->cluster_, id, 0, position++);
380 newNode->parents.resize(this->upperLevelNodesNumber_[0] *
381 this->lowerLevelPortsNumber_[0]);
382 newNode->label.resize(this->levels_);
383 this->computeNodes_.insert(make_pair(id,newNode));
384 this->nodes_.push_back(newNode);
387 void AsClusterFatTree::addLink(FatTreeNode *parent, unsigned int parentPort,
388 FatTreeNode *child, unsigned int childPort) {
389 FatTreeLink *newLink;
390 newLink = new FatTreeLink(this->cluster_, child, parent);
391 XBT_DEBUG("Creating a link between the parent (%d,%d,%u) and the child (%d,%d,%u)",
392 parent->level, parent->position, parentPort, child->level, child->position, childPort);
393 parent->children[parentPort] = newLink;
394 child->parents[childPort] = newLink;
396 this->links_.push_back(newLink);
399 void AsClusterFatTree::parse_specific_arguments(sg_platf_cluster_cbarg_t cluster) {
400 std::vector<std::string> parameters;
401 std::vector<std::string> tmp;
402 boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
404 // TODO : we have to check for zeros and negative numbers, or it might crash
405 if (parameters.size() != 4){
406 surf_parse_error("Fat trees are defined by the levels number and 3 vectors, see the documentation for more informations");
409 // The first parts of topo_parameters should be the levels number
410 this->levels_ = xbt_str_parse_int(parameters[0].c_str(), "First parameter is not the amount of levels: %s");
412 // Then, a l-sized vector standing for the childs number by level
413 boost::split(tmp, parameters[1], 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 informations");
418 for(size_t i = 0 ; i < tmp.size() ; i++){
419 this->lowerLevelNodesNumber_.push_back(xbt_str_parse_int(tmp[i].c_str(), "Invalid lower level node number: %s"));
422 // Then, a l-sized vector standing for the parents number by level
423 boost::split(tmp, parameters[2], boost::is_any_of(","));
424 if(tmp.size() != this->levels_) {
425 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
426 ", see the documentation for more informations");
428 for(size_t i = 0 ; i < tmp.size() ; i++){
429 this->upperLevelNodesNumber_.push_back(xbt_str_parse_int(tmp[i].c_str(), "Invalid upper level node number: %s"));
432 // Finally, a l-sized vector standing for the ports number with the lower level
433 boost::split(tmp, parameters[3], boost::is_any_of(","));
434 if(tmp.size() != this->levels_) {
435 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
436 ", see the documentation for more informations");
439 for(size_t i = 0 ; i < tmp.size() ; i++){
440 this->lowerLevelPortsNumber_.push_back(xbt_str_parse_int(tmp[i].c_str(), "Invalid lower level node number: %s"));
442 this->cluster_ = cluster;
446 void AsClusterFatTree::generateDotFile(const std::string& filename) const {
448 file.open(filename, std::ios::out | std::ios::trunc);
449 xbt_assert(file.is_open(), "Unable to open file %s", filename.c_str());
451 file << "graph AsClusterFatTree {\n";
452 for (unsigned int i = 0 ; i < this->nodes_.size() ; i++) {
453 file << this->nodes_[i]->id;
454 if(this->nodes_[i]->id < 0)
455 file << " [shape=circle];\n";
457 file << " [shape=hexagon];\n";
460 for (unsigned int i = 0 ; i < this->links_.size() ; i++ ) {
461 file << this->links_[i]->downNode->id
463 << this->links_[i]->upNode->id
470 FatTreeNode::FatTreeNode(sg_platf_cluster_cbarg_t cluster, int id, int level,
471 int position) : id(id), level(level),
473 s_sg_platf_link_cbarg_t linkTemplate = SG_PLATF_LINK_INITIALIZER;
474 if(cluster->limiter_link) {
475 memset(&linkTemplate, 0, sizeof(linkTemplate));
476 linkTemplate.bandwidth = cluster->limiter_link;
477 linkTemplate.latency = 0;
478 linkTemplate.policy = SURF_LINK_SHARED;
479 linkTemplate.id = bprintf("limiter_%d", id);
480 sg_platf_new_link(&linkTemplate);
481 this->limiterLink = Link::byName(linkTemplate.id);
482 free((void*)linkTemplate.id);
484 if(cluster->loopback_bw || cluster->loopback_lat) {
485 memset(&linkTemplate, 0, sizeof(linkTemplate));
486 linkTemplate.bandwidth = cluster->loopback_bw;
487 linkTemplate.latency = cluster->loopback_lat;
488 linkTemplate.policy = SURF_LINK_FATPIPE;
489 linkTemplate.id = bprintf("loopback_%d", id);
490 sg_platf_new_link(&linkTemplate);
491 this->loopback = Link::byName(linkTemplate.id);
492 free((void*)linkTemplate.id);
496 FatTreeLink::FatTreeLink(sg_platf_cluster_cbarg_t cluster,
497 FatTreeNode *downNode,
498 FatTreeNode *upNode) : upNode(upNode),
500 static int uniqueId = 0;
501 s_sg_platf_link_cbarg_t linkTemplate = SG_PLATF_LINK_INITIALIZER;
502 memset(&linkTemplate, 0, sizeof(linkTemplate));
503 linkTemplate.bandwidth = cluster->bw;
504 linkTemplate.latency = cluster->lat;
505 linkTemplate.policy = cluster->sharing_policy; // sthg to do with that ?
506 linkTemplate.id = bprintf("link_from_%d_to_%d_%d", downNode->id, upNode->id,
508 sg_platf_new_link(&linkTemplate);
511 if (cluster->sharing_policy == SURF_LINK_FULLDUPLEX) {
512 tmpID = std::string(linkTemplate.id) + "_UP";
513 link = Link::byName(tmpID.c_str());
514 this->upLink = link; // check link?
515 tmpID = std::string(linkTemplate.id) + "_DOWN";
516 link = Link::byName(tmpID.c_str());
517 this->downLink = link; // check link ?
520 link = Link::byName(linkTemplate.id);
522 this->downLink = link;
525 free((void*)linkTemplate.id);