1 #include "surf_routing_cluster_torus.hpp"
3 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_cluster_torus, surf_route_cluster, "Torus Routing part of surf");
6 inline unsigned int* rankId_to_coords(int rankId, xbt_dynar_t dimensions) {
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;
20 AS_t model_torus_cluster_create(void)
22 return new AsClusterTorus();
25 /* Creation routing model functions */
26 AsClusterTorus::AsClusterTorus() : AsCluster()
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;
38 * Create all links that exist in the torus.
39 * Each rank creates #dimensions-1 links
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++) {
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);
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);
63 xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
65 tmp_link = bprintf("%s_DOWN", link_id);
67 xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
70 info.link_up = xbt_lib_get_or_null(link_lib, link_id, SURF_LINK_LEVEL);
71 info.link_down = info.link_up;
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"
78 xbt_dynar_set(p_linkUpDownList, position
81 dim_product *= current_dimension;
87 void AsClusterTorus::parse_specific_arguments(sg_platf_cluster_cbarg_t cluster){
91 p_dimensions = xbt_str_split(cluster->topo_parameters, ",");
93 if (!xbt_dynar_is_empty(p_dimensions)) {
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
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);
105 p_nb_links_per_node = xbt_dynar_length(p_dimensions);
110 void AsClusterTorus::getRouteAndLatency(RoutingEdgePtr src, RoutingEdgePtr dst, sg_platf_route_cbarg_t route, double *lat){
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);
116 if (dst->p_rcType == SURF_NETWORK_ELEMENT_ROUTER || src->p_rcType == SURF_NETWORK_ELEMENT_ROUTER) return;
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);
123 *lat += static_cast<NetworkLinkPtr>(info.link_up)->getLatency();
129 * Dimension based routing routes through each dimension consecutively
130 * TODO Change to dynamic assignment
132 unsigned int j, cur_dim, dim_product = 1;
133 long current_node = src->m_id;
134 long unsigned next_node = 0;
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.
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);
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)
151 long nodeOffset = (xbt_dynar_length(p_dimensions)+1)*src->m_id;
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);
161 // current_node/dim_product = position in current dimension
162 if ((current_node/dim_product) % cur_dim != (dst->m_id/dim_product) % cur_dim) {
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);
169 next_node = (current_node+dim_product);
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;
175 assert(linkOffset >= 0);
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);
181 next_node = (current_node-dim_product);
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;
188 assert(linkOffset >= 0);
190 XBT_DEBUG("torus_get_route_and_latency - current_node: %lu, next_node: %lu, linkOffset is %lu",
191 current_node, next_node, linkOffset);
196 dim_product *= cur_dim;
199 s_surf_parsing_link_up_down_t info;
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);
206 info = xbt_dynar_get_as(p_linkUpDownList,linkOffset, s_surf_parsing_link_up_down_t);
208 if (use_lnk_up == false)
209 xbt_dynar_push_as(route->link_list,void*,info.link_down);
211 xbt_dynar_push_as(route->link_list,void*,info.link_up);
213 current_node = next_node;