Logo AND Algorithmique Numérique Distribuée

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