9 #include "src/surf/surf_routing_private.hpp"
10 #include "src/surf/surf_routing_cluster_fat_tree.hpp"
13 #include <boost/algorithm/string/split.hpp>
14 #include <boost/algorithm/string/classification.hpp>
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_fat_tree, surf, "Routing for fat trees");
21 AS_t model_fat_tree_cluster_create(void)
23 return new simgrid::surf::AsClusterFatTree();
29 AsClusterFatTree::AsClusterFatTree() : levels(0) {
30 XBT_DEBUG("Creating a new fat tree.");
33 AsClusterFatTree::~AsClusterFatTree() {
34 for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
35 delete this->nodes[i];
37 for (unsigned int i = 0 ; i < this->links.size() ; i++) {
38 delete this->links[i];
42 bool AsClusterFatTree::isInSubTree(FatTreeNode *root, FatTreeNode *node) {
43 XBT_DEBUG("Is %d(%u,%u) in the sub tree of %d(%u,%u) ?", node->id,
44 node->level, node->position, root->id, root->level, root->position);
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 AsClusterFatTree::getRouteAndLatency(NetCard *src,
64 sg_platf_route_cbarg_t into,
66 FatTreeNode *source, *destination, *currentNode;
68 std::map<int, FatTreeNode*>::const_iterator tempIter;
70 if (dst->getRcType() == SURF_NETWORK_ELEMENT_ROUTER || src->getRcType() == SURF_NETWORK_ELEMENT_ROUTER) return;
72 /* Let's find the source and the destination in our internal structure */
73 tempIter = this->computeNodes.find(src->getId());
76 if (tempIter == this->computeNodes.end()) {
77 xbt_die("Could not find the source %s [%d] in the fat tree", src->getName(),
80 source = tempIter->second;
81 tempIter = this->computeNodes.find(dst->getId());
82 if (tempIter == this->computeNodes.end()) {
83 xbt_die("Could not find the destination %s [%d] in the fat tree",
84 dst->getName(), dst->getId());
88 destination = tempIter->second;
90 XBT_VERB("Get route and latency from '%s' [%d] to '%s' [%d] in a fat tree",
91 src->getName(), src->getId(), dst->getName(), dst->getId());
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->p_has_loopback) {
96 xbt_dynar_push_as(into->link_list, void*, 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 xbt_dynar_push_as(into->link_list, void*,currentNode->parents[d]->upLink);
118 *latency += currentNode->parents[d]->upLink->getLatency();
121 if (this->p_has_limiter) {
122 xbt_dynar_push_as(into->link_list, void*,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 xbt_dynar_push_as(into->link_list, void*,currentNode->children[i]->downLink);
138 *latency += currentNode->children[i]->downLink->getLatency();
140 currentNode = currentNode->children[i]->downNode;
141 if (this->p_has_limiter) {
142 xbt_dynar_push_as(into->link_list, void*,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 // We 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];
279 if(this->nodesByLevel[0] != this->nodes.size()) {
280 surf_parse_error("The number of provided nodes does not fit with the wanted topology."
281 " Please check your platform description (We need %d nodes, we got %zu)",
282 this->nodesByLevel[0], this->nodes.size());
287 for (unsigned int i = 0 ; i < this->levels ; i++) {
288 int nodesInThisLevel = 1;
290 for (unsigned int j = 0 ; j <= i ; j++) {
291 nodesInThisLevel *= this->upperLevelNodesNumber[j];
294 for (unsigned int j = i+1 ; j < this->levels ; j++) {
295 nodesInThisLevel *= this->lowerLevelNodesNumber[j];
298 this->nodesByLevel[i+1] = nodesInThisLevel;
299 nodesRequired += nodesInThisLevel;
303 // We create the switches
305 for (unsigned int i = 0 ; i < this->levels ; i++) {
306 for (unsigned int j = 0 ; j < this->nodesByLevel[i + 1] ; j++) {
307 FatTreeNode* newNode;
308 newNode = new FatTreeNode(this->cluster, --k, i + 1, j);
309 XBT_DEBUG("We create the switch %d(%d,%d)", newNode->id, newNode->level,
311 newNode->children.resize(this->lowerLevelNodesNumber[i] *
312 this->lowerLevelPortsNumber[i]);
313 if (i != this->levels - 1) {
314 newNode->parents.resize(this->upperLevelNodesNumber[i + 1] *
315 this->lowerLevelPortsNumber[i + 1]);
317 newNode->label.resize(this->levels);
318 this->nodes.push_back(newNode);
323 void AsClusterFatTree::generateLabels() {
324 XBT_DEBUG("Generating labels.");
325 // TODO : check if nodesByLevel and nodes are filled
326 std::vector<int> maxLabel(this->levels);
327 std::vector<int> currentLabel(this->levels);
329 for (unsigned int i = 0 ; i <= this->levels ; i++) {
330 currentLabel.assign(this->levels, 0);
331 for (unsigned int j = 0 ; j < this->levels ; j++) {
332 maxLabel[j] = j + 1 > i ?
333 this->lowerLevelNodesNumber[j] : this->upperLevelNodesNumber[j];
336 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
338 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug )) {
339 std::stringstream msgBuffer;
341 msgBuffer << "Assigning label <";
342 for (unsigned int l = 0 ; l < this->levels ; l++) {
343 msgBuffer << currentLabel[l] << ",";
345 msgBuffer << "> to " << k << " (" << i << "," << j <<")";
347 XBT_DEBUG("%s", msgBuffer.str().c_str());
349 this->nodes[k]->label.assign(currentLabel.begin(), currentLabel.end());
351 bool remainder = true;
353 unsigned int pos = 0;
355 std::stringstream msgBuffer;
358 if (currentLabel[pos] >= maxLabel[pos]) {
359 currentLabel[pos] = 0;
372 while(remainder && pos < this->levels);
379 int AsClusterFatTree::getLevelPosition(const unsigned int level) {
380 if (level > this->levels) {
381 // Well, that should never happen. Maybe should we throw instead.
384 int tempPosition = 0;
386 for (unsigned int i = 0 ; i < level ; i++) {
387 tempPosition += this->nodesByLevel[i];
392 void AsClusterFatTree::addProcessingNode(int id) {
393 using std::make_pair;
394 static int position = 0;
395 FatTreeNode* newNode;
396 newNode = new FatTreeNode(this->cluster, id, 0, position++);
397 newNode->parents.resize(this->upperLevelNodesNumber[0] *
398 this->lowerLevelPortsNumber[0]);
399 newNode->label.resize(this->levels);
400 this->computeNodes.insert(make_pair(id,newNode));
401 this->nodes.push_back(newNode);
404 void AsClusterFatTree::addLink(FatTreeNode *parent, unsigned int parentPort,
405 FatTreeNode *child, unsigned int childPort) {
406 FatTreeLink *newLink;
407 newLink = new FatTreeLink(this->cluster, child, parent);
408 XBT_DEBUG("Creating a link between the parent (%d,%d,%u)"
409 " and the child (%d,%d,%u)", parent->level, parent->position,
410 parentPort, child->level, child->position, childPort);
411 parent->children[parentPort] = newLink;
412 child->parents[childPort] = newLink;
414 this->links.push_back(newLink);
420 void AsClusterFatTree::parse_specific_arguments(sg_platf_cluster_cbarg_t
422 std::vector<std::string> parameters;
423 std::vector<std::string> tmp;
424 boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
427 // TODO : we have to check for zeros and negative numbers, or it might crash
428 if (parameters.size() != 4){
429 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
430 ", see the documentation for more informations");
433 // The first parts of topo_parameters should be the levels number
434 this->levels = std::atoi(parameters[0].c_str()); // stoi() only in C++11...
436 // Then, a l-sized vector standing for the childs number by level
437 boost::split(tmp, parameters[1], boost::is_any_of(","));
438 if(tmp.size() != this->levels) {
439 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
440 ", see the documentation for more informations");
442 for(size_t i = 0 ; i < tmp.size() ; i++){
443 this->lowerLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
446 // Then, a l-sized vector standing for the parents number by level
447 boost::split(tmp, parameters[2], boost::is_any_of(","));
448 if(tmp.size() != this->levels) {
449 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
450 ", see the documentation for more informations");
452 for(size_t i = 0 ; i < tmp.size() ; i++){
453 this->upperLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
456 // Finally, a l-sized vector standing for the ports number with the lower level
457 boost::split(tmp, parameters[3], boost::is_any_of(","));
458 if(tmp.size() != this->levels) {
459 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
460 ", see the documentation for more informations");
463 for(size_t i = 0 ; i < tmp.size() ; i++){
464 this->lowerLevelPortsNumber.push_back(std::atoi(tmp[i].c_str()));
466 this->cluster = cluster;
470 void AsClusterFatTree::generateDotFile(const std::string& filename) const {
472 /* Maybe should we get directly a char*, as open takes strings only beginning
475 file.open(filename.c_str(), std::ios::out | std::ios::trunc);
478 file << "graph AsClusterFatTree {\n";
479 for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
480 file << this->nodes[i]->id;
481 if(this->nodes[i]->id < 0) {
482 file << " [shape=circle];\n";
485 file << " [shape=hexagon];\n";
489 for (unsigned int i = 0 ; i < this->links.size() ; i++ ) {
490 file << this->links[i]->downNode->id
492 << this->links[i]->upNode->id
499 XBT_DEBUG("Unable to open file %s", filename.c_str());
504 FatTreeNode::FatTreeNode(sg_platf_cluster_cbarg_t cluster, int id, int level,
505 int position) : id(id), level(level),
507 s_sg_platf_link_cbarg_t linkTemplate = SG_PLATF_LINK_INITIALIZER;
508 if(cluster->limiter_link) {
509 memset(&linkTemplate, 0, sizeof(linkTemplate));
510 linkTemplate.bandwidth = cluster->limiter_link;
511 linkTemplate.latency = 0;
512 linkTemplate.initiallyOn = 1;
513 linkTemplate.policy = SURF_LINK_SHARED;
514 linkTemplate.id = bprintf("limiter_%d", id);
515 sg_platf_new_link(&linkTemplate);
516 this->limiterLink = Link::byName(linkTemplate.id);
517 free((void*)linkTemplate.id);
519 if(cluster->loopback_bw || cluster->loopback_lat) {
520 memset(&linkTemplate, 0, sizeof(linkTemplate));
521 linkTemplate.bandwidth = cluster->loopback_bw;
522 linkTemplate.latency = cluster->loopback_lat;
523 linkTemplate.initiallyOn = 1;
524 linkTemplate.policy = SURF_LINK_FATPIPE;
525 linkTemplate.id = bprintf("loopback_%d", id);
526 sg_platf_new_link(&linkTemplate);
527 this->loopback = Link::byName(linkTemplate.id);
528 free((void*)linkTemplate.id);
532 FatTreeLink::FatTreeLink(sg_platf_cluster_cbarg_t cluster,
533 FatTreeNode *downNode,
534 FatTreeNode *upNode) : upNode(upNode),
536 static int uniqueId = 0;
537 s_sg_platf_link_cbarg_t linkTemplate = SG_PLATF_LINK_INITIALIZER;
538 memset(&linkTemplate, 0, sizeof(linkTemplate));
539 linkTemplate.bandwidth = cluster->bw;
540 linkTemplate.latency = cluster->lat;
541 linkTemplate.initiallyOn = 1;
542 linkTemplate.policy = cluster->sharing_policy; // sthg to do with that ?
543 linkTemplate.id = bprintf("link_from_%d_to_%d_%d", downNode->id, upNode->id,
545 sg_platf_new_link(&linkTemplate);
548 if (cluster->sharing_policy == SURF_LINK_FULLDUPLEX) {
549 tmpID = std::string(linkTemplate.id) + "_UP";
550 link = Link::byName(tmpID.c_str());
551 this->upLink = link; // check link?
552 tmpID = std::string(linkTemplate.id) + "_DOWN";
553 link = Link::byName(tmpID.c_str());
554 this->downLink = link; // check link ?
557 link = Link::byName(linkTemplate.id);
559 this->downLink = link;
562 free((void*)linkTemplate.id);