Logo AND Algorithmique Numérique Distribuée

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