Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
At most one resource allocation per statement (sonar).
[simgrid.git] / src / kernel / routing / TorusZone.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 "simgrid/kernel/routing/TorusZone.hpp"
7 #include "simgrid/kernel/routing/NetPoint.hpp"
8 #include "src/surf/network_interface.hpp"
9 #include "src/surf/xml/platf_private.hpp"
10
11 #include <boost/algorithm/string/classification.hpp>
12 #include <boost/algorithm/string/split.hpp>
13 #include <string>
14 #include <vector>
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_cluster_torus, surf_route_cluster, "Torus Routing part of surf");
17
18 namespace simgrid {
19 namespace kernel {
20 namespace routing {
21 TorusZone::TorusZone(NetZoneImpl* father, const std::string& name, resource::NetworkModel* netmodel)
22     : ClusterZone(father, name, netmodel)
23 {
24 }
25
26 void TorusZone::create_links_for_node(ClusterCreationArgs* cluster, int id, int rank, unsigned int position)
27 {
28   /* Create all links that exist in the torus. Each rank creates @a dimensions-1 links */
29   int dim_product = 1; // Needed to calculate the next neighbor_id
30
31   for (unsigned int j = 0; j < dimensions_.size(); j++) {
32     LinkCreationArgs link;
33     int current_dimension = dimensions_[j]; // which dimension are we currently in?
34                                             // we need to iterate over all dimensions and create all links there
35     // The other node the link connects
36     int neighbor_rank_id = ((rank / dim_product) % current_dimension == current_dimension - 1)
37                                ? rank - (current_dimension - 1) * dim_product
38                                : rank + dim_product;
39     // name of neighbor is not right for non contiguous cluster radicals (as id != rank in this case)
40     std::string link_id =
41         std::string(cluster->id) + "_link_from_" + std::to_string(id) + "_to_" + std::to_string(neighbor_rank_id);
42     link.id        = link_id;
43     link.bandwidths.push_back(cluster->bw);
44     link.latency   = cluster->lat;
45     link.policy    = cluster->sharing_policy;
46     sg_platf_new_link(&link);
47     resource::LinkImpl* linkUp;
48     resource::LinkImpl* linkDown;
49     if (link.policy == s4u::Link::SharingPolicy::SPLITDUPLEX) {
50       linkUp   = s4u::Link::by_name(link_id + "_UP")->get_impl();
51       linkDown = s4u::Link::by_name(link_id + "_DOWN")->get_impl();
52     } else {
53       linkUp   = s4u::Link::by_name(link_id)->get_impl();
54       linkDown = linkUp;
55     }
56     /*
57      * Add the link to its appropriate position.
58      * Note that position rankId*(xbt_dynar_length(dimensions)+has_loopback?+has_limiter?)
59      * holds the link "rankId->rankId"
60      */
61     private_links_.insert({position + j, {linkUp, linkDown}});
62     dim_product *= current_dimension;
63   }
64 }
65
66 void TorusZone::parse_specific_arguments(ClusterCreationArgs* cluster)
67 {
68   std::vector<std::string> dimensions;
69   boost::split(dimensions, cluster->topo_parameters, boost::is_any_of(","));
70
71   if (not dimensions.empty()) {
72     /* We are in a torus cluster
73      * Parse attribute dimensions="dim1,dim2,dim3,...,dimN" and save them into a vector.
74      * Additionally, we need to know how many ranks we have in total
75      */
76     for (auto const& group : dimensions)
77       dimensions_.push_back(surf_parse_get_int(group));
78
79     num_links_per_node_ = dimensions_.size();
80   }
81 }
82
83 void TorusZone::get_local_route(NetPoint* src, NetPoint* dst, RouteCreationArgs* route, double* lat)
84 {
85   XBT_VERB("torus getLocalRoute from '%s'[%u] to '%s'[%u]", src->get_cname(), src->id(), dst->get_cname(), dst->id());
86
87   if (dst->is_router() || src->is_router())
88     return;
89
90   if (src->id() == dst->id() && has_loopback_) {
91     std::pair<resource::LinkImpl*, resource::LinkImpl*> info = private_links_.at(src->id() * num_links_per_node_);
92
93     route->link_list.push_back(info.first);
94     if (lat)
95       *lat += info.first->get_latency();
96     return;
97   }
98
99   /*
100    * Dimension based routing routes through each dimension consecutively
101    * TODO Change to dynamic assignment
102    */
103
104   /*
105    * Arrays that hold the coordinates of the current node and the target; comparing the values at the i-th position of
106    * both arrays, we can easily assess whether we need to route into this dimension or not.
107    */
108   const unsigned int dsize = dimensions_.size();
109   std::vector<unsigned int> myCoords(dsize);
110   std::vector<unsigned int> targetCoords(dsize);
111   unsigned int dim_size_product = 1;
112   for (unsigned i = 0; i < dsize; i++) {
113     unsigned cur_dim_size = dimensions_[i];
114     myCoords[i]           = (src->id() / dim_size_product) % cur_dim_size;
115     targetCoords[i]       = (dst->id() / dim_size_product) % cur_dim_size;
116     dim_size_product *= cur_dim_size;
117   }
118
119   /*
120    * linkOffset describes the offset where the link we want to use is stored(+1 is added because each node has a link
121    * from itself to itself, which can only be the case if src->m_id == dst->m_id -- see above for this special case)
122    */
123   int nodeOffset = (dsize + 1) * src->id();
124
125   int linkOffset  = nodeOffset;
126   bool use_lnk_up = false; // Is this link of the form "cur -> next" or "next -> cur"? false means: next -> cur
127   unsigned int current_node = src->id();
128   while (current_node != dst->id()) {
129     unsigned int next_node   = 0;
130     unsigned int dim_product = 1; // First, we will route in x-dimension
131     for (unsigned j = 0; j < dsize; j++) {
132       const unsigned cur_dim = dimensions_[j];
133       // current_node/dim_product = position in current dimension
134       if ((current_node / dim_product) % cur_dim != (dst->id() / dim_product) % cur_dim) {
135         if ((targetCoords[j] > myCoords[j] &&
136              targetCoords[j] <= myCoords[j] + cur_dim / 2) // Is the target node on the right, without the wrap-around?
137             || (myCoords[j] > cur_dim / 2 &&
138                 (myCoords[j] + cur_dim / 2) % cur_dim >=
139                     targetCoords[j])) { // Or do we need to use the wrap around to reach it?
140           if ((current_node / dim_product) % cur_dim == cur_dim - 1)
141             next_node = (current_node + dim_product - dim_product * cur_dim);
142           else
143             next_node = (current_node + dim_product);
144
145           // HERE: We use *CURRENT* node for calculation (as opposed to next_node)
146           nodeOffset = current_node * (num_links_per_node_);
147           linkOffset = nodeOffset + (has_loopback_ ? 1 : 0) + (has_limiter_ ? 1 : 0) + j;
148           use_lnk_up = true;
149           assert(linkOffset >= 0);
150         } else { // Route to the left
151           if ((current_node / dim_product) % cur_dim == 0)
152             next_node = (current_node - dim_product + dim_product * cur_dim);
153           else
154             next_node = (current_node - dim_product);
155
156           // HERE: We use *next* node for calculation (as opposed to current_node!)
157           nodeOffset = next_node * (num_links_per_node_);
158           linkOffset = nodeOffset + j + (has_loopback_ ? 1 : 0) + (has_limiter_ ? 1 : 0);
159           use_lnk_up = false;
160
161           assert(linkOffset >= 0);
162         }
163         XBT_DEBUG("torus_get_route_and_latency - current_node: %u, next_node: %u, linkOffset is %i", current_node,
164                   next_node, linkOffset);
165         break;
166       }
167
168       dim_product *= cur_dim;
169     }
170
171     std::pair<resource::LinkImpl*, resource::LinkImpl*> info;
172
173     if (has_limiter_) { // limiter for sender
174       info = private_links_.at(nodeOffset + (has_loopback_ ? 1 : 0));
175       route->link_list.push_back(info.first);
176     }
177
178     info = private_links_.at(linkOffset);
179     resource::LinkImpl* lnk = use_lnk_up ? info.first : info.second;
180
181     route->link_list.push_back(lnk);
182     if (lat)
183       *lat += lnk->get_latency();
184
185     current_node = next_node;
186   }
187 }
188 } // namespace routing
189 } // namespace kernel
190 } // namespace simgrid