Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
various small cosmetics in the routing
[simgrid.git] / src / kernel / routing / AsClusterFatTree.cpp
1 /* Copyright (c) 2014-2016. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <fstream>
7 #include <sstream>
8
9 #include "src/kernel/routing/AsClusterFatTree.hpp"
10 #include "src/surf/network_interface.hpp"
11
12 #include "xbt/lib.h"
13
14 #include <boost/algorithm/string/split.hpp>
15 #include <boost/algorithm/string/classification.hpp>
16
17 #include "src/surf/xml/platf.hpp" // FIXME: move that back to the parsing area
18
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_fat_tree, surf, "Routing for fat trees");
21
22 namespace simgrid {
23 namespace kernel {
24 namespace routing {
25
26 AsClusterFatTree::AsClusterFatTree(As* father, const char* name) : AsCluster(father, name)
27 {
28   XBT_DEBUG("Creating a new fat tree.");
29 }
30
31 AsClusterFatTree::~AsClusterFatTree() {
32   for (unsigned int i = 0 ; i < this->nodes_.size() ; i++) {
33     delete this->nodes_[i];
34   }
35   for (unsigned int i = 0 ; i < this->links_.size() ; i++) {
36     delete this->links_[i];
37   }
38 }
39
40 bool AsClusterFatTree::isInSubTree(FatTreeNode *root, FatTreeNode *node) {
41   XBT_DEBUG("Is %d(%u,%u) in the sub tree of %d(%u,%u) ?", node->id,
42             node->level, node->position, root->id, root->level, root->position);
43   if (root->level <= node->level) {
44     return false;
45   }
46   for (unsigned int i = 0 ; i < node->level ; i++) {
47     if(root->label[i] != node->label[i]) {
48       return false;
49     }
50   }
51   
52   for (unsigned int i = root->level ; i < this->levels_ ; i++) {
53     if(root->label[i] != node->label[i]) {
54       return false;
55     }
56   }
57   return true;
58 }
59
60 void AsClusterFatTree::getRouteAndLatency(NetCard *src,
61                                           NetCard *dst,
62                                           sg_platf_route_cbarg_t into,
63                                           double *latency) {
64   
65   if (dst->isRouter() || src->isRouter())
66     return;
67
68   /* Let's find the source and the destination in our internal structure */
69   std::map<int, FatTreeNode*>::const_iterator tempIter = this->computeNodes_.find(src->id());
70   xbt_assert(tempIter != this->computeNodes_.end(), "Could not find the source %s [%d] in the fat tree",
71              src->name().c_str(), src->id());
72   FatTreeNode* source = tempIter->second;
73
74   tempIter = this->computeNodes_.find(dst->id());
75   xbt_assert(tempIter != this->computeNodes_.end(), "Could not find the destination %s [%d] in the fat tree",
76              dst->name().c_str(), dst->id());
77   FatTreeNode* destination = tempIter->second;
78
79   XBT_VERB("Get route and latency from '%s' [%d] to '%s' [%d] in a fat tree", src->name().c_str(), src->id(),
80            dst->name().c_str(), dst->id());
81
82   /* In case destination is the source, and there is a loopback, let's use it instead of going up to a switch */
83   if (source->id == destination->id && this->hasLoopback_) {
84     into->link_list->push_back(source->loopback);
85     if (latency)
86       *latency += source->loopback->latency();
87     return;
88   }
89
90   FatTreeNode* currentNode = source;
91
92   // up part
93   while (!isInSubTree(currentNode, destination)) {
94     int d, k; // as in d-mod-k
95     d = destination->position;
96
97     for (unsigned int i = 0 ; i < currentNode->level ; i++) {
98       d /= this->upperLevelNodesNumber_[i];
99     }
100     k = this->upperLevelNodesNumber_[currentNode->level];
101     d = d % k;
102     into->link_list->push_back(currentNode->parents[d]->upLink);
103
104     if(latency) {
105       *latency += currentNode->parents[d]->upLink->latency();
106     }
107
108     if (this->hasLimiter_) {
109       into->link_list->push_back(currentNode->limiterLink);
110     }
111     currentNode = currentNode->parents[d]->upNode;
112   }
113
114   XBT_DEBUG("%d(%u,%u) is in the sub tree of %d(%u,%u).", destination->id,
115             destination->level, destination->position, currentNode->id,
116             currentNode->level, currentNode->position);
117
118   // Down part
119   while (currentNode != destination) {
120     for(unsigned int i = 0 ; i < currentNode->children.size() ; i++) {
121       if(i % this->lowerLevelNodesNumber_[currentNode->level - 1] ==
122          destination->label[currentNode->level - 1]) {
123         into->link_list->push_back(currentNode->children[i]->downLink);
124         if(latency) {
125           *latency += currentNode->children[i]->downLink->latency();
126         }
127         currentNode = currentNode->children[i]->downNode;
128         if (this->hasLimiter_) {
129           into->link_list->push_back(currentNode->limiterLink);
130         }
131         XBT_DEBUG("%d(%u,%u) is accessible through %d(%u,%u)", destination->id,
132                   destination->level, destination->position, currentNode->id,
133                   currentNode->level, currentNode->position);
134       }
135     }
136   }
137 }
138
139 /* This function makes the assumption that parse_specific_arguments() and
140  * addNodes() have already been called
141  */
142 void AsClusterFatTree::seal(){
143   if(this->levels_ == 0) {
144     return;
145   }
146   this->generateSwitches();
147
148
149   if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
150     std::stringstream msgBuffer;
151
152     msgBuffer << "We are creating a fat tree of " << this->levels_ << " levels "
153               << "with " << this->nodesByLevel_[0] << " processing nodes";
154     for (unsigned int i = 1 ; i <= this->levels_ ; i++) {
155       msgBuffer << ", " << this->nodesByLevel_[i] << " switches at level " << i;
156     }
157     XBT_DEBUG("%s", msgBuffer.str().c_str());
158     msgBuffer.str("");
159     msgBuffer << "Nodes are : ";
160
161     for (unsigned int i = 0 ;  i < this->nodes_.size() ; i++) {
162       msgBuffer << this->nodes_[i]->id << "(" << this->nodes_[i]->level << ","
163                 << this->nodes_[i]->position << ") ";
164     }
165     XBT_DEBUG("%s", msgBuffer.str().c_str());
166   }
167
168
169   this->generateLabels();
170
171   unsigned int k = 0;
172   // Nodes are totally ordered, by level and then by position, in this->nodes
173   for (unsigned int i = 0 ; i < this->levels_ ; i++) {
174     for (unsigned int j = 0 ; j < this->nodesByLevel_[i] ; j++) {
175         this->connectNodeToParents(this->nodes_[k]);
176         k++;
177     }
178   }
179   
180   if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
181     std::stringstream msgBuffer;
182     msgBuffer << "Links are : ";
183     for (unsigned int i = 0 ; i < this->links_.size() ; i++) {
184       msgBuffer << "(" << this->links_[i]->upNode->id << ","
185                 << this->links_[i]->downNode->id << ") ";
186     }
187     XBT_DEBUG("%s", msgBuffer.str().c_str());
188   }
189
190
191 }
192
193 int AsClusterFatTree::connectNodeToParents(FatTreeNode *node) {
194   std::vector<FatTreeNode*>::iterator currentParentNode = this->nodes_.begin();
195   int connectionsNumber = 0;
196   const int level = node->level;
197   XBT_DEBUG("We are connecting node %d(%u,%u) to his parents.",
198             node->id, node->level, node->position);
199   currentParentNode += this->getLevelPosition(level + 1);
200   for (unsigned int i = 0 ; i < this->nodesByLevel_[level + 1] ; i++ ) {
201     if(this->areRelated(*currentParentNode, node)) {
202       XBT_DEBUG("%d(%u,%u) and %d(%u,%u) are related,"
203                 " with %u links between them.", node->id,
204                 node->level, node->position, (*currentParentNode)->id,
205                 (*currentParentNode)->level, (*currentParentNode)->position, this->lowerLevelPortsNumber_[level]);
206       for (unsigned int j = 0 ; j < this->lowerLevelPortsNumber_[level] ; j++) {
207       this->addLink(*currentParentNode, node->label[level] +
208                     j * this->lowerLevelNodesNumber_[level], node,
209                     (*currentParentNode)->label[level] +
210                     j * this->upperLevelNodesNumber_[level]);
211       }
212       connectionsNumber++;
213     }
214     ++currentParentNode;
215   }
216   return connectionsNumber;
217 }
218
219
220 bool AsClusterFatTree::areRelated(FatTreeNode *parent, FatTreeNode *child) {
221   std::stringstream msgBuffer;
222
223   if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
224     msgBuffer << "Are " << child->id << "(" << child->level << ","
225               << child->position << ") <";
226
227     for (unsigned int i = 0 ; i < this->levels_ ; i++) {
228       msgBuffer << child->label[i] << ",";
229     }
230     msgBuffer << ">";
231     
232     msgBuffer << " and " << parent->id << "(" << parent->level
233               << "," << parent->position << ") <";
234     for (unsigned int i = 0 ; i < this->levels_ ; i++) {
235       msgBuffer << parent->label[i] << ",";
236     }
237     msgBuffer << ">";
238     msgBuffer << " related ? ";
239     XBT_DEBUG("%s", msgBuffer.str().c_str());
240     
241   }
242   if (parent->level != child->level + 1) {
243     return false;
244   }
245   
246   for (unsigned int i = 0 ; i < this->levels_; i++) {
247     if (parent->label[i] != child->label[i] && i + 1 != parent->level) {
248       return false;
249     }
250   }
251   return true;
252 }
253
254 void AsClusterFatTree::generateSwitches() {
255   XBT_DEBUG("Generating switches.");
256   this->nodesByLevel_.resize(this->levels_ + 1, 0);
257   unsigned int nodesRequired = 0;
258
259   // Take care of the number of nodes by level
260   this->nodesByLevel_[0] = 1;
261   for (unsigned int i = 0 ; i < this->levels_ ; i++)
262     this->nodesByLevel_[0] *= this->lowerLevelNodesNumber_[i];
263      
264   if(this->nodesByLevel_[0] != this->nodes_.size()) {
265     surf_parse_error("The number of provided nodes does not fit with the wanted topology."
266                      " Please check your platform description (We need %d nodes, we got %zu)",
267                      this->nodesByLevel_[0], this->nodes_.size());
268     return;
269   }
270
271   
272   for (unsigned int i = 0 ; i < this->levels_ ; i++) {
273     int nodesInThisLevel = 1;
274       
275     for (unsigned int j = 0 ;  j <= i ; j++)
276       nodesInThisLevel *= this->upperLevelNodesNumber_[j];
277       
278     for (unsigned int j = i+1 ; j < this->levels_ ; j++)
279       nodesInThisLevel *= this->lowerLevelNodesNumber_[j];
280
281     this->nodesByLevel_[i+1] = nodesInThisLevel;
282     nodesRequired += nodesInThisLevel;
283   }
284
285
286   // Create the switches
287   int k = 0;
288   for (unsigned int i = 0 ; i < this->levels_ ; i++) {
289     for (unsigned int j = 0 ; j < this->nodesByLevel_[i + 1] ; j++) {
290       FatTreeNode* newNode = new FatTreeNode(this->cluster_, --k, i + 1, j);
291       XBT_DEBUG("We create the switch %d(%d,%d)", newNode->id, newNode->level, newNode->position);
292       newNode->children.resize(this->lowerLevelNodesNumber_[i] *
293                                this->lowerLevelPortsNumber_[i]);
294       if (i != this->levels_ - 1) {
295         newNode->parents.resize(this->upperLevelNodesNumber_[i + 1] *
296                                 this->lowerLevelPortsNumber_[i + 1]);
297       }
298       newNode->label.resize(this->levels_);
299       this->nodes_.push_back(newNode);
300     }
301   }
302 }
303
304 void AsClusterFatTree::generateLabels() {
305   XBT_DEBUG("Generating labels.");
306   // TODO : check if nodesByLevel and nodes are filled
307   std::vector<int> maxLabel(this->levels_);
308   std::vector<int> currentLabel(this->levels_);
309   unsigned int k = 0;
310   for (unsigned int i = 0 ; i <= this->levels_ ; i++) {
311     currentLabel.assign(this->levels_, 0);
312     for (unsigned int j = 0 ; j < this->levels_ ; j++) {
313       maxLabel[j] = j + 1 > i ?
314         this->lowerLevelNodesNumber_[j] : this->upperLevelNodesNumber_[j];
315     }
316     
317     for (unsigned int j = 0 ; j < this->nodesByLevel_[i] ; j++) {
318
319       if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug )) {
320         std::stringstream msgBuffer;
321
322         msgBuffer << "Assigning label <";
323         for (unsigned int l = 0 ; l < this->levels_ ; l++) {
324           msgBuffer << currentLabel[l] << ",";
325         }
326         msgBuffer << "> to " << k << " (" << i << "," << j <<")";
327         
328         XBT_DEBUG("%s", msgBuffer.str().c_str());
329       }
330       this->nodes_[k]->label.assign(currentLabel.begin(), currentLabel.end());
331
332       bool remainder = true;
333       unsigned int pos = 0;
334       while (remainder && pos < this->levels_) {
335         ++currentLabel[pos];
336         if (currentLabel[pos] >= maxLabel[pos]) {
337           currentLabel[pos] = 0;
338           remainder = true;
339           ++pos;
340         }
341         else {
342           pos = 0;
343           remainder = false;
344         }
345       }
346       k++;
347     }
348   }
349 }
350
351
352 int AsClusterFatTree::getLevelPosition(const unsigned  int level) {
353   xbt_assert(level <= this->levels_, "The impossible did happen. Yet again.");
354   int tempPosition = 0;
355
356   for (unsigned int i = 0 ; i < level ; i++)
357     tempPosition += this->nodesByLevel_[i];
358
359   return tempPosition;
360 }
361
362 void AsClusterFatTree::addProcessingNode(int id) {
363   using std::make_pair;
364   static int position = 0;
365   FatTreeNode* newNode;
366   newNode = new FatTreeNode(this->cluster_, id, 0, position++);
367   newNode->parents.resize(this->upperLevelNodesNumber_[0] *
368                           this->lowerLevelPortsNumber_[0]);
369   newNode->label.resize(this->levels_);
370   this->computeNodes_.insert(make_pair(id,newNode));
371   this->nodes_.push_back(newNode);
372 }
373
374 void AsClusterFatTree::addLink(FatTreeNode *parent, unsigned int parentPort,
375                                FatTreeNode *child, unsigned int childPort) {
376   FatTreeLink *newLink;
377   newLink = new FatTreeLink(this->cluster_, child, parent);
378   XBT_DEBUG("Creating a link between the parent (%d,%d,%u) and the child (%d,%d,%u)",
379       parent->level, parent->position, parentPort, child->level, child->position, childPort);
380   parent->children[parentPort] = newLink;
381   child->parents[childPort] = newLink;
382
383   this->links_.push_back(newLink);
384 }
385
386 void AsClusterFatTree::parse_specific_arguments(sg_platf_cluster_cbarg_t cluster) {
387   std::vector<std::string> parameters;
388   std::vector<std::string> tmp;
389   boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
390
391   // TODO : we have to check for zeros and negative numbers, or it might crash
392   if (parameters.size() != 4){
393     surf_parse_error("Fat trees are defined by the levels number and 3 vectors, see the documentation for more information");
394   }
395
396   // The first parts of topo_parameters should be the levels number
397   this->levels_ = xbt_str_parse_int(parameters[0].c_str(), "First parameter is not the amount of levels: %s");
398
399   // Then, a l-sized vector standing for the children 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 information");
404   }
405   for(size_t i = 0 ; i < tmp.size() ; i++){
406     this->lowerLevelNodesNumber_.push_back(xbt_str_parse_int(tmp[i].c_str(), "Invalid lower level node number: %s"));
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 information");
414   }
415   for(size_t i = 0 ; i < tmp.size() ; i++){
416     this->upperLevelNodesNumber_.push_back(xbt_str_parse_int(tmp[i].c_str(), "Invalid upper level node number: %s"));
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 information");
424     
425   }
426   for(size_t i = 0 ; i < tmp.size() ; i++){
427     this->lowerLevelPortsNumber_.push_back(xbt_str_parse_int(tmp[i].c_str(), "Invalid lower level node number: %s"));
428   }
429   this->cluster_ = cluster;
430 }
431
432
433 void AsClusterFatTree::generateDotFile(const std::string& filename) const {
434   std::ofstream file;
435   file.open(filename, std::ios::out | std::ios::trunc);
436   xbt_assert(file.is_open(), "Unable to open file %s", filename.c_str());
437
438   file << "graph AsClusterFatTree {\n";
439   for (unsigned int i = 0 ; i < this->nodes_.size() ; i++) {
440     file << this->nodes_[i]->id;
441     if(this->nodes_[i]->id < 0)
442       file << " [shape=circle];\n";
443     else
444       file << " [shape=hexagon];\n";
445   }
446
447   for (unsigned int i = 0 ; i < this->links_.size() ; i++ ) {
448     file << this->links_[i]->downNode->id
449         << " -- "
450         << this->links_[i]->upNode->id
451         << ";\n";
452   }
453   file << "}";
454   file.close();
455 }
456
457 FatTreeNode::FatTreeNode(sg_platf_cluster_cbarg_t cluster, int id, int level,
458                          int position) : id(id), level(level),
459                                          position(position) {
460   s_sg_platf_link_cbarg_t linkTemplate;
461   if(cluster->limiter_link) {
462     memset(&linkTemplate, 0, sizeof(linkTemplate));
463     linkTemplate.bandwidth = cluster->limiter_link;
464     linkTemplate.latency = 0;
465     linkTemplate.policy = SURF_LINK_SHARED;
466     linkTemplate.id = bprintf("limiter_%d", id);
467     sg_platf_new_link(&linkTemplate);
468     this->limiterLink = Link::byName(linkTemplate.id);
469     free((void*)linkTemplate.id);
470   }
471   if(cluster->loopback_bw || cluster->loopback_lat) {
472     memset(&linkTemplate, 0, sizeof(linkTemplate));
473     linkTemplate.bandwidth = cluster->loopback_bw;
474     linkTemplate.latency = cluster->loopback_lat;
475     linkTemplate.policy = SURF_LINK_FATPIPE;
476     linkTemplate.id = bprintf("loopback_%d", id);
477     sg_platf_new_link(&linkTemplate);
478     this->loopback = Link::byName(linkTemplate.id);
479     free((void*)linkTemplate.id);
480   }  
481 }
482
483 FatTreeLink::FatTreeLink(sg_platf_cluster_cbarg_t cluster,
484                          FatTreeNode *downNode,
485                          FatTreeNode *upNode) : upNode(upNode),
486                                                 downNode(downNode) {
487   static int uniqueId = 0;
488   s_sg_platf_link_cbarg_t linkTemplate;
489   memset(&linkTemplate, 0, sizeof(linkTemplate));
490   linkTemplate.bandwidth = cluster->bw;
491   linkTemplate.latency = cluster->lat;
492   linkTemplate.policy = cluster->sharing_policy; // sthg to do with that ?
493   linkTemplate.id = bprintf("link_from_%d_to_%d_%d", downNode->id, upNode->id, uniqueId);
494   sg_platf_new_link(&linkTemplate);
495   Link* link;
496   std::string tmpID;
497   if (cluster->sharing_policy == SURF_LINK_FULLDUPLEX) {
498     tmpID = std::string(linkTemplate.id) + "_UP";
499     link =  Link::byName(tmpID.c_str());
500     this->upLink = link; // check link?
501     tmpID = std::string(linkTemplate.id) + "_DOWN";
502     link = Link::byName(tmpID.c_str());
503     this->downLink = link; // check link ?
504   }
505   else {
506     link = Link::byName(linkTemplate.id);
507     this->upLink = link;
508     this->downLink = link;
509   }
510   uniqueId++;
511   free((void*)linkTemplate.id);
512 }
513
514 }}} // namespace