Logo AND Algorithmique Numérique Distribuée

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