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::vector<NetworkLink*> route;
53 std::map<int, FatTreeNode*>::const_iterator tempIter;
55 tempIter = this->computeNodes.find(src->getId());
58 if (tempIter == this->computeNodes.end()) {
59 xbt_die("Could not find the source %s [%d] in the fat tree", src->getName(),
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",
66 src->getName(), src->getId());
70 destination = tempIter->second;
71 XBT_VERB("Get route and latency from '%s' [%d] to '%s' [%d] in a fat tree",
72 src->getName(), src->getId(), dst->getName(), dst->getId());
77 while (!isInSubTree(currentNode, destination)) {
78 int d, k; // as in d-mod-k
79 d = destination->position;
81 for (unsigned int i = 0 ; i < currentNode->level ; i++) {
82 d /= this->upperLevelNodesNumber[i];
84 k = this->upperLevelNodesNumber[currentNode->level];
86 route.push_back(currentNode->parents[d]->upLink);
89 *latency += currentNode->parents[d]->upLink->getLatency();
92 if (this->p_has_limiter) {
93 route.push_back(currentNode->limiterLink);
95 currentNode = currentNode->parents[d]->upNode;
97 XBT_DEBUG("%d(%u,%u) is in the sub tree of %d(%u,%u).", destination->id,
98 destination->level, destination->position, currentNode->id,
99 currentNode->level, currentNode->position);
101 while(currentNode != destination) {
102 for(unsigned int i = 0 ; i < currentNode->children.size() ; i++) {
103 if(i % this->lowerLevelNodesNumber[currentNode->level - 1] ==
104 destination->label[currentNode->level - 1]) {
105 route.push_back(currentNode->children[i]->downLink);
107 *latency += currentNode->children[i]->downLink->getLatency();
109 currentNode = currentNode->children[i]->downNode;
110 XBT_DEBUG("%d(%u,%u) is accessible through %d(%u,%u)", destination->id,
111 destination->level, destination->position, currentNode->id,
112 currentNode->level, currentNode->position);
117 for (unsigned int i = 0 ; i < route.size() ; i++) {
118 xbt_dynar_push_as(into->link_list, void*, route[i]);
123 /* This function makes the assumption that parse_specific_arguments() and
124 * addNodes() have already been called
126 void AsClusterFatTree::create_links(){
127 if(this->levels == 0) {
130 this->generateSwitches();
133 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
134 std::stringstream msgBuffer;
136 msgBuffer << "We are creating a fat tree of " << this->levels << " levels "
137 << "with " << this->nodesByLevel[0] << " processing nodes";
138 for (unsigned int i = 1 ; i <= this->levels ; i++) {
139 msgBuffer << ", " << this->nodesByLevel[i] << " switches at level " << i;
141 XBT_DEBUG("%s", msgBuffer.str().c_str());
143 msgBuffer << "Nodes are : ";
145 for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
146 msgBuffer << this->nodes[i]->id << "(" << this->nodes[i]->level << ","
147 << this->nodes[i]->position << ") ";
149 XBT_DEBUG("%s", msgBuffer.str().c_str());
153 this->generateLabels();
156 // Nodes are totally ordered, by level and then by position, in this->nodes
157 for (unsigned int i = 0 ; i < this->levels ; i++) {
158 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
159 this->connectNodeToParents(this->nodes[k]);
164 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
165 std::stringstream msgBuffer;
166 msgBuffer << "Links are : ";
167 for (unsigned int i = 0 ; i < this->links.size() ; i++) {
168 msgBuffer << "(" << this->links[i]->upNode->id << ","
169 << this->links[i]->downNode->id << ") ";
171 XBT_DEBUG("%s", msgBuffer.str().c_str());
177 int AsClusterFatTree::connectNodeToParents(FatTreeNode *node) {
178 std::vector<FatTreeNode*>::iterator currentParentNode = this->nodes.begin();
179 int connectionsNumber = 0;
180 const int level = node->level;
181 XBT_DEBUG("We are connecting node %d(%u,%u) to his parents.",
182 node->id, node->level, node->position);
183 currentParentNode += this->getLevelPosition(level + 1);
184 for (unsigned int i = 0 ; i < this->nodesByLevel[level + 1] ; i++ ) {
185 if(this->areRelated(*currentParentNode, node)) {
186 XBT_DEBUG("%d(%u,%u) and %d(%u,%u) are related,"
187 " with %u links between them.", node->id,
188 node->level, node->position, (*currentParentNode)->id,
189 (*currentParentNode)->level, (*currentParentNode)->position, this->lowerLevelPortsNumber[level]);
190 for (unsigned int j = 0 ; j < this->lowerLevelPortsNumber[level] ; j++) {
191 this->addLink(*currentParentNode, node->label[level] +
192 j * this->lowerLevelNodesNumber[level], node,
193 (*currentParentNode)->label[level] +
194 j * this->upperLevelNodesNumber[level]);
200 return connectionsNumber;
204 bool AsClusterFatTree::areRelated(FatTreeNode *parent, FatTreeNode *child) {
205 std::stringstream msgBuffer;
207 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
208 msgBuffer << "Are " << child->id << "(" << child->level << ","
209 << child->position << ") <";
211 for (unsigned int i = 0 ; i < this->levels ; i++) {
212 msgBuffer << child->label[i] << ",";
216 msgBuffer << " and " << parent->id << "(" << parent->level
217 << "," << parent->position << ") <";
218 for (unsigned int i = 0 ; i < this->levels ; i++) {
219 msgBuffer << parent->label[i] << ",";
222 msgBuffer << " related ? ";
223 XBT_DEBUG("%s", msgBuffer.str().c_str());
226 if (parent->level != child->level + 1) {
230 for (unsigned int i = 0 ; i < this->levels; i++) {
231 if (parent->label[i] != child->label[i] && i + 1 != parent->level) {
238 void AsClusterFatTree::generateSwitches() {
239 XBT_DEBUG("Generating switches.");
240 this->nodesByLevel.resize(this->levels + 1, 0);
241 unsigned int nodesRequired = 0;
243 // We take care of the number of nodes by level
244 this->nodesByLevel[0] = 1;
245 for (unsigned int i = 0 ; i < this->levels ; i++) {
246 this->nodesByLevel[0] *= this->lowerLevelNodesNumber[i];
250 if(this->nodesByLevel[0] != this->nodes.size()) {
251 surf_parse_error("The number of provided nodes does not fit with the wanted topology."
252 " Please check your platform description (We need %d nodes, we got %zu)",
253 this->nodesByLevel[0], this->nodes.size());
258 for (unsigned int i = 0 ; i < this->levels ; i++) {
259 int nodesInThisLevel = 1;
261 for (unsigned int j = 0 ; j <= i ; j++) {
262 nodesInThisLevel *= this->upperLevelNodesNumber[j];
265 for (unsigned int j = i+1 ; j < this->levels ; j++) {
266 nodesInThisLevel *= this->lowerLevelNodesNumber[j];
269 this->nodesByLevel[i+1] = nodesInThisLevel;
270 nodesRequired += nodesInThisLevel;
274 // If we have to many compute nodes, we ditch them
277 // We create the switches
279 for (unsigned int i = 0 ; i < this->levels ; i++) {
280 for (unsigned int j = 0 ; j < this->nodesByLevel[i + 1] ; j++) {
281 FatTreeNode* newNode;
282 newNode = new FatTreeNode(this->cluster, --k, i + 1, j);
283 XBT_DEBUG("We create the switch %d(%d,%d)", newNode->id, newNode->level,
285 newNode->children.resize(this->lowerLevelNodesNumber[i] *
286 this->lowerLevelPortsNumber[i]);
287 if (i != this->levels - 1) {
288 newNode->parents.resize(this->upperLevelNodesNumber[i + 1] *
289 this->lowerLevelPortsNumber[i + 1]);
291 newNode->label.resize(this->levels);
292 this->nodes.push_back(newNode);
297 void AsClusterFatTree::generateLabels() {
298 XBT_DEBUG("Generating labels.");
299 // TODO : check if nodesByLevel and nodes are filled
300 std::vector<int> maxLabel(this->levels);
301 std::vector<int> currentLabel(this->levels);
303 for (unsigned int i = 0 ; i <= this->levels ; i++) {
304 currentLabel.assign(this->levels, 0);
305 for (unsigned int j = 0 ; j < this->levels ; j++) {
306 maxLabel[j] = j + 1 > i ?
307 this->lowerLevelNodesNumber[j] : this->upperLevelNodesNumber[j];
310 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
312 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug )) {
313 std::stringstream msgBuffer;
315 msgBuffer << "Assigning label <";
316 for (unsigned int l = 0 ; l < this->levels ; l++) {
317 msgBuffer << currentLabel[l] << ",";
319 msgBuffer << "> to " << k << " (" << i << "," << j <<")";
321 XBT_DEBUG("%s", msgBuffer.str().c_str());
323 this->nodes[k]->label.assign(currentLabel.begin(), currentLabel.end());
325 bool remainder = true;
327 unsigned int pos = 0;
329 std::stringstream msgBuffer;
332 if (currentLabel[pos] >= maxLabel[pos]) {
333 currentLabel[pos] = 0;
346 while(remainder && pos < this->levels);
353 int AsClusterFatTree::getLevelPosition(const unsigned int level) {
354 if (level > this->levels) {
355 // Well, that should never happen. Maybe should we throw instead.
358 int tempPosition = 0;
360 for (unsigned int i = 0 ; i < level ; i++) {
361 tempPosition += this->nodesByLevel[i];
366 void AsClusterFatTree::addProcessingNode(int id) {
367 using std::make_pair;
368 static int position = 0;
369 FatTreeNode* newNode;
370 newNode = new FatTreeNode(this->cluster, id, 0, position++);
371 newNode->parents.resize(this->upperLevelNodesNumber[0] *
372 this->lowerLevelPortsNumber[0]);
373 newNode->label.resize(this->levels);
374 this->computeNodes.insert(make_pair(id,newNode));
375 this->nodes.push_back(newNode);
378 void AsClusterFatTree::addLink(FatTreeNode *parent, unsigned int parentPort,
379 FatTreeNode *child, unsigned int childPort) {
380 FatTreeLink *newLink;
381 newLink = new FatTreeLink(this->cluster, child, parent);
382 XBT_DEBUG("Creating a link between the parent (%d,%d,%u)"
383 " and the child (%d,%d,%u)", parent->level, parent->position,
384 parentPort, child->level, child->position, childPort);
385 parent->children[parentPort] = newLink;
386 child->parents[childPort] = newLink;
388 this->links.push_back(newLink);
391 void AsClusterFatTree::parse_specific_arguments(sg_platf_cluster_cbarg_t
393 std::vector<string> parameters;
394 std::vector<string> tmp;
395 boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
398 // TODO : we have to check for zeros and negative numbers, or it might crash
399 if (parameters.size() != 4){
400 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
401 ", see the documentation for more informations");
402 // Well, there's no doc, yet
405 // The first parts of topo_parameters should be the levels number
406 this->levels = std::atoi(parameters[0].c_str()); // stoi() only in C++11...
408 // Then, a l-sized vector standing for the childs number by level
409 boost::split(tmp, parameters[1], 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->lowerLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
418 // Then, a l-sized vector standing for the parents number by level
419 boost::split(tmp, parameters[2], 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");
424 for(size_t i = 0 ; i < tmp.size() ; i++){
425 this->upperLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
428 // Finally, a l-sized vector standing for the ports number with the lower level
429 boost::split(tmp, parameters[3], boost::is_any_of(","));
430 if(tmp.size() != this->levels) {
431 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
432 ", see the documentation for more informations");
435 for(size_t i = 0 ; i < tmp.size() ; i++){
436 this->lowerLevelPortsNumber.push_back(std::atoi(tmp[i].c_str()));
438 this->cluster = cluster;
442 void AsClusterFatTree::generateDotFile(const string& filename) const {
444 /* Maybe should we get directly a char*, as open takes strings only beginning
447 file.open(filename.c_str(), ios::out | ios::trunc);
450 file << "graph AsClusterFatTree {\n";
451 for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
452 file << this->nodes[i]->id;
453 if(this->nodes[i]->id < 0) {
454 file << " [shape=circle];\n";
457 file << " [shape=hexagon];\n";
461 for (unsigned int i = 0 ; i < this->links.size() ; i++ ) {
462 file << this->links[i]->downNode->id
464 << this->links[i]->upNode->id
471 XBT_DEBUG("Unable to open file %s", filename.c_str());
476 FatTreeNode::FatTreeNode(sg_platf_cluster_cbarg_t cluster, int id, int level,
477 int position) : id(id), level(level),
479 s_sg_platf_link_cbarg_t linkTemplate;
480 if(cluster->limiter_link) {
481 memset(&linkTemplate, 0, sizeof(linkTemplate));
482 linkTemplate.bandwidth = cluster->limiter_link;
483 linkTemplate.latency = 0;
484 linkTemplate.state = SURF_RESOURCE_ON;
485 linkTemplate.policy = SURF_LINK_SHARED;
486 linkTemplate.id = bprintf("limiter_%d", id);
487 sg_platf_new_link(&linkTemplate);
488 this->limiterLink = (NetworkLink*) xbt_lib_get_or_null(link_lib,
491 free((void*)linkTemplate.id);
493 if(cluster->loopback_bw || cluster->loopback_lat) {
494 memset(&linkTemplate, 0, sizeof(linkTemplate));
495 linkTemplate.bandwidth = cluster->loopback_bw;
496 linkTemplate.latency = cluster->loopback_lat;
497 linkTemplate.state = SURF_RESOURCE_ON;
498 linkTemplate.policy = SURF_LINK_FATPIPE;
499 linkTemplate.id = bprintf("loopback_%d", id);
500 sg_platf_new_link(&linkTemplate);
501 this->loopback = (NetworkLink*) xbt_lib_get_or_null(link_lib,
504 free((void*)linkTemplate.id);
508 FatTreeLink::FatTreeLink(sg_platf_cluster_cbarg_t cluster,
509 FatTreeNode *downNode,
510 FatTreeNode *upNode) : upNode(upNode),
512 static int uniqueId = 0;
513 s_sg_platf_link_cbarg_t linkTemplate;
514 memset(&linkTemplate, 0, sizeof(linkTemplate));
515 linkTemplate.bandwidth = cluster->bw;
516 linkTemplate.latency = cluster->lat;
517 linkTemplate.state = SURF_RESOURCE_ON;
518 linkTemplate.policy = cluster->sharing_policy; // sthg to do with that ?
519 linkTemplate.id = bprintf("link_from_%d_to_%d_%d", downNode->id, upNode->id,
521 sg_platf_new_link(&linkTemplate);
524 if (cluster->sharing_policy == SURF_LINK_FULLDUPLEX) {
525 tmpID = std::string(linkTemplate.id) + "_UP";
526 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, tmpID.c_str(),
528 this->upLink = link; // check link?
529 tmpID = std::string(linkTemplate.id) + "_DOWN";
530 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, tmpID.c_str(),
532 this->downLink = link; // check link ?
535 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, linkTemplate.id,
538 this->downLink = link;
541 free((void*)linkTemplate.id);