1 #include "surf_routing_cluster_fat_tree.hpp"
4 #include <boost/algorithm/string/split.hpp>
5 #include <boost/algorithm/string/classification.hpp>
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_fat_tree, surf, "Routing for fat trees");
12 AS_t model_fat_tree_cluster_create(void)
14 return new AsClusterFatTree();
17 AsClusterFatTree::AsClusterFatTree() : levels(0) {
18 XBT_DEBUG("Creating a new fat tree.");
21 AsClusterFatTree::~AsClusterFatTree() {
22 for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
23 delete this->nodes[i];
27 bool AsClusterFatTree::isInSubTree(FatTreeNode *root, FatTreeNode *node) {
28 XBT_DEBUG("Is %d(%u,%u) in the sub tree of %d(%u,%u) ?", node->id, node->level, node->position, root->id, root->level, root->position);
29 if (root->level <= node->level) {
32 for (unsigned int i = 0 ; i < node->level ; i++) {
33 if(root->label[i] != node->label[i]) {
38 for (unsigned int i = root->level ; i < this->levels ; i++) {
39 if(root->label[i] != node->label[i]) {
46 void AsClusterFatTree::getRouteAndLatency(RoutingEdgePtr src,
48 sg_platf_route_cbarg_t into,
50 FatTreeNode *source, *destination, *currentNode;
51 std::vector<NetworkLink*> route;
52 std::map<int, FatTreeNode*>::const_iterator tempIter;
53 tempIter = this->computeNodes.find(src->getId());
56 if (tempIter == this->computeNodes.end()) {
57 xbt_die("Could not find the source %s [%d] in the fat tree", src->getName(), src->getId());
59 source = tempIter->second;
60 tempIter = this->computeNodes.find(dst->getId());
61 if (tempIter == this->computeNodes.end()) {
62 xbt_die("Could not find the destination %s [%d] in the fat tree", src->getName(), src->getId());
66 destination = tempIter->second;
67 XBT_DEBUG("%d %d", src->getId(), source->id);
68 XBT_DEBUG("Get route and latency from '%s' [%d] to '%s' [%d] in a fat tree",
69 src->getName(), src->getId(), dst->getName(), dst->getId());
74 while (!isInSubTree(currentNode, destination)) {
75 int d, k; // as in d-mod-k
76 d = destination->position;
78 for (unsigned int i = 0 ; i < currentNode->level ; i++) {
79 d /= this->upperLevelNodesNumber[i];
81 k = this->upperLevelNodesNumber[currentNode->level];
83 route.push_back(currentNode->parents[d]->upLink);
86 *latency += currentNode->parents[d]->upLink->getLatency();
88 currentNode = currentNode->parents[d]->upNode;
90 XBT_DEBUG("%d(%u,%u) is in the sub tree of %d(%u,%u).", destination->id, destination->level, destination->position, currentNode->id, currentNode->level, currentNode->position);
92 while(currentNode != destination) {
93 for(unsigned int i = 0 ; i < currentNode->children.size() ; i++) {
94 if(i % this->lowerLevelNodesNumber[currentNode->level - 1] ==
95 destination->label[currentNode->level - 1]) {
96 route.push_back(currentNode->children[i]->downLink);
98 *latency += currentNode->children[i]->downLink->getLatency();
100 currentNode = currentNode->children[i]->downNode;
101 XBT_DEBUG("%d(%u,%u) is accessible through %d(%u,%u)", destination->id, destination->level, destination->position, currentNode->id, currentNode->level, currentNode->position);
106 for (unsigned int i = 0 ; i < route.size() ; i++) {
107 xbt_dynar_push_as(into->link_list, void*, route[i]);
112 /* This function makes the assumption that parse_specific_arguments() and
113 * addNodes() have already been called
115 void AsClusterFatTree::create_links(sg_platf_cluster_cbarg_t cluster){
116 if(this->levels == 0) {
119 this->generateSwitches();
122 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
123 std::stringstream msgBuffer;
125 msgBuffer << "We are creating a fat tree of " << this->levels << " levels "
126 << "with " << this->nodesByLevel[0] << " processing nodes";
127 for (unsigned int i = 1 ; i <= this->levels ; i++) {
128 msgBuffer << ", " << this->nodesByLevel[i] << " switches at level " << i;
130 XBT_DEBUG(msgBuffer.str().c_str());
132 msgBuffer << "Nodes are : ";
134 for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
135 msgBuffer << this->nodes[i]->id << "(" << this->nodes[i]->level << ","
136 << this->nodes[i]->position << ") ";
138 XBT_DEBUG(msgBuffer.str().c_str());
142 this->generateLabels();
145 // Nodes are totally ordered, by level and then by position, in this->nodes
146 for (unsigned int i = 0 ; i < this->levels ; i++) {
147 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
148 this->connectNodeToParents(cluster, this->nodes[k]);
153 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
154 std::stringstream msgBuffer;
155 msgBuffer << "Links are : ";
156 for (unsigned int i = 0 ; i < this->links.size() ; i++) {
157 msgBuffer << "(" << this->links[i]->upNode->id << ","
158 << this->links[i]->downNode->id << ") ";
160 XBT_DEBUG(msgBuffer.str().c_str());
166 int AsClusterFatTree::connectNodeToParents(sg_platf_cluster_cbarg_t cluster,
168 std::vector<FatTreeNode*>::iterator currentParentNode = this->nodes.begin();
169 int connectionsNumber = 0;
170 const int level = node->level;
171 XBT_DEBUG("We are connecting node %d(%u,%u) to his parents.",
172 node->id, node->level, node->position);
173 currentParentNode += this->getLevelPosition(level + 1);
174 for (unsigned int i = 0 ; i < this->nodesByLevel[level + 1] ; i++ ) {
175 if(this->areRelated(*currentParentNode, node)) {
176 XBT_DEBUG("%d(%u,%u) and %d(%u,%u) are related,"
177 " with %u links between them.", node->id,
178 node->level, node->position, (*currentParentNode)->id,
179 (*currentParentNode)->level, (*currentParentNode)->position, this->lowerLevelPortsNumber[level]);
180 for (unsigned int j = 0 ; j < this->lowerLevelPortsNumber[level] ; j++) {
181 this->addLink(cluster, *currentParentNode, node->label[level] +
182 j * this->lowerLevelNodesNumber[level], node,
183 (*currentParentNode)->label[level] +
184 j * this->upperLevelNodesNumber[level]);
190 return connectionsNumber;
194 bool AsClusterFatTree::areRelated(FatTreeNode *parent, FatTreeNode *child) {
195 std::stringstream msgBuffer;
197 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
198 msgBuffer << "Are " << child->id << "(" << child->level << ","
199 << child->position << ") <";
201 for (unsigned int i = 0 ; i < this->levels ; i++) {
202 msgBuffer << child->label[i] << ",";
206 msgBuffer << " and " << parent->id << "(" << parent->level
207 << "," << parent->position << ") <";
208 for (unsigned int i = 0 ; i < this->levels ; i++) {
209 msgBuffer << parent->label[i] << ",";
212 msgBuffer << " related ? ";
213 XBT_DEBUG(msgBuffer.str().c_str());
216 if (parent->level != child->level + 1) {
220 for (unsigned int i = 0 ; i < this->levels; i++) {
221 if (parent->label[i] != child->label[i] && i + 1 != parent->level) {
228 void AsClusterFatTree::generateSwitches() {
229 XBT_DEBUG("Generating switches.");
230 this->nodesByLevel.resize(this->levels + 1, 0);
231 unsigned int nodesRequired = 0;
233 // We take care of the number of nodes by level
234 this->nodesByLevel[0] = 1;
235 for (unsigned int i = 0 ; i < this->levels ; i++) {
236 this->nodesByLevel[0] *= this->lowerLevelNodesNumber[i];
240 if(this->nodesByLevel[0] != this->nodes.size()) {
241 surf_parse_error("The number of provided nodes does not fit with the wanted topology."
242 " Please check your platform description (We need %d nodes, we got %zu)",
243 this->nodesByLevel[0], this->nodes.size());
248 for (unsigned int i = 0 ; i < this->levels ; i++) {
249 int nodesInThisLevel = 1;
251 for (unsigned int j = 0 ; j <= i ; j++) {
252 nodesInThisLevel *= this->upperLevelNodesNumber[j];
255 for (unsigned int j = i+1 ; j < this->levels ; j++) {
256 nodesInThisLevel *= this->lowerLevelNodesNumber[j];
259 this->nodesByLevel[i+1] = nodesInThisLevel;
260 nodesRequired += nodesInThisLevel;
264 // If we have to many compute nodes, we ditch them
267 // We create the switches
269 for (unsigned int i = 0 ; i < this->levels ; i++) {
270 for (unsigned int j = 0 ; j < this->nodesByLevel[i + 1] ; j++) {
271 FatTreeNode* newNode;
272 newNode = new FatTreeNode(--k, i + 1, j);
273 XBT_DEBUG("We create the switch %d(%d,%d)", newNode->id, newNode->level, newNode->position);
274 newNode->children.resize(this->lowerLevelNodesNumber[i] *
275 this->lowerLevelPortsNumber[i]);
276 if (i != this->levels - 1) {
277 newNode->parents.resize(this->upperLevelNodesNumber[i + 1] * this->lowerLevelPortsNumber[i + 1]);
279 newNode->label.resize(this->levels);
280 this->nodes.push_back(newNode);
285 void AsClusterFatTree::generateLabels() {
286 XBT_DEBUG("Generating labels.");
287 // TODO : check if nodesByLevel and nodes are filled
288 std::vector<int> maxLabel(this->levels);
289 std::vector<int> currentLabel(this->levels);
291 for (unsigned int i = 0 ; i <= this->levels ; i++) {
292 currentLabel.assign(this->levels, 0);
293 for (unsigned int j = 0 ; j < this->levels ; j++) {
294 maxLabel[j] = j + 1 > i ?
295 this->lowerLevelNodesNumber[j] : this->upperLevelNodesNumber[j];
298 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
300 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug )) {
301 std::stringstream msgBuffer;
303 msgBuffer << "Assigning label <";
304 for (unsigned int l = 0 ; l < this->levels ; l++) {
305 msgBuffer << currentLabel[l] << ",";
307 msgBuffer << "> to " << k << " (" << i << "," << j <<")";
309 XBT_DEBUG(bprintf(msgBuffer.str().c_str()));
311 this->nodes[k]->label.assign(currentLabel.begin(), currentLabel.end());
313 bool remainder = true;
315 unsigned int pos = 0;
317 std::stringstream msgBuffer;
320 if (currentLabel[pos] >= maxLabel[pos]) {
321 currentLabel[pos] = 0;
334 while(remainder && pos < this->levels);
341 int AsClusterFatTree::getLevelPosition(const unsigned int level) {
342 if (level > this->levels) {
343 // Well, that should never happen. Maybe should we throw instead.
346 int tempPosition = 0;
348 for (unsigned int i = 0 ; i < level ; i++) {
349 tempPosition += this->nodesByLevel[i];
354 void AsClusterFatTree::addProcessingNode(int id) {
355 using std::make_pair;
356 static int position = 0;
357 FatTreeNode* newNode;
358 newNode = new FatTreeNode(id, 0, position++);
359 newNode->parents.resize(this->upperLevelNodesNumber[0] * this->lowerLevelPortsNumber[0]);
360 newNode->label.resize(this->levels);
361 this->computeNodes.insert(make_pair(id,newNode));
362 this->nodes.push_back(newNode);
365 void AsClusterFatTree::addLink(sg_platf_cluster_cbarg_t cluster,
366 FatTreeNode *parent, unsigned int parentPort,
367 FatTreeNode *child, unsigned int childPort) {
368 FatTreeLink *newLink;
369 newLink = new FatTreeLink(cluster, child, parent);
370 XBT_DEBUG("Creating a link between the parent (%d,%d,%u)"
371 " and the child (%d,%d,%u)", parent->level, parent->position,
372 parentPort, child->level, child->position, childPort);
373 parent->children[parentPort] = newLink;
374 child->parents[childPort] = newLink;
376 this->links.push_back(newLink);
382 void AsClusterFatTree::parse_specific_arguments(sg_platf_cluster_cbarg_t
384 std::vector<string> parameters;
385 std::vector<string> tmp;
386 boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
389 // TODO : we have to check for zeros and negative numbers, or it might crash
390 if (parameters.size() != 4){
391 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
392 ", see the documentation for more informations");
393 // Well, there's no doc, yet
396 // The first parts of topo_parameters should be the levels number
397 this->levels = std::atoi(parameters[0].c_str()); // stoi() only in C++11...
399 // Then, a l-sized vector standing for the childs number by level
400 boost::split(tmp, parameters[1], boost::is_any_of(","));
401 if(tmp.size() != this->levels) {
402 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
403 ", see the documentation for more informations");
405 for(size_t i = 0 ; i < tmp.size() ; i++){
406 this->lowerLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
409 // Then, a l-sized vector standing for the parents number by level
410 boost::split(tmp, parameters[2], boost::is_any_of(","));
411 if(tmp.size() != this->levels) {
412 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
413 ", see the documentation for more informations");
415 for(size_t i = 0 ; i < tmp.size() ; i++){
416 this->upperLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
419 // Finally, a l-sized vector standing for the ports number with the lower level
420 boost::split(tmp, parameters[3], boost::is_any_of(","));
421 if(tmp.size() != this->levels) {
422 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
423 ", see the documentation for more informations");
426 for(size_t i = 0 ; i < tmp.size() ; i++){
427 this->lowerLevelPortsNumber.push_back(std::atoi(tmp[i].c_str()));
432 void AsClusterFatTree::generateDotFile(const string& filename) const {
434 /* Maybe should we get directly a char*, as open takes strings only beginning
437 file.open(filename.c_str(), ios::out | ios::trunc);
440 file << "graph AsClusterFatTree {\n";
441 for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
442 file << this->nodes[i]->id;
443 if(this->nodes[i]->id < 0) {
444 file << " [shape=circle];\n";
447 file << " [shape=hexagon];\n";
451 for (unsigned int i = 0 ; i < this->links.size() ; i++ ) {
452 file << this->links[i]->downNode->id
454 << this->links[i]->upNode->id
461 std::cerr << "Unable to open file " << filename << std::endl;
466 FatTreeNode::FatTreeNode(int id, int level, int position) : id(id),
470 FatTreeLink::FatTreeLink(sg_platf_cluster_cbarg_t cluster, FatTreeNode *downNode,
471 FatTreeNode *upNode) : upNode(upNode),
473 static int uniqueId = 0;
474 s_sg_platf_link_cbarg_t linkTemplate;
475 memset(&linkTemplate, 0, sizeof(linkTemplate));
476 linkTemplate.bandwidth = cluster->bw;
477 linkTemplate.latency = cluster->lat;
478 linkTemplate.state = SURF_RESOURCE_ON;
479 linkTemplate.policy = cluster->sharing_policy; // Maybe should we do sthg with that ?
480 linkTemplate.id = bprintf("link_from_%d_to_%d_%d", downNode->id, upNode->id, uniqueId);
481 sg_platf_new_link(&linkTemplate);
483 if (cluster->sharing_policy == SURF_LINK_FULLDUPLEX) {
485 tmpID = std::string(linkTemplate.id) + "_UP";
486 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, tmpID.c_str(), SURF_LINK_LEVEL);
487 this->upLink = link; // check link?
488 tmpID = std::string(linkTemplate.id) + "_DOWN";
489 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, tmpID.c_str(), SURF_LINK_LEVEL);
490 this->downLink = link; // check link ?
493 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, linkTemplate.id, SURF_LINK_LEVEL);
495 this->downLink = link;