Logo AND Algorithmique Numérique Distribuée

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