Logo AND Algorithmique Numérique Distribuée

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