Logo AND Algorithmique Numérique Distribuée

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