Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
99232af44fd981a3ed446990803276c24fd14bdd
[simgrid.git] / src / surf / surf_routing_cluster_fat_tree.cpp
1 #include "surf_routing_cluster_fat_tree.hpp"
2 #include "xbt/lib.h"
3
4 #include <boost/algorithm/string/split.hpp>
5 #include <boost/algorithm/string/classification.hpp>
6 #include <iostream>
7 #include <fstream>
8
9
10
11 AsClusterFatTree::AsClusterFatTree() : levels(0) {}
12
13 AsClusterFatTree::~AsClusterFatTree() {
14   for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
15     delete this->nodes[i];
16   }
17 }
18
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]) {
22       return false;
23     }
24   }
25   
26   for (unsigned int i = root->level + 1 ; i < this->levels ; i++) {
27     if(root->label[i] != node->label[i]) {
28       return false;
29     }
30   }
31   return true;
32 }
33 void AsClusterFatTree::getRouteAndLatency(RoutingEdgePtr src,
34                                           RoutingEdgePtr dst,
35                                           sg_platf_route_cbarg_t into,
36                                           double *latency) {
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;
41
42   int d, k; // as in d-mod-k
43
44   currentNode = source;
45
46   // up part
47   while (!isInSubTree(currentNode, destination)) {
48     d = destination->position;
49
50     for (unsigned int i = 0 ; i < currentNode->level ; i++) {
51       d /= this->upperLevelNodesNumber[i];
52     }
53      k = this->upperLevelNodesNumber[currentNode->level] *
54       this->lowerLevelNodesNumber[currentNode->level];
55      d = d % k;
56      route.push_back(currentNode->parents[d]->upLink);
57      if(latency) {
58        *latency += currentNode->parents[d]->upLink->getLatency();
59      }
60      currentNode = currentNode->parents[d]->upNode;
61   }
62   
63   // Down part
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);
69         if(latency) {
70           *latency += currentNode->children[d]->downLink->getLatency();
71         }
72         currentNode = currentNode->children[i]->downNode;
73       }
74     }
75   }
76   
77   for (unsigned int i = 0 ; i < route.size() ; i++) {
78     xbt_dynar_push_as(into->link_list, void*, route[i]);
79   }
80
81 }
82
83 /* This function makes the assumption that parse_specific_arguments() and
84  * addNodes() have already been called
85  */
86 void AsClusterFatTree::create_links(sg_platf_cluster_cbarg_t cluster){
87   if(this->levels == 0) {
88     return;
89   }
90   this->generateSwitches();
91   this->generateLabels();
92   unsigned int k = 0;
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]);
99         }
100       }
101       k++;
102     }
103   }
104 }
105
106 int AsClusterFatTree::connectNodeToParents(sg_platf_cluster_cbarg_t cluster,
107                                            FatTreeNode *node) {
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]);
119       }
120       connectionsNumber++;
121     }
122   }
123   return connectionsNumber;
124 }
125
126
127 bool AsClusterFatTree::areRelated(FatTreeNode *parent, FatTreeNode *child) {
128   if (parent->level != child->level + 1) {
129     return false;
130   }
131   
132   for (unsigned int i = 0 ; i < this->levels; i++) {
133     if (parent->label[i] != child->label[i] && i != parent->level) {
134       return false;
135     }
136   }
137   return true;
138 }
139
140 void AsClusterFatTree::generateSwitches() {
141   this->nodesByLevel.resize(this->levels, 0);
142   unsigned int nodesRequired = 0;
143
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];
148   }
149
150      
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());
155     return;
156   }
157   
158   for (unsigned int i = 0 ; i < this->levels ; i++) {
159     int nodesInThisLevel = 1;
160       
161     for (unsigned int j = 0 ;  j <= i ; j++) {
162       nodesInThisLevel *= this->upperLevelNodesNumber[j];
163     }
164       
165     for (unsigned int j = i+1 ; j < this->levels ; j++) {
166       nodesInThisLevel *= this->lowerLevelNodesNumber[j];
167     }
168
169     this->nodesByLevel[i+1] = nodesInThisLevel;
170     nodesRequired += nodesInThisLevel;
171   }
172
173
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];
178     }
179   }
180
181   // We create the switches
182   int k = 0;
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]);
191       }
192       this->nodes.insert(std::make_pair(k,newNode));
193     }
194   }
195 }
196
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);
202     unsigned int k = 0;
203     for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
204       maxLabel[j] = j > i ?
205         this->lowerLevelNodesNumber[i] : this->upperLevelNodesNumber[i];
206     }
207     
208     for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
209       this->nodes[k]->label.assign(currentLabel.begin(), currentLabel.end());
210
211       int remainder = 0;
212       
213       do {
214         int pos = currentLabel.size() - 1;
215         remainder = ++currentLabel[pos] / maxLabel[pos];
216         currentLabel[pos] = currentLabel[pos] % maxLabel[pos];
217         --pos;
218       }
219       while(remainder != 0);
220         k++;
221     }
222   }
223 }
224
225
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.
229     return -1;
230   }
231   int tempPosition = 0;
232
233   for (unsigned int i = 0 ; i < level ; i++) {
234     tempPosition += this->nodesByLevel[i];
235   }
236  return tempPosition;
237 }
238
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));
246   }
247 }
248
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);
254
255   parent->children[parentPort] = newLink;
256   child->parents[childPort] = newLink;
257
258   this->links.push_back(newLink);
259
260   
261
262 }
263
264 void AsClusterFatTree::parse_specific_arguments(sg_platf_cluster_cbarg_t 
265                                                 cluster) {
266   std::vector<string> parameters;
267   std::vector<string> tmp;
268   boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
269  
270
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
276   }
277
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...
280   
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"); 
286   }
287   for(size_t i = 0 ; i < tmp.size() ; i++){
288     this->lowerLevelNodesNumber.push_back(std::atoi(tmp[i].c_str())); 
289   }
290   
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"); 
296   }
297   for(size_t i = 0 ; i < tmp.size() ; i++){
298     this->upperLevelNodesNumber.push_back(std::atoi(tmp[i].c_str())); 
299   }
300   
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"); 
306     
307   }
308   for(size_t i = 0 ; i < tmp.size() ; i++){
309     this->lowerLevelPortsNumber.push_back(std::atoi(tmp[i].c_str())); 
310   }
311 }
312
313
314 void AsClusterFatTree::generateDotFile(const string& filename) const {
315   ofstream file;
316   /* Maybe should we get directly a char*, as open takes strings only beginning
317    * with C++11...
318    */
319   file.open(filename.c_str(), ios::out | ios::trunc); 
320   
321   if(file.is_open()) {
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
327              << " -- "
328            << (*iter)->upNode->id
329              << ";\n";
330     }
331     file << "}";
332     file.close();
333   }
334   else {
335     std::cerr << "Unable to open file " << filename << std::endl;
336     return;
337   }
338 }
339
340 FatTreeNode::FatTreeNode(int id, int level, int position) : id(id),
341                                                             level(level),
342                                                             position(position){}
343
344 FatTreeLink::FatTreeLink(sg_platf_cluster_cbarg_t cluster, FatTreeNode *downNode,
345                          FatTreeNode *upNode) : upNode(upNode),
346                                                 downNode(downNode) {
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 ?
353
354
355   NetworkLink* link;
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 ?
364
365   uniqueId++;
366
367 }