Logo AND Algorithmique Numérique Distribuée

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