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;
48 while (!isInSubTree(currentNode, destination)) {
49 int d, k; // as in d-mod-k
50 d = destination->position;
52 for (unsigned int i = 0 ; i < currentNode->level ; i++) {
53 d /= this->upperLevelNodesNumber[i];
55 k = this->upperLevelNodesNumber[currentNode->level] *
56 this->lowerLevelNodesNumber[currentNode->level];
58 route.push_back(currentNode->parents[d]->upLink);
60 *latency += currentNode->parents[d]->upLink->getLatency();
62 currentNode = currentNode->parents[d]->upNode;
66 while(currentNode != destination) {
67 for(unsigned int i = 0 ; i < currentNode->children.size() ; i++) {
68 if(i % this->lowerLevelNodesNumber[currentNode->level] ==
69 destination->label[currentNode->level]) {
70 route.push_back(currentNode->children[i]->downLink);
72 *latency += currentNode->children[i]->downLink->getLatency();
74 currentNode = currentNode->children[i]->downNode;
79 for (unsigned int i = 0 ; i < route.size() ; i++) {
80 xbt_dynar_push_as(into->link_list, void*, route[i]);
85 /* This function makes the assumption that parse_specific_arguments() and
86 * addNodes() have already been called
88 void AsClusterFatTree::create_links(sg_platf_cluster_cbarg_t cluster){
89 if(this->levels == 0) {
92 this->generateSwitches();
93 this->generateLabels();
95 // Nodes are totally ordered, by level and then by position, in this->nodes
96 for (unsigned int i = 0 ; i < this->levels ; i++) {
97 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
98 if(i != this->levels - 1) {
99 for(unsigned int l = 0 ; l < this->nodesByLevel[i+1] ; l++) {
100 this->connectNodeToParents(cluster, this->nodes[k]);
108 int AsClusterFatTree::connectNodeToParents(sg_platf_cluster_cbarg_t cluster,
110 FatTreeNode* currentParentNode;
111 int connectionsNumber = 0;
112 const int level = node->level;
113 currentParentNode = this->nodes[this->getLevelPosition(level + 1)];
114 for (unsigned int i = 0 ; i < this->nodesByLevel[level] ; i++ ) {
115 if(this->areRelated(currentParentNode, node)) {
116 for (unsigned int j = 0 ; j < this->lowerLevelPortsNumber[level + 1] ; j++) {
117 this->addLink(cluster, currentParentNode, node->label[level + 1] +
118 j * this->lowerLevelNodesNumber[level + 1], node,
119 currentParentNode->label[level + 1] +
120 j * this->upperLevelNodesNumber[level + 1]);
125 return connectionsNumber;
129 bool AsClusterFatTree::areRelated(FatTreeNode *parent, FatTreeNode *child) {
130 if (parent->level != child->level + 1) {
134 for (unsigned int i = 0 ; i < this->levels; i++) {
135 if (parent->label[i] != child->label[i] && i != parent->level) {
142 void AsClusterFatTree::generateSwitches() {
143 this->nodesByLevel.resize(this->levels, 0);
144 unsigned int nodesRequired = 0;
146 // We take care of the number of nodes by level
147 this->nodesByLevel[0] = 1;
148 for (unsigned int i = 0 ; i < this->levels ; i++) {
149 this->nodesByLevel[0] *= this->lowerLevelNodesNumber[i];
153 if(this->nodesByLevel[0] < this->nodes.size()) {
154 surf_parse_error("There is not enough nodes to fit to the described topology."
155 " Please check your platform description (We need %d nodes, we only got %zu)",
156 this->nodesByLevel[0], this->nodes.size());
160 for (unsigned int i = 0 ; i < this->levels ; i++) {
161 int nodesInThisLevel = 1;
163 for (unsigned int j = 0 ; j <= i ; j++) {
164 nodesInThisLevel *= this->upperLevelNodesNumber[j];
167 for (unsigned int j = i+1 ; j < this->levels ; j++) {
168 nodesInThisLevel *= this->lowerLevelNodesNumber[j];
171 this->nodesByLevel[i+1] = nodesInThisLevel;
172 nodesRequired += nodesInThisLevel;
176 // If we have to many compute nodes, we ditch them
177 if (this->nodesByLevel[0] > this->nodes.size()) {
178 for (unsigned int i = this->nodesByLevel[0] ; i < this->nodes.size() ; i++) {
179 delete this->nodes[i];
183 // We create the switches
185 for (unsigned int i = 0 ; i < this->levels ; i++) {
186 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
187 FatTreeNode* newNode;
188 newNode = new FatTreeNode(--k, i + 1, j);
189 newNode->children.resize(this->lowerLevelNodesNumber[i] *
190 this->lowerLevelPortsNumber[i]);
191 if (i != this->levels - 1) {
192 newNode->parents.resize(this->upperLevelNodesNumber[i + 1]);
194 this->nodes.insert(std::make_pair(k,newNode));
199 void AsClusterFatTree::generateLabels() {
200 // TODO : check if nodesByLevel and nodes are filled
201 for (unsigned int i = 0 ; i < this->levels ; i++) {
202 std::vector<int> maxLabel(this->nodesByLevel[i]);
203 std::vector<int> currentLabel(this->nodesByLevel[i], 0);
205 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
206 maxLabel[j] = j > i ?
207 this->lowerLevelNodesNumber[i] : this->upperLevelNodesNumber[i];
210 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
211 this->nodes[k]->label.assign(currentLabel.begin(), currentLabel.end());
216 int pos = currentLabel.size() - 1;
217 remainder = ++currentLabel[pos] / maxLabel[pos];
218 currentLabel[pos] = currentLabel[pos] % maxLabel[pos];
221 while(remainder != 0);
228 int AsClusterFatTree::getLevelPosition(const unsigned int level) {
229 if (level > this->levels - 1) {
230 // Well, that should never happen. Maybe should we throw instead.
233 int tempPosition = 0;
235 for (unsigned int i = 0 ; i < level ; i++) {
236 tempPosition += this->nodesByLevel[i];
241 void AsClusterFatTree::addComputeNodes(std::vector<int> const& id) {
242 using std::make_pair;
243 FatTreeNode* newNode;
244 for (size_t i = 0 ; i < id.size() ; i++) {
245 newNode = new FatTreeNode(id[i], 0, i);
246 newNode->parents.resize(this->upperLevelNodesNumber[0] * this->lowerLevelPortsNumber[i]);
247 this->nodes.insert(make_pair(id[i],newNode));
251 void AsClusterFatTree::addLink(sg_platf_cluster_cbarg_t cluster,
252 FatTreeNode *parent, unsigned int parentPort,
253 FatTreeNode *child, unsigned int childPort) {
254 FatTreeLink *newLink;
255 newLink = new FatTreeLink(cluster, parent, child);
257 parent->children[parentPort] = newLink;
258 child->parents[childPort] = newLink;
260 this->links.push_back(newLink);
266 void AsClusterFatTree::parse_specific_arguments(sg_platf_cluster_cbarg_t
268 std::vector<string> parameters;
269 std::vector<string> tmp;
270 boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
273 // TODO : we have to check for zeros and negative numbers, or it might crash
274 if (parameters.size() != 4){
275 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
276 ", see the documentation for more informations");
277 // Well, there's no doc, yet
280 // The first parts of topo_parameters should be the levels number
281 this->levels = std::atoi(tmp[0].c_str()); // stoi() only in C++11...
283 // Then, a l-sized vector standing for the childs number by level
284 boost::split(tmp, parameters[1], boost::is_any_of(","));
285 if(tmp.size() != this->levels) {
286 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
287 ", see the documentation for more informations");
289 for(size_t i = 0 ; i < tmp.size() ; i++){
290 this->lowerLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
293 // Then, a l-sized vector standing for the parents number by level
294 boost::split(tmp, parameters[2], boost::is_any_of(","));
295 if(tmp.size() != this->levels) {
296 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
297 ", see the documentation for more informations");
299 for(size_t i = 0 ; i < tmp.size() ; i++){
300 this->upperLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
303 // Finally, a l-sized vector standing for the ports number with the lower level
304 boost::split(tmp, parameters[3], boost::is_any_of(","));
305 if(tmp.size() != this->levels) {
306 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
307 ", see the documentation for more informations");
310 for(size_t i = 0 ; i < tmp.size() ; i++){
311 this->lowerLevelPortsNumber.push_back(std::atoi(tmp[i].c_str()));
316 void AsClusterFatTree::generateDotFile(const string& filename) const {
318 /* Maybe should we get directly a char*, as open takes strings only beginning
321 file.open(filename.c_str(), ios::out | ios::trunc);
324 // That could also be greatly clarified with C++11
325 std::vector<FatTreeLink*>::const_iterator iter;
326 file << "graph AsClusterFatTree {\n";
327 for (iter = this->links.begin() ; iter != this->links.end() ; iter++ ) {
328 file << (*iter)->downNode->id
330 << (*iter)->upNode->id
337 std::cerr << "Unable to open file " << filename << std::endl;
342 FatTreeNode::FatTreeNode(int id, int level, int position) : id(id),
346 FatTreeLink::FatTreeLink(sg_platf_cluster_cbarg_t cluster, FatTreeNode *downNode,
347 FatTreeNode *upNode) : upNode(upNode),
349 static int uniqueId = 0;
350 s_sg_platf_link_cbarg_t linkTemplate;
351 linkTemplate.bandwidth = cluster->bw;
352 linkTemplate.latency = cluster->lat;
353 linkTemplate.state = SURF_RESOURCE_ON;
354 linkTemplate.policy = cluster->sharing_policy; // Maybe should we do sthg with that ?
358 linkTemplate.id = bprintf("link_from_%d_to_%d_%d_UP", downNode->id, upNode->id, uniqueId);
359 sg_platf_new_link(&linkTemplate);
360 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, linkTemplate.id, SURF_LINK_LEVEL);
361 this->upLink = link; // check link?
362 linkTemplate.id = bprintf("link_from_%d_to_%d_%d_DOWN", downNode->id, upNode->id, uniqueId);
363 sg_platf_new_link(&linkTemplate);
364 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, linkTemplate.id, SURF_LINK_LEVEL);
365 this->downLink = link; // check link ?