Logo AND Algorithmique Numérique Distribuée

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