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("Get route and latency from '%s' [%d] to '%s' [%d] in a fat tree",
68 src->getName(), src->getId(), dst->getName(), dst->getId());
73 while (!isInSubTree(currentNode, destination)) {
74 int d, k; // as in d-mod-k
75 d = destination->position;
77 for (unsigned int i = 0 ; i < currentNode->level ; i++) {
78 d /= this->upperLevelNodesNumber[i];
80 k = this->upperLevelNodesNumber[currentNode->level];
82 route.push_back(currentNode->parents[d]->upLink);
85 *latency += currentNode->parents[d]->upLink->getLatency();
87 currentNode = currentNode->parents[d]->upNode;
89 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);
91 while(currentNode != destination) {
92 for(unsigned int i = 0 ; i < currentNode->children.size() ; i++) {
93 if(i % this->lowerLevelNodesNumber[currentNode->level - 1] ==
94 destination->label[currentNode->level - 1]) {
95 route.push_back(currentNode->children[i]->downLink);
97 *latency += currentNode->children[i]->downLink->getLatency();
99 currentNode = currentNode->children[i]->downNode;
100 XBT_DEBUG("%d(%u,%u) is accessible through %d(%u,%u)", destination->id, destination->level, destination->position, currentNode->id, currentNode->level, currentNode->position);
105 for (unsigned int i = 0 ; i < route.size() ; i++) {
106 xbt_dynar_push_as(into->link_list, void*, route[i]);
111 /* This function makes the assumption that parse_specific_arguments() and
112 * addNodes() have already been called
114 void AsClusterFatTree::create_links(sg_platf_cluster_cbarg_t cluster){
115 if(this->levels == 0) {
118 this->generateSwitches();
121 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
122 std::stringstream msgBuffer;
124 msgBuffer << "We are creating a fat tree of " << this->levels << " levels "
125 << "with " << this->nodesByLevel[0] << " processing nodes";
126 for (unsigned int i = 1 ; i <= this->levels ; i++) {
127 msgBuffer << ", " << this->nodesByLevel[i] << " switches at level " << i;
129 XBT_DEBUG("%s", msgBuffer.str().c_str());
131 msgBuffer << "Nodes are : ";
133 for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
134 msgBuffer << this->nodes[i]->id << "(" << this->nodes[i]->level << ","
135 << this->nodes[i]->position << ") ";
137 XBT_DEBUG("%s", msgBuffer.str().c_str());
141 this->generateLabels();
144 // Nodes are totally ordered, by level and then by position, in this->nodes
145 for (unsigned int i = 0 ; i < this->levels ; i++) {
146 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
147 this->connectNodeToParents(cluster, this->nodes[k]);
152 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
153 std::stringstream msgBuffer;
154 msgBuffer << "Links are : ";
155 for (unsigned int i = 0 ; i < this->links.size() ; i++) {
156 msgBuffer << "(" << this->links[i]->upNode->id << ","
157 << this->links[i]->downNode->id << ") ";
159 XBT_DEBUG("%s", msgBuffer.str().c_str());
165 int AsClusterFatTree::connectNodeToParents(sg_platf_cluster_cbarg_t cluster,
167 std::vector<FatTreeNode*>::iterator currentParentNode = this->nodes.begin();
168 int connectionsNumber = 0;
169 const int level = node->level;
170 XBT_DEBUG("We are connecting node %d(%u,%u) to his parents.",
171 node->id, node->level, node->position);
172 currentParentNode += this->getLevelPosition(level + 1);
173 for (unsigned int i = 0 ; i < this->nodesByLevel[level + 1] ; i++ ) {
174 if(this->areRelated(*currentParentNode, node)) {
175 XBT_DEBUG("%d(%u,%u) and %d(%u,%u) are related,"
176 " with %u links between them.", node->id,
177 node->level, node->position, (*currentParentNode)->id,
178 (*currentParentNode)->level, (*currentParentNode)->position, this->lowerLevelPortsNumber[level]);
179 for (unsigned int j = 0 ; j < this->lowerLevelPortsNumber[level] ; j++) {
180 this->addLink(cluster, *currentParentNode, node->label[level] +
181 j * this->lowerLevelNodesNumber[level], node,
182 (*currentParentNode)->label[level] +
183 j * this->upperLevelNodesNumber[level]);
189 return connectionsNumber;
193 bool AsClusterFatTree::areRelated(FatTreeNode *parent, FatTreeNode *child) {
194 std::stringstream msgBuffer;
196 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
197 msgBuffer << "Are " << child->id << "(" << child->level << ","
198 << child->position << ") <";
200 for (unsigned int i = 0 ; i < this->levels ; i++) {
201 msgBuffer << child->label[i] << ",";
205 msgBuffer << " and " << parent->id << "(" << parent->level
206 << "," << parent->position << ") <";
207 for (unsigned int i = 0 ; i < this->levels ; i++) {
208 msgBuffer << parent->label[i] << ",";
211 msgBuffer << " related ? ";
212 XBT_DEBUG("%s", msgBuffer.str().c_str());
215 if (parent->level != child->level + 1) {
219 for (unsigned int i = 0 ; i < this->levels; i++) {
220 if (parent->label[i] != child->label[i] && i + 1 != parent->level) {
227 void AsClusterFatTree::generateSwitches() {
228 XBT_DEBUG("Generating switches.");
229 this->nodesByLevel.resize(this->levels + 1, 0);
230 unsigned int nodesRequired = 0;
232 // We take care of the number of nodes by level
233 this->nodesByLevel[0] = 1;
234 for (unsigned int i = 0 ; i < this->levels ; i++) {
235 this->nodesByLevel[0] *= this->lowerLevelNodesNumber[i];
239 if(this->nodesByLevel[0] != this->nodes.size()) {
240 surf_parse_error("The number of provided nodes does not fit with the wanted topology."
241 " Please check your platform description (We need %d nodes, we got %zu)",
242 this->nodesByLevel[0], this->nodes.size());
247 for (unsigned int i = 0 ; i < this->levels ; i++) {
248 int nodesInThisLevel = 1;
250 for (unsigned int j = 0 ; j <= i ; j++) {
251 nodesInThisLevel *= this->upperLevelNodesNumber[j];
254 for (unsigned int j = i+1 ; j < this->levels ; j++) {
255 nodesInThisLevel *= this->lowerLevelNodesNumber[j];
258 this->nodesByLevel[i+1] = nodesInThisLevel;
259 nodesRequired += nodesInThisLevel;
263 // If we have to many compute nodes, we ditch them
266 // We create the switches
268 for (unsigned int i = 0 ; i < this->levels ; i++) {
269 for (unsigned int j = 0 ; j < this->nodesByLevel[i + 1] ; j++) {
270 FatTreeNode* newNode;
271 newNode = new FatTreeNode(--k, i + 1, j);
272 XBT_DEBUG("We create the switch %d(%d,%d)", newNode->id, newNode->level, newNode->position);
273 newNode->children.resize(this->lowerLevelNodesNumber[i] *
274 this->lowerLevelPortsNumber[i]);
275 if (i != this->levels - 1) {
276 newNode->parents.resize(this->upperLevelNodesNumber[i + 1] * this->lowerLevelPortsNumber[i + 1]);
278 newNode->label.resize(this->levels);
279 this->nodes.push_back(newNode);
284 void AsClusterFatTree::generateLabels() {
285 XBT_DEBUG("Generating labels.");
286 // TODO : check if nodesByLevel and nodes are filled
287 std::vector<int> maxLabel(this->levels);
288 std::vector<int> currentLabel(this->levels);
290 for (unsigned int i = 0 ; i <= this->levels ; i++) {
291 currentLabel.assign(this->levels, 0);
292 for (unsigned int j = 0 ; j < this->levels ; j++) {
293 maxLabel[j] = j + 1 > i ?
294 this->lowerLevelNodesNumber[j] : this->upperLevelNodesNumber[j];
297 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
299 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug )) {
300 std::stringstream msgBuffer;
302 msgBuffer << "Assigning label <";
303 for (unsigned int l = 0 ; l < this->levels ; l++) {
304 msgBuffer << currentLabel[l] << ",";
306 msgBuffer << "> to " << k << " (" << i << "," << j <<")";
308 XBT_DEBUG("%s", msgBuffer.str().c_str());
310 this->nodes[k]->label.assign(currentLabel.begin(), currentLabel.end());
312 bool remainder = true;
314 unsigned int pos = 0;
316 std::stringstream msgBuffer;
319 if (currentLabel[pos] >= maxLabel[pos]) {
320 currentLabel[pos] = 0;
333 while(remainder && pos < this->levels);
340 int AsClusterFatTree::getLevelPosition(const unsigned int level) {
341 if (level > this->levels) {
342 // Well, that should never happen. Maybe should we throw instead.
345 int tempPosition = 0;
347 for (unsigned int i = 0 ; i < level ; i++) {
348 tempPosition += this->nodesByLevel[i];
353 void AsClusterFatTree::addProcessingNode(int id) {
354 using std::make_pair;
355 static int position = 0;
356 FatTreeNode* newNode;
357 newNode = new FatTreeNode(id, 0, position++);
358 newNode->parents.resize(this->upperLevelNodesNumber[0] * this->lowerLevelPortsNumber[0]);
359 newNode->label.resize(this->levels);
360 this->computeNodes.insert(make_pair(id,newNode));
361 this->nodes.push_back(newNode);
364 void AsClusterFatTree::addLink(sg_platf_cluster_cbarg_t cluster,
365 FatTreeNode *parent, unsigned int parentPort,
366 FatTreeNode *child, unsigned int childPort) {
367 FatTreeLink *newLink;
368 newLink = new FatTreeLink(cluster, child, parent);
369 XBT_DEBUG("Creating a link between the parent (%d,%d,%u)"
370 " and the child (%d,%d,%u)", parent->level, parent->position,
371 parentPort, child->level, child->position, childPort);
372 parent->children[parentPort] = newLink;
373 child->parents[childPort] = newLink;
375 this->links.push_back(newLink);
381 void AsClusterFatTree::parse_specific_arguments(sg_platf_cluster_cbarg_t
383 std::vector<string> parameters;
384 std::vector<string> tmp;
385 boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
388 // TODO : we have to check for zeros and negative numbers, or it might crash
389 if (parameters.size() != 4){
390 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
391 ", see the documentation for more informations");
392 // Well, there's no doc, yet
395 // The first parts of topo_parameters should be the levels number
396 this->levels = std::atoi(parameters[0].c_str()); // stoi() only in C++11...
398 // Then, a l-sized vector standing for the childs number by level
399 boost::split(tmp, parameters[1], boost::is_any_of(","));
400 if(tmp.size() != this->levels) {
401 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
402 ", see the documentation for more informations");
404 for(size_t i = 0 ; i < tmp.size() ; i++){
405 this->lowerLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
408 // Then, a l-sized vector standing for the parents number by level
409 boost::split(tmp, parameters[2], boost::is_any_of(","));
410 if(tmp.size() != this->levels) {
411 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
412 ", see the documentation for more informations");
414 for(size_t i = 0 ; i < tmp.size() ; i++){
415 this->upperLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
418 // Finally, a l-sized vector standing for the ports number with the lower level
419 boost::split(tmp, parameters[3], boost::is_any_of(","));
420 if(tmp.size() != this->levels) {
421 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
422 ", see the documentation for more informations");
425 for(size_t i = 0 ; i < tmp.size() ; i++){
426 this->lowerLevelPortsNumber.push_back(std::atoi(tmp[i].c_str()));
431 void AsClusterFatTree::generateDotFile(const string& filename) const {
433 /* Maybe should we get directly a char*, as open takes strings only beginning
436 file.open(filename.c_str(), ios::out | ios::trunc);
439 file << "graph AsClusterFatTree {\n";
440 for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
441 file << this->nodes[i]->id;
442 if(this->nodes[i]->id < 0) {
443 file << " [shape=circle];\n";
446 file << " [shape=hexagon];\n";
450 for (unsigned int i = 0 ; i < this->links.size() ; i++ ) {
451 file << this->links[i]->downNode->id
453 << this->links[i]->upNode->id
460 std::cerr << "Unable to open file " << filename << std::endl;
465 FatTreeNode::FatTreeNode(int id, int level, int position) : id(id),
469 FatTreeLink::FatTreeLink(sg_platf_cluster_cbarg_t cluster, FatTreeNode *downNode,
470 FatTreeNode *upNode) : upNode(upNode),
472 static int uniqueId = 0;
473 s_sg_platf_link_cbarg_t linkTemplate;
474 memset(&linkTemplate, 0, sizeof(linkTemplate));
475 linkTemplate.bandwidth = cluster->bw;
476 linkTemplate.latency = cluster->lat;
477 linkTemplate.state = SURF_RESOURCE_ON;
478 linkTemplate.policy = cluster->sharing_policy; // Maybe should we do sthg with that ?
479 linkTemplate.id = bprintf("link_from_%d_to_%d_%d", downNode->id, upNode->id, uniqueId);
480 sg_platf_new_link(&linkTemplate);
482 if (cluster->sharing_policy == SURF_LINK_FULLDUPLEX) {
484 tmpID = std::string(linkTemplate.id) + "_UP";
485 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, tmpID.c_str(), SURF_LINK_LEVEL);
486 this->upLink = link; // check link?
487 tmpID = std::string(linkTemplate.id) + "_DOWN";
488 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, tmpID.c_str(), SURF_LINK_LEVEL);
489 this->downLink = link; // check link ?
492 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, linkTemplate.id, SURF_LINK_LEVEL);
494 this->downLink = link;