Logo AND Algorithmique Numérique Distribuée

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