Logo AND Algorithmique Numérique Distribuée

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