Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rewrite of the fat trees implementation
[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 void AsClusterFatTree::getRouteAndLatency(RoutingEdgePtr src,
19                                           RoutingEdgePtr dst,
20                                           sg_platf_route_cbarg_t into,
21                                           double *latency) {
22   // TODO
23 }
24
25 /* This function makes the assumption that parse_specific_arguments() and
26  * addNodes() have already been called
27  */
28 void AsClusterFatTree::create_links(sg_platf_cluster_cbarg_t cluster){
29   if(this->levels == 0) {
30     return;
31   }
32   this->generateSwitches();
33   this->generateLabels();
34   unsigned int k = 0;
35   // Nodes are totally ordered, by level and then by position, in this->nodes
36   for (unsigned int i = 0 ; i < this->levels ; i++) {
37     for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
38       if(i != this->levels - 1) {
39         for(unsigned int l = 0 ; l < this->nodesByLevel[i+1] ; l++) {
40           this->connectNodeToParents(cluster, this->nodes[k]);
41         }
42       }
43       k++;
44     }
45   }
46 }
47
48 int AsClusterFatTree::connectNodeToParents(sg_platf_cluster_cbarg_t cluster,
49                                            FatTreeNode *node) {
50   FatTreeNode* currentParentNode;
51   int connectionsNumber = 0;
52   const int level = node->level;
53   currentParentNode = this->nodes[this->getLevelPosition(level + 1)];
54   for (unsigned int i = 0 ; i < this->nodesByLevel[level] ; i++ ) {
55     if(this->areRelated(currentParentNode, node)) {
56       this->addLink(cluster, currentParentNode, node->label[level + 1], node,
57                     currentParentNode->label[level + 1]);
58       connectionsNumber++;
59     }
60   }
61   return connectionsNumber;
62 }
63
64
65 bool AsClusterFatTree::areRelated(FatTreeNode *parent, FatTreeNode *child) {
66   if (parent->level != child->level + 1) {
67     return false;
68   }
69   
70   for (unsigned int i = 0 ; i < this->levels; i++) {
71     if (parent->label[i] != child->label[i] && i != parent->level) {
72       return false;
73     }
74   }
75   return true;
76 }
77
78 void AsClusterFatTree::generateSwitches() {
79   this->nodesByLevel.resize(this->levels, 0);
80   unsigned int nodesRequired = 0;
81
82   // We take care of the number of nodes by level
83   this->nodesByLevel[0] = 1;
84   for (unsigned int i = 0 ; i < this->levels ; i++) {
85     this->nodesByLevel[0] *= this->lowerLevelNodesNumber[i];
86   }
87
88      
89   if(this->nodesByLevel[0] < this->nodes.size()) {
90     surf_parse_error("There is not enough nodes to fit to the described topology."
91                      " Please check your platform description (We need %d nodes, we only got %zu)",
92                      this->nodesByLevel[0], this->nodes.size());
93     return;
94   }
95   
96   for (unsigned int i = 0 ; i < this->levels ; i++) {
97     int nodesInThisLevel = 1;
98       
99     for (unsigned int j = 0 ;  j <= i ; j++) {
100       nodesInThisLevel *= this->upperLevelNodesNumber[j];
101     }
102       
103     for (unsigned int j = i+1 ; j < this->levels ; j++) {
104       nodesInThisLevel *= this->lowerLevelNodesNumber[j];
105     }
106
107     this->nodesByLevel[i+1] = nodesInThisLevel;
108     nodesRequired += nodesInThisLevel;
109   }
110
111
112   // If we have to many compute nodes, we ditch them
113   if (this->nodesByLevel[0] > this->nodes.size()) {
114     for (unsigned int i = this->nodesByLevel[0] ; i < this->nodes.size() ; i++) {
115       delete this->nodes[i];
116     }
117     this->nodes.resize(this->nodesByLevel[0]);
118   }
119
120   // We create the switches
121   int k = 0;
122   for (unsigned int i = 1 ; i < this->levels ; i++) {
123     for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
124       FatTreeNode* newNode;
125       newNode = new FatTreeNode(--k, i, j);
126       newNode->children.resize(this->lowerLevelNodesNumber[i]);
127       newNode->parents.resize(this->upperLevelNodesNumber[i]);
128       this->nodes.push_back(newNode);
129     }
130   }
131 }
132
133 void AsClusterFatTree::generateLabels() {
134   // TODO : check if nodesByLevel and nodes are filled
135   for (unsigned int i = 0 ; i < this->levels ; i++) {
136     std::vector<int> maxLabel(this->nodesByLevel[i]);
137     std::vector<int> currentLabel(this->nodesByLevel[i], 0);
138     unsigned int k = 0;
139     for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
140       maxLabel[j] = j > i ?
141         this->lowerLevelNodesNumber[i] : this->upperLevelNodesNumber[i];
142     }
143     
144     for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
145       this->nodes[k]->label.assign(currentLabel.begin(), currentLabel.end());
146
147       int remainder = 0;
148       
149       do {
150         int pos = currentLabel.size() - 1;
151         remainder = ++currentLabel[pos] / maxLabel[pos];
152         currentLabel[pos] = currentLabel[pos] % maxLabel[pos];
153         --pos;
154       }
155       while(remainder != 0);
156         k++;
157     }
158   }
159 }
160
161
162 int AsClusterFatTree::getLevelPosition(const unsigned  int level) {
163   if (level > this->levels - 1) {
164     // Well, that should never happen. Maybe should we throw instead.
165     return -1;
166   }
167   int tempPosition = 0;
168
169   for (unsigned int i = 0 ; i < level ; i++) {
170     tempPosition += this->nodesByLevel[i];
171   }
172  return tempPosition;
173 }
174
175 void AsClusterFatTree::addComputeNodes(std::vector<int> const& id) {
176   for (size_t  i = 0 ; i < id.size() ; i++) {
177     this->nodes.push_back(new FatTreeNode(id[i]));
178   }
179 }
180
181 void AsClusterFatTree::addLink(sg_platf_cluster_cbarg_t cluster, 
182                                FatTreeNode *parent, unsigned int parentPort,
183                                FatTreeNode *child, unsigned int childPort) {
184   using std::make_pair;
185
186
187   FatTreeLink *newLink;
188   newLink = new FatTreeLink(cluster, parent, child);
189
190   parent->children[parentPort] = newLink;
191   child->parents[childPort] = newLink;
192
193   this->links.insert(make_pair(make_pair(parent->id, child->id), newLink));
194
195   
196
197 }
198
199 void AsClusterFatTree::parse_specific_arguments(sg_platf_cluster_cbarg_t 
200                                                 cluster) {
201   std::vector<string> parameters;
202   std::vector<string> tmp;
203   boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
204  
205
206   // TODO : we have to check for zeros and negative numbers, or it might crash
207   if (parameters.size() != 4){
208     surf_parse_error("Fat trees are defined by the levels number and 3 vectors" 
209                      ", see the documentation for more informations");
210     // Well, there's no doc, yet
211   }
212
213   // The first parts of topo_parameters should be the levels number
214   this->levels = std::atoi(tmp[0].c_str()); // stoi() only in C++11...
215   
216   // Then, a l-sized vector standing for the childs number by level
217   boost::split(tmp, parameters[1], boost::is_any_of(","));
218   if(tmp.size() != this->levels) {
219     surf_parse_error("Fat trees are defined by the levels number and 3 vectors" 
220                      ", see the documentation for more informations"); 
221   }
222   for(size_t i = 0 ; i < tmp.size() ; i++){
223     this->lowerLevelNodesNumber.push_back(std::atoi(tmp[i].c_str())); 
224   }
225   
226   // Then, a l-sized vector standing for the parents number by level
227   boost::split(tmp, parameters[2], boost::is_any_of(","));
228   if(tmp.size() != this->levels) {
229     surf_parse_error("Fat trees are defined by the levels number and 3 vectors" 
230                      ", see the documentation for more informations"); 
231   }
232   for(size_t i = 0 ; i < tmp.size() ; i++){
233     this->upperLevelNodesNumber.push_back(std::atoi(tmp[i].c_str())); 
234   }
235   
236   // Finally, a l-sized vector standing for the ports number with the lower level
237   boost::split(tmp, parameters[3], boost::is_any_of(","));
238   if(tmp.size() != this->levels) {
239     surf_parse_error("Fat trees are defined by the levels number and 3 vectors" 
240                      ", see the documentation for more informations"); 
241     
242   }
243   for(size_t i = 0 ; i < tmp.size() ; i++){
244     this->lowerLevelPortsNumber.push_back(std::atoi(tmp[i].c_str())); 
245   }
246 }
247
248
249 void AsClusterFatTree::generateDotFile(const string& filename) const {
250   ofstream file;
251   /* Maybe should we get directly a char*, as open takes strings only beginning
252    * with C++11...
253    */
254   file.open(filename.c_str(), ios::out | ios::trunc); 
255   
256   if(file.is_open()) {
257     // That could also be greatly clarified with C++11
258     std::map<std::pair<int,int>,FatTreeLink*>::const_iterator iter;
259     file << "graph AsClusterFatTree {\n";
260     for (iter = this->links.begin() ; iter != this->links.end() ; iter++ ) {
261         file << iter->second->source->id
262              << " -- "
263              << iter->second->destination->id
264              << ";\n";
265     }
266     file << "}";
267     file.close();
268   }
269   else {
270     std::cerr << "Unable to open file " << filename << std::endl;
271     return;
272   }
273 }
274
275 FatTreeNode::FatTreeNode(int id, int level, int position) : id(id),
276                                                             level(level),
277                                                             position(position){}
278
279 FatTreeLink::FatTreeLink(sg_platf_cluster_cbarg_t cluster, FatTreeNode *source,
280                          FatTreeNode *destination) : source(source),
281                                                      destination(destination) {
282   static int uniqueId = 0;
283   s_sg_platf_link_cbarg_t linkTemplate;
284   linkTemplate.bandwidth = cluster->bw;
285   linkTemplate.latency = cluster->lat;
286   linkTemplate.state = SURF_RESOURCE_ON;
287   linkTemplate.policy = cluster->sharing_policy; // Maybe should we do sthg with that ?
288
289
290   NetworkLink* link;
291   linkTemplate.id = bprintf("link_from_%d_to_%d_%d_UP", source->id, destination->id, uniqueId);
292   sg_platf_new_link(&linkTemplate);
293   link = (NetworkLink*) xbt_lib_get_or_null(link_lib, linkTemplate.id, SURF_LINK_LEVEL);
294   this->linkUp = link; // check link?
295   linkTemplate.id = bprintf("link_from_%d_to_%d_%d_DOWN", source->id, destination->id, uniqueId);
296   sg_platf_new_link(&linkTemplate);
297   link = (NetworkLink*) xbt_lib_get_or_null(link_lib, linkTemplate.id, SURF_LINK_LEVEL);
298   this->linkDown = link; // check link ?
299
300   uniqueId++;
301
302 }