Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add some routing for the fat trees
[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 AsClusterFatTree::AsClusterFatTree() : levels(0) {}
11
12 AsClusterFatTree::~AsClusterFatTree() {
13   for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
14     delete this->nodes[i];
15   }
16 }
17
18 bool AsClusterFatTree::isInSubTree(FatTreeNode *root, FatTreeNode *node) {
19   // stub
20   return false;
21 }
22 void AsClusterFatTree::getRouteAndLatency(RoutingEdgePtr src,
23                                           RoutingEdgePtr dst,
24                                           sg_platf_route_cbarg_t into,
25                                           double *latency) {
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;
30
31   int d, k; // as in d-mod-k
32
33   currentNode = source;
34
35   // up part
36   while (!isInSubTree(currentNode, destination)) {
37     d = destination->position;
38
39     for (unsigned int i = 0 ; i < currentNode->level ; i++) {
40       d /= this->upperLevelNodesNumber[i];
41     }
42      k = this->upperLevelNodesNumber[currentNode->level] *
43       this->lowerLevelNodesNumber[currentNode->level];
44      d = d % k;
45      route.push_back(currentNode->parents[d]->upLink);
46      currentNode = currentNode->parents[d]->upNode;
47   }
48   
49   // Down part
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;
56       }
57     }
58   }
59 }
60
61 /* This function makes the assumption that parse_specific_arguments() and
62  * addNodes() have already been called
63  */
64 void AsClusterFatTree::create_links(sg_platf_cluster_cbarg_t cluster){
65   if(this->levels == 0) {
66     return;
67   }
68   this->generateSwitches();
69   this->generateLabels();
70   unsigned int k = 0;
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]);
77         }
78       }
79       k++;
80     }
81   }
82 }
83
84 int AsClusterFatTree::connectNodeToParents(sg_platf_cluster_cbarg_t cluster,
85                                            FatTreeNode *node) {
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]);
97       }
98       connectionsNumber++;
99     }
100   }
101   return connectionsNumber;
102 }
103
104
105 bool AsClusterFatTree::areRelated(FatTreeNode *parent, FatTreeNode *child) {
106   if (parent->level != child->level + 1) {
107     return false;
108   }
109   
110   for (unsigned int i = 0 ; i < this->levels; i++) {
111     if (parent->label[i] != child->label[i] && i != parent->level) {
112       return false;
113     }
114   }
115   return true;
116 }
117
118 void AsClusterFatTree::generateSwitches() {
119   this->nodesByLevel.resize(this->levels, 0);
120   unsigned int nodesRequired = 0;
121
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];
126   }
127
128      
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());
133     return;
134   }
135   
136   for (unsigned int i = 0 ; i < this->levels ; i++) {
137     int nodesInThisLevel = 1;
138       
139     for (unsigned int j = 0 ;  j <= i ; j++) {
140       nodesInThisLevel *= this->upperLevelNodesNumber[j];
141     }
142       
143     for (unsigned int j = i+1 ; j < this->levels ; j++) {
144       nodesInThisLevel *= this->lowerLevelNodesNumber[j];
145     }
146
147     this->nodesByLevel[i+1] = nodesInThisLevel;
148     nodesRequired += nodesInThisLevel;
149   }
150
151
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];
156     }
157   }
158
159   // We create the switches
160   int k = 0;
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]);
169       }
170       this->nodes.insert(std::make_pair(k,newNode));
171     }
172   }
173 }
174
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);
180     unsigned int k = 0;
181     for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
182       maxLabel[j] = j > i ?
183         this->lowerLevelNodesNumber[i] : this->upperLevelNodesNumber[i];
184     }
185     
186     for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
187       this->nodes[k]->label.assign(currentLabel.begin(), currentLabel.end());
188
189       int remainder = 0;
190       
191       do {
192         int pos = currentLabel.size() - 1;
193         remainder = ++currentLabel[pos] / maxLabel[pos];
194         currentLabel[pos] = currentLabel[pos] % maxLabel[pos];
195         --pos;
196       }
197       while(remainder != 0);
198         k++;
199     }
200   }
201 }
202
203
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.
207     return -1;
208   }
209   int tempPosition = 0;
210
211   for (unsigned int i = 0 ; i < level ; i++) {
212     tempPosition += this->nodesByLevel[i];
213   }
214  return tempPosition;
215 }
216
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));
224   }
225 }
226
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);
232
233   parent->children[parentPort] = newLink;
234   child->parents[childPort] = newLink;
235
236   this->links.push_back(newLink);
237
238   
239
240 }
241
242 void AsClusterFatTree::parse_specific_arguments(sg_platf_cluster_cbarg_t 
243                                                 cluster) {
244   std::vector<string> parameters;
245   std::vector<string> tmp;
246   boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
247  
248
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
254   }
255
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...
258   
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"); 
264   }
265   for(size_t i = 0 ; i < tmp.size() ; i++){
266     this->lowerLevelNodesNumber.push_back(std::atoi(tmp[i].c_str())); 
267   }
268   
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"); 
274   }
275   for(size_t i = 0 ; i < tmp.size() ; i++){
276     this->upperLevelNodesNumber.push_back(std::atoi(tmp[i].c_str())); 
277   }
278   
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"); 
284     
285   }
286   for(size_t i = 0 ; i < tmp.size() ; i++){
287     this->lowerLevelPortsNumber.push_back(std::atoi(tmp[i].c_str())); 
288   }
289 }
290
291
292 void AsClusterFatTree::generateDotFile(const string& filename) const {
293   ofstream file;
294   /* Maybe should we get directly a char*, as open takes strings only beginning
295    * with C++11...
296    */
297   file.open(filename.c_str(), ios::out | ios::trunc); 
298   
299   if(file.is_open()) {
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
305              << " -- "
306            << (*iter)->upNode->id
307              << ";\n";
308     }
309     file << "}";
310     file.close();
311   }
312   else {
313     std::cerr << "Unable to open file " << filename << std::endl;
314     return;
315   }
316 }
317
318 FatTreeNode::FatTreeNode(int id, int level, int position) : id(id),
319                                                             level(level),
320                                                             position(position){}
321
322 FatTreeLink::FatTreeLink(sg_platf_cluster_cbarg_t cluster, FatTreeNode *downNode,
323                          FatTreeNode *upNode) : upNode(upNode),
324                                                 downNode(downNode) {
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 ?
331
332
333   NetworkLink* link;
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 ?
342
343   uniqueId++;
344
345 }
346