Logo AND Algorithmique Numérique Distribuée

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