Logo AND Algorithmique Numérique Distribuée

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