Logo AND Algorithmique Numérique Distribuée

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