Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
initialize field As::p_linkUpDownList at declaration (+ cosmetics)
[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 AS_t model_fat_tree_cluster_create(void)
22 {
23   return new simgrid::surf::AsClusterFatTree();
24 }
25
26 namespace simgrid {
27 namespace surf {
28
29 AsClusterFatTree::AsClusterFatTree() : levels(0) {
30   XBT_DEBUG("Creating a new fat tree.");
31 }
32
33 AsClusterFatTree::~AsClusterFatTree() {
34   for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
35     delete this->nodes[i];
36   }
37   for (unsigned int i = 0 ; i < this->links.size() ; i++) {
38     delete this->links[i];
39   }
40 }
41
42 bool AsClusterFatTree::isInSubTree(FatTreeNode *root, FatTreeNode *node) {
43   XBT_DEBUG("Is %d(%u,%u) in the sub tree of %d(%u,%u) ?", node->id,
44             node->level, node->position, root->id, root->level, root->position);
45   if (root->level <= node->level) {
46     return false;
47   }
48   for (unsigned int i = 0 ; i < node->level ; i++) {
49     if(root->label[i] != node->label[i]) {
50       return false;
51     }
52   }
53   
54   for (unsigned int i = root->level ; i < this->levels ; i++) {
55     if(root->label[i] != node->label[i]) {
56       return false;
57     }
58   }
59   return true;
60 }
61
62 void AsClusterFatTree::getRouteAndLatency(NetCard *src,
63                                           NetCard *dst,
64                                           sg_platf_route_cbarg_t into,
65                                           double *latency) {
66   FatTreeNode *source, *destination, *currentNode;
67
68   std::map<int, FatTreeNode*>::const_iterator tempIter;
69   
70 if (dst->getRcType() == SURF_NETWORK_ELEMENT_ROUTER || src->getRcType() == SURF_NETWORK_ELEMENT_ROUTER) return;
71
72   /* Let's find the source and the destination in our internal structure */
73   tempIter = this->computeNodes.find(src->getId());
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->getName(),
78             src->getId());
79   }
80   source = tempIter->second;
81   tempIter = this->computeNodes.find(dst->getId());
82   if (tempIter == this->computeNodes.end()) {
83     xbt_die("Could not find the destination %s [%d] in the fat tree",
84             dst->getName(), dst->getId());
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->getName(), src->getId(), dst->getName(), dst->getId());
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->p_has_loopback) {
96     xbt_dynar_push_as(into->link_list, void*, 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     xbt_dynar_push_as(into->link_list, void*,currentNode->parents[d]->upLink);
116
117     if(latency) {
118       *latency += currentNode->parents[d]->upLink->getLatency();
119     }
120
121     if (this->p_has_limiter) {
122       xbt_dynar_push_as(into->link_list, void*,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         xbt_dynar_push_as(into->link_list, void*,currentNode->children[i]->downLink);
137         if(latency) {
138           *latency += currentNode->children[i]->downLink->getLatency();
139         }
140         currentNode = currentNode->children[i]->downNode;
141         if (this->p_has_limiter) {
142           xbt_dynar_push_as(into->link_list, void*,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::create_links(){
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   // We 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
278      
279   if(this->nodesByLevel[0] != this->nodes.size()) {
280     surf_parse_error("The number of provided nodes does not fit with the wanted topology."
281                      " Please check your platform description (We need %d nodes, we got %zu)",
282                      this->nodesByLevel[0], this->nodes.size());
283     return;
284   }
285
286   
287   for (unsigned int i = 0 ; i < this->levels ; i++) {
288     int nodesInThisLevel = 1;
289       
290     for (unsigned int j = 0 ;  j <= i ; j++) {
291       nodesInThisLevel *= this->upperLevelNodesNumber[j];
292     }
293       
294     for (unsigned int j = i+1 ; j < this->levels ; j++) {
295       nodesInThisLevel *= this->lowerLevelNodesNumber[j];
296     }
297
298     this->nodesByLevel[i+1] = nodesInThisLevel;
299     nodesRequired += nodesInThisLevel;
300   }
301
302
303   // We create the switches
304   int k = 0;
305   for (unsigned int i = 0 ; i < this->levels ; i++) {
306     for (unsigned int j = 0 ; j < this->nodesByLevel[i + 1] ; j++) {
307       FatTreeNode* newNode;
308       newNode = new FatTreeNode(this->cluster, --k, i + 1, j);
309       XBT_DEBUG("We create the switch %d(%d,%d)", newNode->id, newNode->level,
310                 newNode->position);
311       newNode->children.resize(this->lowerLevelNodesNumber[i] *
312                                this->lowerLevelPortsNumber[i]);
313       if (i != this->levels - 1) {
314         newNode->parents.resize(this->upperLevelNodesNumber[i + 1] *
315                                 this->lowerLevelPortsNumber[i + 1]);
316       }
317       newNode->label.resize(this->levels);
318       this->nodes.push_back(newNode);
319     }
320   }
321 }
322
323 void AsClusterFatTree::generateLabels() {
324   XBT_DEBUG("Generating labels.");
325   // TODO : check if nodesByLevel and nodes are filled
326   std::vector<int> maxLabel(this->levels);
327   std::vector<int> currentLabel(this->levels);
328   unsigned int k = 0;
329   for (unsigned int i = 0 ; i <= this->levels ; i++) {
330     currentLabel.assign(this->levels, 0);
331     for (unsigned int j = 0 ; j < this->levels ; j++) {
332       maxLabel[j] = j + 1 > i ?
333         this->lowerLevelNodesNumber[j] : this->upperLevelNodesNumber[j];
334     }
335     
336     for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
337
338       if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug )) {
339         std::stringstream msgBuffer;
340
341         msgBuffer << "Assigning label <";
342         for (unsigned int l = 0 ; l < this->levels ; l++) {
343           msgBuffer << currentLabel[l] << ",";
344         }
345         msgBuffer << "> to " << k << " (" << i << "," << j <<")";
346         
347         XBT_DEBUG("%s", msgBuffer.str().c_str());
348       }
349       this->nodes[k]->label.assign(currentLabel.begin(), currentLabel.end());
350
351       bool remainder = true;
352       
353       unsigned int pos = 0;
354       do {
355         std::stringstream msgBuffer;
356
357         ++currentLabel[pos];
358         if (currentLabel[pos] >= maxLabel[pos]) {
359           currentLabel[pos] = 0;
360           remainder = true;
361         }
362         else {
363           remainder = false;
364         }
365         if (!remainder) {
366           pos = 0;
367         }
368         else {
369           ++pos;
370         }
371       }
372       while(remainder && pos < this->levels);
373       k++;
374     }
375   }
376 }
377
378
379 int AsClusterFatTree::getLevelPosition(const unsigned  int level) {
380   if (level > this->levels) {
381     // Well, that should never happen. Maybe should we throw instead.
382     return -1;
383   }
384   int tempPosition = 0;
385
386   for (unsigned int i = 0 ; i < level ; i++) {
387     tempPosition += this->nodesByLevel[i];
388   }
389  return tempPosition;
390 }
391
392 void AsClusterFatTree::addProcessingNode(int id) {
393   using std::make_pair;
394   static int position = 0;
395   FatTreeNode* newNode;
396   newNode = new FatTreeNode(this->cluster, id, 0, position++);
397   newNode->parents.resize(this->upperLevelNodesNumber[0] *
398                           this->lowerLevelPortsNumber[0]);
399   newNode->label.resize(this->levels);
400   this->computeNodes.insert(make_pair(id,newNode));
401   this->nodes.push_back(newNode);
402 }
403
404 void AsClusterFatTree::addLink(FatTreeNode *parent, unsigned int parentPort,
405                                FatTreeNode *child, unsigned int childPort) {
406   FatTreeLink *newLink;
407   newLink = new FatTreeLink(this->cluster, child, parent);
408   XBT_DEBUG("Creating a link between the parent (%d,%d,%u)"
409             " and the child (%d,%d,%u)", parent->level, parent->position,
410             parentPort, child->level, child->position, childPort);
411   parent->children[parentPort] = newLink;
412   child->parents[childPort] = newLink;
413
414   this->links.push_back(newLink);
415
416   
417
418 }
419
420 void AsClusterFatTree::parse_specific_arguments(sg_platf_cluster_cbarg_t 
421                                                 cluster) {
422   std::vector<std::string> parameters;
423   std::vector<std::string> tmp;
424   boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
425  
426
427   // TODO : we have to check for zeros and negative numbers, or it might crash
428   if (parameters.size() != 4){
429     surf_parse_error("Fat trees are defined by the levels number and 3 vectors" 
430                      ", see the documentation for more informations");
431   }
432
433   // The first parts of topo_parameters should be the levels number
434   this->levels = xbt_str_parse_int(parameters[0].c_str(), "First parameter is not the amount of levels: %s");
435   
436   // Then, a l-sized vector standing for the childs number by level
437   boost::split(tmp, parameters[1], boost::is_any_of(","));
438   if(tmp.size() != this->levels) {
439     surf_parse_error("Fat trees are defined by the levels number and 3 vectors" 
440                      ", see the documentation for more informations"); 
441   }
442   for(size_t i = 0 ; i < tmp.size() ; i++){
443     this->lowerLevelNodesNumber.push_back(xbt_str_parse_int(tmp[i].c_str(), "Invalid lower level node number: %s"));
444   }
445   
446   // Then, a l-sized vector standing for the parents number by level
447   boost::split(tmp, parameters[2], boost::is_any_of(","));
448   if(tmp.size() != this->levels) {
449     surf_parse_error("Fat trees are defined by the levels number and 3 vectors" 
450                      ", see the documentation for more informations"); 
451   }
452   for(size_t i = 0 ; i < tmp.size() ; i++){
453     this->upperLevelNodesNumber.push_back(xbt_str_parse_int(tmp[i].c_str(), "Invalid upper level node number: %s"));
454   }
455   
456   // Finally, a l-sized vector standing for the ports number with the lower level
457   boost::split(tmp, parameters[3], boost::is_any_of(","));
458   if(tmp.size() != this->levels) {
459     surf_parse_error("Fat trees are defined by the levels number and 3 vectors" 
460                      ", see the documentation for more informations"); 
461     
462   }
463   for(size_t i = 0 ; i < tmp.size() ; i++){
464     this->lowerLevelPortsNumber.push_back(xbt_str_parse_int(tmp[i].c_str(), "Invalid lower level node number: %s"));
465   }
466   this->cluster = cluster;
467 }
468
469
470 void AsClusterFatTree::generateDotFile(const std::string& filename) const {
471   std::ofstream file;
472   /* Maybe should we get directly a char*, as open takes strings only beginning
473    * with C++11...
474    */
475   file.open(filename.c_str(), std::ios::out | std::ios::trunc); 
476   
477   if(file.is_open()) {
478     file << "graph AsClusterFatTree {\n";
479     for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
480       file << this->nodes[i]->id;
481       if(this->nodes[i]->id < 0) {
482         file << " [shape=circle];\n";
483       }
484       else {
485         file << " [shape=hexagon];\n";
486       }
487     }
488
489     for (unsigned int i = 0 ; i < this->links.size() ; i++ ) {
490       file << this->links[i]->downNode->id
491              << " -- "
492            << this->links[i]->upNode->id
493              << ";\n";
494     }
495     file << "}";
496     file.close();
497   }
498   else {
499     XBT_DEBUG("Unable to open file %s", filename.c_str());
500     return;
501   }
502 }
503
504 FatTreeNode::FatTreeNode(sg_platf_cluster_cbarg_t cluster, int id, int level,
505                          int position) : id(id), level(level),
506                                          position(position) {
507   s_sg_platf_link_cbarg_t linkTemplate = SG_PLATF_LINK_INITIALIZER;
508   if(cluster->limiter_link) {
509     memset(&linkTemplate, 0, sizeof(linkTemplate));
510     linkTemplate.bandwidth = cluster->limiter_link;
511     linkTemplate.latency = 0;
512     linkTemplate.initiallyOn = 1;
513     linkTemplate.policy = SURF_LINK_SHARED;
514     linkTemplate.id = bprintf("limiter_%d", id);
515     sg_platf_new_link(&linkTemplate);
516     this->limiterLink = Link::byName(linkTemplate.id);
517     free((void*)linkTemplate.id);
518   }
519   if(cluster->loopback_bw || cluster->loopback_lat) {
520     memset(&linkTemplate, 0, sizeof(linkTemplate));
521     linkTemplate.bandwidth = cluster->loopback_bw;
522     linkTemplate.latency = cluster->loopback_lat;
523     linkTemplate.initiallyOn = 1;
524     linkTemplate.policy = SURF_LINK_FATPIPE;
525     linkTemplate.id = bprintf("loopback_%d", id);
526     sg_platf_new_link(&linkTemplate);
527     this->loopback = Link::byName(linkTemplate.id);
528     free((void*)linkTemplate.id);
529   }  
530 }
531
532 FatTreeLink::FatTreeLink(sg_platf_cluster_cbarg_t cluster,
533                          FatTreeNode *downNode,
534                          FatTreeNode *upNode) : upNode(upNode),
535                                                 downNode(downNode) {
536   static int uniqueId = 0;
537   s_sg_platf_link_cbarg_t linkTemplate = SG_PLATF_LINK_INITIALIZER;
538   memset(&linkTemplate, 0, sizeof(linkTemplate));
539   linkTemplate.bandwidth = cluster->bw;
540   linkTemplate.latency = cluster->lat;
541   linkTemplate.initiallyOn = 1;
542   linkTemplate.policy = cluster->sharing_policy; // sthg to do with that ?
543   linkTemplate.id = bprintf("link_from_%d_to_%d_%d", downNode->id, upNode->id,
544                             uniqueId);
545   sg_platf_new_link(&linkTemplate);
546   Link* link;
547   std::string tmpID;
548   if (cluster->sharing_policy == SURF_LINK_FULLDUPLEX) {
549     tmpID = std::string(linkTemplate.id) + "_UP";
550     link =  Link::byName(tmpID.c_str());
551     this->upLink = link; // check link?
552     tmpID = std::string(linkTemplate.id) + "_DOWN";
553     link = Link::byName(tmpID.c_str());
554     this->downLink = link; // check link ?
555   }
556   else {
557     link = Link::byName(linkTemplate.id);
558     this->upLink = link;
559     this->downLink = link;
560   }
561   uniqueId++;
562   free((void*)linkTemplate.id);
563 }
564
565 }
566 }