1 /* Copyright (c) 2014-2016. The SimGrid Team. All rights reserved. */
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. */
6 #include "src/routing/AsClusterDragonfly.hpp"
7 #include "src/surf/network_interface.hpp"
8 #include "src/surf/xml/platf.hpp" // FIXME: move that back to the parsing area
10 #include <boost/algorithm/string/split.hpp>
11 #include <boost/algorithm/string/classification.hpp>
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_cluster_dragonfly, surf_route_cluster, "Dragonfly Routing part of surf");
18 AsClusterDragonfly::AsClusterDragonfly(const char*name)
22 AsClusterDragonfly::~AsClusterDragonfly() {
23 if(this->routers_ != nullptr){
24 for (unsigned int i=0; i<this->numGroups_*this->numChassisPerGroup_*this->numBladesPerChassis_;i++)
30 unsigned int *AsClusterDragonfly::rankId_to_coords(int rankId)
32 //coords : group, chassis, blade, node
33 unsigned int *coords = (unsigned int *) malloc(4 * sizeof(unsigned int));
34 coords[0] = rankId/ (numChassisPerGroup_*numBladesPerChassis_*numNodesPerBlade_);
35 rankId=rankId%(numChassisPerGroup_*numBladesPerChassis_*numNodesPerBlade_);
36 coords[1] = rankId/ (numBladesPerChassis_*numNodesPerBlade_);
37 rankId=rankId%(numBladesPerChassis_*numNodesPerBlade_);
38 coords[2] = rankId/ numNodesPerBlade_;
39 coords[3]=rankId%numNodesPerBlade_;
44 void AsClusterDragonfly::parse_specific_arguments(sg_platf_cluster_cbarg_t cluster) {
45 std::vector<std::string> parameters;
46 std::vector<std::string> tmp;
47 boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
49 // TODO : we have to check for zeros and negative numbers, or it might crash
50 if (parameters.size() != 4){
51 surf_parse_error("Dragonfly are defined by the number of groups, chassiss per groups, blades per chassis, nodes per blade");
54 // Blue network : number of groups, number of links between each group
55 boost::split(tmp, parameters[0], boost::is_any_of(","));
57 surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
60 this->numGroups_=xbt_str_parse_int(tmp[0].c_str(), "Invalid number of groups: %s");
61 this->numLinksBlue_=xbt_str_parse_int(tmp[1].c_str(), "Invalid number of links for the blue level: %s");
63 // Black network : number of chassiss/group, number of links between each router on the black network
64 boost::split(tmp, parameters[1], boost::is_any_of(","));
66 surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
69 this->numChassisPerGroup_=xbt_str_parse_int(tmp[0].c_str(), "Invalid number of groups: %s");
70 this->numLinksBlack_=xbt_str_parse_int(tmp[1].c_str(), "Invalid number of links for the black level: %s");
73 // Green network : number of blades/chassis, number of links between each router on the green network
74 boost::split(tmp, parameters[2], boost::is_any_of(","));
76 surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
79 this->numBladesPerChassis_=xbt_str_parse_int(tmp[0].c_str(), "Invalid number of groups: %s");
80 this->numLinksGreen_=xbt_str_parse_int(tmp[1].c_str(), "Invalid number of links for the green level: %s");
83 // The last part of topo_parameters should be the number of nodes per blade
84 this->numNodesPerBlade_ = xbt_str_parse_int(parameters[3].c_str(), "Last parameter is not the amount of nodes per blade: %s");
85 this->cluster_ = cluster;
89 * Generate the cluster once every node is created
91 void AsClusterDragonfly::seal(){
92 if(this->numNodesPerBlade_ == 0) {
96 this->generateRouters();
97 this->generateLinks();
100 DragonflyRouter::DragonflyRouter(int group, int chassis, int blade):group_(group),chassis_(chassis),blade_(blade){ }
102 DragonflyRouter::~DragonflyRouter(){
103 if(this->myNodes_!=nullptr)
105 if(this->greenLinks_!=nullptr)
106 xbt_free(greenLinks_);
107 if(this->blackLinks_!=nullptr)
108 xbt_free(blackLinks_);
109 if(this->blueLinks_!=nullptr)
110 xbt_free(blueLinks_);
114 void AsClusterDragonfly::generateRouters() {
115 this->routers_=static_cast<DragonflyRouter**>(xbt_malloc0(this->numGroups_*this->numChassisPerGroup_*this->numBladesPerChassis_*sizeof(DragonflyRouter*)));
117 for(unsigned int i=0;i<this->numGroups_;i++){
118 for(unsigned int j=0;j<this->numChassisPerGroup_;j++){
119 for(unsigned int k=0;k<this->numBladesPerChassis_;k++){
120 DragonflyRouter* router = new DragonflyRouter(i,j,k);
121 this->routers_[i*this->numChassisPerGroup_*this->numBladesPerChassis_+j*this->numBladesPerChassis_+k]=router;
127 void AsClusterDragonfly::createLink(char* id, int numlinks, Link** linkup, Link** linkdown){
130 s_sg_platf_link_cbarg_t linkTemplate;
131 memset(&linkTemplate, 0, sizeof(linkTemplate));
132 linkTemplate.bandwidth = this->cluster_->bw * numlinks;
133 linkTemplate.latency = this->cluster_->lat;
134 linkTemplate.policy = this->cluster_->sharing_policy; // sthg to do with that ?
135 linkTemplate.id = id;
136 sg_platf_new_link(&linkTemplate);
137 XBT_DEBUG("Generating link %s", id);
140 if (this->cluster_->sharing_policy == SURF_LINK_FULLDUPLEX) {
141 tmpID = std::string(linkTemplate.id) + "_UP";
142 link = Link::byName(tmpID.c_str());
143 *linkup = link; // check link?
144 tmpID = std::string(linkTemplate.id) + "_DOWN";
145 link = Link::byName(tmpID.c_str());
146 *linkdown = link; // check link ?
149 link = Link::byName(linkTemplate.id);
154 free((void*)linkTemplate.id);
158 void AsClusterDragonfly::generateLinks() {
160 static int uniqueId = 0;
165 unsigned int numRouters = this->numGroups_*this->numChassisPerGroup_*this->numBladesPerChassis_;
167 if (this->cluster_->sharing_policy == SURF_LINK_FULLDUPLEX)
170 //Links from routers to their local nodes.
171 for(unsigned int i=0; i<numRouters;i++){
172 //allocate structures
173 this->routers_[i]->myNodes_=static_cast<Link**>(xbt_malloc0(numLinksperLink_*this->numNodesPerBlade_*sizeof(Link*)));
174 this->routers_[i]->greenLinks_=static_cast<Link**>(xbt_malloc0(this->numBladesPerChassis_*sizeof(Link*)));
175 this->routers_[i]->blackLinks_=static_cast<Link**>(xbt_malloc0(this->numChassisPerGroup_*sizeof(Link*)));
177 for(unsigned int j=0; j< numLinksperLink_*this->numNodesPerBlade_; j+=numLinksperLink_){
178 id = bprintf("local_link_from_router_%d_to_node_%d_%d", i, j/numLinksperLink_, uniqueId);
179 this->createLink(id, 1, &linkup, &linkdown);
180 if (this->cluster_->sharing_policy == SURF_LINK_FULLDUPLEX) {
181 this->routers_[i]->myNodes_[j] = linkup;
182 this->routers_[i]->myNodes_[j+1] = linkdown;
185 this->routers_[i]->myNodes_[j] = linkup;
191 //Green links from routers to same chassis routers - alltoall
192 for(unsigned int i=0; i<this->numGroups_*this->numChassisPerGroup_;i++){
193 for(unsigned int j=0; j<this->numBladesPerChassis_;j++){
194 for(unsigned int k=j+1;k<this->numBladesPerChassis_;k++){
195 id = bprintf("green_link_in_chassis_%d_between_routers_%d_and_%d_%d", i%numChassisPerGroup_, j, k, uniqueId);
196 this->createLink(id, this->numLinksGreen_, &linkup, &linkdown);
197 this->routers_[i*numBladesPerChassis_+j]->greenLinks_[k] = linkup;
198 this->routers_[i*numBladesPerChassis_+k]->greenLinks_[j] = linkdown;
204 //Black links from routers to same group routers - alltoall
205 for(unsigned int i=0; i<this->numGroups_;i++){
206 for(unsigned int j=0; j<this->numChassisPerGroup_;j++){
207 for(unsigned int k=j+1;k<this->numChassisPerGroup_;k++){
208 for(unsigned int l=0;l<this->numBladesPerChassis_;l++){
209 id = bprintf("black_link_in_group_%d_between_chassis_%d_and_%d_blade_%d_%d", i, j, k,l, uniqueId);
210 this->createLink(id, this->numLinksBlack_,&linkup, &linkdown);
211 this->routers_[i*numBladesPerChassis_*numChassisPerGroup_+j*numBladesPerChassis_+l]->blackLinks_[k] = linkup;
212 this->routers_[i*numBladesPerChassis_*numChassisPerGroup_+k*numBladesPerChassis_+l]->blackLinks_[j] = linkdown;
220 //Blue links between groups - Not all routers involved, only one per group is linked to others. Let's say router n of each group is linked to group n.
221 //FIXME: in reality blue links may be attached to several different routers
222 for(unsigned int i=0; i<this->numGroups_;i++){
223 for(unsigned int j=i+1; j<this->numGroups_;j++){
224 unsigned int routernumi=i*numBladesPerChassis_*numChassisPerGroup_+j;
225 unsigned int routernumj=j*numBladesPerChassis_*numChassisPerGroup_+i;
226 this->routers_[routernumi]->blueLinks_=static_cast<Link**>(xbt_malloc0(sizeof(Link*)));
227 this->routers_[routernumj]->blueLinks_=static_cast<Link**>(xbt_malloc0(sizeof(Link*)));
228 id = bprintf("blue_link_between_group_%d_and_%d_routers_%d_and_%d_%d", i, j, routernumi,routernumj, uniqueId);
229 this->createLink(id, this->numLinksBlue_, &linkup, &linkdown);
230 this->routers_[routernumi]->blueLinks_[0] = linkup;
231 this->routers_[routernumj]->blueLinks_[0] = linkdown;
237 void AsClusterDragonfly::getRouteAndLatency(NetCard * src, NetCard * dst, sg_platf_route_cbarg_t route, double *latency) {
238 //Minimal routing version.
239 // TODO : non-minimal random one, and adaptive ?
241 if (dst->isRouter() || src->isRouter())
244 XBT_VERB("dragonfly_get_route_and_latency from '%s'[%d] to '%s'[%d]", src->name(), src->id(), dst->name(), dst->id());
246 if ((src->id() == dst->id()) && hasLoopback_) {
247 s_surf_parsing_link_up_down_t info = xbt_dynar_get_as(privateLinks_, src->id() * linkCountPerNode_, s_surf_parsing_link_up_down_t);
249 route->link_list->push_back(info.linkUp);
251 *latency += info.linkUp->getLatency();
255 unsigned int *myCoords = rankId_to_coords(src->id());
256 unsigned int *targetCoords = rankId_to_coords(dst->id());
257 XBT_DEBUG("src : %u group, %u chassis, %u blade, %u node", myCoords[0], myCoords[1], myCoords[2], myCoords[3]);
258 XBT_DEBUG("dst : %u group, %u chassis, %u blade, %u node", targetCoords[0], targetCoords[1], targetCoords[2], targetCoords[3]);
260 DragonflyRouter* myRouter = routers_[myCoords[0]*(numChassisPerGroup_*numBladesPerChassis_)+myCoords[1] * numBladesPerChassis_+myCoords[2]];
261 DragonflyRouter* targetRouter = routers_[targetCoords[0]*(numChassisPerGroup_*numBladesPerChassis_)+targetCoords[1] *numBladesPerChassis_ +targetCoords[2]];
262 DragonflyRouter* currentRouter=myRouter;
264 //node->router local link
265 route->link_list->push_back(myRouter->myNodes_[myCoords[3]*numLinksperLink_]);
267 *latency += myRouter->myNodes_[myCoords[3]*numLinksperLink_]->getLatency();
270 if (hasLimiter_) { // limiter for sender
271 s_surf_parsing_link_up_down_t info;
272 info = xbt_dynar_get_as(privateLinks_, src->id() * linkCountPerNode_ + hasLoopback_, s_surf_parsing_link_up_down_t);
273 route->link_list->push_back(info.linkUp);
276 if(targetRouter!=myRouter){
278 //are we on a different group ?
279 if(targetRouter->group_ != currentRouter->group_){
280 //go to the router of our group connected to this one.
281 if(currentRouter->blade_!=targetCoords[0]){
282 //go to the nth router in our chassis
283 route->link_list->push_back(currentRouter->greenLinks_[targetCoords[0]]);
285 *latency += currentRouter->greenLinks_[targetCoords[0]]->getLatency();
287 currentRouter=routers_[myCoords[0]*(numChassisPerGroup_*numBladesPerChassis_)+myCoords[1] * numBladesPerChassis_+targetCoords[0]];
290 if(currentRouter->chassis_!=0){
291 //go to the first chassis of our group
292 route->link_list->push_back(currentRouter->blackLinks_[0]);
294 *latency += currentRouter->blackLinks_[0]->getLatency();
296 currentRouter=routers_[myCoords[0]*(numChassisPerGroup_*numBladesPerChassis_)+targetCoords[0]];
299 //go to destination group - the only optical hop
300 route->link_list->push_back(currentRouter->blueLinks_[0]);
302 *latency += currentRouter->blueLinks_[0]->getLatency();
304 currentRouter=routers_[targetCoords[0]*(numChassisPerGroup_*numBladesPerChassis_)+myCoords[0]];
308 //same group, but same blade ?
309 if(targetRouter->blade_ != currentRouter->blade_){
310 route->link_list->push_back(currentRouter->greenLinks_[targetCoords[2]]);
312 *latency += currentRouter->greenLinks_[targetCoords[2]]->getLatency();
314 currentRouter=routers_[targetCoords[0]*(numChassisPerGroup_*numBladesPerChassis_)+targetCoords[2]];
317 //same blade, but same chassis ?
318 if(targetRouter->chassis_ != currentRouter->chassis_){
319 route->link_list->push_back(currentRouter->blackLinks_[targetCoords[1]]);
321 *latency += currentRouter->blackLinks_[targetCoords[1]]->getLatency();
323 currentRouter=routers_[targetCoords[0]*(numChassisPerGroup_*numBladesPerChassis_)+targetCoords[1]*numBladesPerChassis_+targetCoords[2]];
327 if (hasLimiter_) { // limiter for receiver
328 s_surf_parsing_link_up_down_t info;
329 info = xbt_dynar_get_as(privateLinks_, dst->id() * linkCountPerNode_ + hasLoopback_, s_surf_parsing_link_up_down_t);
330 route->link_list->push_back(info.linkUp);
333 //router->node local link
334 route->link_list->push_back(targetRouter->myNodes_[targetCoords[3]*numLinksperLink_+numLinksperLink_-1]);
336 *latency += targetRouter->myNodes_[targetCoords[3]*numLinksperLink_+numLinksperLink_-1]->getLatency();
340 xbt_free(targetCoords);