Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Loopback links properly added to fat trees.
[simgrid.git] / src / surf / surf_routing_cluster_fat_tree.cpp
1 #include "surf_routing_cluster_fat_tree.hpp"
2 #include "xbt/lib.h"
3
4 #include <boost/algorithm/string/split.hpp>
5 #include <boost/algorithm/string/classification.hpp>
6 #include <iostream>
7 #include <fstream>
8 #include <sstream>
9
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_fat_tree, surf, "Routing for fat trees");
11
12 AS_t model_fat_tree_cluster_create(void)
13 {
14   return new AsClusterFatTree();
15 }
16
17 AsClusterFatTree::AsClusterFatTree() : levels(0) {
18   XBT_DEBUG("Creating a new fat tree.");
19 }
20
21 AsClusterFatTree::~AsClusterFatTree() {
22   for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
23     delete this->nodes[i];
24   }
25 }
26
27 bool AsClusterFatTree::isInSubTree(FatTreeNode *root, FatTreeNode *node) {
28   XBT_DEBUG("Is %d(%u,%u) in the sub tree of %d(%u,%u) ?", node->id,
29             node->level, node->position, root->id, root->level, root->position);
30   if (root->level <= node->level) {
31     return false;
32   }
33   for (unsigned int i = 0 ; i < node->level ; i++) {
34     if(root->label[i] != node->label[i]) {
35       return false;
36     }
37   }
38   
39   for (unsigned int i = root->level ; i < this->levels ; i++) {
40     if(root->label[i] != node->label[i]) {
41       return false;
42     }
43   }
44   return true;
45 }
46
47 void AsClusterFatTree::getRouteAndLatency(RoutingEdgePtr src,
48                                           RoutingEdgePtr dst,
49                                           sg_platf_route_cbarg_t into,
50                                           double *latency) {
51   FatTreeNode *source, *destination, *currentNode;
52   std::map<int, FatTreeNode*>::const_iterator tempIter;
53
54   /* Let's find the source and the destination in our internal structure */
55   tempIter = this->computeNodes.find(src->getId());
56   // xbt_die -> assert
57   if (tempIter == this->computeNodes.end()) {
58     xbt_die("Could not find the source %s [%d] in the fat tree", src->getName(),
59             src->getId());
60   }
61   source = tempIter->second;
62   tempIter = this->computeNodes.find(dst->getId());
63   if (tempIter == this->computeNodes.end()) {
64     xbt_die("Could not find the destination %s [%d] in the fat tree",
65             src->getName(), src->getId());
66   }
67   destination = tempIter->second;
68   
69   XBT_VERB("Get route and latency from '%s' [%d] to '%s' [%d] in a fat tree",
70             src->getName(), src->getId(), dst->getName(), dst->getId());
71
72   /* In case destination is the source, and there is a loopback, let's get
73      through it instead of going up to a switch*/
74   if(source->id == destination->id && this->p_has_loopback) {
75     xbt_dynar_push_as(into->link_list, void*, source->loopback);
76     if(latency) {
77       *latency += source->loopback->getLatency();
78     }
79   }
80
81   currentNode = source;
82   // up part
83   while (!isInSubTree(currentNode, destination)) {
84     int d, k; // as in d-mod-k
85     d = destination->position;
86
87     for (unsigned int i = 0 ; i < currentNode->level ; i++) {
88       d /= this->upperLevelNodesNumber[i];
89     }
90     k = this->upperLevelNodesNumber[currentNode->level];
91     d = d % k;
92     xbt_dynar_push_as(into->link_list, void*,currentNode->parents[d]->upLink);
93
94     if(latency) {
95       *latency += currentNode->parents[d]->upLink->getLatency();
96     }
97
98     if (this->p_has_limiter) {
99       xbt_dynar_push_as(into->link_list, void*,currentNode->limiterLink);
100     }
101     currentNode = currentNode->parents[d]->upNode;
102   }
103
104   XBT_DEBUG("%d(%u,%u) is in the sub tree of %d(%u,%u).", destination->id,
105             destination->level, destination->position, currentNode->id,
106             currentNode->level, currentNode->position);
107
108   // Down part
109   while(currentNode != destination) {
110     for(unsigned int i = 0 ; i < currentNode->children.size() ; i++) {
111       if(i % this->lowerLevelNodesNumber[currentNode->level - 1] ==
112          destination->label[currentNode->level - 1]) {
113         xbt_dynar_push_as(into->link_list, void*,currentNode->children[i]->downLink);
114         if(latency) {
115           *latency += currentNode->children[i]->downLink->getLatency();
116         }
117         currentNode = currentNode->children[i]->downNode;
118         if (this->p_has_limiter) {
119           xbt_dynar_push_as(into->link_list, void*,currentNode->limiterLink);
120         }
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::create_links(){
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   // We 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
255      
256   if(this->nodesByLevel[0] != this->nodes.size()) {
257     surf_parse_error("The number of provided nodes does not fit with the wanted topology."
258                      " Please check your platform description (We need %d nodes, we got %zu)",
259                      this->nodesByLevel[0], this->nodes.size());
260     return;
261   }
262
263   
264   for (unsigned int i = 0 ; i < this->levels ; i++) {
265     int nodesInThisLevel = 1;
266       
267     for (unsigned int j = 0 ;  j <= i ; j++) {
268       nodesInThisLevel *= this->upperLevelNodesNumber[j];
269     }
270       
271     for (unsigned int j = i+1 ; j < this->levels ; j++) {
272       nodesInThisLevel *= this->lowerLevelNodesNumber[j];
273     }
274
275     this->nodesByLevel[i+1] = nodesInThisLevel;
276     nodesRequired += nodesInThisLevel;
277   }
278
279
280   // If we have to many compute nodes, we ditch them
281   
282
283   // We create the switches
284   int k = 0;
285   for (unsigned int i = 0 ; i < this->levels ; i++) {
286     for (unsigned int j = 0 ; j < this->nodesByLevel[i + 1] ; j++) {
287       FatTreeNode* newNode;
288       newNode = new FatTreeNode(this->cluster, --k, i + 1, j);
289       XBT_DEBUG("We create the switch %d(%d,%d)", newNode->id, newNode->level,
290                 newNode->position);
291       newNode->children.resize(this->lowerLevelNodesNumber[i] *
292                                this->lowerLevelPortsNumber[i]);
293       if (i != this->levels - 1) {
294         newNode->parents.resize(this->upperLevelNodesNumber[i + 1] *
295                                 this->lowerLevelPortsNumber[i + 1]);
296       }
297       newNode->label.resize(this->levels);
298       this->nodes.push_back(newNode);
299     }
300   }
301 }
302
303 void AsClusterFatTree::generateLabels() {
304   XBT_DEBUG("Generating labels.");
305   // TODO : check if nodesByLevel and nodes are filled
306   std::vector<int> maxLabel(this->levels);
307   std::vector<int> currentLabel(this->levels);
308   unsigned int k = 0;
309   for (unsigned int i = 0 ; i <= this->levels ; i++) {
310     currentLabel.assign(this->levels, 0);
311     for (unsigned int j = 0 ; j < this->levels ; j++) {
312       maxLabel[j] = j + 1 > i ?
313         this->lowerLevelNodesNumber[j] : this->upperLevelNodesNumber[j];
314     }
315     
316     for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
317
318       if(XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug )) {
319         std::stringstream msgBuffer;
320
321         msgBuffer << "Assigning label <";
322         for (unsigned int l = 0 ; l < this->levels ; l++) {
323           msgBuffer << currentLabel[l] << ",";
324         }
325         msgBuffer << "> to " << k << " (" << i << "," << j <<")";
326         
327         XBT_DEBUG("%s", msgBuffer.str().c_str());
328       }
329       this->nodes[k]->label.assign(currentLabel.begin(), currentLabel.end());
330
331       bool remainder = true;
332       
333       unsigned int pos = 0;
334       do {
335         std::stringstream msgBuffer;
336
337         ++currentLabel[pos];
338         if (currentLabel[pos] >= maxLabel[pos]) {
339           currentLabel[pos] = 0;
340           remainder = true;
341         }
342         else {
343           remainder = false;
344         }
345         if (!remainder) {
346           pos = 0;
347         }
348         else {
349           ++pos;
350         }
351       }
352       while(remainder && pos < this->levels);
353       k++;
354     }
355   }
356 }
357
358
359 int AsClusterFatTree::getLevelPosition(const unsigned  int level) {
360   if (level > this->levels) {
361     // Well, that should never happen. Maybe should we throw instead.
362     return -1;
363   }
364   int tempPosition = 0;
365
366   for (unsigned int i = 0 ; i < level ; i++) {
367     tempPosition += this->nodesByLevel[i];
368   }
369  return tempPosition;
370 }
371
372 void AsClusterFatTree::addProcessingNode(int id) {
373   using std::make_pair;
374   static int position = 0;
375   FatTreeNode* newNode;
376   newNode = new FatTreeNode(this->cluster, id, 0, position++);
377   newNode->parents.resize(this->upperLevelNodesNumber[0] *
378                           this->lowerLevelPortsNumber[0]);
379   newNode->label.resize(this->levels);
380   this->computeNodes.insert(make_pair(id,newNode));
381   this->nodes.push_back(newNode);
382 }
383
384 void AsClusterFatTree::addLink(FatTreeNode *parent, unsigned int parentPort,
385                                FatTreeNode *child, unsigned int childPort) {
386   FatTreeLink *newLink;
387   newLink = new FatTreeLink(this->cluster, child, parent);
388   XBT_DEBUG("Creating a link between the parent (%d,%d,%u)"
389             " and the child (%d,%d,%u)", parent->level, parent->position,
390             parentPort, child->level, child->position, childPort);
391   parent->children[parentPort] = newLink;
392   child->parents[childPort] = newLink;
393
394   this->links.push_back(newLink);
395 }
396
397 void AsClusterFatTree::parse_specific_arguments(sg_platf_cluster_cbarg_t 
398                                                 cluster) {
399   std::vector<string> parameters;
400   std::vector<string> tmp;
401   boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
402  
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" 
407                      ", see the documentation for more informations");
408     // Well, there's no doc, yet
409   }
410
411   // The first parts of topo_parameters should be the levels number
412   this->levels = std::atoi(parameters[0].c_str()); // stoi() only in C++11...
413   
414   // Then, a l-sized vector standing for the childs number by level
415   boost::split(tmp, parameters[1], boost::is_any_of(","));
416   if(tmp.size() != this->levels) {
417     surf_parse_error("Fat trees are defined by the levels number and 3 vectors" 
418                      ", see the documentation for more informations"); 
419   }
420   for(size_t i = 0 ; i < tmp.size() ; i++){
421     this->lowerLevelNodesNumber.push_back(std::atoi(tmp[i].c_str())); 
422   }
423   
424   // Then, a l-sized vector standing for the parents number by level
425   boost::split(tmp, parameters[2], boost::is_any_of(","));
426   if(tmp.size() != this->levels) {
427     surf_parse_error("Fat trees are defined by the levels number and 3 vectors" 
428                      ", see the documentation for more informations"); 
429   }
430   for(size_t i = 0 ; i < tmp.size() ; i++){
431     this->upperLevelNodesNumber.push_back(std::atoi(tmp[i].c_str())); 
432   }
433   
434   // Finally, a l-sized vector standing for the ports number with the lower level
435   boost::split(tmp, parameters[3], boost::is_any_of(","));
436   if(tmp.size() != this->levels) {
437     surf_parse_error("Fat trees are defined by the levels number and 3 vectors" 
438                      ", see the documentation for more informations"); 
439     
440   }
441   for(size_t i = 0 ; i < tmp.size() ; i++){
442     this->lowerLevelPortsNumber.push_back(std::atoi(tmp[i].c_str())); 
443   }
444   this->cluster = cluster;
445 }
446
447
448 void AsClusterFatTree::generateDotFile(const string& filename) const {
449   ofstream file;
450   /* Maybe should we get directly a char*, as open takes strings only beginning
451    * with C++11...
452    */
453   file.open(filename.c_str(), ios::out | ios::trunc); 
454   
455   if(file.is_open()) {
456     file << "graph AsClusterFatTree {\n";
457     for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
458       file << this->nodes[i]->id;
459       if(this->nodes[i]->id < 0) {
460         file << " [shape=circle];\n";
461       }
462       else {
463         file << " [shape=hexagon];\n";
464       }
465     }
466
467     for (unsigned int i = 0 ; i < this->links.size() ; i++ ) {
468       file << this->links[i]->downNode->id
469              << " -- "
470            << this->links[i]->upNode->id
471              << ";\n";
472     }
473     file << "}";
474     file.close();
475   }
476   else {
477     XBT_DEBUG("Unable to open file %s", filename.c_str());
478     return;
479   }
480 }
481
482 FatTreeNode::FatTreeNode(sg_platf_cluster_cbarg_t cluster, int id, int level,
483                          int position) : id(id), level(level),
484                                          position(position) {
485   s_sg_platf_link_cbarg_t linkTemplate;
486   if(cluster->limiter_link) {
487     memset(&linkTemplate, 0, sizeof(linkTemplate));
488     linkTemplate.bandwidth = cluster->limiter_link;
489     linkTemplate.latency = 0;
490     linkTemplate.state = SURF_RESOURCE_ON;
491     linkTemplate.policy = SURF_LINK_SHARED;
492     linkTemplate.id = bprintf("limiter_%d", id);
493     sg_platf_new_link(&linkTemplate);
494     this->limiterLink = (NetworkLink*) xbt_lib_get_or_null(link_lib,
495                                                            linkTemplate.id,
496                                                            SURF_LINK_LEVEL);
497     free((void*)linkTemplate.id);
498   }
499   if(cluster->loopback_bw || cluster->loopback_lat) {
500     memset(&linkTemplate, 0, sizeof(linkTemplate));
501     linkTemplate.bandwidth = cluster->loopback_bw;
502     linkTemplate.latency = cluster->loopback_lat;
503     linkTemplate.state = SURF_RESOURCE_ON;
504     linkTemplate.policy = SURF_LINK_FATPIPE;
505     linkTemplate.id = bprintf("loopback_%d", id);
506     sg_platf_new_link(&linkTemplate);
507     this->loopback = (NetworkLink*) xbt_lib_get_or_null(link_lib,
508                                                         linkTemplate.id,
509                                                         SURF_LINK_LEVEL);
510     free((void*)linkTemplate.id);
511   }  
512 }
513
514 FatTreeLink::FatTreeLink(sg_platf_cluster_cbarg_t cluster,
515                          FatTreeNode *downNode,
516                          FatTreeNode *upNode) : upNode(upNode),
517                                                 downNode(downNode) {
518   static int uniqueId = 0;
519   s_sg_platf_link_cbarg_t linkTemplate;
520   memset(&linkTemplate, 0, sizeof(linkTemplate));
521   linkTemplate.bandwidth = cluster->bw;
522   linkTemplate.latency = cluster->lat;
523   linkTemplate.state = SURF_RESOURCE_ON;
524   linkTemplate.policy = cluster->sharing_policy; // sthg to do with that ?
525   linkTemplate.id = bprintf("link_from_%d_to_%d_%d", downNode->id, upNode->id,
526                             uniqueId);
527   sg_platf_new_link(&linkTemplate);
528   NetworkLink* link;
529   std::string tmpID;
530   if (cluster->sharing_policy == SURF_LINK_FULLDUPLEX) {
531     tmpID = std::string(linkTemplate.id) + "_UP";
532     link = (NetworkLink*) xbt_lib_get_or_null(link_lib, tmpID.c_str(),
533                                               SURF_LINK_LEVEL);
534     this->upLink = link; // check link?
535     tmpID = std::string(linkTemplate.id) + "_DOWN";
536     link = (NetworkLink*) xbt_lib_get_or_null(link_lib, tmpID.c_str(),
537                                               SURF_LINK_LEVEL);
538     this->downLink = link; // check link ?
539   }
540   else {
541     link = (NetworkLink*) xbt_lib_get_or_null(link_lib, linkTemplate.id,
542                                               SURF_LINK_LEVEL);
543     this->upLink = link;
544     this->downLink = link;
545   }
546   uniqueId++;
547   free((void*)linkTemplate.id);
548 }