Logo AND Algorithmique Numérique Distribuée

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