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/kernel/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");
19 AsClusterDragonfly::AsClusterDragonfly(As* father, const char* name) : AsCluster(father, name)
23 AsClusterDragonfly::~AsClusterDragonfly() {
24 if(this->routers_ != nullptr){
25 for (unsigned int i=0; i<this->numGroups_*this->numChassisPerGroup_*this->numBladesPerChassis_;i++)
31 unsigned int *AsClusterDragonfly::rankId_to_coords(int rankId)
33 //coords : group, chassis, blade, node
34 unsigned int *coords = (unsigned int *) malloc(4 * sizeof(unsigned int));
35 coords[0] = rankId/ (numChassisPerGroup_*numBladesPerChassis_*numNodesPerBlade_);
36 rankId=rankId%(numChassisPerGroup_*numBladesPerChassis_*numNodesPerBlade_);
37 coords[1] = rankId/ (numBladesPerChassis_*numNodesPerBlade_);
38 rankId=rankId%(numBladesPerChassis_*numNodesPerBlade_);
39 coords[2] = rankId/ numNodesPerBlade_;
40 coords[3]=rankId%numNodesPerBlade_;
45 void AsClusterDragonfly::parse_specific_arguments(sg_platf_cluster_cbarg_t cluster) {
46 std::vector<std::string> parameters;
47 std::vector<std::string> tmp;
48 boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
50 // TODO : we have to check for zeros and negative numbers, or it might crash
51 if (parameters.size() != 4){
53 "Dragonfly are defined by the number of groups, chassis per groups, blades per chassis, nodes per blade");
56 // Blue network : number of groups, number of links between each group
57 boost::split(tmp, parameters[0], boost::is_any_of(","));
59 surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
62 this->numGroups_=xbt_str_parse_int(tmp[0].c_str(), "Invalid number of groups: %s");
63 this->numLinksBlue_=xbt_str_parse_int(tmp[1].c_str(), "Invalid number of links for the blue level: %s");
65 // Black network : number of chassis/group, number of links between each router on the black network
66 boost::split(tmp, parameters[1], boost::is_any_of(","));
68 surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
71 this->numChassisPerGroup_=xbt_str_parse_int(tmp[0].c_str(), "Invalid number of groups: %s");
72 this->numLinksBlack_=xbt_str_parse_int(tmp[1].c_str(), "Invalid number of links for the black level: %s");
75 // Green network : number of blades/chassis, number of links between each router on the green network
76 boost::split(tmp, parameters[2], boost::is_any_of(","));
78 surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
81 this->numBladesPerChassis_=xbt_str_parse_int(tmp[0].c_str(), "Invalid number of groups: %s");
82 this->numLinksGreen_=xbt_str_parse_int(tmp[1].c_str(), "Invalid number of links for the green level: %s");
85 // The last part of topo_parameters should be the number of nodes per blade
86 this->numNodesPerBlade_ = xbt_str_parse_int(parameters[3].c_str(), "Last parameter is not the amount of nodes per blade: %s");
87 this->cluster_ = cluster;
91 * Generate the cluster once every node is created
93 void AsClusterDragonfly::seal(){
94 if(this->numNodesPerBlade_ == 0) {
98 this->generateRouters();
99 this->generateLinks();
102 DragonflyRouter::DragonflyRouter(int group, int chassis, int blade):group_(group),chassis_(chassis),blade_(blade){ }
104 DragonflyRouter::~DragonflyRouter(){
105 if(this->myNodes_!=nullptr)
107 if(this->greenLinks_!=nullptr)
108 xbt_free(greenLinks_);
109 if(this->blackLinks_!=nullptr)
110 xbt_free(blackLinks_);
111 if(this->blueLinks_!=nullptr)
112 xbt_free(blueLinks_);
116 void AsClusterDragonfly::generateRouters() {
117 this->routers_=static_cast<DragonflyRouter**>(xbt_malloc0(this->numGroups_*this->numChassisPerGroup_*this->numBladesPerChassis_*sizeof(DragonflyRouter*)));
119 for(unsigned int i=0;i<this->numGroups_;i++){
120 for(unsigned int j=0;j<this->numChassisPerGroup_;j++){
121 for(unsigned int k=0;k<this->numBladesPerChassis_;k++){
122 DragonflyRouter* router = new DragonflyRouter(i,j,k);
123 this->routers_[i*this->numChassisPerGroup_*this->numBladesPerChassis_+j*this->numBladesPerChassis_+k]=router;
129 void AsClusterDragonfly::createLink(char* id, int numlinks, Link** linkup, Link** linkdown){
132 s_sg_platf_link_cbarg_t linkTemplate;
133 memset(&linkTemplate, 0, sizeof(linkTemplate));
134 linkTemplate.bandwidth = this->cluster_->bw * numlinks;
135 linkTemplate.latency = this->cluster_->lat;
136 linkTemplate.policy = this->cluster_->sharing_policy; // sthg to do with that ?
137 linkTemplate.id = id;
138 sg_platf_new_link(&linkTemplate);
139 XBT_DEBUG("Generating link %s", id);
142 if (this->cluster_->sharing_policy == SURF_LINK_FULLDUPLEX) {
143 tmpID = std::string(linkTemplate.id) + "_UP";
144 link = Link::byName(tmpID.c_str());
145 *linkup = link; // check link?
146 tmpID = std::string(linkTemplate.id) + "_DOWN";
147 link = Link::byName(tmpID.c_str());
148 *linkdown = link; // check link ?
151 link = Link::byName(linkTemplate.id);
156 free((void*)linkTemplate.id);
160 void AsClusterDragonfly::generateLinks() {
162 static int uniqueId = 0;
167 unsigned int numRouters = this->numGroups_*this->numChassisPerGroup_*this->numBladesPerChassis_;
169 if (this->cluster_->sharing_policy == SURF_LINK_FULLDUPLEX)
172 //Links from routers to their local nodes.
173 for(unsigned int i=0; i<numRouters;i++){
174 //allocate structures
175 this->routers_[i]->myNodes_=static_cast<Link**>(xbt_malloc0(numLinksperLink_*this->numNodesPerBlade_*sizeof(Link*)));
176 this->routers_[i]->greenLinks_=static_cast<Link**>(xbt_malloc0(this->numBladesPerChassis_*sizeof(Link*)));
177 this->routers_[i]->blackLinks_=static_cast<Link**>(xbt_malloc0(this->numChassisPerGroup_*sizeof(Link*)));
179 for(unsigned int j=0; j< numLinksperLink_*this->numNodesPerBlade_; j+=numLinksperLink_){
180 id = bprintf("local_link_from_router_%d_to_node_%d_%d", i, j/numLinksperLink_, uniqueId);
181 this->createLink(id, 1, &linkup, &linkdown);
182 if (this->cluster_->sharing_policy == SURF_LINK_FULLDUPLEX) {
183 this->routers_[i]->myNodes_[j] = linkup;
184 this->routers_[i]->myNodes_[j+1] = linkdown;
187 this->routers_[i]->myNodes_[j] = linkup;
193 //Green links from routers to same chassis routers - alltoall
194 for(unsigned int i=0; i<this->numGroups_*this->numChassisPerGroup_;i++){
195 for(unsigned int j=0; j<this->numBladesPerChassis_;j++){
196 for(unsigned int k=j+1;k<this->numBladesPerChassis_;k++){
197 id = bprintf("green_link_in_chassis_%d_between_routers_%d_and_%d_%d", i%numChassisPerGroup_, j, k, uniqueId);
198 this->createLink(id, this->numLinksGreen_, &linkup, &linkdown);
199 this->routers_[i*numBladesPerChassis_+j]->greenLinks_[k] = linkup;
200 this->routers_[i*numBladesPerChassis_+k]->greenLinks_[j] = linkdown;
206 //Black links from routers to same group routers - alltoall
207 for(unsigned int i=0; i<this->numGroups_;i++){
208 for(unsigned int j=0; j<this->numChassisPerGroup_;j++){
209 for(unsigned int k=j+1;k<this->numChassisPerGroup_;k++){
210 for(unsigned int l=0;l<this->numBladesPerChassis_;l++){
211 id = bprintf("black_link_in_group_%d_between_chassis_%d_and_%d_blade_%d_%d", i, j, k,l, uniqueId);
212 this->createLink(id, this->numLinksBlack_,&linkup, &linkdown);
213 this->routers_[i*numBladesPerChassis_*numChassisPerGroup_+j*numBladesPerChassis_+l]->blackLinks_[k] = linkup;
214 this->routers_[i*numBladesPerChassis_*numChassisPerGroup_+k*numBladesPerChassis_+l]->blackLinks_[j] = linkdown;
222 //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.
223 //FIXME: in reality blue links may be attached to several different routers
224 for(unsigned int i=0; i<this->numGroups_;i++){
225 for(unsigned int j=i+1; j<this->numGroups_;j++){
226 unsigned int routernumi=i*numBladesPerChassis_*numChassisPerGroup_+j;
227 unsigned int routernumj=j*numBladesPerChassis_*numChassisPerGroup_+i;
228 this->routers_[routernumi]->blueLinks_=static_cast<Link**>(xbt_malloc0(sizeof(Link*)));
229 this->routers_[routernumj]->blueLinks_=static_cast<Link**>(xbt_malloc0(sizeof(Link*)));
230 id = bprintf("blue_link_between_group_%d_and_%d_routers_%d_and_%d_%d", i, j, routernumi,routernumj, uniqueId);
231 this->createLink(id, this->numLinksBlue_, &linkup, &linkdown);
232 this->routers_[routernumi]->blueLinks_[0] = linkup;
233 this->routers_[routernumj]->blueLinks_[0] = linkdown;
239 void AsClusterDragonfly::getRouteAndLatency(NetCard * src, NetCard * dst, sg_platf_route_cbarg_t route, double *latency) {
240 //Minimal routing version.
241 // TODO : non-minimal random one, and adaptive ?
243 if (dst->isRouter() || src->isRouter())
246 XBT_VERB("dragonfly_get_route_and_latency from '%s'[%d] to '%s'[%d]", src->name().c_str(), src->id(),
247 dst->name().c_str(), dst->id());
249 if ((src->id() == dst->id()) && hasLoopback_) {
250 s_surf_parsing_link_up_down_t info = privateLinks_.at(src->id() * linkCountPerNode_);
252 route->link_list->push_back(info.linkUp);
254 *latency += info.linkUp->latency();
258 unsigned int *myCoords = rankId_to_coords(src->id());
259 unsigned int *targetCoords = rankId_to_coords(dst->id());
260 XBT_DEBUG("src : %u group, %u chassis, %u blade, %u node", myCoords[0], myCoords[1], myCoords[2], myCoords[3]);
261 XBT_DEBUG("dst : %u group, %u chassis, %u blade, %u node", targetCoords[0], targetCoords[1], targetCoords[2], targetCoords[3]);
263 DragonflyRouter* myRouter = routers_[myCoords[0]*(numChassisPerGroup_*numBladesPerChassis_)+myCoords[1] * numBladesPerChassis_+myCoords[2]];
264 DragonflyRouter* targetRouter = routers_[targetCoords[0]*(numChassisPerGroup_*numBladesPerChassis_)+targetCoords[1] *numBladesPerChassis_ +targetCoords[2]];
265 DragonflyRouter* currentRouter=myRouter;
267 //node->router local link
268 route->link_list->push_back(myRouter->myNodes_[myCoords[3]*numLinksperLink_]);
270 *latency += myRouter->myNodes_[myCoords[3] * numLinksperLink_]->latency();
273 if (hasLimiter_) { // limiter for sender
274 s_surf_parsing_link_up_down_t info;
275 info = privateLinks_.at(src->id() * linkCountPerNode_ + hasLoopback_);
276 route->link_list->push_back(info.linkUp);
279 if(targetRouter!=myRouter){
281 //are we on a different group ?
282 if(targetRouter->group_ != currentRouter->group_){
283 //go to the router of our group connected to this one.
284 if(currentRouter->blade_!=targetCoords[0]){
285 //go to the nth router in our chassis
286 route->link_list->push_back(currentRouter->greenLinks_[targetCoords[0]]);
288 *latency += currentRouter->greenLinks_[targetCoords[0]]->latency();
290 currentRouter=routers_[myCoords[0]*(numChassisPerGroup_*numBladesPerChassis_)+myCoords[1] * numBladesPerChassis_+targetCoords[0]];
293 if(currentRouter->chassis_!=0){
294 //go to the first chassis of our group
295 route->link_list->push_back(currentRouter->blackLinks_[0]);
297 *latency += currentRouter->blackLinks_[0]->latency();
299 currentRouter=routers_[myCoords[0]*(numChassisPerGroup_*numBladesPerChassis_)+targetCoords[0]];
302 //go to destination group - the only optical hop
303 route->link_list->push_back(currentRouter->blueLinks_[0]);
305 *latency += currentRouter->blueLinks_[0]->latency();
307 currentRouter=routers_[targetCoords[0]*(numChassisPerGroup_*numBladesPerChassis_)+myCoords[0]];
311 //same group, but same blade ?
312 if(targetRouter->blade_ != currentRouter->blade_){
313 route->link_list->push_back(currentRouter->greenLinks_[targetCoords[2]]);
315 *latency += currentRouter->greenLinks_[targetCoords[2]]->latency();
317 currentRouter=routers_[targetCoords[0]*(numChassisPerGroup_*numBladesPerChassis_)+targetCoords[2]];
320 //same blade, but same chassis ?
321 if(targetRouter->chassis_ != currentRouter->chassis_){
322 route->link_list->push_back(currentRouter->blackLinks_[targetCoords[1]]);
324 *latency += currentRouter->blackLinks_[targetCoords[1]]->latency();
326 currentRouter=routers_[targetCoords[0]*(numChassisPerGroup_*numBladesPerChassis_)+targetCoords[1]*numBladesPerChassis_+targetCoords[2]];
330 if (hasLimiter_) { // limiter for receiver
331 s_surf_parsing_link_up_down_t info;
332 info = privateLinks_.at(dst->id() * linkCountPerNode_ + hasLoopback_);
333 route->link_list->push_back(info.linkUp);
336 //router->node local link
337 route->link_list->push_back(targetRouter->myNodes_[targetCoords[3]*numLinksperLink_+numLinksperLink_-1]);
339 *latency += targetRouter->myNodes_[targetCoords[3] * numLinksperLink_ + numLinksperLink_ - 1]->latency();
343 xbt_free(targetCoords);