Logo AND Algorithmique Numérique Distribuée

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