Logo AND Algorithmique Numérique Distribuée

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