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];
25 for (unsigned int i = 0 ; i < this->links.size() ; i++) {
26 delete this->links[i];
30 bool AsClusterFatTree::isInSubTree(FatTreeNode *root, FatTreeNode *node) {
31 XBT_DEBUG("Is %d(%u,%u) in the sub tree of %d(%u,%u) ?", node->id,
32 node->level, node->position, root->id, root->level, root->position);
33 if (root->level <= node->level) {
36 for (unsigned int i = 0 ; i < node->level ; i++) {
37 if(root->label[i] != node->label[i]) {
42 for (unsigned int i = root->level ; i < this->levels ; i++) {
43 if(root->label[i] != node->label[i]) {
50 void AsClusterFatTree::getRouteAndLatency(RoutingEdgePtr src,
52 sg_platf_route_cbarg_t into,
54 FatTreeNode *source, *destination, *currentNode;
56 std::map<int, FatTreeNode*>::const_iterator tempIter;
58 if (dst->getRcType() == SURF_NETWORK_ELEMENT_ROUTER || src->getRcType() == SURF_NETWORK_ELEMENT_ROUTER) return;
60 /* Let's find the source and the destination in our internal structure */
61 tempIter = this->computeNodes.find(src->getId());
64 if (tempIter == this->computeNodes.end()) {
65 xbt_die("Could not find the source %s [%d] in the fat tree", src->getName(),
68 source = tempIter->second;
69 tempIter = this->computeNodes.find(dst->getId());
70 if (tempIter == this->computeNodes.end()) {
71 xbt_die("Could not find the destination %s [%d] in the fat tree",
72 dst->getName(), dst->getId());
76 destination = tempIter->second;
78 XBT_VERB("Get route and latency from '%s' [%d] to '%s' [%d] in a fat tree",
79 src->getName(), src->getId(), dst->getName(), dst->getId());
81 /* In case destination is the source, and there is a loopback, let's get
82 through it instead of going up to a switch*/
83 if(source->id == destination->id && this->p_has_loopback) {
84 xbt_dynar_push_as(into->link_list, void*, source->loopback);
86 *latency += source->loopback->getLatency();
94 while (!isInSubTree(currentNode, destination)) {
95 int d, k; // as in d-mod-k
96 d = destination->position;
98 for (unsigned int i = 0 ; i < currentNode->level ; i++) {
99 d /= this->upperLevelNodesNumber[i];
101 k = this->upperLevelNodesNumber[currentNode->level];
103 xbt_dynar_push_as(into->link_list, void*,currentNode->parents[d]->upLink);
106 *latency += currentNode->parents[d]->upLink->getLatency();
109 if (this->p_has_limiter) {
110 xbt_dynar_push_as(into->link_list, void*,currentNode->limiterLink);
112 currentNode = currentNode->parents[d]->upNode;
115 XBT_DEBUG("%d(%u,%u) is in the sub tree of %d(%u,%u).", destination->id,
116 destination->level, destination->position, currentNode->id,
117 currentNode->level, currentNode->position);
120 while(currentNode != destination) {
121 for(unsigned int i = 0 ; i < currentNode->children.size() ; i++) {
122 if(i % this->lowerLevelNodesNumber[currentNode->level - 1] ==
123 destination->label[currentNode->level - 1]) {
124 xbt_dynar_push_as(into->link_list, void*,currentNode->children[i]->downLink);
126 *latency += currentNode->children[i]->downLink->getLatency();
128 currentNode = currentNode->children[i]->downNode;
129 if (this->p_has_limiter) {
130 xbt_dynar_push_as(into->link_list, void*,currentNode->limiterLink);
132 XBT_DEBUG("%d(%u,%u) is accessible through %d(%u,%u)", destination->id,
133 destination->level, destination->position, currentNode->id,
134 currentNode->level, currentNode->position);
140 /* This function makes the assumption that parse_specific_arguments() and
141 * addNodes() have already been called
143 void AsClusterFatTree::create_links(){
144 if(this->levels == 0) {
147 this->generateSwitches();
150 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
151 std::stringstream msgBuffer;
153 msgBuffer << "We are creating a fat tree of " << this->levels << " levels "
154 << "with " << this->nodesByLevel[0] << " processing nodes";
155 for (unsigned int i = 1 ; i <= this->levels ; i++) {
156 msgBuffer << ", " << this->nodesByLevel[i] << " switches at level " << i;
158 XBT_DEBUG("%s", msgBuffer.str().c_str());
160 msgBuffer << "Nodes are : ";
162 for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
163 msgBuffer << this->nodes[i]->id << "(" << this->nodes[i]->level << ","
164 << this->nodes[i]->position << ") ";
166 XBT_DEBUG("%s", msgBuffer.str().c_str());
170 this->generateLabels();
173 // Nodes are totally ordered, by level and then by position, in this->nodes
174 for (unsigned int i = 0 ; i < this->levels ; i++) {
175 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
176 this->connectNodeToParents(this->nodes[k]);
181 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
182 std::stringstream msgBuffer;
183 msgBuffer << "Links are : ";
184 for (unsigned int i = 0 ; i < this->links.size() ; i++) {
185 msgBuffer << "(" << this->links[i]->upNode->id << ","
186 << this->links[i]->downNode->id << ") ";
188 XBT_DEBUG("%s", msgBuffer.str().c_str());
194 int AsClusterFatTree::connectNodeToParents(FatTreeNode *node) {
195 std::vector<FatTreeNode*>::iterator currentParentNode = this->nodes.begin();
196 int connectionsNumber = 0;
197 const int level = node->level;
198 XBT_DEBUG("We are connecting node %d(%u,%u) to his parents.",
199 node->id, node->level, node->position);
200 currentParentNode += this->getLevelPosition(level + 1);
201 for (unsigned int i = 0 ; i < this->nodesByLevel[level + 1] ; i++ ) {
202 if(this->areRelated(*currentParentNode, node)) {
203 XBT_DEBUG("%d(%u,%u) and %d(%u,%u) are related,"
204 " with %u links between them.", node->id,
205 node->level, node->position, (*currentParentNode)->id,
206 (*currentParentNode)->level, (*currentParentNode)->position, this->lowerLevelPortsNumber[level]);
207 for (unsigned int j = 0 ; j < this->lowerLevelPortsNumber[level] ; j++) {
208 this->addLink(*currentParentNode, node->label[level] +
209 j * this->lowerLevelNodesNumber[level], node,
210 (*currentParentNode)->label[level] +
211 j * this->upperLevelNodesNumber[level]);
217 return connectionsNumber;
221 bool AsClusterFatTree::areRelated(FatTreeNode *parent, FatTreeNode *child) {
222 std::stringstream msgBuffer;
224 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
225 msgBuffer << "Are " << child->id << "(" << child->level << ","
226 << child->position << ") <";
228 for (unsigned int i = 0 ; i < this->levels ; i++) {
229 msgBuffer << child->label[i] << ",";
233 msgBuffer << " and " << parent->id << "(" << parent->level
234 << "," << parent->position << ") <";
235 for (unsigned int i = 0 ; i < this->levels ; i++) {
236 msgBuffer << parent->label[i] << ",";
239 msgBuffer << " related ? ";
240 XBT_DEBUG("%s", msgBuffer.str().c_str());
243 if (parent->level != child->level + 1) {
247 for (unsigned int i = 0 ; i < this->levels; i++) {
248 if (parent->label[i] != child->label[i] && i + 1 != parent->level) {
255 void AsClusterFatTree::generateSwitches() {
256 XBT_DEBUG("Generating switches.");
257 this->nodesByLevel.resize(this->levels + 1, 0);
258 unsigned int nodesRequired = 0;
260 // We take care of the number of nodes by level
261 this->nodesByLevel[0] = 1;
262 for (unsigned int i = 0 ; i < this->levels ; i++) {
263 this->nodesByLevel[0] *= this->lowerLevelNodesNumber[i];
267 if(this->nodesByLevel[0] != this->nodes.size()) {
268 surf_parse_error("The number of provided nodes does not fit with the wanted topology."
269 " Please check your platform description (We need %d nodes, we got %zu)",
270 this->nodesByLevel[0], this->nodes.size());
275 for (unsigned int i = 0 ; i < this->levels ; i++) {
276 int nodesInThisLevel = 1;
278 for (unsigned int j = 0 ; j <= i ; j++) {
279 nodesInThisLevel *= this->upperLevelNodesNumber[j];
282 for (unsigned int j = i+1 ; j < this->levels ; j++) {
283 nodesInThisLevel *= this->lowerLevelNodesNumber[j];
286 this->nodesByLevel[i+1] = nodesInThisLevel;
287 nodesRequired += nodesInThisLevel;
291 // We create the switches
293 for (unsigned int i = 0 ; i < this->levels ; i++) {
294 for (unsigned int j = 0 ; j < this->nodesByLevel[i + 1] ; j++) {
295 FatTreeNode* newNode;
296 newNode = new FatTreeNode(this->cluster, --k, i + 1, j);
297 XBT_DEBUG("We create the switch %d(%d,%d)", newNode->id, newNode->level,
299 newNode->children.resize(this->lowerLevelNodesNumber[i] *
300 this->lowerLevelPortsNumber[i]);
301 if (i != this->levels - 1) {
302 newNode->parents.resize(this->upperLevelNodesNumber[i + 1] *
303 this->lowerLevelPortsNumber[i + 1]);
305 newNode->label.resize(this->levels);
306 this->nodes.push_back(newNode);
311 void AsClusterFatTree::generateLabels() {
312 XBT_DEBUG("Generating labels.");
313 // TODO : check if nodesByLevel and nodes are filled
314 std::vector<int> maxLabel(this->levels);
315 std::vector<int> currentLabel(this->levels);
317 for (unsigned int i = 0 ; i <= this->levels ; i++) {
318 currentLabel.assign(this->levels, 0);
319 for (unsigned int j = 0 ; j < this->levels ; j++) {
320 maxLabel[j] = j + 1 > i ?
321 this->lowerLevelNodesNumber[j] : this->upperLevelNodesNumber[j];
324 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
326 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug )) {
327 std::stringstream msgBuffer;
329 msgBuffer << "Assigning label <";
330 for (unsigned int l = 0 ; l < this->levels ; l++) {
331 msgBuffer << currentLabel[l] << ",";
333 msgBuffer << "> to " << k << " (" << i << "," << j <<")";
335 XBT_DEBUG("%s", msgBuffer.str().c_str());
337 this->nodes[k]->label.assign(currentLabel.begin(), currentLabel.end());
339 bool remainder = true;
341 unsigned int pos = 0;
343 std::stringstream msgBuffer;
346 if (currentLabel[pos] >= maxLabel[pos]) {
347 currentLabel[pos] = 0;
360 while(remainder && pos < this->levels);
367 int AsClusterFatTree::getLevelPosition(const unsigned int level) {
368 if (level > this->levels) {
369 // Well, that should never happen. Maybe should we throw instead.
372 int tempPosition = 0;
374 for (unsigned int i = 0 ; i < level ; i++) {
375 tempPosition += this->nodesByLevel[i];
380 void AsClusterFatTree::addProcessingNode(int id) {
381 using std::make_pair;
382 static int position = 0;
383 FatTreeNode* newNode;
384 newNode = new FatTreeNode(this->cluster, id, 0, position++);
385 newNode->parents.resize(this->upperLevelNodesNumber[0] *
386 this->lowerLevelPortsNumber[0]);
387 newNode->label.resize(this->levels);
388 this->computeNodes.insert(make_pair(id,newNode));
389 this->nodes.push_back(newNode);
392 void AsClusterFatTree::addLink(FatTreeNode *parent, unsigned int parentPort,
393 FatTreeNode *child, unsigned int childPort) {
394 FatTreeLink *newLink;
395 newLink = new FatTreeLink(this->cluster, child, parent);
396 XBT_DEBUG("Creating a link between the parent (%d,%d,%u)"
397 " and the child (%d,%d,%u)", parent->level, parent->position,
398 parentPort, child->level, child->position, childPort);
399 parent->children[parentPort] = newLink;
400 child->parents[childPort] = newLink;
402 this->links.push_back(newLink);
408 void AsClusterFatTree::parse_specific_arguments(sg_platf_cluster_cbarg_t
410 std::vector<string> parameters;
411 std::vector<string> tmp;
412 boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
415 // TODO : we have to check for zeros and negative numbers, or it might crash
416 if (parameters.size() != 4){
417 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
418 ", see the documentation for more informations");
421 // The first parts of topo_parameters should be the levels number
422 this->levels = std::atoi(parameters[0].c_str()); // stoi() only in C++11...
424 // Then, a l-sized vector standing for the childs number by level
425 boost::split(tmp, parameters[1], 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->lowerLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
434 // Then, a l-sized vector standing for the parents number by level
435 boost::split(tmp, parameters[2], 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");
440 for(size_t i = 0 ; i < tmp.size() ; i++){
441 this->upperLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
444 // Finally, a l-sized vector standing for the ports number with the lower level
445 boost::split(tmp, parameters[3], boost::is_any_of(","));
446 if(tmp.size() != this->levels) {
447 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
448 ", see the documentation for more informations");
451 for(size_t i = 0 ; i < tmp.size() ; i++){
452 this->lowerLevelPortsNumber.push_back(std::atoi(tmp[i].c_str()));
454 this->cluster = cluster;
458 void AsClusterFatTree::generateDotFile(const string& filename) const {
460 /* Maybe should we get directly a char*, as open takes strings only beginning
463 file.open(filename.c_str(), ios::out | ios::trunc);
466 file << "graph AsClusterFatTree {\n";
467 for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
468 file << this->nodes[i]->id;
469 if(this->nodes[i]->id < 0) {
470 file << " [shape=circle];\n";
473 file << " [shape=hexagon];\n";
477 for (unsigned int i = 0 ; i < this->links.size() ; i++ ) {
478 file << this->links[i]->downNode->id
480 << this->links[i]->upNode->id
487 XBT_DEBUG("Unable to open file %s", filename.c_str());
492 FatTreeNode::FatTreeNode(sg_platf_cluster_cbarg_t cluster, int id, int level,
493 int position) : id(id), level(level),
495 s_sg_platf_link_cbarg_t linkTemplate;
496 if(cluster->limiter_link) {
497 memset(&linkTemplate, 0, sizeof(linkTemplate));
498 linkTemplate.bandwidth = cluster->limiter_link;
499 linkTemplate.latency = 0;
500 linkTemplate.state = SURF_RESOURCE_ON;
501 linkTemplate.policy = SURF_LINK_SHARED;
502 linkTemplate.id = bprintf("limiter_%d", id);
503 sg_platf_new_link(&linkTemplate);
504 this->limiterLink = (NetworkLink*) xbt_lib_get_or_null(link_lib,
507 free((void*)linkTemplate.id);
509 if(cluster->loopback_bw || cluster->loopback_lat) {
510 memset(&linkTemplate, 0, sizeof(linkTemplate));
511 linkTemplate.bandwidth = cluster->loopback_bw;
512 linkTemplate.latency = cluster->loopback_lat;
513 linkTemplate.state = SURF_RESOURCE_ON;
514 linkTemplate.policy = SURF_LINK_FATPIPE;
515 linkTemplate.id = bprintf("loopback_%d", id);
516 sg_platf_new_link(&linkTemplate);
517 this->loopback = (NetworkLink*) xbt_lib_get_or_null(link_lib,
520 free((void*)linkTemplate.id);
524 FatTreeLink::FatTreeLink(sg_platf_cluster_cbarg_t cluster,
525 FatTreeNode *downNode,
526 FatTreeNode *upNode) : upNode(upNode),
528 static int uniqueId = 0;
529 s_sg_platf_link_cbarg_t linkTemplate;
530 memset(&linkTemplate, 0, sizeof(linkTemplate));
531 linkTemplate.bandwidth = cluster->bw;
532 linkTemplate.latency = cluster->lat;
533 linkTemplate.state = SURF_RESOURCE_ON;
534 linkTemplate.policy = cluster->sharing_policy; // sthg to do with that ?
535 linkTemplate.id = bprintf("link_from_%d_to_%d_%d", downNode->id, upNode->id,
537 sg_platf_new_link(&linkTemplate);
540 if (cluster->sharing_policy == SURF_LINK_FULLDUPLEX) {
541 tmpID = std::string(linkTemplate.id) + "_UP";
542 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, tmpID.c_str(),
544 this->upLink = link; // check link?
545 tmpID = std::string(linkTemplate.id) + "_DOWN";
546 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, tmpID.c_str(),
548 this->downLink = link; // check link ?
551 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, linkTemplate.id,
554 this->downLink = link;
557 free((void*)linkTemplate.id);