Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
change log level for a message
[simgrid.git] / src / surf / surf_routing_cluster_torus.cpp
1 #include "surf_routing_cluster_torus.hpp"
2
3 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_cluster_torus, surf_route_cluster, "Torus Routing part of surf");
4
5
6 inline unsigned int* rankId_to_coords(int rankId, xbt_dynar_t dimensions) {
7
8     unsigned int i = 0, cur_dim_size = 1, dim_size_product = 1;
9     unsigned int* coords = (unsigned int*)malloc(xbt_dynar_length(dimensions)*sizeof(unsigned int));
10     for (i = 0; i < xbt_dynar_length(dimensions); i++) {
11         cur_dim_size = xbt_dynar_get_as(dimensions, i, int);
12         coords[i] = (rankId / dim_size_product) % cur_dim_size;
13         dim_size_product *= cur_dim_size;
14     }
15
16     return coords;
17 }
18
19
20 AS_t model_torus_cluster_create(void)
21 {
22   return new AsClusterTorus();
23 }
24
25 /* Creation routing model functions */
26 AsClusterTorus::AsClusterTorus() : AsCluster()
27 {
28   p_dimensions = NULL;
29 }
30
31
32
33 void AsClusterTorus::create_links_for_node(sg_platf_cluster_cbarg_t cluster, int id, int rank, int position){
34   s_sg_platf_link_cbarg_t link;
35   char* link_id;
36   unsigned int j = 0;
37   /**
38    * Create all links that exist in the torus.
39    * Each rank creates #dimensions-1 links
40    */
41   int neighbour_rank_id = 0; // The other node the link connects
42   int current_dimension = 0, // which dimension are we currently in?
43       // we need to iterate over all dimensions
44       // and create all links there
45       dim_product       = 1; // Needed to calculate the next neighbour_id
46   for (j = 0; j < xbt_dynar_length(p_dimensions); j++) {
47
48       memset(&link, 0, sizeof(link));
49       current_dimension = xbt_dynar_get_as(p_dimensions, j, int);
50       neighbour_rank_id = ( ((int) rank / dim_product) % current_dimension == current_dimension-1) ? rank - (current_dimension-1)*dim_product : rank + dim_product;
51       //name of neighbour is not right for non contiguous cluster radicals (as id != rank in this case)
52       link_id           = bprintf("%s_link_from_%i_to_%i", cluster->id, id, neighbour_rank_id);
53       link.id           = link_id;
54       link.bandwidth    = cluster->bw;
55       link.latency      = cluster->lat;
56       link.state        = SURF_RESOURCE_ON;
57       link.policy       = cluster->sharing_policy;
58       sg_platf_new_link(&link);
59       s_surf_parsing_link_up_down_t info;
60       if (link.policy == SURF_LINK_FULLDUPLEX) {
61           char *tmp_link = bprintf("%s_UP", link_id);
62           info.link_up =
63               xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
64           free(tmp_link);
65           tmp_link = bprintf("%s_DOWN", link_id);
66           info.link_down =
67               xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
68           free(tmp_link);
69       } else {
70           info.link_up = xbt_lib_get_or_null(link_lib, link_id, SURF_LINK_LEVEL);
71           info.link_down = info.link_up;
72       }
73       /**
74        * Add the link to its appropriate position;
75        * note that position rankId*(xbt_dynar_length(dimensions)+has_loopack?+has_limiter?)
76        * holds the link "rankId->rankId"
77        */
78       xbt_dynar_set(p_linkUpDownList, position
79           + j,
80           &info);
81       dim_product   *= current_dimension;
82       xbt_free(link_id);
83   }
84   rank++;
85 }
86
87 void AsClusterTorus::parse_specific_arguments(sg_platf_cluster_cbarg_t cluster){
88
89   unsigned int iter;
90   char *groups;
91   p_dimensions = xbt_str_split(cluster->topo_parameters, ",");
92
93     if (!xbt_dynar_is_empty(p_dimensions)) {
94       /**
95        * We are in a torus cluster
96        * Parse attribute dimensions="dim1,dim2,dim3,...,dimN"
97        * and safe it in a dynarray.
98        * Additionally, we need to know how many ranks we have in total
99        */
100       xbt_dynar_foreach(p_dimensions, iter, groups) {
101           int tmp = surf_parse_get_int(xbt_dynar_get_as(p_dimensions, iter, char *));
102           xbt_dynar_set_as(p_dimensions, iter, int, tmp);
103       }
104
105       p_nb_links_per_node = xbt_dynar_length(p_dimensions);
106
107     }
108 }
109
110 void AsClusterTorus::getRouteAndLatency(RoutingEdgePtr src, RoutingEdgePtr dst, sg_platf_route_cbarg_t route, double *lat){
111
112   XBT_VERB("torus_get_route_and_latency from '%s'[%d] to '%s'[%d]",
113                src->p_name,src->m_id,
114                dst->p_name,dst->m_id);
115
116      if (dst->p_rcType == SURF_NETWORK_ELEMENT_ROUTER || src->p_rcType == SURF_NETWORK_ELEMENT_ROUTER) return;
117
118      if((src->m_id == dst->m_id) && p_has_loopback  ){
119        s_surf_parsing_link_up_down_t info = xbt_dynar_get_as(p_linkUpDownList, src->m_id * p_nb_links_per_node, s_surf_parsing_link_up_down_t);
120        xbt_dynar_push_as(route->link_list, void *, info.link_up);
121
122        if (lat)
123          *lat += static_cast<NetworkLinkPtr>(info.link_up)->getLatency();
124        return;
125      }
126
127
128      /**
129       * Dimension based routing routes through each dimension consecutively
130       * TODO Change to dynamic assignment
131       */
132      unsigned int j, cur_dim, dim_product   = 1;
133      long current_node    = src->m_id;
134      long unsigned next_node       = 0;
135      /**
136       * Arrays that hold the coordinates of the current node and
137       * the target; comparing the values at the i-th position of
138       * both arrays, we can easily assess whether we need to route
139       * into this dimension or not.
140       */
141      unsigned int* myCoords, *targetCoords;
142      myCoords     = rankId_to_coords(src->m_id, p_dimensions);
143      targetCoords = rankId_to_coords(dst->m_id, p_dimensions);
144      /**
145       * linkOffset describes the offset where the link
146       * we want to use is stored
147       * (+1 is added because each node has a link from itself to itself,
148       * which can only be the case if src->m_id == dst->m_id -- see above
149       * for this special case)
150       */
151      long nodeOffset = (xbt_dynar_length(p_dimensions)+1)*src->m_id;
152
153      long linkOffset = nodeOffset;
154      bool use_lnk_up          = false; // Is this link of the form "cur -> next" or "next -> cur"?
155                                        // false means: next -> cur
156      while (current_node != dst->m_id) {
157        dim_product = 1; // First, we will route in x-dimension
158        for (j = 0; j < xbt_dynar_length(p_dimensions); j++) {
159            cur_dim = xbt_dynar_get_as(p_dimensions, j, int);
160
161            // current_node/dim_product = position in current dimension
162            if ((current_node/dim_product) % cur_dim != (dst->m_id/dim_product) % cur_dim) {
163
164                if (( targetCoords[j] > myCoords[j] && targetCoords[j] <= myCoords[j]+cur_dim/2) // Is the target node on the right, without the wrap-around?
165                    || ( myCoords[j] > cur_dim/2 && (myCoords[j]+cur_dim/2)%cur_dim >= targetCoords[j] )) { // Or do we need to use the wrap around to reach it?
166                  if ((current_node / dim_product) % cur_dim == cur_dim-1)
167                      next_node = (current_node+dim_product-dim_product*cur_dim);
168                  else
169                      next_node = (current_node+dim_product);
170
171                  // HERE: We use *CURRENT* node for calculation (as opposed to next_node)
172                  nodeOffset = current_node*(p_nb_links_per_node);
173                  linkOffset = nodeOffset+p_has_loopback+p_has_limiter+j;
174                  use_lnk_up = true;
175                  assert(linkOffset >= 0);
176                }
177                else { // Route to the left
178                  if ((current_node / dim_product) % cur_dim == 0)
179                      next_node = (current_node-dim_product+dim_product*cur_dim);
180                  else
181                      next_node = (current_node-dim_product);
182
183                  // HERE: We use *next* node for calculation (as opposed to current_node!)
184                  nodeOffset = next_node*(p_nb_links_per_node);
185                  linkOffset = nodeOffset+j+p_has_loopback+p_has_limiter;
186                  use_lnk_up = false;
187
188                  assert(linkOffset >= 0);
189                }
190                XBT_DEBUG("torus_get_route_and_latency - current_node: %lu, next_node: %lu, linkOffset is %lu",
191                current_node, next_node, linkOffset);
192
193                break;
194            }
195
196            dim_product *= cur_dim;
197        }
198
199        s_surf_parsing_link_up_down_t info;
200
201        if (p_has_limiter){          // limiter for sender
202              info = xbt_dynar_get_as(p_linkUpDownList, nodeOffset + p_has_loopback, s_surf_parsing_link_up_down_t);
203              xbt_dynar_push_as(route->link_list, void *, info.link_up);
204        }
205
206        info = xbt_dynar_get_as(p_linkUpDownList,linkOffset, s_surf_parsing_link_up_down_t);
207
208        if (use_lnk_up == false)
209            xbt_dynar_push_as(route->link_list,void*,info.link_down);
210        else
211            xbt_dynar_push_as(route->link_list,void*,info.link_up);
212
213        current_node = next_node;
214        next_node = 0;
215      }
216      free(myCoords);
217      free(targetCoords);
218
219
220
221   return;
222 }