Logo AND Algorithmique Numérique Distribuée

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