Logo AND Algorithmique Numérique Distribuée

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