Logo AND Algorithmique Numérique Distribuée

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