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]) {
33 void AsClusterFatTree::getRouteAndLatency(RoutingEdgePtr src,
35 sg_platf_route_cbarg_t into,
37 FatTreeNode *source, *destination, *currentNode;
38 std::vector<NetworkLink*> route;
39 source = this->nodes.find(src->getId())->second;
40 destination = this->nodes.find(dst->getId())->second;
42 int d, k; // as in d-mod-k
47 while (!isInSubTree(currentNode, destination)) {
48 d = destination->position;
50 for (unsigned int i = 0 ; i < currentNode->level ; i++) {
51 d /= this->upperLevelNodesNumber[i];
53 k = this->upperLevelNodesNumber[currentNode->level] *
54 this->lowerLevelNodesNumber[currentNode->level];
56 route.push_back(currentNode->parents[d]->upLink);
58 *latency += currentNode->parents[d]->upLink->getLatency();
60 currentNode = currentNode->parents[d]->upNode;
64 while(currentNode != destination) {
65 for(unsigned int i = 0 ; i < currentNode->children.size() ; i++) {
66 if(i % this->lowerLevelNodesNumber[currentNode->level] ==
67 destination->label[currentNode->level]) {
68 route.push_back(currentNode->children[i]->downLink);
70 *latency += currentNode->children[d]->downLink->getLatency();
72 currentNode = currentNode->children[i]->downNode;
77 for (unsigned int i = 0 ; i < route.size() ; i++) {
78 xbt_dynar_push_as(into->link_list, void*, route[i]);
83 /* This function makes the assumption that parse_specific_arguments() and
84 * addNodes() have already been called
86 void AsClusterFatTree::create_links(sg_platf_cluster_cbarg_t cluster){
87 if(this->levels == 0) {
90 this->generateSwitches();
91 this->generateLabels();
93 // Nodes are totally ordered, by level and then by position, in this->nodes
94 for (unsigned int i = 0 ; i < this->levels ; i++) {
95 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
96 if(i != this->levels - 1) {
97 for(unsigned int l = 0 ; l < this->nodesByLevel[i+1] ; l++) {
98 this->connectNodeToParents(cluster, this->nodes[k]);
106 int AsClusterFatTree::connectNodeToParents(sg_platf_cluster_cbarg_t cluster,
108 FatTreeNode* currentParentNode;
109 int connectionsNumber = 0;
110 const int level = node->level;
111 currentParentNode = this->nodes[this->getLevelPosition(level + 1)];
112 for (unsigned int i = 0 ; i < this->nodesByLevel[level] ; i++ ) {
113 if(this->areRelated(currentParentNode, node)) {
114 for (unsigned int j = 0 ; j < this->lowerLevelPortsNumber[level + 1] ; j++) {
115 this->addLink(cluster, currentParentNode, node->label[level + 1] +
116 j * this->lowerLevelNodesNumber[level + 1], node,
117 currentParentNode->label[level + 1] +
118 j * this->upperLevelNodesNumber[level + 1]);
123 return connectionsNumber;
127 bool AsClusterFatTree::areRelated(FatTreeNode *parent, FatTreeNode *child) {
128 if (parent->level != child->level + 1) {
132 for (unsigned int i = 0 ; i < this->levels; i++) {
133 if (parent->label[i] != child->label[i] && i != parent->level) {
140 void AsClusterFatTree::generateSwitches() {
141 this->nodesByLevel.resize(this->levels, 0);
142 unsigned int nodesRequired = 0;
144 // We take care of the number of nodes by level
145 this->nodesByLevel[0] = 1;
146 for (unsigned int i = 0 ; i < this->levels ; i++) {
147 this->nodesByLevel[0] *= this->lowerLevelNodesNumber[i];
151 if(this->nodesByLevel[0] < this->nodes.size()) {
152 surf_parse_error("There is not enough nodes to fit to the described topology."
153 " Please check your platform description (We need %d nodes, we only got %zu)",
154 this->nodesByLevel[0], this->nodes.size());
158 for (unsigned int i = 0 ; i < this->levels ; i++) {
159 int nodesInThisLevel = 1;
161 for (unsigned int j = 0 ; j <= i ; j++) {
162 nodesInThisLevel *= this->upperLevelNodesNumber[j];
165 for (unsigned int j = i+1 ; j < this->levels ; j++) {
166 nodesInThisLevel *= this->lowerLevelNodesNumber[j];
169 this->nodesByLevel[i+1] = nodesInThisLevel;
170 nodesRequired += nodesInThisLevel;
174 // If we have to many compute nodes, we ditch them
175 if (this->nodesByLevel[0] > this->nodes.size()) {
176 for (unsigned int i = this->nodesByLevel[0] ; i < this->nodes.size() ; i++) {
177 delete this->nodes[i];
181 // We create the switches
183 for (unsigned int i = 0 ; i < this->levels ; i++) {
184 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
185 FatTreeNode* newNode;
186 newNode = new FatTreeNode(--k, i + 1, j);
187 newNode->children.resize(this->lowerLevelNodesNumber[i] *
188 this->lowerLevelPortsNumber[i]);
189 if (i != this->levels - 1) {
190 newNode->parents.resize(this->upperLevelNodesNumber[i + 1]);
192 this->nodes.insert(std::make_pair(k,newNode));
197 void AsClusterFatTree::generateLabels() {
198 // TODO : check if nodesByLevel and nodes are filled
199 for (unsigned int i = 0 ; i < this->levels ; i++) {
200 std::vector<int> maxLabel(this->nodesByLevel[i]);
201 std::vector<int> currentLabel(this->nodesByLevel[i], 0);
203 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
204 maxLabel[j] = j > i ?
205 this->lowerLevelNodesNumber[i] : this->upperLevelNodesNumber[i];
208 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
209 this->nodes[k]->label.assign(currentLabel.begin(), currentLabel.end());
214 int pos = currentLabel.size() - 1;
215 remainder = ++currentLabel[pos] / maxLabel[pos];
216 currentLabel[pos] = currentLabel[pos] % maxLabel[pos];
219 while(remainder != 0);
226 int AsClusterFatTree::getLevelPosition(const unsigned int level) {
227 if (level > this->levels - 1) {
228 // Well, that should never happen. Maybe should we throw instead.
231 int tempPosition = 0;
233 for (unsigned int i = 0 ; i < level ; i++) {
234 tempPosition += this->nodesByLevel[i];
239 void AsClusterFatTree::addComputeNodes(std::vector<int> const& id) {
240 using std::make_pair;
241 FatTreeNode* newNode;
242 for (size_t i = 0 ; i < id.size() ; i++) {
243 newNode = new FatTreeNode(id[i], 0, i);
244 newNode->parents.resize(this->upperLevelNodesNumber[0] * this->lowerLevelPortsNumber[i]);
245 this->nodes.insert(make_pair(id[i],newNode));
249 void AsClusterFatTree::addLink(sg_platf_cluster_cbarg_t cluster,
250 FatTreeNode *parent, unsigned int parentPort,
251 FatTreeNode *child, unsigned int childPort) {
252 FatTreeLink *newLink;
253 newLink = new FatTreeLink(cluster, parent, child);
255 parent->children[parentPort] = newLink;
256 child->parents[childPort] = newLink;
258 this->links.push_back(newLink);
264 void AsClusterFatTree::parse_specific_arguments(sg_platf_cluster_cbarg_t
266 std::vector<string> parameters;
267 std::vector<string> tmp;
268 boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
271 // TODO : we have to check for zeros and negative numbers, or it might crash
272 if (parameters.size() != 4){
273 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
274 ", see the documentation for more informations");
275 // Well, there's no doc, yet
278 // The first parts of topo_parameters should be the levels number
279 this->levels = std::atoi(tmp[0].c_str()); // stoi() only in C++11...
281 // Then, a l-sized vector standing for the childs number by level
282 boost::split(tmp, parameters[1], boost::is_any_of(","));
283 if(tmp.size() != this->levels) {
284 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
285 ", see the documentation for more informations");
287 for(size_t i = 0 ; i < tmp.size() ; i++){
288 this->lowerLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
291 // Then, a l-sized vector standing for the parents number by level
292 boost::split(tmp, parameters[2], boost::is_any_of(","));
293 if(tmp.size() != this->levels) {
294 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
295 ", see the documentation for more informations");
297 for(size_t i = 0 ; i < tmp.size() ; i++){
298 this->upperLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
301 // Finally, a l-sized vector standing for the ports number with the lower level
302 boost::split(tmp, parameters[3], boost::is_any_of(","));
303 if(tmp.size() != this->levels) {
304 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
305 ", see the documentation for more informations");
308 for(size_t i = 0 ; i < tmp.size() ; i++){
309 this->lowerLevelPortsNumber.push_back(std::atoi(tmp[i].c_str()));
314 void AsClusterFatTree::generateDotFile(const string& filename) const {
316 /* Maybe should we get directly a char*, as open takes strings only beginning
319 file.open(filename.c_str(), ios::out | ios::trunc);
322 // That could also be greatly clarified with C++11
323 std::vector<FatTreeLink*>::const_iterator iter;
324 file << "graph AsClusterFatTree {\n";
325 for (iter = this->links.begin() ; iter != this->links.end() ; iter++ ) {
326 file << (*iter)->downNode->id
328 << (*iter)->upNode->id
335 std::cerr << "Unable to open file " << filename << std::endl;
340 FatTreeNode::FatTreeNode(int id, int level, int position) : id(id),
344 FatTreeLink::FatTreeLink(sg_platf_cluster_cbarg_t cluster, FatTreeNode *downNode,
345 FatTreeNode *upNode) : upNode(upNode),
347 static int uniqueId = 0;
348 s_sg_platf_link_cbarg_t linkTemplate;
349 linkTemplate.bandwidth = cluster->bw;
350 linkTemplate.latency = cluster->lat;
351 linkTemplate.state = SURF_RESOURCE_ON;
352 linkTemplate.policy = cluster->sharing_policy; // Maybe should we do sthg with that ?
356 linkTemplate.id = bprintf("link_from_%d_to_%d_%d_UP", downNode->id, upNode->id, uniqueId);
357 sg_platf_new_link(&linkTemplate);
358 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, linkTemplate.id, SURF_LINK_LEVEL);
359 this->upLink = link; // check link?
360 linkTemplate.id = bprintf("link_from_%d_to_%d_%d_DOWN", downNode->id, upNode->id, uniqueId);
361 sg_platf_new_link(&linkTemplate);
362 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, linkTemplate.id, SURF_LINK_LEVEL);
363 this->downLink = link; // check link ?