1 #include "surf_routing_cluster_fat_tree.hpp"
4 #include <boost/algorithm/string/split.hpp>
5 #include <boost/algorithm/string/classification.hpp>
11 AsClusterFatTree::AsClusterFatTree() : levels(0) {}
13 AsClusterFatTree::~AsClusterFatTree() {
14 for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
15 delete this->nodes[i];
19 bool AsClusterFatTree::isInSubTree(FatTreeNode *root, FatTreeNode *node) {
20 for (unsigned int i = 0 ; i < node->level ; i++) {
21 if(root->label[i] != node->label[i]) {
26 for (unsigned int i = root->level + 1 ; i < this->levels ; i++) {
27 if(root->label[i] != node->label[i]) {
34 void AsClusterFatTree::getRouteAndLatency(RoutingEdgePtr src,
36 sg_platf_route_cbarg_t into,
38 FatTreeNode *source, *destination, *currentNode;
39 std::vector<NetworkLink*> route;
40 source = this->nodes.find(src->getId())->second;
41 destination = this->nodes.find(dst->getId())->second;
43 int d = 0, k = 0; // as in d-mod-k
48 while (!isInSubTree(currentNode, destination)) {
49 d = destination->position;
51 for (unsigned int i = 0 ; i < currentNode->level ; i++) {
52 d /= this->upperLevelNodesNumber[i];
54 k = this->upperLevelNodesNumber[currentNode->level] *
55 this->lowerLevelNodesNumber[currentNode->level];
57 route.push_back(currentNode->parents[d]->upLink);
59 *latency += currentNode->parents[d]->upLink->getLatency();
61 currentNode = currentNode->parents[d]->upNode;
65 while(currentNode != destination) {
66 for(unsigned int i = 0 ; i < currentNode->children.size() ; i++) {
67 if(i % this->lowerLevelNodesNumber[currentNode->level] ==
68 destination->label[currentNode->level]) {
69 route.push_back(currentNode->children[i]->downLink);
71 *latency += currentNode->children[d]->downLink->getLatency();
73 currentNode = currentNode->children[i]->downNode;
78 for (unsigned int i = 0 ; i < route.size() ; i++) {
79 xbt_dynar_push_as(into->link_list, void*, route[i]);
84 /* This function makes the assumption that parse_specific_arguments() and
85 * addNodes() have already been called
87 void AsClusterFatTree::create_links(sg_platf_cluster_cbarg_t cluster){
88 if(this->levels == 0) {
91 this->generateSwitches();
92 this->generateLabels();
94 // Nodes are totally ordered, by level and then by position, in this->nodes
95 for (unsigned int i = 0 ; i < this->levels ; i++) {
96 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
97 if(i != this->levels - 1) {
98 for(unsigned int l = 0 ; l < this->nodesByLevel[i+1] ; l++) {
99 this->connectNodeToParents(cluster, this->nodes[k]);
107 int AsClusterFatTree::connectNodeToParents(sg_platf_cluster_cbarg_t cluster,
109 FatTreeNode* currentParentNode;
110 int connectionsNumber = 0;
111 const int level = node->level;
112 currentParentNode = this->nodes[this->getLevelPosition(level + 1)];
113 for (unsigned int i = 0 ; i < this->nodesByLevel[level] ; i++ ) {
114 if(this->areRelated(currentParentNode, node)) {
115 for (unsigned int j = 0 ; j < this->lowerLevelPortsNumber[level + 1] ; j++) {
116 this->addLink(cluster, currentParentNode, node->label[level + 1] +
117 j * this->lowerLevelNodesNumber[level + 1], node,
118 currentParentNode->label[level + 1] +
119 j * this->upperLevelNodesNumber[level + 1]);
124 return connectionsNumber;
128 bool AsClusterFatTree::areRelated(FatTreeNode *parent, FatTreeNode *child) {
129 if (parent->level != child->level + 1) {
133 for (unsigned int i = 0 ; i < this->levels; i++) {
134 if (parent->label[i] != child->label[i] && i != parent->level) {
141 void AsClusterFatTree::generateSwitches() {
142 this->nodesByLevel.resize(this->levels, 0);
143 unsigned int nodesRequired = 0;
145 // We take care of the number of nodes by level
146 this->nodesByLevel[0] = 1;
147 for (unsigned int i = 0 ; i < this->levels ; i++) {
148 this->nodesByLevel[0] *= this->lowerLevelNodesNumber[i];
152 if(this->nodesByLevel[0] < this->nodes.size()) {
153 surf_parse_error("There is not enough nodes to fit to the described topology."
154 " Please check your platform description (We need %d nodes, we only got %zu)",
155 this->nodesByLevel[0], this->nodes.size());
159 for (unsigned int i = 0 ; i < this->levels ; i++) {
160 int nodesInThisLevel = 1;
162 for (unsigned int j = 0 ; j <= i ; j++) {
163 nodesInThisLevel *= this->upperLevelNodesNumber[j];
166 for (unsigned int j = i+1 ; j < this->levels ; j++) {
167 nodesInThisLevel *= this->lowerLevelNodesNumber[j];
170 this->nodesByLevel[i+1] = nodesInThisLevel;
171 nodesRequired += nodesInThisLevel;
175 // If we have to many compute nodes, we ditch them
176 if (this->nodesByLevel[0] > this->nodes.size()) {
177 for (unsigned int i = this->nodesByLevel[0] ; i < this->nodes.size() ; i++) {
178 delete this->nodes[i];
182 // We create the switches
184 for (unsigned int i = 0 ; i < this->levels ; i++) {
185 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
186 FatTreeNode* newNode;
187 newNode = new FatTreeNode(--k, i + 1, j);
188 newNode->children.resize(this->lowerLevelNodesNumber[i] *
189 this->lowerLevelPortsNumber[i]);
190 if (i != this->levels - 1) {
191 newNode->parents.resize(this->upperLevelNodesNumber[i + 1]);
193 this->nodes.insert(std::make_pair(k,newNode));
198 void AsClusterFatTree::generateLabels() {
199 // TODO : check if nodesByLevel and nodes are filled
200 for (unsigned int i = 0 ; i < this->levels ; i++) {
201 std::vector<int> maxLabel(this->nodesByLevel[i]);
202 std::vector<int> currentLabel(this->nodesByLevel[i], 0);
204 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
205 maxLabel[j] = j > i ?
206 this->lowerLevelNodesNumber[i] : this->upperLevelNodesNumber[i];
209 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
210 this->nodes[k]->label.assign(currentLabel.begin(), currentLabel.end());
215 int pos = currentLabel.size() - 1;
216 remainder = ++currentLabel[pos] / maxLabel[pos];
217 currentLabel[pos] = currentLabel[pos] % maxLabel[pos];
220 while(remainder != 0);
227 int AsClusterFatTree::getLevelPosition(const unsigned int level) {
228 if (level > this->levels - 1) {
229 // Well, that should never happen. Maybe should we throw instead.
232 int tempPosition = 0;
234 for (unsigned int i = 0 ; i < level ; i++) {
235 tempPosition += this->nodesByLevel[i];
240 void AsClusterFatTree::addComputeNodes(std::vector<int> const& id) {
241 using std::make_pair;
242 FatTreeNode* newNode;
243 for (size_t i = 0 ; i < id.size() ; i++) {
244 newNode = new FatTreeNode(id[i], 0, i);
245 newNode->parents.resize(this->upperLevelNodesNumber[0] * this->lowerLevelPortsNumber[i]);
246 this->nodes.insert(make_pair(id[i],newNode));
250 void AsClusterFatTree::addLink(sg_platf_cluster_cbarg_t cluster,
251 FatTreeNode *parent, unsigned int parentPort,
252 FatTreeNode *child, unsigned int childPort) {
253 FatTreeLink *newLink;
254 newLink = new FatTreeLink(cluster, parent, child);
256 parent->children[parentPort] = newLink;
257 child->parents[childPort] = newLink;
259 this->links.push_back(newLink);
265 void AsClusterFatTree::parse_specific_arguments(sg_platf_cluster_cbarg_t
267 std::vector<string> parameters;
268 std::vector<string> tmp;
269 boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
272 // TODO : we have to check for zeros and negative numbers, or it might crash
273 if (parameters.size() != 4){
274 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
275 ", see the documentation for more informations");
276 // Well, there's no doc, yet
279 // The first parts of topo_parameters should be the levels number
280 this->levels = std::atoi(tmp[0].c_str()); // stoi() only in C++11...
282 // Then, a l-sized vector standing for the childs number by level
283 boost::split(tmp, parameters[1], boost::is_any_of(","));
284 if(tmp.size() != this->levels) {
285 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
286 ", see the documentation for more informations");
288 for(size_t i = 0 ; i < tmp.size() ; i++){
289 this->lowerLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
292 // Then, a l-sized vector standing for the parents number by level
293 boost::split(tmp, parameters[2], boost::is_any_of(","));
294 if(tmp.size() != this->levels) {
295 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
296 ", see the documentation for more informations");
298 for(size_t i = 0 ; i < tmp.size() ; i++){
299 this->upperLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
302 // Finally, a l-sized vector standing for the ports number with the lower level
303 boost::split(tmp, parameters[3], boost::is_any_of(","));
304 if(tmp.size() != this->levels) {
305 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
306 ", see the documentation for more informations");
309 for(size_t i = 0 ; i < tmp.size() ; i++){
310 this->lowerLevelPortsNumber.push_back(std::atoi(tmp[i].c_str()));
315 void AsClusterFatTree::generateDotFile(const string& filename) const {
317 /* Maybe should we get directly a char*, as open takes strings only beginning
320 file.open(filename.c_str(), ios::out | ios::trunc);
323 // That could also be greatly clarified with C++11
324 std::vector<FatTreeLink*>::const_iterator iter;
325 file << "graph AsClusterFatTree {\n";
326 for (iter = this->links.begin() ; iter != this->links.end() ; iter++ ) {
327 file << (*iter)->downNode->id
329 << (*iter)->upNode->id
336 std::cerr << "Unable to open file " << filename << std::endl;
341 FatTreeNode::FatTreeNode(int id, int level, int position) : id(id),
345 FatTreeLink::FatTreeLink(sg_platf_cluster_cbarg_t cluster, FatTreeNode *downNode,
346 FatTreeNode *upNode) : upNode(upNode),
348 static int uniqueId = 0;
349 s_sg_platf_link_cbarg_t linkTemplate;
350 linkTemplate.bandwidth = cluster->bw;
351 linkTemplate.latency = cluster->lat;
352 linkTemplate.state = SURF_RESOURCE_ON;
353 linkTemplate.policy = cluster->sharing_policy; // Maybe should we do sthg with that ?
357 linkTemplate.id = bprintf("link_from_%d_to_%d_%d_UP", downNode->id, upNode->id, uniqueId);
358 sg_platf_new_link(&linkTemplate);
359 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, linkTemplate.id, SURF_LINK_LEVEL);
360 this->upLink = link; // check link?
361 linkTemplate.id = bprintf("link_from_%d_to_%d_%d_DOWN", downNode->id, upNode->id, uniqueId);
362 sg_platf_new_link(&linkTemplate);
363 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, linkTemplate.id, SURF_LINK_LEVEL);
364 this->downLink = link; // check link ?