Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fat trees may actually be used
[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_fat_tree_cluster_create(void)
12 {
13   return new AsClusterFatTree();
14 }
15
16 AsClusterFatTree::AsClusterFatTree() : levels(0) {}
17
18 AsClusterFatTree::~AsClusterFatTree() {
19   for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
20     delete this->nodes[i];
21   }
22 }
23
24 bool AsClusterFatTree::isInSubTree(FatTreeNode *root, FatTreeNode *node) {
25   for (unsigned int i = 0 ; i < node->level ; i++) {
26     if(root->label[i] != node->label[i]) {
27       return false;
28     }
29   }
30   
31   for (unsigned int i = root->level + 1 ; i < this->levels ; i++) {
32     if(root->label[i] != node->label[i]) {
33       return false;
34     }
35   }
36   return true;
37 }
38
39 void AsClusterFatTree::getRouteAndLatency(RoutingEdgePtr src,
40                                           RoutingEdgePtr dst,
41                                           sg_platf_route_cbarg_t into,
42                                           double *latency) {
43   FatTreeNode *source, *destination, *currentNode;
44   std::vector<NetworkLink*> route;
45   source = this->nodes.find(src->getId())->second;
46   destination = this->nodes.find(dst->getId())->second;
47
48  
49
50   currentNode = source;
51
52   // up part
53   while (!isInSubTree(currentNode, destination)) {
54     int d, k; // as in d-mod-k
55     d = destination->position;
56
57     for (unsigned int i = 0 ; i < currentNode->level ; i++) {
58       d /= this->upperLevelNodesNumber[i];
59     }
60      k = this->upperLevelNodesNumber[currentNode->level] *
61       this->lowerLevelNodesNumber[currentNode->level];
62      d = d % k;
63      route.push_back(currentNode->parents[d]->upLink);
64      if(latency) {
65        *latency += currentNode->parents[d]->upLink->getLatency();
66      }
67      currentNode = currentNode->parents[d]->upNode;
68   }
69   
70   // Down part
71   while(currentNode != destination) {
72     for(unsigned int i = 0 ; i < currentNode->children.size() ; i++) {
73       if(i % this->lowerLevelNodesNumber[currentNode->level] ==
74          destination->label[currentNode->level]) {
75         route.push_back(currentNode->children[i]->downLink);
76         if(latency) {
77           *latency += currentNode->children[i]->downLink->getLatency();
78         }
79         currentNode = currentNode->children[i]->downNode;
80       }
81     }
82   }
83   
84   for (unsigned int i = 0 ; i < route.size() ; i++) {
85     xbt_dynar_push_as(into->link_list, void*, route[i]);
86   }
87
88 }
89
90 /* This function makes the assumption that parse_specific_arguments() and
91  * addNodes() have already been called
92  */
93 void AsClusterFatTree::create_links(sg_platf_cluster_cbarg_t cluster){
94   if(this->levels == 0) {
95     return;
96   }
97   this->generateSwitches();
98   this->generateLabels();
99   unsigned int k = 0;
100   // Nodes are totally ordered, by level and then by position, in this->nodes
101   for (unsigned int i = 0 ; i < this->levels ; i++) {
102     for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
103       if(i != this->levels - 1) {
104         for(unsigned int l = 0 ; l < this->nodesByLevel[i+1] ; l++) {
105           this->connectNodeToParents(cluster, this->nodes[k]);
106         }
107       }
108       k++;
109     }
110   }
111 }
112
113 int AsClusterFatTree::connectNodeToParents(sg_platf_cluster_cbarg_t cluster,
114                                            FatTreeNode *node) {
115   FatTreeNode* currentParentNode;
116   int connectionsNumber = 0;
117   const int level = node->level;
118   currentParentNode = this->nodes[this->getLevelPosition(level + 1)];
119   for (unsigned int i = 0 ; i < this->nodesByLevel[level] ; i++ ) {
120     if(this->areRelated(currentParentNode, node)) {
121       for (unsigned int j = 0 ; j < this->lowerLevelPortsNumber[level + 1] ; j++) {
122       this->addLink(cluster, currentParentNode, node->label[level + 1] +
123                     j * this->lowerLevelNodesNumber[level + 1], node,
124                     currentParentNode->label[level + 1] +
125                     j * this->upperLevelNodesNumber[level + 1]);
126       }
127       connectionsNumber++;
128     }
129   }
130   return connectionsNumber;
131 }
132
133
134 bool AsClusterFatTree::areRelated(FatTreeNode *parent, FatTreeNode *child) {
135   if (parent->level != child->level + 1) {
136     return false;
137   }
138   
139   for (unsigned int i = 0 ; i < this->levels; i++) {
140     if (parent->label[i] != child->label[i] && i != parent->level) {
141       return false;
142     }
143   }
144   return true;
145 }
146
147 void AsClusterFatTree::generateSwitches() {
148   this->nodesByLevel.resize(this->levels, 0);
149   unsigned int nodesRequired = 0;
150
151   // We take care of the number of nodes by level
152   this->nodesByLevel[0] = 1;
153   for (unsigned int i = 0 ; i < this->levels ; i++) {
154     this->nodesByLevel[0] *= this->lowerLevelNodesNumber[i];
155   }
156
157      
158   if(this->nodesByLevel[0] > this->nodes.size()) {
159     surf_parse_error("There is not enough nodes to fit to the described topology."
160                      " Please check your platform description (We need %d nodes, we only got %zu)",
161                      this->nodesByLevel[0], this->nodes.size());
162     return;
163   }
164   
165   for (unsigned int i = 0 ; i < this->levels ; i++) {
166     int nodesInThisLevel = 1;
167       
168     for (unsigned int j = 0 ;  j <= i ; j++) {
169       nodesInThisLevel *= this->upperLevelNodesNumber[j];
170     }
171       
172     for (unsigned int j = i+1 ; j < this->levels ; j++) {
173       nodesInThisLevel *= this->lowerLevelNodesNumber[j];
174     }
175
176     this->nodesByLevel[i+1] = nodesInThisLevel;
177     nodesRequired += nodesInThisLevel;
178   }
179
180
181   // If we have to many compute nodes, we ditch them
182   if (this->nodesByLevel[0] > this->nodes.size()) {
183     for (unsigned int i = this->nodesByLevel[0] ; i < this->nodes.size() ; i++) {
184       delete this->nodes[i];
185     }
186   }
187
188   // We create the switches
189   int k = 0;
190   for (unsigned int i = 0 ; i < this->levels ; i++) {
191     for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
192       FatTreeNode* newNode;
193       newNode = new FatTreeNode(--k, i + 1, j);
194       newNode->children.resize(this->lowerLevelNodesNumber[i] *
195                                this->lowerLevelPortsNumber[i]);
196       if (i != this->levels - 1) {
197         newNode->parents.resize(this->upperLevelNodesNumber[i + 1]);
198       }
199       this->nodes.insert(std::make_pair(k,newNode));
200     }
201   }
202 }
203
204 void AsClusterFatTree::generateLabels() {
205   // TODO : check if nodesByLevel and nodes are filled
206   for (unsigned int i = 0 ; i < this->levels ; i++) {
207     std::vector<int> maxLabel(this->nodesByLevel[i]);
208     std::vector<int> currentLabel(this->nodesByLevel[i], 0);
209     unsigned int k = 0;
210     for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
211       maxLabel[j] = j > i ?
212         this->lowerLevelNodesNumber[i] : this->upperLevelNodesNumber[i];
213     }
214     
215     for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
216       this->nodes[k]->label.assign(currentLabel.begin(), currentLabel.end());
217
218       int remainder = 0;
219       
220       do {
221         int pos = currentLabel.size() - 1;
222         remainder = ++currentLabel[pos] / maxLabel[pos];
223         currentLabel[pos] = currentLabel[pos] % maxLabel[pos];
224         --pos;
225       }
226       while(remainder != 0);
227         k++;
228     }
229   }
230 }
231
232
233 int AsClusterFatTree::getLevelPosition(const unsigned  int level) {
234   if (level > this->levels - 1) {
235     // Well, that should never happen. Maybe should we throw instead.
236     return -1;
237   }
238   int tempPosition = 0;
239
240   for (unsigned int i = 0 ; i < level ; i++) {
241     tempPosition += this->nodesByLevel[i];
242   }
243  return tempPosition;
244 }
245
246 void AsClusterFatTree::addComputeNode(int id) {
247   using std::make_pair;
248   static int position = 0;
249   FatTreeNode* newNode;
250   newNode = new FatTreeNode(id, 0, position);
251   newNode->parents.resize(this->upperLevelNodesNumber[0] * this->lowerLevelPortsNumber[0]);
252   this->nodes.insert(make_pair(id,newNode));
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(parameters[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 }