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();
93 while (!isInSubTree(currentNode, destination)) {
94 int d, k; // as in d-mod-k
95 d = destination->position;
97 for (unsigned int i = 0 ; i < currentNode->level ; i++) {
98 d /= this->upperLevelNodesNumber[i];
100 k = this->upperLevelNodesNumber[currentNode->level];
102 xbt_dynar_push_as(into->link_list, void*,currentNode->parents[d]->upLink);
105 *latency += currentNode->parents[d]->upLink->getLatency();
108 if (this->p_has_limiter) {
109 xbt_dynar_push_as(into->link_list, void*,currentNode->limiterLink);
111 currentNode = currentNode->parents[d]->upNode;
114 XBT_DEBUG("%d(%u,%u) is in the sub tree of %d(%u,%u).", destination->id,
115 destination->level, destination->position, currentNode->id,
116 currentNode->level, currentNode->position);
119 while(currentNode != destination) {
120 for(unsigned int i = 0 ; i < currentNode->children.size() ; i++) {
121 if(i % this->lowerLevelNodesNumber[currentNode->level - 1] ==
122 destination->label[currentNode->level - 1]) {
123 xbt_dynar_push_as(into->link_list, void*,currentNode->children[i]->downLink);
125 *latency += currentNode->children[i]->downLink->getLatency();
127 currentNode = currentNode->children[i]->downNode;
128 if (this->p_has_limiter) {
129 xbt_dynar_push_as(into->link_list, void*,currentNode->limiterLink);
131 XBT_DEBUG("%d(%u,%u) is accessible through %d(%u,%u)", destination->id,
132 destination->level, destination->position, currentNode->id,
133 currentNode->level, currentNode->position);
139 /* This function makes the assumption that parse_specific_arguments() and
140 * addNodes() have already been called
142 void AsClusterFatTree::create_links(){
143 if(this->levels == 0) {
146 this->generateSwitches();
149 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
150 std::stringstream msgBuffer;
152 msgBuffer << "We are creating a fat tree of " << this->levels << " levels "
153 << "with " << this->nodesByLevel[0] << " processing nodes";
154 for (unsigned int i = 1 ; i <= this->levels ; i++) {
155 msgBuffer << ", " << this->nodesByLevel[i] << " switches at level " << i;
157 XBT_DEBUG("%s", msgBuffer.str().c_str());
159 msgBuffer << "Nodes are : ";
161 for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
162 msgBuffer << this->nodes[i]->id << "(" << this->nodes[i]->level << ","
163 << this->nodes[i]->position << ") ";
165 XBT_DEBUG("%s", msgBuffer.str().c_str());
169 this->generateLabels();
172 // Nodes are totally ordered, by level and then by position, in this->nodes
173 for (unsigned int i = 0 ; i < this->levels ; i++) {
174 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
175 this->connectNodeToParents(this->nodes[k]);
180 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
181 std::stringstream msgBuffer;
182 msgBuffer << "Links are : ";
183 for (unsigned int i = 0 ; i < this->links.size() ; i++) {
184 msgBuffer << "(" << this->links[i]->upNode->id << ","
185 << this->links[i]->downNode->id << ") ";
187 XBT_DEBUG("%s", msgBuffer.str().c_str());
193 int AsClusterFatTree::connectNodeToParents(FatTreeNode *node) {
194 std::vector<FatTreeNode*>::iterator currentParentNode = this->nodes.begin();
195 int connectionsNumber = 0;
196 const int level = node->level;
197 XBT_DEBUG("We are connecting node %d(%u,%u) to his parents.",
198 node->id, node->level, node->position);
199 currentParentNode += this->getLevelPosition(level + 1);
200 for (unsigned int i = 0 ; i < this->nodesByLevel[level + 1] ; i++ ) {
201 if(this->areRelated(*currentParentNode, node)) {
202 XBT_DEBUG("%d(%u,%u) and %d(%u,%u) are related,"
203 " with %u links between them.", node->id,
204 node->level, node->position, (*currentParentNode)->id,
205 (*currentParentNode)->level, (*currentParentNode)->position, this->lowerLevelPortsNumber[level]);
206 for (unsigned int j = 0 ; j < this->lowerLevelPortsNumber[level] ; j++) {
207 this->addLink(*currentParentNode, node->label[level] +
208 j * this->lowerLevelNodesNumber[level], node,
209 (*currentParentNode)->label[level] +
210 j * this->upperLevelNodesNumber[level]);
216 return connectionsNumber;
220 bool AsClusterFatTree::areRelated(FatTreeNode *parent, FatTreeNode *child) {
221 std::stringstream msgBuffer;
223 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
224 msgBuffer << "Are " << child->id << "(" << child->level << ","
225 << child->position << ") <";
227 for (unsigned int i = 0 ; i < this->levels ; i++) {
228 msgBuffer << child->label[i] << ",";
232 msgBuffer << " and " << parent->id << "(" << parent->level
233 << "," << parent->position << ") <";
234 for (unsigned int i = 0 ; i < this->levels ; i++) {
235 msgBuffer << parent->label[i] << ",";
238 msgBuffer << " related ? ";
239 XBT_DEBUG("%s", msgBuffer.str().c_str());
242 if (parent->level != child->level + 1) {
246 for (unsigned int i = 0 ; i < this->levels; i++) {
247 if (parent->label[i] != child->label[i] && i + 1 != parent->level) {
254 void AsClusterFatTree::generateSwitches() {
255 XBT_DEBUG("Generating switches.");
256 this->nodesByLevel.resize(this->levels + 1, 0);
257 unsigned int nodesRequired = 0;
259 // We take care of the number of nodes by level
260 this->nodesByLevel[0] = 1;
261 for (unsigned int i = 0 ; i < this->levels ; i++) {
262 this->nodesByLevel[0] *= this->lowerLevelNodesNumber[i];
266 if(this->nodesByLevel[0] != this->nodes.size()) {
267 surf_parse_error("The number of provided nodes does not fit with the wanted topology."
268 " Please check your platform description (We need %d nodes, we got %zu)",
269 this->nodesByLevel[0], this->nodes.size());
274 for (unsigned int i = 0 ; i < this->levels ; i++) {
275 int nodesInThisLevel = 1;
277 for (unsigned int j = 0 ; j <= i ; j++) {
278 nodesInThisLevel *= this->upperLevelNodesNumber[j];
281 for (unsigned int j = i+1 ; j < this->levels ; j++) {
282 nodesInThisLevel *= this->lowerLevelNodesNumber[j];
285 this->nodesByLevel[i+1] = nodesInThisLevel;
286 nodesRequired += nodesInThisLevel;
290 // We create the switches
292 for (unsigned int i = 0 ; i < this->levels ; i++) {
293 for (unsigned int j = 0 ; j < this->nodesByLevel[i + 1] ; j++) {
294 FatTreeNode* newNode;
295 newNode = new FatTreeNode(this->cluster, --k, i + 1, j);
296 XBT_DEBUG("We create the switch %d(%d,%d)", newNode->id, newNode->level,
298 newNode->children.resize(this->lowerLevelNodesNumber[i] *
299 this->lowerLevelPortsNumber[i]);
300 if (i != this->levels - 1) {
301 newNode->parents.resize(this->upperLevelNodesNumber[i + 1] *
302 this->lowerLevelPortsNumber[i + 1]);
304 newNode->label.resize(this->levels);
305 this->nodes.push_back(newNode);
310 void AsClusterFatTree::generateLabels() {
311 XBT_DEBUG("Generating labels.");
312 // TODO : check if nodesByLevel and nodes are filled
313 std::vector<int> maxLabel(this->levels);
314 std::vector<int> currentLabel(this->levels);
316 for (unsigned int i = 0 ; i <= this->levels ; i++) {
317 currentLabel.assign(this->levels, 0);
318 for (unsigned int j = 0 ; j < this->levels ; j++) {
319 maxLabel[j] = j + 1 > i ?
320 this->lowerLevelNodesNumber[j] : this->upperLevelNodesNumber[j];
323 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
325 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug )) {
326 std::stringstream msgBuffer;
328 msgBuffer << "Assigning label <";
329 for (unsigned int l = 0 ; l < this->levels ; l++) {
330 msgBuffer << currentLabel[l] << ",";
332 msgBuffer << "> to " << k << " (" << i << "," << j <<")";
334 XBT_DEBUG("%s", msgBuffer.str().c_str());
336 this->nodes[k]->label.assign(currentLabel.begin(), currentLabel.end());
338 bool remainder = true;
340 unsigned int pos = 0;
342 std::stringstream msgBuffer;
345 if (currentLabel[pos] >= maxLabel[pos]) {
346 currentLabel[pos] = 0;
359 while(remainder && pos < this->levels);
366 int AsClusterFatTree::getLevelPosition(const unsigned int level) {
367 if (level > this->levels) {
368 // Well, that should never happen. Maybe should we throw instead.
371 int tempPosition = 0;
373 for (unsigned int i = 0 ; i < level ; i++) {
374 tempPosition += this->nodesByLevel[i];
379 void AsClusterFatTree::addProcessingNode(int id) {
380 using std::make_pair;
381 static int position = 0;
382 FatTreeNode* newNode;
383 newNode = new FatTreeNode(this->cluster, id, 0, position++);
384 newNode->parents.resize(this->upperLevelNodesNumber[0] *
385 this->lowerLevelPortsNumber[0]);
386 newNode->label.resize(this->levels);
387 this->computeNodes.insert(make_pair(id,newNode));
388 this->nodes.push_back(newNode);
391 void AsClusterFatTree::addLink(FatTreeNode *parent, unsigned int parentPort,
392 FatTreeNode *child, unsigned int childPort) {
393 FatTreeLink *newLink;
394 newLink = new FatTreeLink(this->cluster, child, parent);
395 XBT_DEBUG("Creating a link between the parent (%d,%d,%u)"
396 " and the child (%d,%d,%u)", parent->level, parent->position,
397 parentPort, child->level, child->position, childPort);
398 parent->children[parentPort] = newLink;
399 child->parents[childPort] = newLink;
401 this->links.push_back(newLink);
407 void AsClusterFatTree::parse_specific_arguments(sg_platf_cluster_cbarg_t
409 std::vector<string> parameters;
410 std::vector<string> tmp;
411 boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
414 // TODO : we have to check for zeros and negative numbers, or it might crash
415 if (parameters.size() != 4){
416 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
417 ", see the documentation for more informations");
420 // The first parts of topo_parameters should be the levels number
421 this->levels = std::atoi(parameters[0].c_str()); // stoi() only in C++11...
423 // Then, a l-sized vector standing for the childs number by level
424 boost::split(tmp, parameters[1], boost::is_any_of(","));
425 if(tmp.size() != this->levels) {
426 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
427 ", see the documentation for more informations");
429 for(size_t i = 0 ; i < tmp.size() ; i++){
430 this->lowerLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
433 // Then, a l-sized vector standing for the parents number by level
434 boost::split(tmp, parameters[2], 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->upperLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
443 // Finally, a l-sized vector standing for the ports number with the lower level
444 boost::split(tmp, parameters[3], 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");
450 for(size_t i = 0 ; i < tmp.size() ; i++){
451 this->lowerLevelPortsNumber.push_back(std::atoi(tmp[i].c_str()));
453 this->cluster = cluster;
457 void AsClusterFatTree::generateDotFile(const string& filename) const {
459 /* Maybe should we get directly a char*, as open takes strings only beginning
462 file.open(filename.c_str(), ios::out | ios::trunc);
465 file << "graph AsClusterFatTree {\n";
466 for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
467 file << this->nodes[i]->id;
468 if(this->nodes[i]->id < 0) {
469 file << " [shape=circle];\n";
472 file << " [shape=hexagon];\n";
476 for (unsigned int i = 0 ; i < this->links.size() ; i++ ) {
477 file << this->links[i]->downNode->id
479 << this->links[i]->upNode->id
486 XBT_DEBUG("Unable to open file %s", filename.c_str());
491 FatTreeNode::FatTreeNode(sg_platf_cluster_cbarg_t cluster, int id, int level,
492 int position) : id(id), level(level),
494 s_sg_platf_link_cbarg_t linkTemplate;
495 if(cluster->limiter_link) {
496 memset(&linkTemplate, 0, sizeof(linkTemplate));
497 linkTemplate.bandwidth = cluster->limiter_link;
498 linkTemplate.latency = 0;
499 linkTemplate.state = SURF_RESOURCE_ON;
500 linkTemplate.policy = SURF_LINK_SHARED;
501 linkTemplate.id = bprintf("limiter_%d", id);
502 sg_platf_new_link(&linkTemplate);
503 this->limiterLink = (NetworkLink*) xbt_lib_get_or_null(link_lib,
506 free((void*)linkTemplate.id);
508 if(cluster->loopback_bw || cluster->loopback_lat) {
509 memset(&linkTemplate, 0, sizeof(linkTemplate));
510 linkTemplate.bandwidth = cluster->loopback_bw;
511 linkTemplate.latency = cluster->loopback_lat;
512 linkTemplate.state = SURF_RESOURCE_ON;
513 linkTemplate.policy = SURF_LINK_FATPIPE;
514 linkTemplate.id = bprintf("loopback_%d", id);
515 sg_platf_new_link(&linkTemplate);
516 this->loopback = (NetworkLink*) xbt_lib_get_or_null(link_lib,
519 free((void*)linkTemplate.id);
523 FatTreeLink::FatTreeLink(sg_platf_cluster_cbarg_t cluster,
524 FatTreeNode *downNode,
525 FatTreeNode *upNode) : upNode(upNode),
527 static int uniqueId = 0;
528 s_sg_platf_link_cbarg_t linkTemplate;
529 memset(&linkTemplate, 0, sizeof(linkTemplate));
530 linkTemplate.bandwidth = cluster->bw;
531 linkTemplate.latency = cluster->lat;
532 linkTemplate.state = SURF_RESOURCE_ON;
533 linkTemplate.policy = cluster->sharing_policy; // sthg to do with that ?
534 linkTemplate.id = bprintf("link_from_%d_to_%d_%d", downNode->id, upNode->id,
536 sg_platf_new_link(&linkTemplate);
539 if (cluster->sharing_policy == SURF_LINK_FULLDUPLEX) {
540 tmpID = std::string(linkTemplate.id) + "_UP";
541 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, tmpID.c_str(),
543 this->upLink = link; // check link?
544 tmpID = std::string(linkTemplate.id) + "_DOWN";
545 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, tmpID.c_str(),
547 this->downLink = link; // check link ?
550 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, linkTemplate.id,
553 this->downLink = link;
556 free((void*)linkTemplate.id);