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,
29 node->level, node->position, root->id, root->level, root->position);
30 if (root->level <= node->level) {
33 for (unsigned int i = 0 ; i < node->level ; i++) {
34 if(root->label[i] != node->label[i]) {
39 for (unsigned int i = root->level ; i < this->levels ; i++) {
40 if(root->label[i] != node->label[i]) {
47 void AsClusterFatTree::getRouteAndLatency(RoutingEdgePtr src,
49 sg_platf_route_cbarg_t into,
51 FatTreeNode *source, *destination, *currentNode;
52 std::map<int, FatTreeNode*>::const_iterator tempIter;
54 /* Let's find the source and the destination in our internal structure */
55 tempIter = this->computeNodes.find(src->getId());
57 if (tempIter == this->computeNodes.end()) {
58 xbt_die("Could not find the source %s [%d] in the fat tree", src->getName(),
61 source = tempIter->second;
62 tempIter = this->computeNodes.find(dst->getId());
63 if (tempIter == this->computeNodes.end()) {
64 xbt_die("Could not find the destination %s [%d] in the fat tree",
65 src->getName(), src->getId());
67 destination = tempIter->second;
69 XBT_VERB("Get route and latency from '%s' [%d] to '%s' [%d] in a fat tree",
70 src->getName(), src->getId(), dst->getName(), dst->getId());
72 /* In case destination is the source, and there is a loopback, let's get
73 through it instead of going up to a switch*/
74 if(source->id == destination->id && this->p_has_loopback) {
75 xbt_dynar_push_as(into->link_list, void*, source->loopback);
77 *latency += source->loopback->getLatency();
83 while (!isInSubTree(currentNode, destination)) {
84 int d, k; // as in d-mod-k
85 d = destination->position;
87 for (unsigned int i = 0 ; i < currentNode->level ; i++) {
88 d /= this->upperLevelNodesNumber[i];
90 k = this->upperLevelNodesNumber[currentNode->level];
92 xbt_dynar_push_as(into->link_list, void*,currentNode->parents[d]->upLink);
95 *latency += currentNode->parents[d]->upLink->getLatency();
98 if (this->p_has_limiter) {
99 xbt_dynar_push_as(into->link_list, void*,currentNode->limiterLink);
101 currentNode = currentNode->parents[d]->upNode;
104 XBT_DEBUG("%d(%u,%u) is in the sub tree of %d(%u,%u).", destination->id,
105 destination->level, destination->position, currentNode->id,
106 currentNode->level, currentNode->position);
109 while(currentNode != destination) {
110 for(unsigned int i = 0 ; i < currentNode->children.size() ; i++) {
111 if(i % this->lowerLevelNodesNumber[currentNode->level - 1] ==
112 destination->label[currentNode->level - 1]) {
113 xbt_dynar_push_as(into->link_list, void*,currentNode->children[i]->downLink);
115 *latency += currentNode->children[i]->downLink->getLatency();
117 currentNode = currentNode->children[i]->downNode;
118 if (this->p_has_limiter) {
119 xbt_dynar_push_as(into->link_list, void*,currentNode->limiterLink);
121 XBT_DEBUG("%d(%u,%u) is accessible through %d(%u,%u)", destination->id,
122 destination->level, destination->position, currentNode->id,
123 currentNode->level, currentNode->position);
129 /* This function makes the assumption that parse_specific_arguments() and
130 * addNodes() have already been called
132 void AsClusterFatTree::create_links(){
133 if(this->levels == 0) {
136 this->generateSwitches();
139 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
140 std::stringstream msgBuffer;
142 msgBuffer << "We are creating a fat tree of " << this->levels << " levels "
143 << "with " << this->nodesByLevel[0] << " processing nodes";
144 for (unsigned int i = 1 ; i <= this->levels ; i++) {
145 msgBuffer << ", " << this->nodesByLevel[i] << " switches at level " << i;
147 XBT_DEBUG("%s", msgBuffer.str().c_str());
149 msgBuffer << "Nodes are : ";
151 for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
152 msgBuffer << this->nodes[i]->id << "(" << this->nodes[i]->level << ","
153 << this->nodes[i]->position << ") ";
155 XBT_DEBUG("%s", msgBuffer.str().c_str());
159 this->generateLabels();
162 // Nodes are totally ordered, by level and then by position, in this->nodes
163 for (unsigned int i = 0 ; i < this->levels ; i++) {
164 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
165 this->connectNodeToParents(this->nodes[k]);
170 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
171 std::stringstream msgBuffer;
172 msgBuffer << "Links are : ";
173 for (unsigned int i = 0 ; i < this->links.size() ; i++) {
174 msgBuffer << "(" << this->links[i]->upNode->id << ","
175 << this->links[i]->downNode->id << ") ";
177 XBT_DEBUG("%s", msgBuffer.str().c_str());
183 int AsClusterFatTree::connectNodeToParents(FatTreeNode *node) {
184 std::vector<FatTreeNode*>::iterator currentParentNode = this->nodes.begin();
185 int connectionsNumber = 0;
186 const int level = node->level;
187 XBT_DEBUG("We are connecting node %d(%u,%u) to his parents.",
188 node->id, node->level, node->position);
189 currentParentNode += this->getLevelPosition(level + 1);
190 for (unsigned int i = 0 ; i < this->nodesByLevel[level + 1] ; i++ ) {
191 if(this->areRelated(*currentParentNode, node)) {
192 XBT_DEBUG("%d(%u,%u) and %d(%u,%u) are related,"
193 " with %u links between them.", node->id,
194 node->level, node->position, (*currentParentNode)->id,
195 (*currentParentNode)->level, (*currentParentNode)->position, this->lowerLevelPortsNumber[level]);
196 for (unsigned int j = 0 ; j < this->lowerLevelPortsNumber[level] ; j++) {
197 this->addLink(*currentParentNode, node->label[level] +
198 j * this->lowerLevelNodesNumber[level], node,
199 (*currentParentNode)->label[level] +
200 j * this->upperLevelNodesNumber[level]);
206 return connectionsNumber;
210 bool AsClusterFatTree::areRelated(FatTreeNode *parent, FatTreeNode *child) {
211 std::stringstream msgBuffer;
213 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
214 msgBuffer << "Are " << child->id << "(" << child->level << ","
215 << child->position << ") <";
217 for (unsigned int i = 0 ; i < this->levels ; i++) {
218 msgBuffer << child->label[i] << ",";
222 msgBuffer << " and " << parent->id << "(" << parent->level
223 << "," << parent->position << ") <";
224 for (unsigned int i = 0 ; i < this->levels ; i++) {
225 msgBuffer << parent->label[i] << ",";
228 msgBuffer << " related ? ";
229 XBT_DEBUG("%s", msgBuffer.str().c_str());
232 if (parent->level != child->level + 1) {
236 for (unsigned int i = 0 ; i < this->levels; i++) {
237 if (parent->label[i] != child->label[i] && i + 1 != parent->level) {
244 void AsClusterFatTree::generateSwitches() {
245 XBT_DEBUG("Generating switches.");
246 this->nodesByLevel.resize(this->levels + 1, 0);
247 unsigned int nodesRequired = 0;
249 // We take care of the number of nodes by level
250 this->nodesByLevel[0] = 1;
251 for (unsigned int i = 0 ; i < this->levels ; i++) {
252 this->nodesByLevel[0] *= this->lowerLevelNodesNumber[i];
256 if(this->nodesByLevel[0] != this->nodes.size()) {
257 surf_parse_error("The number of provided nodes does not fit with the wanted topology."
258 " Please check your platform description (We need %d nodes, we got %zu)",
259 this->nodesByLevel[0], this->nodes.size());
264 for (unsigned int i = 0 ; i < this->levels ; i++) {
265 int nodesInThisLevel = 1;
267 for (unsigned int j = 0 ; j <= i ; j++) {
268 nodesInThisLevel *= this->upperLevelNodesNumber[j];
271 for (unsigned int j = i+1 ; j < this->levels ; j++) {
272 nodesInThisLevel *= this->lowerLevelNodesNumber[j];
275 this->nodesByLevel[i+1] = nodesInThisLevel;
276 nodesRequired += nodesInThisLevel;
280 // If we have to many compute nodes, we ditch them
283 // We create the switches
285 for (unsigned int i = 0 ; i < this->levels ; i++) {
286 for (unsigned int j = 0 ; j < this->nodesByLevel[i + 1] ; j++) {
287 FatTreeNode* newNode;
288 newNode = new FatTreeNode(this->cluster, --k, i + 1, j);
289 XBT_DEBUG("We create the switch %d(%d,%d)", newNode->id, newNode->level,
291 newNode->children.resize(this->lowerLevelNodesNumber[i] *
292 this->lowerLevelPortsNumber[i]);
293 if (i != this->levels - 1) {
294 newNode->parents.resize(this->upperLevelNodesNumber[i + 1] *
295 this->lowerLevelPortsNumber[i + 1]);
297 newNode->label.resize(this->levels);
298 this->nodes.push_back(newNode);
303 void AsClusterFatTree::generateLabels() {
304 XBT_DEBUG("Generating labels.");
305 // TODO : check if nodesByLevel and nodes are filled
306 std::vector<int> maxLabel(this->levels);
307 std::vector<int> currentLabel(this->levels);
309 for (unsigned int i = 0 ; i <= this->levels ; i++) {
310 currentLabel.assign(this->levels, 0);
311 for (unsigned int j = 0 ; j < this->levels ; j++) {
312 maxLabel[j] = j + 1 > i ?
313 this->lowerLevelNodesNumber[j] : this->upperLevelNodesNumber[j];
316 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
318 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug )) {
319 std::stringstream msgBuffer;
321 msgBuffer << "Assigning label <";
322 for (unsigned int l = 0 ; l < this->levels ; l++) {
323 msgBuffer << currentLabel[l] << ",";
325 msgBuffer << "> to " << k << " (" << i << "," << j <<")";
327 XBT_DEBUG("%s", msgBuffer.str().c_str());
329 this->nodes[k]->label.assign(currentLabel.begin(), currentLabel.end());
331 bool remainder = true;
333 unsigned int pos = 0;
335 std::stringstream msgBuffer;
338 if (currentLabel[pos] >= maxLabel[pos]) {
339 currentLabel[pos] = 0;
352 while(remainder && pos < this->levels);
359 int AsClusterFatTree::getLevelPosition(const unsigned int level) {
360 if (level > this->levels) {
361 // Well, that should never happen. Maybe should we throw instead.
364 int tempPosition = 0;
366 for (unsigned int i = 0 ; i < level ; i++) {
367 tempPosition += this->nodesByLevel[i];
372 void AsClusterFatTree::addProcessingNode(int id) {
373 using std::make_pair;
374 static int position = 0;
375 FatTreeNode* newNode;
376 newNode = new FatTreeNode(this->cluster, id, 0, position++);
377 newNode->parents.resize(this->upperLevelNodesNumber[0] *
378 this->lowerLevelPortsNumber[0]);
379 newNode->label.resize(this->levels);
380 this->computeNodes.insert(make_pair(id,newNode));
381 this->nodes.push_back(newNode);
384 void AsClusterFatTree::addLink(FatTreeNode *parent, unsigned int parentPort,
385 FatTreeNode *child, unsigned int childPort) {
386 FatTreeLink *newLink;
387 newLink = new FatTreeLink(this->cluster, child, parent);
388 XBT_DEBUG("Creating a link between the parent (%d,%d,%u)"
389 " and the child (%d,%d,%u)", parent->level, parent->position,
390 parentPort, child->level, child->position, childPort);
391 parent->children[parentPort] = newLink;
392 child->parents[childPort] = newLink;
394 this->links.push_back(newLink);
397 void AsClusterFatTree::parse_specific_arguments(sg_platf_cluster_cbarg_t
399 std::vector<string> parameters;
400 std::vector<string> tmp;
401 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"
407 ", see the documentation for more informations");
408 // Well, there's no doc, yet
411 // The first parts of topo_parameters should be the levels number
412 this->levels = std::atoi(parameters[0].c_str()); // stoi() only in C++11...
414 // Then, a l-sized vector standing for the childs number by level
415 boost::split(tmp, parameters[1], boost::is_any_of(","));
416 if(tmp.size() != this->levels) {
417 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
418 ", see the documentation for more informations");
420 for(size_t i = 0 ; i < tmp.size() ; i++){
421 this->lowerLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
424 // Then, a l-sized vector standing for the parents number by level
425 boost::split(tmp, parameters[2], boost::is_any_of(","));
426 if(tmp.size() != this->levels) {
427 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
428 ", see the documentation for more informations");
430 for(size_t i = 0 ; i < tmp.size() ; i++){
431 this->upperLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
434 // Finally, a l-sized vector standing for the ports number with the lower level
435 boost::split(tmp, parameters[3], boost::is_any_of(","));
436 if(tmp.size() != this->levels) {
437 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
438 ", see the documentation for more informations");
441 for(size_t i = 0 ; i < tmp.size() ; i++){
442 this->lowerLevelPortsNumber.push_back(std::atoi(tmp[i].c_str()));
444 this->cluster = cluster;
448 void AsClusterFatTree::generateDotFile(const string& filename) const {
450 /* Maybe should we get directly a char*, as open takes strings only beginning
453 file.open(filename.c_str(), ios::out | ios::trunc);
456 file << "graph AsClusterFatTree {\n";
457 for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
458 file << this->nodes[i]->id;
459 if(this->nodes[i]->id < 0) {
460 file << " [shape=circle];\n";
463 file << " [shape=hexagon];\n";
467 for (unsigned int i = 0 ; i < this->links.size() ; i++ ) {
468 file << this->links[i]->downNode->id
470 << this->links[i]->upNode->id
477 XBT_DEBUG("Unable to open file %s", filename.c_str());
482 FatTreeNode::FatTreeNode(sg_platf_cluster_cbarg_t cluster, int id, int level,
483 int position) : id(id), level(level),
485 s_sg_platf_link_cbarg_t linkTemplate;
486 if(cluster->limiter_link) {
487 memset(&linkTemplate, 0, sizeof(linkTemplate));
488 linkTemplate.bandwidth = cluster->limiter_link;
489 linkTemplate.latency = 0;
490 linkTemplate.state = SURF_RESOURCE_ON;
491 linkTemplate.policy = SURF_LINK_SHARED;
492 linkTemplate.id = bprintf("limiter_%d", id);
493 sg_platf_new_link(&linkTemplate);
494 this->limiterLink = (NetworkLink*) xbt_lib_get_or_null(link_lib,
497 free((void*)linkTemplate.id);
499 if(cluster->loopback_bw || cluster->loopback_lat) {
500 memset(&linkTemplate, 0, sizeof(linkTemplate));
501 linkTemplate.bandwidth = cluster->loopback_bw;
502 linkTemplate.latency = cluster->loopback_lat;
503 linkTemplate.state = SURF_RESOURCE_ON;
504 linkTemplate.policy = SURF_LINK_FATPIPE;
505 linkTemplate.id = bprintf("loopback_%d", id);
506 sg_platf_new_link(&linkTemplate);
507 this->loopback = (NetworkLink*) xbt_lib_get_or_null(link_lib,
510 free((void*)linkTemplate.id);
514 FatTreeLink::FatTreeLink(sg_platf_cluster_cbarg_t cluster,
515 FatTreeNode *downNode,
516 FatTreeNode *upNode) : upNode(upNode),
518 static int uniqueId = 0;
519 s_sg_platf_link_cbarg_t linkTemplate;
520 memset(&linkTemplate, 0, sizeof(linkTemplate));
521 linkTemplate.bandwidth = cluster->bw;
522 linkTemplate.latency = cluster->lat;
523 linkTemplate.state = SURF_RESOURCE_ON;
524 linkTemplate.policy = cluster->sharing_policy; // sthg to do with that ?
525 linkTemplate.id = bprintf("link_from_%d_to_%d_%d", downNode->id, upNode->id,
527 sg_platf_new_link(&linkTemplate);
530 if (cluster->sharing_policy == SURF_LINK_FULLDUPLEX) {
531 tmpID = std::string(linkTemplate.id) + "_UP";
532 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, tmpID.c_str(),
534 this->upLink = link; // check link?
535 tmpID = std::string(linkTemplate.id) + "_DOWN";
536 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, tmpID.c_str(),
538 this->downLink = link; // check link ?
541 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, linkTemplate.id,
544 this->downLink = link;
547 free((void*)linkTemplate.id);