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];
27 bool AsClusterFatTree::isInSubTree(FatTreeNode *root, FatTreeNode *node) {
28 for (unsigned int i = 0 ; i < node->level ; i++) {
29 if(root->label[i] != node->label[i]) {
34 for (unsigned int i = root->level + 1 ; i < this->levels ; i++) {
35 if(root->label[i] != node->label[i]) {
42 void AsClusterFatTree::getRouteAndLatency(RoutingEdgePtr src,
44 sg_platf_route_cbarg_t into,
46 FatTreeNode *source, *destination, *currentNode;
47 std::vector<NetworkLink*> route;
48 source = this->computeNodes.find(src->getId())->second;
49 destination = this->computeNodes.find(dst->getId())->second;
51 XBT_DEBUG("Get route and latency from '%s' [%d] to '%s' [%d] in a fat tree",
52 src->getName(), src->getId(), dst->getName(), dst->getId());
57 while (!isInSubTree(currentNode, destination)) {
58 int d, k; // as in d-mod-k
59 d = destination->position;
61 for (unsigned int i = 0 ; i < currentNode->level ; i++) {
62 d /= this->upperLevelNodesNumber[i];
64 k = this->upperLevelNodesNumber[currentNode->level] *
65 this->lowerLevelNodesNumber[currentNode->level];
67 route.push_back(currentNode->parents[d]->upLink);
69 *latency += currentNode->parents[d]->upLink->getLatency();
71 currentNode = currentNode->parents[d]->upNode;
75 while(currentNode != destination) {
76 for(unsigned int i = 0 ; i < currentNode->children.size() ; i++) {
77 if(i % this->lowerLevelNodesNumber[currentNode->level] ==
78 destination->label[currentNode->level]) {
79 route.push_back(currentNode->children[i]->downLink);
81 *latency += currentNode->children[i]->downLink->getLatency();
83 currentNode = currentNode->children[i]->downNode;
88 for (unsigned int i = 0 ; i < route.size() ; i++) {
89 xbt_dynar_push_as(into->link_list, void*, route[i]);
94 /* This function makes the assumption that parse_specific_arguments() and
95 * addNodes() have already been called
97 void AsClusterFatTree::create_links(sg_platf_cluster_cbarg_t cluster){
98 if(this->levels == 0) {
101 this->generateSwitches();
104 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
105 std::stringstream msgBuffer;
107 msgBuffer << "We are creating a fat tree of " << this->levels << " levels "
108 << "with " << this->nodesByLevel[0] << " processing nodes";
109 for (unsigned int i = 1 ; i <= this->levels ; i++) {
110 msgBuffer << ", " << this->nodesByLevel[i] << " switches at level " << i;
112 XBT_DEBUG(msgBuffer.str().c_str());
114 msgBuffer << "Nodes are : ";
116 for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
117 msgBuffer << this->nodes[i]->id << "(" << this->nodes[i]->level << ","
118 << this->nodes[i]->position << ") ";
120 XBT_DEBUG(msgBuffer.str().c_str());
124 this->generateLabels();
127 // Nodes are totally ordered, by level and then by position, in this->nodes
128 for (unsigned int i = 0 ; i < this->levels ; i++) {
129 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
130 this->connectNodeToParents(cluster, this->nodes[k]);
135 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
136 std::stringstream msgBuffer;
137 msgBuffer << "Links are : ";
138 for (unsigned int i = 0 ; i < this->links.size() ; i++) {
139 msgBuffer << "(" << this->links[i]->upNode->id << ","
140 << this->links[i]->downNode->id << ") ";
142 XBT_DEBUG(msgBuffer.str().c_str());
148 int AsClusterFatTree::connectNodeToParents(sg_platf_cluster_cbarg_t cluster,
150 std::vector<FatTreeNode*>::iterator currentParentNode = this->nodes.begin();
151 int connectionsNumber = 0;
152 const int level = node->level;
153 XBT_DEBUG("We are connecting node %d(%u,%u) to his parents.",
154 node->id, node->level, node->position);
155 currentParentNode += this->getLevelPosition(level + 1);
156 for (unsigned int i = 0 ; i < this->nodesByLevel[level + 1] ; i++ ) {
157 if(this->areRelated(*currentParentNode, node)) {
158 XBT_DEBUG("%d(%u,%u) and %d(%u,%u) are related,"
159 " with %u links between them.", node->id,
160 node->level, node->position, (*currentParentNode)->id,
161 (*currentParentNode)->level, (*currentParentNode)->position, this->lowerLevelPortsNumber[level]);
162 for (unsigned int j = 0 ; j < this->lowerLevelPortsNumber[level] ; j++) {
163 this->addLink(cluster, *currentParentNode, node->label[level + 1] +
164 j * this->lowerLevelNodesNumber[level], node,
165 (*currentParentNode)->label[level + 1] +
166 j * this->upperLevelNodesNumber[level]);
172 return connectionsNumber;
176 bool AsClusterFatTree::areRelated(FatTreeNode *parent, FatTreeNode *child) {
177 std::stringstream msgBuffer;
179 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
180 msgBuffer << "Are " << child->id << "(" << child->level << ","
181 << child->position << ") <";
183 for (unsigned int i = 0 ; i < this->levels ; i++) {
184 msgBuffer << child->label[i] << ",";
188 msgBuffer << " and " << parent->id << "(" << parent->level
189 << "," << parent->position << ") <";
190 for (unsigned int i = 0 ; i < this->levels ; i++) {
191 msgBuffer << parent->label[i] << ",";
194 msgBuffer << " related ? ";
195 XBT_DEBUG(msgBuffer.str().c_str());
198 if (parent->level != child->level + 1) {
202 for (unsigned int i = 1 ; i <= this->levels; i++) {
203 if (parent->label[this->levels - i] != child->label[this->levels - i] && i != parent->level) {
210 void AsClusterFatTree::generateSwitches() {
211 XBT_DEBUG("Generating switches.");
212 this->nodesByLevel.resize(this->levels + 1, 0);
213 unsigned int nodesRequired = 0;
215 // We take care of the number of nodes by level
216 this->nodesByLevel[0] = 1;
217 for (unsigned int i = 0 ; i < this->levels ; i++) {
218 this->nodesByLevel[0] *= this->lowerLevelNodesNumber[i];
222 if(this->nodesByLevel[0] > this->nodes.size()) {
223 surf_parse_error("There is not enough nodes to fit to the described topology."
224 " Please check your platform description (We need %d nodes, we only got %zu)",
225 this->nodesByLevel[0], this->nodes.size());
229 for (unsigned int i = 0 ; i < this->levels ; i++) {
230 int nodesInThisLevel = 1;
232 for (unsigned int j = 0 ; j <= i ; j++) {
233 nodesInThisLevel *= this->upperLevelNodesNumber[j];
236 for (unsigned int j = i+1 ; j < this->levels ; j++) {
237 nodesInThisLevel *= this->lowerLevelNodesNumber[j];
240 this->nodesByLevel[i+1] = nodesInThisLevel;
241 nodesRequired += nodesInThisLevel;
245 // If we have to many compute nodes, we ditch them
246 if (this->nodesByLevel[0] < this->nodes.size()) {
247 for (unsigned int i = this->nodesByLevel[0] ; i < this->nodes.size() ; i++) {
248 this->computeNodes.erase(this->nodes[i]->id);
249 delete this->nodes[i];
251 this->nodes.resize(this->nodesByLevel[0]);
254 // We create the switches
256 for (unsigned int i = 0 ; i < this->levels ; i++) {
257 for (unsigned int j = 0 ; j < this->nodesByLevel[i + 1] ; j++) {
258 FatTreeNode* newNode;
259 newNode = new FatTreeNode(--k, i + 1, j);
260 XBT_DEBUG("We create the switch %d(%d,%d)", newNode->id, newNode->level, newNode->position);
261 newNode->children.resize(this->lowerLevelNodesNumber[i] *
262 this->lowerLevelPortsNumber[i]);
263 if (i != this->levels - 1) {
264 newNode->parents.resize(this->upperLevelNodesNumber[i + 1]);
266 newNode->label.resize(this->levels);
267 this->nodes.push_back(newNode);
272 void AsClusterFatTree::generateLabels() {
273 XBT_DEBUG("Generating labels.");
274 // TODO : check if nodesByLevel and nodes are filled
275 std::vector<int> maxLabel(this->levels);
276 std::vector<int> currentLabel(this->levels);
278 for (unsigned int i = 0 ; i <= this->levels ; i++) {
279 currentLabel.assign(this->levels, 0);
280 for (unsigned int j = 1 ; j <= this->levels ; j++) {
281 maxLabel[maxLabel.size() - j] = j > i ?
282 this->lowerLevelNodesNumber[j - 1] : this->upperLevelNodesNumber[j - 1];
285 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
287 if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug )) {
288 std::stringstream msgBuffer;
290 msgBuffer << "Assigning label <";
291 for (unsigned int l = 0 ; l < this->levels ; l++) {
292 msgBuffer << currentLabel[l] << ",";
294 msgBuffer << "> to " << k << " (" << i << "," << j <<")";
296 XBT_DEBUG(bprintf(msgBuffer.str().c_str()));
298 this->nodes[k]->label.assign(currentLabel.begin(), currentLabel.end());
300 bool remainder = true;
302 int pos = currentLabel.size() - 1;
304 std::stringstream msgBuffer;
307 if (currentLabel[pos] >= maxLabel[pos] && pos > 0) {
308 currentLabel[pos] = 0;
315 pos = currentLabel.size() - 1;
328 int AsClusterFatTree::getLevelPosition(const unsigned int level) {
329 if (level > this->levels) {
330 // Well, that should never happen. Maybe should we throw instead.
333 int tempPosition = 0;
335 for (unsigned int i = 0 ; i < level ; i++) {
336 tempPosition += this->nodesByLevel[i];
341 void AsClusterFatTree::addProcessingNode(int id) {
342 using std::make_pair;
343 static int position = 0;
344 FatTreeNode* newNode;
345 newNode = new FatTreeNode(id, 0, position++);
346 newNode->parents.resize(this->upperLevelNodesNumber[0] * this->lowerLevelPortsNumber[0]);
347 newNode->label.resize(this->levels);
348 this->computeNodes.insert(make_pair(id,newNode));
349 this->nodes.push_back(newNode);
352 void AsClusterFatTree::addLink(sg_platf_cluster_cbarg_t cluster,
353 FatTreeNode *parent, unsigned int parentPort,
354 FatTreeNode *child, unsigned int childPort) {
355 FatTreeLink *newLink;
356 newLink = new FatTreeLink(cluster, parent, child);
357 XBT_DEBUG("Creating a link between the parent (%d,%d,%u)"
358 " and the child (%d,%d,%u)", parent->level, parent->position,
359 parentPort, child->level, child->position, childPort);
360 parent->children[parentPort] = newLink;
361 child->parents[childPort] = newLink;
363 this->links.push_back(newLink);
369 void AsClusterFatTree::parse_specific_arguments(sg_platf_cluster_cbarg_t
371 std::vector<string> parameters;
372 std::vector<string> tmp;
373 boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
376 // TODO : we have to check for zeros and negative numbers, or it might crash
377 if (parameters.size() != 4){
378 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
379 ", see the documentation for more informations");
380 // Well, there's no doc, yet
383 // The first parts of topo_parameters should be the levels number
384 this->levels = std::atoi(parameters[0].c_str()); // stoi() only in C++11...
386 // Then, a l-sized vector standing for the childs number by level
387 boost::split(tmp, parameters[1], boost::is_any_of(","));
388 if(tmp.size() != this->levels) {
389 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
390 ", see the documentation for more informations");
392 for(size_t i = 0 ; i < tmp.size() ; i++){
393 this->lowerLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
396 // Then, a l-sized vector standing for the parents number by level
397 boost::split(tmp, parameters[2], boost::is_any_of(","));
398 if(tmp.size() != this->levels) {
399 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
400 ", see the documentation for more informations");
402 for(size_t i = 0 ; i < tmp.size() ; i++){
403 this->upperLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
406 // Finally, a l-sized vector standing for the ports number with the lower level
407 boost::split(tmp, parameters[3], boost::is_any_of(","));
408 if(tmp.size() != this->levels) {
409 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
410 ", see the documentation for more informations");
413 for(size_t i = 0 ; i < tmp.size() ; i++){
414 this->lowerLevelPortsNumber.push_back(std::atoi(tmp[i].c_str()));
419 void AsClusterFatTree::generateDotFile(const string& filename) const {
421 /* Maybe should we get directly a char*, as open takes strings only beginning
424 file.open(filename.c_str(), ios::out | ios::trunc);
427 // That could also be greatly clarified with C++11
428 std::vector<FatTreeLink*>::const_iterator iter;
429 file << "graph AsClusterFatTree {\n";
430 for (iter = this->links.begin() ; iter != this->links.end() ; iter++ ) {
431 file << (*iter)->downNode->id
433 << (*iter)->upNode->id
440 std::cerr << "Unable to open file " << filename << std::endl;
445 FatTreeNode::FatTreeNode(int id, int level, int position) : id(id),
449 FatTreeLink::FatTreeLink(sg_platf_cluster_cbarg_t cluster, FatTreeNode *downNode,
450 FatTreeNode *upNode) : upNode(upNode),
452 static int uniqueId = 0;
453 s_sg_platf_link_cbarg_t linkTemplate;
454 memset(&linkTemplate, 0, sizeof(linkTemplate));
455 linkTemplate.bandwidth = cluster->bw;
456 linkTemplate.latency = cluster->lat;
457 linkTemplate.state = SURF_RESOURCE_ON;
458 linkTemplate.policy = cluster->sharing_policy; // Maybe should we do sthg with that ?
459 linkTemplate.id = bprintf("link_from_%d_to_%d_%d", downNode->id, upNode->id, uniqueId);
460 sg_platf_new_link(&linkTemplate);
462 if (cluster->sharing_policy == SURF_LINK_FULLDUPLEX) {
464 tmpID = std::string(linkTemplate.id) + "_UP";
465 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, tmpID.c_str(), SURF_LINK_LEVEL);
466 this->upLink = link; // check link?
467 tmpID = std::string(linkTemplate.id) + "_DOWN";
468 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, tmpID.c_str(), SURF_LINK_LEVEL);
469 this->downLink = link; // check link ?
472 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, linkTemplate.id, SURF_LINK_LEVEL);
474 this->downLink = link;