Logo AND Algorithmique Numérique Distribuée

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