Logo AND Algorithmique Numérique Distribuée

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