Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
97c57d3bcc9b1ec6e000a83ffa9360271951cb06
[simgrid.git] / src / kernel / routing / FatTreeZone.cpp
1 /* Copyright (c) 2014-2016. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <fstream>
7 #include <sstream>
8
9 #include "src/kernel/routing/FatTreeZone.hpp"
10 #include "src/kernel/routing/NetCard.hpp"
11 #include "src/surf/network_interface.hpp"
12
13 #include "xbt/lib.h"
14
15 #include <boost/algorithm/string/classification.hpp>
16 #include <boost/algorithm/string/split.hpp>
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_fat_tree, surf, "Routing for fat trees");
19
20 namespace simgrid {
21 namespace kernel {
22 namespace routing {
23
24 AsClusterFatTree::AsClusterFatTree(As* father, const char* name) : AsCluster(father, name)
25 {
26   XBT_DEBUG("Creating a new fat tree.");
27 }
28
29 AsClusterFatTree::~AsClusterFatTree()
30 {
31   for (unsigned int i = 0; i < this->nodes_.size(); i++) {
32     delete this->nodes_[i];
33   }
34   for (unsigned int i = 0; i < this->links_.size(); i++) {
35     delete this->links_[i];
36   }
37 }
38
39 bool AsClusterFatTree::isInSubTree(FatTreeNode* root, FatTreeNode* node)
40 {
41   XBT_DEBUG("Is %d(%u,%u) in the sub tree of %d(%u,%u) ?", node->id, node->level, node->position, root->id, root->level,
42             root->position);
43   if (root->level <= node->level) {
44     return false;
45   }
46   for (unsigned int i = 0; i < node->level; i++) {
47     if (root->label[i] != node->label[i]) {
48       return false;
49     }
50   }
51
52   for (unsigned int i = root->level; i < this->levels_; i++) {
53     if (root->label[i] != node->label[i]) {
54       return false;
55     }
56   }
57   return true;
58 }
59
60 void AsClusterFatTree::getLocalRoute(NetCard* src, NetCard* dst, sg_platf_route_cbarg_t into, double* latency)
61 {
62
63   if (dst->isRouter() || src->isRouter())
64     return;
65
66   /* Let's find the source and the destination in our internal structure */
67   auto searchedNode = this->computeNodes_.find(src->id());
68   xbt_assert(searchedNode != this->computeNodes_.end(), "Could not find the source %s [%d] in the fat tree",
69              src->name().c_str(), src->id());
70   FatTreeNode* source = searchedNode->second;
71
72   searchedNode = this->computeNodes_.find(dst->id());
73   xbt_assert(searchedNode != this->computeNodes_.end(), "Could not find the destination %s [%d] in the fat tree",
74              dst->name().c_str(), dst->id());
75   FatTreeNode* destination = searchedNode->second;
76
77   XBT_VERB("Get route and latency from '%s' [%d] to '%s' [%d] in a fat tree", src->name().c_str(), src->id(),
78            dst->name().c_str(), dst->id());
79
80   /* In case destination is the source, and there is a loopback, let's use it instead of going up to a switch */
81   if (source->id == destination->id && this->hasLoopback_) {
82     into->link_list->push_back(source->loopback);
83     if (latency)
84       *latency += source->loopback->latency();
85     return;
86   }
87
88   FatTreeNode* currentNode = source;
89
90   // up part
91   while (!isInSubTree(currentNode, destination)) {
92     int d = destination->position; // as in d-mod-k
93
94     for (unsigned int i = 0; i < currentNode->level; i++)
95       d /= this->upperLevelNodesNumber_[i];
96
97     int k = this->upperLevelNodesNumber_[currentNode->level];
98     d     = d % k;
99     into->link_list->push_back(currentNode->parents[d]->upLink);
100
101     if (latency)
102       *latency += currentNode->parents[d]->upLink->latency();
103
104     if (this->hasLimiter_)
105       into->link_list->push_back(currentNode->limiterLink);
106     currentNode = currentNode->parents[d]->upNode;
107   }
108
109   XBT_DEBUG("%d(%u,%u) is in the sub tree of %d(%u,%u).", destination->id, destination->level, destination->position,
110             currentNode->id, currentNode->level, currentNode->position);
111
112   // Down part
113   while (currentNode != destination) {
114     for (unsigned int i = 0; i < currentNode->children.size(); i++) {
115       if (i % this->lowerLevelNodesNumber_[currentNode->level - 1] == destination->label[currentNode->level - 1]) {
116         into->link_list->push_back(currentNode->children[i]->downLink);
117         if (latency)
118           *latency += currentNode->children[i]->downLink->latency();
119         currentNode = currentNode->children[i]->downNode;
120         if (this->hasLimiter_)
121           into->link_list->push_back(currentNode->limiterLink);
122         XBT_DEBUG("%d(%u,%u) is accessible through %d(%u,%u)", destination->id, destination->level,
123                   destination->position, currentNode->id, 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::seal()
133 {
134   if (this->levels_ == 0) {
135     return;
136   }
137   this->generateSwitches();
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 << "," << this->nodes_[i]->position << ") ";
153     }
154     XBT_DEBUG("%s", msgBuffer.str().c_str());
155   }
156
157   this->generateLabels();
158
159   unsigned int k = 0;
160   // Nodes are totally ordered, by level and then by position, in this->nodes
161   for (unsigned int i = 0; i < this->levels_; i++) {
162     for (unsigned int j = 0; j < this->nodesByLevel_[i]; j++) {
163       this->connectNodeToParents(this->nodes_[k]);
164       k++;
165     }
166   }
167
168   if (XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
169     std::stringstream msgBuffer;
170     msgBuffer << "Links are : ";
171     for (unsigned int i = 0; i < this->links_.size(); i++) {
172       msgBuffer << "(" << this->links_[i]->upNode->id << "," << this->links_[i]->downNode->id << ") ";
173     }
174     XBT_DEBUG("%s", msgBuffer.str().c_str());
175   }
176 }
177
178 int AsClusterFatTree::connectNodeToParents(FatTreeNode* node)
179 {
180   std::vector<FatTreeNode*>::iterator currentParentNode = this->nodes_.begin();
181   int connectionsNumber                                 = 0;
182   const int level                                       = node->level;
183   XBT_DEBUG("We are connecting node %d(%u,%u) to his parents.", node->id, node->level, node->position);
184   currentParentNode += this->getLevelPosition(level + 1);
185   for (unsigned int i = 0; i < this->nodesByLevel_[level + 1]; i++) {
186     if (this->areRelated(*currentParentNode, node)) {
187       XBT_DEBUG("%d(%u,%u) and %d(%u,%u) are related,"
188                 " with %u links between them.",
189                 node->id, node->level, node->position, (*currentParentNode)->id, (*currentParentNode)->level,
190                 (*currentParentNode)->position, this->lowerLevelPortsNumber_[level]);
191       for (unsigned int j = 0; j < this->lowerLevelPortsNumber_[level]; j++) {
192         this->addLink(*currentParentNode, node->label[level] + j * this->lowerLevelNodesNumber_[level], node,
193                       (*currentParentNode)->label[level] + j * this->upperLevelNodesNumber_[level]);
194       }
195       connectionsNumber++;
196     }
197     ++currentParentNode;
198   }
199   return connectionsNumber;
200 }
201
202 bool AsClusterFatTree::areRelated(FatTreeNode* parent, FatTreeNode* child)
203 {
204   std::stringstream msgBuffer;
205
206   if (XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
207     msgBuffer << "Are " << child->id << "(" << child->level << "," << child->position << ") <";
208
209     for (unsigned int i = 0; i < this->levels_; i++) {
210       msgBuffer << child->label[i] << ",";
211     }
212     msgBuffer << ">";
213
214     msgBuffer << " and " << parent->id << "(" << parent->level << "," << parent->position << ") <";
215     for (unsigned int i = 0; i < this->levels_; i++) {
216       msgBuffer << parent->label[i] << ",";
217     }
218     msgBuffer << ">";
219     msgBuffer << " related ? ";
220     XBT_DEBUG("%s", msgBuffer.str().c_str());
221   }
222   if (parent->level != child->level + 1) {
223     return false;
224   }
225
226   for (unsigned int i = 0; i < this->levels_; i++) {
227     if (parent->label[i] != child->label[i] && i + 1 != parent->level) {
228       return false;
229     }
230   }
231   return true;
232 }
233
234 void AsClusterFatTree::generateSwitches()
235 {
236   XBT_DEBUG("Generating switches.");
237   this->nodesByLevel_.resize(this->levels_ + 1, 0);
238   unsigned int nodesRequired = 0;
239
240   // Take care of the number of nodes by level
241   this->nodesByLevel_[0] = 1;
242   for (unsigned int i = 0; i < this->levels_; i++)
243     this->nodesByLevel_[0] *= this->lowerLevelNodesNumber_[i];
244
245   if (this->nodesByLevel_[0] != this->nodes_.size()) {
246     surf_parse_error("The number of provided nodes does not fit with the wanted topology."
247                      " Please check your platform description (We need %d nodes, we got %zu)",
248                      this->nodesByLevel_[0], this->nodes_.size());
249     return;
250   }
251
252   for (unsigned int i = 0; i < this->levels_; i++) {
253     int nodesInThisLevel = 1;
254
255     for (unsigned int j = 0; j <= i; j++)
256       nodesInThisLevel *= this->upperLevelNodesNumber_[j];
257
258     for (unsigned int j = i + 1; j < this->levels_; j++)
259       nodesInThisLevel *= this->lowerLevelNodesNumber_[j];
260
261     this->nodesByLevel_[i + 1] = nodesInThisLevel;
262     nodesRequired += nodesInThisLevel;
263   }
264
265   // Create the switches
266   int k = 0;
267   for (unsigned int i = 0; i < this->levels_; i++) {
268     for (unsigned int j = 0; j < this->nodesByLevel_[i + 1]; j++) {
269       FatTreeNode* newNode = new FatTreeNode(this->cluster_, --k, i + 1, j);
270       XBT_DEBUG("We create the switch %d(%d,%d)", newNode->id, newNode->level, newNode->position);
271       newNode->children.resize(this->lowerLevelNodesNumber_[i] * this->lowerLevelPortsNumber_[i]);
272       if (i != this->levels_ - 1) {
273         newNode->parents.resize(this->upperLevelNodesNumber_[i + 1] * this->lowerLevelPortsNumber_[i + 1]);
274       }
275       newNode->label.resize(this->levels_);
276       this->nodes_.push_back(newNode);
277     }
278   }
279 }
280
281 void AsClusterFatTree::generateLabels()
282 {
283   XBT_DEBUG("Generating labels.");
284   // TODO : check if nodesByLevel and nodes are filled
285   std::vector<int> maxLabel(this->levels_);
286   std::vector<int> currentLabel(this->levels_);
287   unsigned int k = 0;
288   for (unsigned int i = 0; i <= this->levels_; i++) {
289     currentLabel.assign(this->levels_, 0);
290     for (unsigned int j = 0; j < this->levels_; j++) {
291       maxLabel[j] = j + 1 > i ? this->lowerLevelNodesNumber_[j] : this->upperLevelNodesNumber_[j];
292     }
293
294     for (unsigned int j = 0; j < this->nodesByLevel_[i]; j++) {
295
296       if (XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
297         std::stringstream msgBuffer;
298
299         msgBuffer << "Assigning label <";
300         for (unsigned int l = 0; l < this->levels_; l++) {
301           msgBuffer << currentLabel[l] << ",";
302         }
303         msgBuffer << "> to " << k << " (" << i << "," << j << ")";
304
305         XBT_DEBUG("%s", msgBuffer.str().c_str());
306       }
307       this->nodes_[k]->label.assign(currentLabel.begin(), currentLabel.end());
308
309       bool remainder   = true;
310       unsigned int pos = 0;
311       while (remainder && pos < this->levels_) {
312         ++currentLabel[pos];
313         if (currentLabel[pos] >= maxLabel[pos]) {
314           currentLabel[pos] = 0;
315           remainder         = true;
316           ++pos;
317         } else {
318           pos       = 0;
319           remainder = false;
320         }
321       }
322       k++;
323     }
324   }
325 }
326
327 int AsClusterFatTree::getLevelPosition(const unsigned int level)
328 {
329   xbt_assert(level <= this->levels_, "The impossible did happen. Yet again.");
330   int tempPosition = 0;
331
332   for (unsigned int i = 0; i < level; i++)
333     tempPosition += this->nodesByLevel_[i];
334
335   return tempPosition;
336 }
337
338 void AsClusterFatTree::addProcessingNode(int id)
339 {
340   using std::make_pair;
341   static int position = 0;
342   FatTreeNode* newNode;
343   newNode = new FatTreeNode(this->cluster_, id, 0, position++);
344   newNode->parents.resize(this->upperLevelNodesNumber_[0] * this->lowerLevelPortsNumber_[0]);
345   newNode->label.resize(this->levels_);
346   this->computeNodes_.insert(make_pair(id, newNode));
347   this->nodes_.push_back(newNode);
348 }
349
350 void AsClusterFatTree::addLink(FatTreeNode* parent, unsigned int parentPort, FatTreeNode* child, unsigned int childPort)
351 {
352   FatTreeLink* newLink;
353   newLink = new FatTreeLink(this->cluster_, child, parent);
354   XBT_DEBUG("Creating a link between the parent (%d,%d,%u) and the child (%d,%d,%u)", parent->level, parent->position,
355             parentPort, child->level, child->position, childPort);
356   parent->children[parentPort] = newLink;
357   child->parents[childPort]    = newLink;
358
359   this->links_.push_back(newLink);
360 }
361
362 void AsClusterFatTree::parse_specific_arguments(sg_platf_cluster_cbarg_t cluster)
363 {
364   std::vector<std::string> parameters;
365   std::vector<std::string> tmp;
366   boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
367
368   // TODO : we have to check for zeros and negative numbers, or it might crash
369   if (parameters.size() != 4) {
370     surf_parse_error(
371         "Fat trees are defined by the levels number and 3 vectors, see the documentation for more information");
372   }
373
374   // The first parts of topo_parameters should be the levels number
375   this->levels_ = xbt_str_parse_int(parameters[0].c_str(), "First parameter is not the amount of levels: %s");
376
377   // Then, a l-sized vector standing for the children number by level
378   boost::split(tmp, parameters[1], boost::is_any_of(","));
379   if (tmp.size() != this->levels_) {
380     surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
381                      ", see the documentation for more information");
382   }
383   for (size_t i = 0; i < tmp.size(); i++) {
384     this->lowerLevelNodesNumber_.push_back(xbt_str_parse_int(tmp[i].c_str(), "Invalid lower level node number: %s"));
385   }
386
387   // Then, a l-sized vector standing for the parents number by level
388   boost::split(tmp, parameters[2], boost::is_any_of(","));
389   if (tmp.size() != this->levels_) {
390     surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
391                      ", see the documentation for more information");
392   }
393   for (size_t i = 0; i < tmp.size(); i++) {
394     this->upperLevelNodesNumber_.push_back(xbt_str_parse_int(tmp[i].c_str(), "Invalid upper level node number: %s"));
395   }
396
397   // Finally, a l-sized vector standing for the ports number with the lower level
398   boost::split(tmp, parameters[3], boost::is_any_of(","));
399   if (tmp.size() != this->levels_) {
400     surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
401                      ", see the documentation for more information");
402   }
403   for (size_t i = 0; i < tmp.size(); i++) {
404     this->lowerLevelPortsNumber_.push_back(xbt_str_parse_int(tmp[i].c_str(), "Invalid lower level node number: %s"));
405   }
406   this->cluster_ = cluster;
407 }
408
409 void AsClusterFatTree::generateDotFile(const std::string& filename) const
410 {
411   std::ofstream file;
412   file.open(filename, std::ios::out | std::ios::trunc);
413   xbt_assert(file.is_open(), "Unable to open file %s", filename.c_str());
414
415   file << "graph AsClusterFatTree {\n";
416   for (unsigned int i = 0; i < this->nodes_.size(); i++) {
417     file << this->nodes_[i]->id;
418     if (this->nodes_[i]->id < 0)
419       file << " [shape=circle];\n";
420     else
421       file << " [shape=hexagon];\n";
422   }
423
424   for (unsigned int i = 0; i < this->links_.size(); i++) {
425     file << this->links_[i]->downNode->id << " -- " << this->links_[i]->upNode->id << ";\n";
426   }
427   file << "}";
428   file.close();
429 }
430
431 FatTreeNode::FatTreeNode(sg_platf_cluster_cbarg_t cluster, int id, int level, int position)
432     : id(id), level(level), position(position)
433 {
434   s_sg_platf_link_cbarg_t linkTemplate;
435   if (cluster->limiter_link) {
436     memset(&linkTemplate, 0, sizeof(linkTemplate));
437     linkTemplate.bandwidth = cluster->limiter_link;
438     linkTemplate.latency   = 0;
439     linkTemplate.policy    = SURF_LINK_SHARED;
440     linkTemplate.id        = bprintf("limiter_%d", id);
441     sg_platf_new_link(&linkTemplate);
442     this->limiterLink = Link::byName(linkTemplate.id);
443     free((void*)linkTemplate.id);
444   }
445   if (cluster->loopback_bw || cluster->loopback_lat) {
446     memset(&linkTemplate, 0, sizeof(linkTemplate));
447     linkTemplate.bandwidth = cluster->loopback_bw;
448     linkTemplate.latency   = cluster->loopback_lat;
449     linkTemplate.policy    = SURF_LINK_FATPIPE;
450     linkTemplate.id        = bprintf("loopback_%d", id);
451     sg_platf_new_link(&linkTemplate);
452     this->loopback = Link::byName(linkTemplate.id);
453     free((void*)linkTemplate.id);
454   }
455 }
456
457 FatTreeLink::FatTreeLink(sg_platf_cluster_cbarg_t cluster, FatTreeNode* downNode, FatTreeNode* upNode)
458     : upNode(upNode), downNode(downNode)
459 {
460   static int uniqueId = 0;
461   s_sg_platf_link_cbarg_t linkTemplate;
462   memset(&linkTemplate, 0, sizeof(linkTemplate));
463   linkTemplate.bandwidth = cluster->bw;
464   linkTemplate.latency   = cluster->lat;
465   linkTemplate.policy    = cluster->sharing_policy; // sthg to do with that ?
466   linkTemplate.id        = bprintf("link_from_%d_to_%d_%d", downNode->id, upNode->id, uniqueId);
467   sg_platf_new_link(&linkTemplate);
468   Link* link;
469   std::string tmpID;
470   if (cluster->sharing_policy == SURF_LINK_FULLDUPLEX) {
471     tmpID          = std::string(linkTemplate.id) + "_UP";
472     link           = Link::byName(tmpID.c_str());
473     this->upLink   = link; // check link?
474     tmpID          = std::string(linkTemplate.id) + "_DOWN";
475     link           = Link::byName(tmpID.c_str());
476     this->downLink = link; // check link ?
477   } else {
478     link           = Link::byName(linkTemplate.id);
479     this->upLink   = link;
480     this->downLink = link;
481   }
482   uniqueId++;
483   free((void*)linkTemplate.id);
484 }
485 }
486 }
487 } // namespace