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(const char*name)
27 XBT_DEBUG("Creating a new fat tree.");
30 AsClusterFatTree::~AsClusterFatTree() {
31 for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
32 delete this->nodes[i];
34 for (unsigned int i = 0 ; i < this->links.size() ; i++) {
35 delete this->links[i];
39 bool AsClusterFatTree::isInSubTree(FatTreeNode *root, FatTreeNode *node) {
40 XBT_DEBUG("Is %d(%u,%u) in the sub tree of %d(%u,%u) ?", node->id,
41 node->level, node->position, root->id, root->level, root->position);
42 if (root->level <= node->level) {
45 for (unsigned int i = 0 ; i < node->level ; i++) {
46 if(root->label[i] != node->label[i]) {
51 for (unsigned int i = root->level ; i < this->levels ; i++) {
52 if(root->label[i] != node->label[i]) {
59 void AsClusterFatTree::getRouteAndLatency(NetCard *src,
61 sg_platf_route_cbarg_t into,
63 FatTreeNode *source, *destination, *currentNode;
65 std::map<int, FatTreeNode*>::const_iterator tempIter;
67 if (dst->getRcType() == SURF_NETWORK_ELEMENT_ROUTER || src->getRcType() == SURF_NETWORK_ELEMENT_ROUTER) return;
69 /* Let's find the source and the destination in our internal structure */
70 tempIter = this->computeNodes.find(src->id());
73 if (tempIter == this->computeNodes.end()) {
74 xbt_die("Could not find the source %s [%d] in the fat tree", src->name(),
77 source = tempIter->second;
78 tempIter = this->computeNodes.find(dst->id());
79 if (tempIter == this->computeNodes.end()) {
80 xbt_die("Could not find the destination %s [%d] in the fat tree",
81 dst->name(), dst->id());
85 destination = tempIter->second;
87 XBT_VERB("Get route and latency from '%s' [%d] to '%s' [%d] in a fat tree",
88 src->name(), src->id(), dst->name(), dst->id());
90 /* In case destination is the source, and there is a loopback, let's get
91 through it instead of going up to a switch*/
92 if(source->id == destination->id && this->p_has_loopback) {
93 xbt_dynar_push_as(into->link_list, void*, source->loopback);
95 *latency += source->loopback->getLatency();
100 currentNode = source;
103 while (!isInSubTree(currentNode, destination)) {
104 int d, k; // as in d-mod-k
105 d = destination->position;
107 for (unsigned int i = 0 ; i < currentNode->level ; i++) {
108 d /= this->upperLevelNodesNumber[i];
110 k = this->upperLevelNodesNumber[currentNode->level];
112 xbt_dynar_push_as(into->link_list, void*,currentNode->parents[d]->upLink);
115 *latency += currentNode->parents[d]->upLink->getLatency();
118 if (this->p_has_limiter) {
119 xbt_dynar_push_as(into->link_list, void*,currentNode->limiterLink);
121 currentNode = currentNode->parents[d]->upNode;
124 XBT_DEBUG("%d(%u,%u) is in the sub tree of %d(%u,%u).", destination->id,
125 destination->level, destination->position, currentNode->id,
126 currentNode->level, currentNode->position);
129 while(currentNode != destination) {
130 for(unsigned int i = 0 ; i < currentNode->children.size() ; i++) {
131 if(i % this->lowerLevelNodesNumber[currentNode->level - 1] ==
132 destination->label[currentNode->level - 1]) {
133 xbt_dynar_push_as(into->link_list, void*,currentNode->children[i]->downLink);
135 *latency += currentNode->children[i]->downLink->getLatency();
137 currentNode = currentNode->children[i]->downNode;
138 if (this->p_has_limiter) {
139 xbt_dynar_push_as(into->link_list, void*,currentNode->limiterLink);
141 XBT_DEBUG("%d(%u,%u) is accessible through %d(%u,%u)", destination->id,
142 destination->level, destination->position, currentNode->id,
143 currentNode->level, currentNode->position);
149 /* This function makes the assumption that parse_specific_arguments() and
150 * addNodes() have already been called
152 void AsClusterFatTree::create_links(){
153 if(this->levels == 0) {
156 this->generateSwitches();
159 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
160 std::stringstream msgBuffer;
162 msgBuffer << "We are creating a fat tree of " << this->levels << " levels "
163 << "with " << this->nodesByLevel[0] << " processing nodes";
164 for (unsigned int i = 1 ; i <= this->levels ; i++) {
165 msgBuffer << ", " << this->nodesByLevel[i] << " switches at level " << i;
167 XBT_DEBUG("%s", msgBuffer.str().c_str());
169 msgBuffer << "Nodes are : ";
171 for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
172 msgBuffer << this->nodes[i]->id << "(" << this->nodes[i]->level << ","
173 << this->nodes[i]->position << ") ";
175 XBT_DEBUG("%s", msgBuffer.str().c_str());
179 this->generateLabels();
182 // Nodes are totally ordered, by level and then by position, in this->nodes
183 for (unsigned int i = 0 ; i < this->levels ; i++) {
184 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
185 this->connectNodeToParents(this->nodes[k]);
190 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
191 std::stringstream msgBuffer;
192 msgBuffer << "Links are : ";
193 for (unsigned int i = 0 ; i < this->links.size() ; i++) {
194 msgBuffer << "(" << this->links[i]->upNode->id << ","
195 << this->links[i]->downNode->id << ") ";
197 XBT_DEBUG("%s", msgBuffer.str().c_str());
203 int AsClusterFatTree::connectNodeToParents(FatTreeNode *node) {
204 std::vector<FatTreeNode*>::iterator currentParentNode = this->nodes.begin();
205 int connectionsNumber = 0;
206 const int level = node->level;
207 XBT_DEBUG("We are connecting node %d(%u,%u) to his parents.",
208 node->id, node->level, node->position);
209 currentParentNode += this->getLevelPosition(level + 1);
210 for (unsigned int i = 0 ; i < this->nodesByLevel[level + 1] ; i++ ) {
211 if(this->areRelated(*currentParentNode, node)) {
212 XBT_DEBUG("%d(%u,%u) and %d(%u,%u) are related,"
213 " with %u links between them.", node->id,
214 node->level, node->position, (*currentParentNode)->id,
215 (*currentParentNode)->level, (*currentParentNode)->position, this->lowerLevelPortsNumber[level]);
216 for (unsigned int j = 0 ; j < this->lowerLevelPortsNumber[level] ; j++) {
217 this->addLink(*currentParentNode, node->label[level] +
218 j * this->lowerLevelNodesNumber[level], node,
219 (*currentParentNode)->label[level] +
220 j * this->upperLevelNodesNumber[level]);
226 return connectionsNumber;
230 bool AsClusterFatTree::areRelated(FatTreeNode *parent, FatTreeNode *child) {
231 std::stringstream msgBuffer;
233 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
234 msgBuffer << "Are " << child->id << "(" << child->level << ","
235 << child->position << ") <";
237 for (unsigned int i = 0 ; i < this->levels ; i++) {
238 msgBuffer << child->label[i] << ",";
242 msgBuffer << " and " << parent->id << "(" << parent->level
243 << "," << parent->position << ") <";
244 for (unsigned int i = 0 ; i < this->levels ; i++) {
245 msgBuffer << parent->label[i] << ",";
248 msgBuffer << " related ? ";
249 XBT_DEBUG("%s", msgBuffer.str().c_str());
252 if (parent->level != child->level + 1) {
256 for (unsigned int i = 0 ; i < this->levels; i++) {
257 if (parent->label[i] != child->label[i] && i + 1 != parent->level) {
264 void AsClusterFatTree::generateSwitches() {
265 XBT_DEBUG("Generating switches.");
266 this->nodesByLevel.resize(this->levels + 1, 0);
267 unsigned int nodesRequired = 0;
269 // We take care of the number of nodes by level
270 this->nodesByLevel[0] = 1;
271 for (unsigned int i = 0 ; i < this->levels ; i++) {
272 this->nodesByLevel[0] *= this->lowerLevelNodesNumber[i];
276 if(this->nodesByLevel[0] != this->nodes.size()) {
277 surf_parse_error("The number of provided nodes does not fit with the wanted topology."
278 " Please check your platform description (We need %d nodes, we got %zu)",
279 this->nodesByLevel[0], this->nodes.size());
284 for (unsigned int i = 0 ; i < this->levels ; i++) {
285 int nodesInThisLevel = 1;
287 for (unsigned int j = 0 ; j <= i ; j++) {
288 nodesInThisLevel *= this->upperLevelNodesNumber[j];
291 for (unsigned int j = i+1 ; j < this->levels ; j++) {
292 nodesInThisLevel *= this->lowerLevelNodesNumber[j];
295 this->nodesByLevel[i+1] = nodesInThisLevel;
296 nodesRequired += nodesInThisLevel;
300 // We create the switches
302 for (unsigned int i = 0 ; i < this->levels ; i++) {
303 for (unsigned int j = 0 ; j < this->nodesByLevel[i + 1] ; j++) {
304 FatTreeNode* newNode;
305 newNode = new FatTreeNode(this->cluster, --k, i + 1, j);
306 XBT_DEBUG("We create the switch %d(%d,%d)", newNode->id, newNode->level,
308 newNode->children.resize(this->lowerLevelNodesNumber[i] *
309 this->lowerLevelPortsNumber[i]);
310 if (i != this->levels - 1) {
311 newNode->parents.resize(this->upperLevelNodesNumber[i + 1] *
312 this->lowerLevelPortsNumber[i + 1]);
314 newNode->label.resize(this->levels);
315 this->nodes.push_back(newNode);
320 void AsClusterFatTree::generateLabels() {
321 XBT_DEBUG("Generating labels.");
322 // TODO : check if nodesByLevel and nodes are filled
323 std::vector<int> maxLabel(this->levels);
324 std::vector<int> currentLabel(this->levels);
326 for (unsigned int i = 0 ; i <= this->levels ; i++) {
327 currentLabel.assign(this->levels, 0);
328 for (unsigned int j = 0 ; j < this->levels ; j++) {
329 maxLabel[j] = j + 1 > i ?
330 this->lowerLevelNodesNumber[j] : this->upperLevelNodesNumber[j];
333 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
335 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug )) {
336 std::stringstream msgBuffer;
338 msgBuffer << "Assigning label <";
339 for (unsigned int l = 0 ; l < this->levels ; l++) {
340 msgBuffer << currentLabel[l] << ",";
342 msgBuffer << "> to " << k << " (" << i << "," << j <<")";
344 XBT_DEBUG("%s", msgBuffer.str().c_str());
346 this->nodes[k]->label.assign(currentLabel.begin(), currentLabel.end());
348 bool remainder = true;
350 unsigned int pos = 0;
352 std::stringstream msgBuffer;
355 if (currentLabel[pos] >= maxLabel[pos]) {
356 currentLabel[pos] = 0;
369 while(remainder && pos < this->levels);
376 int AsClusterFatTree::getLevelPosition(const unsigned int level) {
377 if (level > this->levels) {
378 // Well, that should never happen. Maybe should we throw instead.
381 int tempPosition = 0;
383 for (unsigned int i = 0 ; i < level ; i++) {
384 tempPosition += this->nodesByLevel[i];
389 void AsClusterFatTree::addProcessingNode(int id) {
390 using std::make_pair;
391 static int position = 0;
392 FatTreeNode* newNode;
393 newNode = new FatTreeNode(this->cluster, id, 0, position++);
394 newNode->parents.resize(this->upperLevelNodesNumber[0] *
395 this->lowerLevelPortsNumber[0]);
396 newNode->label.resize(this->levels);
397 this->computeNodes.insert(make_pair(id,newNode));
398 this->nodes.push_back(newNode);
401 void AsClusterFatTree::addLink(FatTreeNode *parent, unsigned int parentPort,
402 FatTreeNode *child, unsigned int childPort) {
403 FatTreeLink *newLink;
404 newLink = new FatTreeLink(this->cluster, child, parent);
405 XBT_DEBUG("Creating a link between the parent (%d,%d,%u)"
406 " and the child (%d,%d,%u)", parent->level, parent->position,
407 parentPort, child->level, child->position, childPort);
408 parent->children[parentPort] = newLink;
409 child->parents[childPort] = newLink;
411 this->links.push_back(newLink);
417 void AsClusterFatTree::parse_specific_arguments(sg_platf_cluster_cbarg_t
419 std::vector<std::string> parameters;
420 std::vector<std::string> tmp;
421 boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
424 // TODO : we have to check for zeros and negative numbers, or it might crash
425 if (parameters.size() != 4){
426 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
427 ", see the documentation for more informations");
430 // The first parts of topo_parameters should be the levels number
431 this->levels = xbt_str_parse_int(parameters[0].c_str(), "First parameter is not the amount of levels: %s");
433 // Then, a l-sized vector standing for the childs number by level
434 boost::split(tmp, parameters[1], boost::is_any_of(","));
435 if(tmp.size() != this->levels) {
436 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
437 ", see the documentation for more informations");
439 for(size_t i = 0 ; i < tmp.size() ; i++){
440 this->lowerLevelNodesNumber.push_back(xbt_str_parse_int(tmp[i].c_str(), "Invalid lower level node number: %s"));
443 // Then, a l-sized vector standing for the parents number by level
444 boost::split(tmp, parameters[2], boost::is_any_of(","));
445 if(tmp.size() != this->levels) {
446 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
447 ", see the documentation for more informations");
449 for(size_t i = 0 ; i < tmp.size() ; i++){
450 this->upperLevelNodesNumber.push_back(xbt_str_parse_int(tmp[i].c_str(), "Invalid upper level node number: %s"));
453 // Finally, a l-sized vector standing for the ports number with the lower level
454 boost::split(tmp, parameters[3], boost::is_any_of(","));
455 if(tmp.size() != this->levels) {
456 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
457 ", see the documentation for more informations");
460 for(size_t i = 0 ; i < tmp.size() ; i++){
461 this->lowerLevelPortsNumber.push_back(xbt_str_parse_int(tmp[i].c_str(), "Invalid lower level node number: %s"));
463 this->cluster = cluster;
467 void AsClusterFatTree::generateDotFile(const std::string& filename) const {
469 /* Maybe should we get directly a char*, as open takes strings only beginning
472 file.open(filename.c_str(), std::ios::out | std::ios::trunc);
475 file << "graph AsClusterFatTree {\n";
476 for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
477 file << this->nodes[i]->id;
478 if(this->nodes[i]->id < 0) {
479 file << " [shape=circle];\n";
482 file << " [shape=hexagon];\n";
486 for (unsigned int i = 0 ; i < this->links.size() ; i++ ) {
487 file << this->links[i]->downNode->id
489 << this->links[i]->upNode->id
496 XBT_DEBUG("Unable to open file %s", filename.c_str());
501 FatTreeNode::FatTreeNode(sg_platf_cluster_cbarg_t cluster, int id, int level,
502 int position) : id(id), level(level),
504 s_sg_platf_link_cbarg_t linkTemplate = SG_PLATF_LINK_INITIALIZER;
505 if(cluster->limiter_link) {
506 memset(&linkTemplate, 0, sizeof(linkTemplate));
507 linkTemplate.bandwidth = cluster->limiter_link;
508 linkTemplate.latency = 0;
509 linkTemplate.initiallyOn = 1;
510 linkTemplate.policy = SURF_LINK_SHARED;
511 linkTemplate.id = bprintf("limiter_%d", id);
512 sg_platf_new_link(&linkTemplate);
513 this->limiterLink = Link::byName(linkTemplate.id);
514 free((void*)linkTemplate.id);
516 if(cluster->loopback_bw || cluster->loopback_lat) {
517 memset(&linkTemplate, 0, sizeof(linkTemplate));
518 linkTemplate.bandwidth = cluster->loopback_bw;
519 linkTemplate.latency = cluster->loopback_lat;
520 linkTemplate.initiallyOn = 1;
521 linkTemplate.policy = SURF_LINK_FATPIPE;
522 linkTemplate.id = bprintf("loopback_%d", id);
523 sg_platf_new_link(&linkTemplate);
524 this->loopback = Link::byName(linkTemplate.id);
525 free((void*)linkTemplate.id);
529 FatTreeLink::FatTreeLink(sg_platf_cluster_cbarg_t cluster,
530 FatTreeNode *downNode,
531 FatTreeNode *upNode) : upNode(upNode),
533 static int uniqueId = 0;
534 s_sg_platf_link_cbarg_t linkTemplate = SG_PLATF_LINK_INITIALIZER;
535 memset(&linkTemplate, 0, sizeof(linkTemplate));
536 linkTemplate.bandwidth = cluster->bw;
537 linkTemplate.latency = cluster->lat;
538 linkTemplate.initiallyOn = 1;
539 linkTemplate.policy = cluster->sharing_policy; // sthg to do with that ?
540 linkTemplate.id = bprintf("link_from_%d_to_%d_%d", downNode->id, upNode->id,
542 sg_platf_new_link(&linkTemplate);
545 if (cluster->sharing_policy == SURF_LINK_FULLDUPLEX) {
546 tmpID = std::string(linkTemplate.id) + "_UP";
547 link = Link::byName(tmpID.c_str());
548 this->upLink = link; // check link?
549 tmpID = std::string(linkTemplate.id) + "_DOWN";
550 link = Link::byName(tmpID.c_str());
551 this->downLink = link; // check link ?
554 link = Link::byName(linkTemplate.id);
556 this->downLink = link;
559 free((void*)linkTemplate.id);