Logo AND Algorithmique Numérique Distribuée

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