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;
54 if (dst->getRcType() == SURF_NETWORK_ELEMENT_ROUTER || src->getRcType() == SURF_NETWORK_ELEMENT_ROUTER) return;
56 tempIter = this->computeNodes.find(src->getId());
59 if (tempIter == this->computeNodes.end()) {
60 xbt_die("Could not find the source %s [%d] in the fat tree", src->getName(), src->getId());
62 source = tempIter->second;
63 tempIter = this->computeNodes.find(dst->getId());
64 if (tempIter == this->computeNodes.end()) {
65 xbt_die("Could not find the destination %s [%d] in the fat tree", dst->getName(), dst->getId());
69 destination = tempIter->second;
70 XBT_DEBUG("Get route and latency from '%s' [%d] to '%s' [%d] in a fat tree",
71 src->getName(), src->getId(), dst->getName(), dst->getId());
76 while (!isInSubTree(currentNode, destination)) {
77 int d, k; // as in d-mod-k
78 d = destination->position;
80 for (unsigned int i = 0 ; i < currentNode->level ; i++) {
81 d /= this->upperLevelNodesNumber[i];
83 k = this->upperLevelNodesNumber[currentNode->level];
85 route.push_back(currentNode->parents[d]->upLink);
88 *latency += currentNode->parents[d]->upLink->getLatency();
90 currentNode = currentNode->parents[d]->upNode;
92 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);
94 while(currentNode != destination) {
95 for(unsigned int i = 0 ; i < currentNode->children.size() ; i++) {
96 if(i % this->lowerLevelNodesNumber[currentNode->level - 1] ==
97 destination->label[currentNode->level - 1]) {
98 route.push_back(currentNode->children[i]->downLink);
100 *latency += currentNode->children[i]->downLink->getLatency();
102 currentNode = currentNode->children[i]->downNode;
103 XBT_DEBUG("%d(%u,%u) is accessible through %d(%u,%u)", destination->id, destination->level, destination->position, currentNode->id, currentNode->level, currentNode->position);
108 for (unsigned int i = 0 ; i < route.size() ; i++) {
109 xbt_dynar_push_as(into->link_list, void*, route[i]);
114 /* This function makes the assumption that parse_specific_arguments() and
115 * addNodes() have already been called
117 void AsClusterFatTree::create_links(sg_platf_cluster_cbarg_t cluster){
118 if(this->levels == 0) {
121 this->generateSwitches();
124 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
125 std::stringstream msgBuffer;
127 msgBuffer << "We are creating a fat tree of " << this->levels << " levels "
128 << "with " << this->nodesByLevel[0] << " processing nodes";
129 for (unsigned int i = 1 ; i <= this->levels ; i++) {
130 msgBuffer << ", " << this->nodesByLevel[i] << " switches at level " << i;
132 XBT_DEBUG("%s", msgBuffer.str().c_str());
134 msgBuffer << "Nodes are : ";
136 for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
137 msgBuffer << this->nodes[i]->id << "(" << this->nodes[i]->level << ","
138 << this->nodes[i]->position << ") ";
140 XBT_DEBUG("%s", msgBuffer.str().c_str());
144 this->generateLabels();
147 // Nodes are totally ordered, by level and then by position, in this->nodes
148 for (unsigned int i = 0 ; i < this->levels ; i++) {
149 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
150 this->connectNodeToParents(cluster, this->nodes[k]);
155 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
156 std::stringstream msgBuffer;
157 msgBuffer << "Links are : ";
158 for (unsigned int i = 0 ; i < this->links.size() ; i++) {
159 msgBuffer << "(" << this->links[i]->upNode->id << ","
160 << this->links[i]->downNode->id << ") ";
162 XBT_DEBUG("%s", msgBuffer.str().c_str());
168 int AsClusterFatTree::connectNodeToParents(sg_platf_cluster_cbarg_t cluster,
170 std::vector<FatTreeNode*>::iterator currentParentNode = this->nodes.begin();
171 int connectionsNumber = 0;
172 const int level = node->level;
173 XBT_DEBUG("We are connecting node %d(%u,%u) to his parents.",
174 node->id, node->level, node->position);
175 currentParentNode += this->getLevelPosition(level + 1);
176 for (unsigned int i = 0 ; i < this->nodesByLevel[level + 1] ; i++ ) {
177 if(this->areRelated(*currentParentNode, node)) {
178 XBT_DEBUG("%d(%u,%u) and %d(%u,%u) are related,"
179 " with %u links between them.", node->id,
180 node->level, node->position, (*currentParentNode)->id,
181 (*currentParentNode)->level, (*currentParentNode)->position, this->lowerLevelPortsNumber[level]);
182 for (unsigned int j = 0 ; j < this->lowerLevelPortsNumber[level] ; j++) {
183 this->addLink(cluster, *currentParentNode, node->label[level] +
184 j * this->lowerLevelNodesNumber[level], node,
185 (*currentParentNode)->label[level] +
186 j * this->upperLevelNodesNumber[level]);
192 return connectionsNumber;
196 bool AsClusterFatTree::areRelated(FatTreeNode *parent, FatTreeNode *child) {
197 std::stringstream msgBuffer;
199 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
200 msgBuffer << "Are " << child->id << "(" << child->level << ","
201 << child->position << ") <";
203 for (unsigned int i = 0 ; i < this->levels ; i++) {
204 msgBuffer << child->label[i] << ",";
208 msgBuffer << " and " << parent->id << "(" << parent->level
209 << "," << parent->position << ") <";
210 for (unsigned int i = 0 ; i < this->levels ; i++) {
211 msgBuffer << parent->label[i] << ",";
214 msgBuffer << " related ? ";
215 XBT_DEBUG("%s", msgBuffer.str().c_str());
218 if (parent->level != child->level + 1) {
222 for (unsigned int i = 0 ; i < this->levels; i++) {
223 if (parent->label[i] != child->label[i] && i + 1 != parent->level) {
230 void AsClusterFatTree::generateSwitches() {
231 XBT_DEBUG("Generating switches.");
232 this->nodesByLevel.resize(this->levels + 1, 0);
233 unsigned int nodesRequired = 0;
235 // We take care of the number of nodes by level
236 this->nodesByLevel[0] = 1;
237 for (unsigned int i = 0 ; i < this->levels ; i++) {
238 this->nodesByLevel[0] *= this->lowerLevelNodesNumber[i];
242 if(this->nodesByLevel[0] != this->nodes.size()) {
243 surf_parse_error("The number of provided nodes does not fit with the wanted topology."
244 " Please check your platform description (We need %d nodes, we got %zu)",
245 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];
257 for (unsigned int j = i+1 ; j < this->levels ; j++) {
258 nodesInThisLevel *= this->lowerLevelNodesNumber[j];
261 this->nodesByLevel[i+1] = nodesInThisLevel;
262 nodesRequired += nodesInThisLevel;
266 // If we have to many compute nodes, we ditch them
269 // We create the switches
271 for (unsigned int i = 0 ; i < this->levels ; i++) {
272 for (unsigned int j = 0 ; j < this->nodesByLevel[i + 1] ; j++) {
273 FatTreeNode* newNode;
274 newNode = new FatTreeNode(--k, i + 1, j);
275 XBT_DEBUG("We create the switch %d(%d,%d)", newNode->id, newNode->level, newNode->position);
276 newNode->children.resize(this->lowerLevelNodesNumber[i] *
277 this->lowerLevelPortsNumber[i]);
278 if (i != this->levels - 1) {
279 newNode->parents.resize(this->upperLevelNodesNumber[i + 1] * this->lowerLevelPortsNumber[i + 1]);
281 newNode->label.resize(this->levels);
282 this->nodes.push_back(newNode);
287 void AsClusterFatTree::generateLabels() {
288 XBT_DEBUG("Generating labels.");
289 // TODO : check if nodesByLevel and nodes are filled
290 std::vector<int> maxLabel(this->levels);
291 std::vector<int> currentLabel(this->levels);
293 for (unsigned int i = 0 ; i <= this->levels ; i++) {
294 currentLabel.assign(this->levels, 0);
295 for (unsigned int j = 0 ; j < this->levels ; j++) {
296 maxLabel[j] = j + 1 > i ?
297 this->lowerLevelNodesNumber[j] : this->upperLevelNodesNumber[j];
300 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
302 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug )) {
303 std::stringstream msgBuffer;
305 msgBuffer << "Assigning label <";
306 for (unsigned int l = 0 ; l < this->levels ; l++) {
307 msgBuffer << currentLabel[l] << ",";
309 msgBuffer << "> to " << k << " (" << i << "," << j <<")";
311 XBT_DEBUG("%s", msgBuffer.str().c_str());
313 this->nodes[k]->label.assign(currentLabel.begin(), currentLabel.end());
315 bool remainder = true;
317 unsigned int pos = 0;
319 std::stringstream msgBuffer;
322 if (currentLabel[pos] >= maxLabel[pos]) {
323 currentLabel[pos] = 0;
336 while(remainder && pos < this->levels);
343 int AsClusterFatTree::getLevelPosition(const unsigned int level) {
344 if (level > this->levels) {
345 // Well, that should never happen. Maybe should we throw instead.
348 int tempPosition = 0;
350 for (unsigned int i = 0 ; i < level ; i++) {
351 tempPosition += this->nodesByLevel[i];
356 void AsClusterFatTree::addProcessingNode(int id) {
357 using std::make_pair;
358 static int position = 0;
359 FatTreeNode* newNode;
360 newNode = new FatTreeNode(id, 0, position++);
361 newNode->parents.resize(this->upperLevelNodesNumber[0] * 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(sg_platf_cluster_cbarg_t cluster,
368 FatTreeNode *parent, unsigned int parentPort,
369 FatTreeNode *child, unsigned int childPort) {
370 FatTreeLink *newLink;
371 newLink = new FatTreeLink(cluster, child, parent);
372 XBT_DEBUG("Creating a link between the parent (%d,%d,%u)"
373 " and the child (%d,%d,%u)", parent->level, parent->position,
374 parentPort, child->level, child->position, childPort);
375 parent->children[parentPort] = newLink;
376 child->parents[childPort] = newLink;
378 this->links.push_back(newLink);
384 void AsClusterFatTree::parse_specific_arguments(sg_platf_cluster_cbarg_t
386 std::vector<string> parameters;
387 std::vector<string> tmp;
388 boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
391 // TODO : we have to check for zeros and negative numbers, or it might crash
392 if (parameters.size() != 4){
393 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
394 ", see the documentation for more informations");
395 // Well, there's no doc, yet
398 // The first parts of topo_parameters should be the levels number
399 this->levels = std::atoi(parameters[0].c_str()); // stoi() only in C++11...
401 // Then, a l-sized vector standing for the childs number by level
402 boost::split(tmp, parameters[1], boost::is_any_of(","));
403 if(tmp.size() != this->levels) {
404 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
405 ", see the documentation for more informations");
407 for(size_t i = 0 ; i < tmp.size() ; i++){
408 this->lowerLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
411 // Then, a l-sized vector standing for the parents number by level
412 boost::split(tmp, parameters[2], boost::is_any_of(","));
413 if(tmp.size() != this->levels) {
414 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
415 ", see the documentation for more informations");
417 for(size_t i = 0 ; i < tmp.size() ; i++){
418 this->upperLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
421 // Finally, a l-sized vector standing for the ports number with the lower level
422 boost::split(tmp, parameters[3], boost::is_any_of(","));
423 if(tmp.size() != this->levels) {
424 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
425 ", see the documentation for more informations");
428 for(size_t i = 0 ; i < tmp.size() ; i++){
429 this->lowerLevelPortsNumber.push_back(std::atoi(tmp[i].c_str()));
434 void AsClusterFatTree::generateDotFile(const string& filename) const {
436 /* Maybe should we get directly a char*, as open takes strings only beginning
439 file.open(filename.c_str(), ios::out | ios::trunc);
442 file << "graph AsClusterFatTree {\n";
443 for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
444 file << this->nodes[i]->id;
445 if(this->nodes[i]->id < 0) {
446 file << " [shape=circle];\n";
449 file << " [shape=hexagon];\n";
453 for (unsigned int i = 0 ; i < this->links.size() ; i++ ) {
454 file << this->links[i]->downNode->id
456 << this->links[i]->upNode->id
463 std::cerr << "Unable to open file " << filename << std::endl;
468 FatTreeNode::FatTreeNode(int id, int level, int position) : id(id),
472 FatTreeLink::FatTreeLink(sg_platf_cluster_cbarg_t cluster, FatTreeNode *downNode,
473 FatTreeNode *upNode) : upNode(upNode),
475 static int uniqueId = 0;
476 s_sg_platf_link_cbarg_t linkTemplate;
477 memset(&linkTemplate, 0, sizeof(linkTemplate));
478 linkTemplate.bandwidth = cluster->bw;
479 linkTemplate.latency = cluster->lat;
480 linkTemplate.state = SURF_RESOURCE_ON;
481 linkTemplate.policy = cluster->sharing_policy; // Maybe should we do sthg with that ?
482 linkTemplate.id = bprintf("link_from_%d_to_%d_%d", downNode->id, upNode->id, uniqueId);
483 sg_platf_new_link(&linkTemplate);
485 if (cluster->sharing_policy == SURF_LINK_FULLDUPLEX) {
487 tmpID = std::string(linkTemplate.id) + "_UP";
488 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, tmpID.c_str(), SURF_LINK_LEVEL);
489 this->upLink = link; // check link?
490 tmpID = std::string(linkTemplate.id) + "_DOWN";
491 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, tmpID.c_str(), SURF_LINK_LEVEL);
492 this->downLink = link; // check link ?
495 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, linkTemplate.id, SURF_LINK_LEVEL);
497 this->downLink = link;