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()
31 /* Creation routing model functions */
32 AsClusterTorus::~AsClusterTorus()
34 if(p_dimensions) xbt_dynar_free(&p_dimensions);
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;
43 * Create all links that exist in the torus.
44 * Each rank creates #dimensions-1 links
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++) {
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);
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);
68 xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
70 tmp_link = bprintf("%s_DOWN", link_id);
72 xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
75 info.link_up = xbt_lib_get_or_null(link_lib, link_id, SURF_LINK_LEVEL);
76 info.link_down = info.link_up;
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"
83 xbt_dynar_set(p_linkUpDownList, position
86 dim_product *= current_dimension;
92 void AsClusterTorus::parse_specific_arguments(sg_platf_cluster_cbarg_t cluster){
96 xbt_dynar_t dimensions = xbt_str_split(cluster->topo_parameters, ",");
98 if (!xbt_dynar_is_empty(dimensions)) {
99 p_dimensions= xbt_dynar_new(sizeof(int), NULL);
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
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);
111 p_nb_links_per_node = xbt_dynar_length(p_dimensions);
114 xbt_dynar_free(&dimensions);
117 void AsClusterTorus::getRouteAndLatency(RoutingEdgePtr src, RoutingEdgePtr dst, sg_platf_route_cbarg_t route, double *lat){
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);
123 if (dst->p_rcType == SURF_NETWORK_ELEMENT_ROUTER || src->p_rcType == SURF_NETWORK_ELEMENT_ROUTER) return;
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);
130 *lat += static_cast<NetworkLinkPtr>(info.link_up)->getLatency();
136 * Dimension based routing routes through each dimension consecutively
137 * TODO Change to dynamic assignment
139 unsigned int j, cur_dim, dim_product = 1;
140 int current_node = src->m_id;
141 int unsigned next_node = 0;
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.
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);
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)
158 int nodeOffset = (xbt_dynar_length(p_dimensions)+1)*src->m_id;
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);
168 // current_node/dim_product = position in current dimension
169 if ((current_node/dim_product) % cur_dim != (dst->m_id/dim_product) % cur_dim) {
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);
176 next_node = (current_node+dim_product);
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;
182 assert(linkOffset >= 0);
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);
188 next_node = (current_node-dim_product);
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;
195 assert(linkOffset >= 0);
197 XBT_DEBUG("torus_get_route_and_latency - current_node: %i, next_node: %u, linkOffset is %i",
198 current_node, next_node, linkOffset);
203 dim_product *= cur_dim;
206 s_surf_parsing_link_up_down_t info;
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);
213 info = xbt_dynar_get_as(p_linkUpDownList,linkOffset, s_surf_parsing_link_up_down_t);
215 if (use_lnk_up == false)
216 xbt_dynar_push_as(route->link_list,void*,info.link_down);
218 xbt_dynar_push_as(route->link_list,void*,info.link_up);
220 current_node = next_node;