1 #include "surf_routing_cluster_fat_tree.hpp"
4 #include <boost/algorithm/string/split.hpp>
5 #include <boost/algorithm/string/classification.hpp>
10 AsClusterFatTree::AsClusterFatTree() : levels(0) {}
12 AsClusterFatTree::~AsClusterFatTree() {
13 for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
14 delete this->nodes[i];
18 bool AsClusterFatTree::isInSubTree(FatTreeNode *root, FatTreeNode *node) {
22 void AsClusterFatTree::getRouteAndLatency(RoutingEdgePtr src,
24 sg_platf_route_cbarg_t into,
26 FatTreeNode *source, *destination, *currentNode;
27 std::vector<NetworkLink*> route;
28 source = this->nodes.find(src->getId())->second;
29 destination = this->nodes.find(dst->getId())->second;
31 int d, k; // as in d-mod-k
36 while (!isInSubTree(currentNode, destination)) {
37 d = destination->position;
39 for (unsigned int i = 0 ; i < currentNode->level ; i++) {
40 d /= this->upperLevelNodesNumber[i];
42 k = this->upperLevelNodesNumber[currentNode->level] *
43 this->lowerLevelNodesNumber[currentNode->level];
45 route.push_back(currentNode->parents[d]->upLink);
46 currentNode = currentNode->parents[d]->upNode;
50 while(currentNode != destination) {
51 for(unsigned int i = 0 ; i < currentNode->children.size() ; i++) {
52 if(i % this->lowerLevelNodesNumber[currentNode->level] ==
53 destination->label[currentNode->level]) {
54 route.push_back(currentNode->children[i]->downLink);
55 currentNode = currentNode->children[i]->downNode;
61 /* This function makes the assumption that parse_specific_arguments() and
62 * addNodes() have already been called
64 void AsClusterFatTree::create_links(sg_platf_cluster_cbarg_t cluster){
65 if(this->levels == 0) {
68 this->generateSwitches();
69 this->generateLabels();
71 // Nodes are totally ordered, by level and then by position, in this->nodes
72 for (unsigned int i = 0 ; i < this->levels ; i++) {
73 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
74 if(i != this->levels - 1) {
75 for(unsigned int l = 0 ; l < this->nodesByLevel[i+1] ; l++) {
76 this->connectNodeToParents(cluster, this->nodes[k]);
84 int AsClusterFatTree::connectNodeToParents(sg_platf_cluster_cbarg_t cluster,
86 FatTreeNode* currentParentNode;
87 int connectionsNumber = 0;
88 const int level = node->level;
89 currentParentNode = this->nodes[this->getLevelPosition(level + 1)];
90 for (unsigned int i = 0 ; i < this->nodesByLevel[level] ; i++ ) {
91 if(this->areRelated(currentParentNode, node)) {
92 for (unsigned int j = 0 ; j < this->lowerLevelPortsNumber[level + 1] ; j++) {
93 this->addLink(cluster, currentParentNode, node->label[level + 1] +
94 j * this->lowerLevelNodesNumber[level + 1], node,
95 currentParentNode->label[level + 1] +
96 j * this->upperLevelNodesNumber[level + 1]);
101 return connectionsNumber;
105 bool AsClusterFatTree::areRelated(FatTreeNode *parent, FatTreeNode *child) {
106 if (parent->level != child->level + 1) {
110 for (unsigned int i = 0 ; i < this->levels; i++) {
111 if (parent->label[i] != child->label[i] && i != parent->level) {
118 void AsClusterFatTree::generateSwitches() {
119 this->nodesByLevel.resize(this->levels, 0);
120 unsigned int nodesRequired = 0;
122 // We take care of the number of nodes by level
123 this->nodesByLevel[0] = 1;
124 for (unsigned int i = 0 ; i < this->levels ; i++) {
125 this->nodesByLevel[0] *= this->lowerLevelNodesNumber[i];
129 if(this->nodesByLevel[0] < this->nodes.size()) {
130 surf_parse_error("There is not enough nodes to fit to the described topology."
131 " Please check your platform description (We need %d nodes, we only got %zu)",
132 this->nodesByLevel[0], this->nodes.size());
136 for (unsigned int i = 0 ; i < this->levels ; i++) {
137 int nodesInThisLevel = 1;
139 for (unsigned int j = 0 ; j <= i ; j++) {
140 nodesInThisLevel *= this->upperLevelNodesNumber[j];
143 for (unsigned int j = i+1 ; j < this->levels ; j++) {
144 nodesInThisLevel *= this->lowerLevelNodesNumber[j];
147 this->nodesByLevel[i+1] = nodesInThisLevel;
148 nodesRequired += nodesInThisLevel;
152 // If we have to many compute nodes, we ditch them
153 if (this->nodesByLevel[0] > this->nodes.size()) {
154 for (unsigned int i = this->nodesByLevel[0] ; i < this->nodes.size() ; i++) {
155 delete this->nodes[i];
159 // We create the switches
161 for (unsigned int i = 0 ; i < this->levels ; i++) {
162 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
163 FatTreeNode* newNode;
164 newNode = new FatTreeNode(--k, i + 1, j);
165 newNode->children.resize(this->lowerLevelNodesNumber[i] *
166 this->lowerLevelPortsNumber[i]);
167 if (i != this->levels - 1) {
168 newNode->parents.resize(this->upperLevelNodesNumber[i + 1]);
170 this->nodes.insert(std::make_pair(k,newNode));
175 void AsClusterFatTree::generateLabels() {
176 // TODO : check if nodesByLevel and nodes are filled
177 for (unsigned int i = 0 ; i < this->levels ; i++) {
178 std::vector<int> maxLabel(this->nodesByLevel[i]);
179 std::vector<int> currentLabel(this->nodesByLevel[i], 0);
181 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
182 maxLabel[j] = j > i ?
183 this->lowerLevelNodesNumber[i] : this->upperLevelNodesNumber[i];
186 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
187 this->nodes[k]->label.assign(currentLabel.begin(), currentLabel.end());
192 int pos = currentLabel.size() - 1;
193 remainder = ++currentLabel[pos] / maxLabel[pos];
194 currentLabel[pos] = currentLabel[pos] % maxLabel[pos];
197 while(remainder != 0);
204 int AsClusterFatTree::getLevelPosition(const unsigned int level) {
205 if (level > this->levels - 1) {
206 // Well, that should never happen. Maybe should we throw instead.
209 int tempPosition = 0;
211 for (unsigned int i = 0 ; i < level ; i++) {
212 tempPosition += this->nodesByLevel[i];
217 void AsClusterFatTree::addComputeNodes(std::vector<int> const& id) {
218 using std::make_pair;
219 FatTreeNode* newNode;
220 for (size_t i = 0 ; i < id.size() ; i++) {
221 newNode = new FatTreeNode(id[i], 0, i);
222 newNode->parents.resize(this->upperLevelNodesNumber[0] * this->lowerLevelPortsNumber[i]);
223 this->nodes.insert(make_pair(id[i],newNode));
227 void AsClusterFatTree::addLink(sg_platf_cluster_cbarg_t cluster,
228 FatTreeNode *parent, unsigned int parentPort,
229 FatTreeNode *child, unsigned int childPort) {
230 FatTreeLink *newLink;
231 newLink = new FatTreeLink(cluster, parent, child);
233 parent->children[parentPort] = newLink;
234 child->parents[childPort] = newLink;
236 this->links.push_back(newLink);
242 void AsClusterFatTree::parse_specific_arguments(sg_platf_cluster_cbarg_t
244 std::vector<string> parameters;
245 std::vector<string> tmp;
246 boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
249 // TODO : we have to check for zeros and negative numbers, or it might crash
250 if (parameters.size() != 4){
251 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
252 ", see the documentation for more informations");
253 // Well, there's no doc, yet
256 // The first parts of topo_parameters should be the levels number
257 this->levels = std::atoi(tmp[0].c_str()); // stoi() only in C++11...
259 // Then, a l-sized vector standing for the childs number by level
260 boost::split(tmp, parameters[1], boost::is_any_of(","));
261 if(tmp.size() != this->levels) {
262 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
263 ", see the documentation for more informations");
265 for(size_t i = 0 ; i < tmp.size() ; i++){
266 this->lowerLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
269 // Then, a l-sized vector standing for the parents number by level
270 boost::split(tmp, parameters[2], boost::is_any_of(","));
271 if(tmp.size() != this->levels) {
272 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
273 ", see the documentation for more informations");
275 for(size_t i = 0 ; i < tmp.size() ; i++){
276 this->upperLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
279 // Finally, a l-sized vector standing for the ports number with the lower level
280 boost::split(tmp, parameters[3], boost::is_any_of(","));
281 if(tmp.size() != this->levels) {
282 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
283 ", see the documentation for more informations");
286 for(size_t i = 0 ; i < tmp.size() ; i++){
287 this->lowerLevelPortsNumber.push_back(std::atoi(tmp[i].c_str()));
292 void AsClusterFatTree::generateDotFile(const string& filename) const {
294 /* Maybe should we get directly a char*, as open takes strings only beginning
297 file.open(filename.c_str(), ios::out | ios::trunc);
300 // That could also be greatly clarified with C++11
301 std::vector<FatTreeLink*>::const_iterator iter;
302 file << "graph AsClusterFatTree {\n";
303 for (iter = this->links.begin() ; iter != this->links.end() ; iter++ ) {
304 file << (*iter)->downNode->id
306 << (*iter)->upNode->id
313 std::cerr << "Unable to open file " << filename << std::endl;
318 FatTreeNode::FatTreeNode(int id, int level, int position) : id(id),
322 FatTreeLink::FatTreeLink(sg_platf_cluster_cbarg_t cluster, FatTreeNode *downNode,
323 FatTreeNode *upNode) : upNode(upNode),
325 static int uniqueId = 0;
326 s_sg_platf_link_cbarg_t linkTemplate;
327 linkTemplate.bandwidth = cluster->bw;
328 linkTemplate.latency = cluster->lat;
329 linkTemplate.state = SURF_RESOURCE_ON;
330 linkTemplate.policy = cluster->sharing_policy; // Maybe should we do sthg with that ?
334 linkTemplate.id = bprintf("link_from_%d_to_%d_%d_UP", downNode->id, upNode->id, uniqueId);
335 sg_platf_new_link(&linkTemplate);
336 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, linkTemplate.id, SURF_LINK_LEVEL);
337 this->upLink = link; // check link?
338 linkTemplate.id = bprintf("link_from_%d_to_%d_%d_DOWN", downNode->id, upNode->id, uniqueId);
339 sg_platf_new_link(&linkTemplate);
340 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, linkTemplate.id, SURF_LINK_LEVEL);
341 this->downLink = link; // check link ?