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 // If we have to many compute nodes, we ditch them
293 // We create the switches
295 for (unsigned int i = 0 ; i < this->levels ; i++) {
296 for (unsigned int j = 0 ; j < this->nodesByLevel[i + 1] ; j++) {
297 FatTreeNode* newNode;
298 newNode = new FatTreeNode(this->cluster, --k, i + 1, j);
299 XBT_DEBUG("We create the switch %d(%d,%d)", newNode->id, newNode->level,
301 newNode->children.resize(this->lowerLevelNodesNumber[i] *
302 this->lowerLevelPortsNumber[i]);
303 if (i != this->levels - 1) {
304 newNode->parents.resize(this->upperLevelNodesNumber[i + 1] *
305 this->lowerLevelPortsNumber[i + 1]);
307 newNode->label.resize(this->levels);
308 this->nodes.push_back(newNode);
313 void AsClusterFatTree::generateLabels() {
314 XBT_DEBUG("Generating labels.");
315 // TODO : check if nodesByLevel and nodes are filled
316 std::vector<int> maxLabel(this->levels);
317 std::vector<int> currentLabel(this->levels);
319 for (unsigned int i = 0 ; i <= this->levels ; i++) {
320 currentLabel.assign(this->levels, 0);
321 for (unsigned int j = 0 ; j < this->levels ; j++) {
322 maxLabel[j] = j + 1 > i ?
323 this->lowerLevelNodesNumber[j] : this->upperLevelNodesNumber[j];
326 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
328 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug )) {
329 std::stringstream msgBuffer;
331 msgBuffer << "Assigning label <";
332 for (unsigned int l = 0 ; l < this->levels ; l++) {
333 msgBuffer << currentLabel[l] << ",";
335 msgBuffer << "> to " << k << " (" << i << "," << j <<")";
337 XBT_DEBUG("%s", msgBuffer.str().c_str());
339 this->nodes[k]->label.assign(currentLabel.begin(), currentLabel.end());
341 bool remainder = true;
343 unsigned int pos = 0;
345 std::stringstream msgBuffer;
348 if (currentLabel[pos] >= maxLabel[pos]) {
349 currentLabel[pos] = 0;
362 while(remainder && pos < this->levels);
369 int AsClusterFatTree::getLevelPosition(const unsigned int level) {
370 if (level > this->levels) {
371 // Well, that should never happen. Maybe should we throw instead.
374 int tempPosition = 0;
376 for (unsigned int i = 0 ; i < level ; i++) {
377 tempPosition += this->nodesByLevel[i];
382 void AsClusterFatTree::addProcessingNode(int id) {
383 using std::make_pair;
384 static int position = 0;
385 FatTreeNode* newNode;
386 newNode = new FatTreeNode(this->cluster, id, 0, position++);
387 newNode->parents.resize(this->upperLevelNodesNumber[0] *
388 this->lowerLevelPortsNumber[0]);
389 newNode->label.resize(this->levels);
390 this->computeNodes.insert(make_pair(id,newNode));
391 this->nodes.push_back(newNode);
394 void AsClusterFatTree::addLink(FatTreeNode *parent, unsigned int parentPort,
395 FatTreeNode *child, unsigned int childPort) {
396 FatTreeLink *newLink;
397 newLink = new FatTreeLink(this->cluster, child, parent);
398 XBT_DEBUG("Creating a link between the parent (%d,%d,%u)"
399 " and the child (%d,%d,%u)", parent->level, parent->position,
400 parentPort, child->level, child->position, childPort);
401 parent->children[parentPort] = newLink;
402 child->parents[childPort] = newLink;
404 this->links.push_back(newLink);
410 void AsClusterFatTree::parse_specific_arguments(sg_platf_cluster_cbarg_t
412 std::vector<string> parameters;
413 std::vector<string> tmp;
414 boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
417 // TODO : we have to check for zeros and negative numbers, or it might crash
418 if (parameters.size() != 4){
419 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
420 ", see the documentation for more informations");
421 // Well, there's no doc, yet
424 // The first parts of topo_parameters should be the levels number
425 this->levels = std::atoi(parameters[0].c_str()); // stoi() only in C++11...
427 // Then, a l-sized vector standing for the childs number by level
428 boost::split(tmp, parameters[1], boost::is_any_of(","));
429 if(tmp.size() != this->levels) {
430 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
431 ", see the documentation for more informations");
433 for(size_t i = 0 ; i < tmp.size() ; i++){
434 this->lowerLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
437 // Then, a l-sized vector standing for the parents number by level
438 boost::split(tmp, parameters[2], boost::is_any_of(","));
439 if(tmp.size() != this->levels) {
440 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
441 ", see the documentation for more informations");
443 for(size_t i = 0 ; i < tmp.size() ; i++){
444 this->upperLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
447 // Finally, a l-sized vector standing for the ports number with the lower level
448 boost::split(tmp, parameters[3], boost::is_any_of(","));
449 if(tmp.size() != this->levels) {
450 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
451 ", see the documentation for more informations");
454 for(size_t i = 0 ; i < tmp.size() ; i++){
455 this->lowerLevelPortsNumber.push_back(std::atoi(tmp[i].c_str()));
457 this->cluster = cluster;
461 void AsClusterFatTree::generateDotFile(const string& filename) const {
463 /* Maybe should we get directly a char*, as open takes strings only beginning
466 file.open(filename.c_str(), ios::out | ios::trunc);
469 file << "graph AsClusterFatTree {\n";
470 for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
471 file << this->nodes[i]->id;
472 if(this->nodes[i]->id < 0) {
473 file << " [shape=circle];\n";
476 file << " [shape=hexagon];\n";
480 for (unsigned int i = 0 ; i < this->links.size() ; i++ ) {
481 file << this->links[i]->downNode->id
483 << this->links[i]->upNode->id
490 XBT_DEBUG("Unable to open file %s", filename.c_str());
495 FatTreeNode::FatTreeNode(sg_platf_cluster_cbarg_t cluster, int id, int level,
496 int position) : id(id), level(level),
498 s_sg_platf_link_cbarg_t linkTemplate;
499 if(cluster->limiter_link) {
500 memset(&linkTemplate, 0, sizeof(linkTemplate));
501 linkTemplate.bandwidth = cluster->limiter_link;
502 linkTemplate.latency = 0;
503 linkTemplate.state = SURF_RESOURCE_ON;
504 linkTemplate.policy = SURF_LINK_SHARED;
505 linkTemplate.id = bprintf("limiter_%d", id);
506 sg_platf_new_link(&linkTemplate);
507 this->limiterLink = (NetworkLink*) xbt_lib_get_or_null(link_lib,
510 free((void*)linkTemplate.id);
512 if(cluster->loopback_bw || cluster->loopback_lat) {
513 memset(&linkTemplate, 0, sizeof(linkTemplate));
514 linkTemplate.bandwidth = cluster->loopback_bw;
515 linkTemplate.latency = cluster->loopback_lat;
516 linkTemplate.state = SURF_RESOURCE_ON;
517 linkTemplate.policy = SURF_LINK_FATPIPE;
518 linkTemplate.id = bprintf("loopback_%d", id);
519 sg_platf_new_link(&linkTemplate);
520 this->loopback = (NetworkLink*) xbt_lib_get_or_null(link_lib,
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;
533 memset(&linkTemplate, 0, sizeof(linkTemplate));
534 linkTemplate.bandwidth = cluster->bw;
535 linkTemplate.latency = cluster->lat;
536 linkTemplate.state = SURF_RESOURCE_ON;
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 = (NetworkLink*) xbt_lib_get_or_null(link_lib, tmpID.c_str(),
547 this->upLink = link; // check link?
548 tmpID = std::string(linkTemplate.id) + "_DOWN";
549 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, tmpID.c_str(),
551 this->downLink = link; // check link ?
554 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, linkTemplate.id,
557 this->downLink = link;
560 free((void*)linkTemplate.id);