Logo AND Algorithmique Numérique Distribuée

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