9 #include "src/surf/surf_routing_private.hpp"
10 #include "src/surf/surf_routing_cluster_fat_tree.hpp"
13 #include <boost/algorithm/string/split.hpp>
14 #include <boost/algorithm/string/classification.hpp>
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_fat_tree, surf, "Routing for fat trees");
24 AsClusterFatTree::AsClusterFatTree() : levels(0) {
25 XBT_DEBUG("Creating a new fat tree.");
28 AsClusterFatTree::~AsClusterFatTree() {
29 for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
30 delete this->nodes[i];
32 for (unsigned int i = 0 ; i < this->links.size() ; i++) {
33 delete this->links[i];
37 bool AsClusterFatTree::isInSubTree(FatTreeNode *root, FatTreeNode *node) {
38 XBT_DEBUG("Is %d(%u,%u) in the sub tree of %d(%u,%u) ?", node->id,
39 node->level, node->position, root->id, root->level, root->position);
40 if (root->level <= node->level) {
43 for (unsigned int i = 0 ; i < node->level ; i++) {
44 if(root->label[i] != node->label[i]) {
49 for (unsigned int i = root->level ; i < this->levels ; i++) {
50 if(root->label[i] != node->label[i]) {
57 void AsClusterFatTree::getRouteAndLatency(NetCard *src,
59 sg_platf_route_cbarg_t into,
61 FatTreeNode *source, *destination, *currentNode;
63 std::map<int, FatTreeNode*>::const_iterator tempIter;
65 if (dst->getRcType() == SURF_NETWORK_ELEMENT_ROUTER || src->getRcType() == SURF_NETWORK_ELEMENT_ROUTER) return;
67 /* Let's find the source and the destination in our internal structure */
68 tempIter = this->computeNodes.find(src->getId());
71 if (tempIter == this->computeNodes.end()) {
72 xbt_die("Could not find the source %s [%d] in the fat tree", src->getName(),
75 source = tempIter->second;
76 tempIter = this->computeNodes.find(dst->getId());
77 if (tempIter == this->computeNodes.end()) {
78 xbt_die("Could not find the destination %s [%d] in the fat tree",
79 dst->getName(), dst->getId());
83 destination = tempIter->second;
85 XBT_VERB("Get route and latency from '%s' [%d] to '%s' [%d] in a fat tree",
86 src->getName(), src->getId(), dst->getName(), dst->getId());
88 /* In case destination is the source, and there is a loopback, let's get
89 through it instead of going up to a switch*/
90 if(source->id == destination->id && this->p_has_loopback) {
91 xbt_dynar_push_as(into->link_list, void*, source->loopback);
93 *latency += source->loopback->getLatency();
101 while (!isInSubTree(currentNode, destination)) {
102 int d, k; // as in d-mod-k
103 d = destination->position;
105 for (unsigned int i = 0 ; i < currentNode->level ; i++) {
106 d /= this->upperLevelNodesNumber[i];
108 k = this->upperLevelNodesNumber[currentNode->level];
110 xbt_dynar_push_as(into->link_list, void*,currentNode->parents[d]->upLink);
113 *latency += currentNode->parents[d]->upLink->getLatency();
116 if (this->p_has_limiter) {
117 xbt_dynar_push_as(into->link_list, void*,currentNode->limiterLink);
119 currentNode = currentNode->parents[d]->upNode;
122 XBT_DEBUG("%d(%u,%u) is in the sub tree of %d(%u,%u).", destination->id,
123 destination->level, destination->position, currentNode->id,
124 currentNode->level, currentNode->position);
127 while(currentNode != destination) {
128 for(unsigned int i = 0 ; i < currentNode->children.size() ; i++) {
129 if(i % this->lowerLevelNodesNumber[currentNode->level - 1] ==
130 destination->label[currentNode->level - 1]) {
131 xbt_dynar_push_as(into->link_list, void*,currentNode->children[i]->downLink);
133 *latency += currentNode->children[i]->downLink->getLatency();
135 currentNode = currentNode->children[i]->downNode;
136 if (this->p_has_limiter) {
137 xbt_dynar_push_as(into->link_list, void*,currentNode->limiterLink);
139 XBT_DEBUG("%d(%u,%u) is accessible through %d(%u,%u)", destination->id,
140 destination->level, destination->position, currentNode->id,
141 currentNode->level, currentNode->position);
147 /* This function makes the assumption that parse_specific_arguments() and
148 * addNodes() have already been called
150 void AsClusterFatTree::create_links(){
151 if(this->levels == 0) {
154 this->generateSwitches();
157 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
158 std::stringstream msgBuffer;
160 msgBuffer << "We are creating a fat tree of " << this->levels << " levels "
161 << "with " << this->nodesByLevel[0] << " processing nodes";
162 for (unsigned int i = 1 ; i <= this->levels ; i++) {
163 msgBuffer << ", " << this->nodesByLevel[i] << " switches at level " << i;
165 XBT_DEBUG("%s", msgBuffer.str().c_str());
167 msgBuffer << "Nodes are : ";
169 for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
170 msgBuffer << this->nodes[i]->id << "(" << this->nodes[i]->level << ","
171 << this->nodes[i]->position << ") ";
173 XBT_DEBUG("%s", msgBuffer.str().c_str());
177 this->generateLabels();
180 // Nodes are totally ordered, by level and then by position, in this->nodes
181 for (unsigned int i = 0 ; i < this->levels ; i++) {
182 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
183 this->connectNodeToParents(this->nodes[k]);
188 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
189 std::stringstream msgBuffer;
190 msgBuffer << "Links are : ";
191 for (unsigned int i = 0 ; i < this->links.size() ; i++) {
192 msgBuffer << "(" << this->links[i]->upNode->id << ","
193 << this->links[i]->downNode->id << ") ";
195 XBT_DEBUG("%s", msgBuffer.str().c_str());
201 int AsClusterFatTree::connectNodeToParents(FatTreeNode *node) {
202 std::vector<FatTreeNode*>::iterator currentParentNode = this->nodes.begin();
203 int connectionsNumber = 0;
204 const int level = node->level;
205 XBT_DEBUG("We are connecting node %d(%u,%u) to his parents.",
206 node->id, node->level, node->position);
207 currentParentNode += this->getLevelPosition(level + 1);
208 for (unsigned int i = 0 ; i < this->nodesByLevel[level + 1] ; i++ ) {
209 if(this->areRelated(*currentParentNode, node)) {
210 XBT_DEBUG("%d(%u,%u) and %d(%u,%u) are related,"
211 " with %u links between them.", node->id,
212 node->level, node->position, (*currentParentNode)->id,
213 (*currentParentNode)->level, (*currentParentNode)->position, this->lowerLevelPortsNumber[level]);
214 for (unsigned int j = 0 ; j < this->lowerLevelPortsNumber[level] ; j++) {
215 this->addLink(*currentParentNode, node->label[level] +
216 j * this->lowerLevelNodesNumber[level], node,
217 (*currentParentNode)->label[level] +
218 j * this->upperLevelNodesNumber[level]);
224 return connectionsNumber;
228 bool AsClusterFatTree::areRelated(FatTreeNode *parent, FatTreeNode *child) {
229 std::stringstream msgBuffer;
231 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
232 msgBuffer << "Are " << child->id << "(" << child->level << ","
233 << child->position << ") <";
235 for (unsigned int i = 0 ; i < this->levels ; i++) {
236 msgBuffer << child->label[i] << ",";
240 msgBuffer << " and " << parent->id << "(" << parent->level
241 << "," << parent->position << ") <";
242 for (unsigned int i = 0 ; i < this->levels ; i++) {
243 msgBuffer << parent->label[i] << ",";
246 msgBuffer << " related ? ";
247 XBT_DEBUG("%s", msgBuffer.str().c_str());
250 if (parent->level != child->level + 1) {
254 for (unsigned int i = 0 ; i < this->levels; i++) {
255 if (parent->label[i] != child->label[i] && i + 1 != parent->level) {
262 void AsClusterFatTree::generateSwitches() {
263 XBT_DEBUG("Generating switches.");
264 this->nodesByLevel.resize(this->levels + 1, 0);
265 unsigned int nodesRequired = 0;
267 // We take care of the number of nodes by level
268 this->nodesByLevel[0] = 1;
269 for (unsigned int i = 0 ; i < this->levels ; i++) {
270 this->nodesByLevel[0] *= this->lowerLevelNodesNumber[i];
274 if(this->nodesByLevel[0] != this->nodes.size()) {
275 surf_parse_error("The number of provided nodes does not fit with the wanted topology."
276 " Please check your platform description (We need %d nodes, we got %zu)",
277 this->nodesByLevel[0], this->nodes.size());
282 for (unsigned int i = 0 ; i < this->levels ; i++) {
283 int nodesInThisLevel = 1;
285 for (unsigned int j = 0 ; j <= i ; j++) {
286 nodesInThisLevel *= this->upperLevelNodesNumber[j];
289 for (unsigned int j = i+1 ; j < this->levels ; j++) {
290 nodesInThisLevel *= this->lowerLevelNodesNumber[j];
293 this->nodesByLevel[i+1] = nodesInThisLevel;
294 nodesRequired += nodesInThisLevel;
298 // We create the switches
300 for (unsigned int i = 0 ; i < this->levels ; i++) {
301 for (unsigned int j = 0 ; j < this->nodesByLevel[i + 1] ; j++) {
302 FatTreeNode* newNode;
303 newNode = new FatTreeNode(this->cluster, --k, i + 1, j);
304 XBT_DEBUG("We create the switch %d(%d,%d)", newNode->id, newNode->level,
306 newNode->children.resize(this->lowerLevelNodesNumber[i] *
307 this->lowerLevelPortsNumber[i]);
308 if (i != this->levels - 1) {
309 newNode->parents.resize(this->upperLevelNodesNumber[i + 1] *
310 this->lowerLevelPortsNumber[i + 1]);
312 newNode->label.resize(this->levels);
313 this->nodes.push_back(newNode);
318 void AsClusterFatTree::generateLabels() {
319 XBT_DEBUG("Generating labels.");
320 // TODO : check if nodesByLevel and nodes are filled
321 std::vector<int> maxLabel(this->levels);
322 std::vector<int> currentLabel(this->levels);
324 for (unsigned int i = 0 ; i <= this->levels ; i++) {
325 currentLabel.assign(this->levels, 0);
326 for (unsigned int j = 0 ; j < this->levels ; j++) {
327 maxLabel[j] = j + 1 > i ?
328 this->lowerLevelNodesNumber[j] : this->upperLevelNodesNumber[j];
331 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
333 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug )) {
334 std::stringstream msgBuffer;
336 msgBuffer << "Assigning label <";
337 for (unsigned int l = 0 ; l < this->levels ; l++) {
338 msgBuffer << currentLabel[l] << ",";
340 msgBuffer << "> to " << k << " (" << i << "," << j <<")";
342 XBT_DEBUG("%s", msgBuffer.str().c_str());
344 this->nodes[k]->label.assign(currentLabel.begin(), currentLabel.end());
346 bool remainder = true;
348 unsigned int pos = 0;
350 std::stringstream msgBuffer;
353 if (currentLabel[pos] >= maxLabel[pos]) {
354 currentLabel[pos] = 0;
367 while(remainder && pos < this->levels);
374 int AsClusterFatTree::getLevelPosition(const unsigned int level) {
375 if (level > this->levels) {
376 // Well, that should never happen. Maybe should we throw instead.
379 int tempPosition = 0;
381 for (unsigned int i = 0 ; i < level ; i++) {
382 tempPosition += this->nodesByLevel[i];
387 void AsClusterFatTree::addProcessingNode(int id) {
388 using std::make_pair;
389 static int position = 0;
390 FatTreeNode* newNode;
391 newNode = new FatTreeNode(this->cluster, id, 0, position++);
392 newNode->parents.resize(this->upperLevelNodesNumber[0] *
393 this->lowerLevelPortsNumber[0]);
394 newNode->label.resize(this->levels);
395 this->computeNodes.insert(make_pair(id,newNode));
396 this->nodes.push_back(newNode);
399 void AsClusterFatTree::addLink(FatTreeNode *parent, unsigned int parentPort,
400 FatTreeNode *child, unsigned int childPort) {
401 FatTreeLink *newLink;
402 newLink = new FatTreeLink(this->cluster, child, parent);
403 XBT_DEBUG("Creating a link between the parent (%d,%d,%u)"
404 " and the child (%d,%d,%u)", parent->level, parent->position,
405 parentPort, child->level, child->position, childPort);
406 parent->children[parentPort] = newLink;
407 child->parents[childPort] = newLink;
409 this->links.push_back(newLink);
415 void AsClusterFatTree::parse_specific_arguments(sg_platf_cluster_cbarg_t
417 std::vector<std::string> parameters;
418 std::vector<std::string> tmp;
419 boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
422 // TODO : we have to check for zeros and negative numbers, or it might crash
423 if (parameters.size() != 4){
424 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
425 ", see the documentation for more informations");
428 // The first parts of topo_parameters should be the levels number
429 this->levels = xbt_str_parse_int(parameters[0].c_str(), "First parameter is not the amount of levels: %s");
431 // Then, a l-sized vector standing for the childs number by level
432 boost::split(tmp, parameters[1], boost::is_any_of(","));
433 if(tmp.size() != this->levels) {
434 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
435 ", see the documentation for more informations");
437 for(size_t i = 0 ; i < tmp.size() ; i++){
438 this->lowerLevelNodesNumber.push_back(xbt_str_parse_int(tmp[i].c_str(), "Invalid lower level node number: %s"));
441 // Then, a l-sized vector standing for the parents number by level
442 boost::split(tmp, parameters[2], boost::is_any_of(","));
443 if(tmp.size() != this->levels) {
444 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
445 ", see the documentation for more informations");
447 for(size_t i = 0 ; i < tmp.size() ; i++){
448 this->upperLevelNodesNumber.push_back(xbt_str_parse_int(tmp[i].c_str(), "Invalid upper level node number: %s"));
451 // Finally, a l-sized vector standing for the ports number with the lower level
452 boost::split(tmp, parameters[3], boost::is_any_of(","));
453 if(tmp.size() != this->levels) {
454 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
455 ", see the documentation for more informations");
458 for(size_t i = 0 ; i < tmp.size() ; i++){
459 this->lowerLevelPortsNumber.push_back(xbt_str_parse_int(tmp[i].c_str(), "Invalid lower level node number: %s"));
461 this->cluster = cluster;
465 void AsClusterFatTree::generateDotFile(const std::string& filename) const {
467 /* Maybe should we get directly a char*, as open takes strings only beginning
470 file.open(filename.c_str(), std::ios::out | std::ios::trunc);
473 file << "graph AsClusterFatTree {\n";
474 for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
475 file << this->nodes[i]->id;
476 if(this->nodes[i]->id < 0) {
477 file << " [shape=circle];\n";
480 file << " [shape=hexagon];\n";
484 for (unsigned int i = 0 ; i < this->links.size() ; i++ ) {
485 file << this->links[i]->downNode->id
487 << this->links[i]->upNode->id
494 XBT_DEBUG("Unable to open file %s", filename.c_str());
499 FatTreeNode::FatTreeNode(sg_platf_cluster_cbarg_t cluster, int id, int level,
500 int position) : id(id), level(level),
502 s_sg_platf_link_cbarg_t linkTemplate = SG_PLATF_LINK_INITIALIZER;
503 if(cluster->limiter_link) {
504 memset(&linkTemplate, 0, sizeof(linkTemplate));
505 linkTemplate.bandwidth = cluster->limiter_link;
506 linkTemplate.latency = 0;
507 linkTemplate.initiallyOn = 1;
508 linkTemplate.policy = SURF_LINK_SHARED;
509 linkTemplate.id = bprintf("limiter_%d", id);
510 sg_platf_new_link(&linkTemplate);
511 this->limiterLink = Link::byName(linkTemplate.id);
512 free((void*)linkTemplate.id);
514 if(cluster->loopback_bw || cluster->loopback_lat) {
515 memset(&linkTemplate, 0, sizeof(linkTemplate));
516 linkTemplate.bandwidth = cluster->loopback_bw;
517 linkTemplate.latency = cluster->loopback_lat;
518 linkTemplate.initiallyOn = 1;
519 linkTemplate.policy = SURF_LINK_FATPIPE;
520 linkTemplate.id = bprintf("loopback_%d", id);
521 sg_platf_new_link(&linkTemplate);
522 this->loopback = Link::byName(linkTemplate.id);
523 free((void*)linkTemplate.id);
527 FatTreeLink::FatTreeLink(sg_platf_cluster_cbarg_t cluster,
528 FatTreeNode *downNode,
529 FatTreeNode *upNode) : upNode(upNode),
531 static int uniqueId = 0;
532 s_sg_platf_link_cbarg_t linkTemplate = SG_PLATF_LINK_INITIALIZER;
533 memset(&linkTemplate, 0, sizeof(linkTemplate));
534 linkTemplate.bandwidth = cluster->bw;
535 linkTemplate.latency = cluster->lat;
536 linkTemplate.initiallyOn = 1;
537 linkTemplate.policy = cluster->sharing_policy; // sthg to do with that ?
538 linkTemplate.id = bprintf("link_from_%d_to_%d_%d", downNode->id, upNode->id,
540 sg_platf_new_link(&linkTemplate);
543 if (cluster->sharing_policy == SURF_LINK_FULLDUPLEX) {
544 tmpID = std::string(linkTemplate.id) + "_UP";
545 link = Link::byName(tmpID.c_str());
546 this->upLink = link; // check link?
547 tmpID = std::string(linkTemplate.id) + "_DOWN";
548 link = Link::byName(tmpID.c_str());
549 this->downLink = link; // check link ?
552 link = Link::byName(linkTemplate.id);
554 this->downLink = link;
557 free((void*)linkTemplate.id);