Logo AND Algorithmique Numérique Distribuée

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