Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
use std::pair instead of s_surf_parsing_link_up_down_t for Cluster private links
[simgrid.git] / src / kernel / routing / AsClusterDragonfly.cpp
1 /* Copyright (c) 2014-2016. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "src/kernel/routing/AsClusterDragonfly.hpp"
7 #include "src/kernel/routing/NetCard.hpp"
8 #include "src/surf/network_interface.hpp"
9 #include "src/surf/xml/platf.hpp" // FIXME: move that back to the parsing area
10
11 #include <boost/algorithm/string/split.hpp>
12 #include <boost/algorithm/string/classification.hpp>
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_cluster_dragonfly, surf_route_cluster, "Dragonfly Routing part of surf");
15
16 namespace simgrid {
17 namespace kernel {
18 namespace routing {
19
20 AsClusterDragonfly::AsClusterDragonfly(As* father, const char* name) : AsCluster(father, name)
21 {
22 }
23
24 AsClusterDragonfly::~AsClusterDragonfly() {
25   if(this->routers_ != nullptr){
26     for (unsigned int i=0; i<this->numGroups_*this->numChassisPerGroup_*this->numBladesPerChassis_;i++)
27         delete(routers_[i]);
28     xbt_free(routers_);
29   }
30 }
31
32 unsigned int *AsClusterDragonfly::rankId_to_coords(int rankId)
33 {
34   //coords : group, chassis, blade, node
35   unsigned int *coords = (unsigned int *) malloc(4 * sizeof(unsigned int));
36   coords[0] = rankId/ (numChassisPerGroup_*numBladesPerChassis_*numNodesPerBlade_);
37   rankId=rankId%(numChassisPerGroup_*numBladesPerChassis_*numNodesPerBlade_);
38   coords[1] = rankId/ (numBladesPerChassis_*numNodesPerBlade_);
39   rankId=rankId%(numBladesPerChassis_*numNodesPerBlade_);
40   coords[2] = rankId/ numNodesPerBlade_;
41   coords[3]=rankId%numNodesPerBlade_;
42
43   return coords;
44 }
45
46 void AsClusterDragonfly::parse_specific_arguments(sg_platf_cluster_cbarg_t cluster) {
47   std::vector<std::string> parameters;
48   std::vector<std::string> tmp;
49   boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
50
51   // TODO : we have to check for zeros and negative numbers, or it might crash
52   if (parameters.size() != 4){
53     surf_parse_error(
54         "Dragonfly are defined by the number of groups, chassis per groups, blades per chassis, nodes per blade");
55   }
56
57   // Blue network : number of groups, number of links between each group
58   boost::split(tmp, parameters[0], boost::is_any_of(","));
59   if(tmp.size() != 2) {
60     surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
61   }
62
63   this->numGroups_=xbt_str_parse_int(tmp[0].c_str(), "Invalid number of groups: %s");
64   this->numLinksBlue_=xbt_str_parse_int(tmp[1].c_str(), "Invalid number of links for the blue level: %s");
65
66   // Black network : number of chassis/group, number of links between each router on the black network
67   boost::split(tmp, parameters[1], boost::is_any_of(","));
68   if(tmp.size() != 2) {
69     surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
70   }
71
72   this->numChassisPerGroup_=xbt_str_parse_int(tmp[0].c_str(), "Invalid number of groups: %s");
73   this->numLinksBlack_=xbt_str_parse_int(tmp[1].c_str(), "Invalid number of links  for the black level: %s");
74
75
76  // Green network : number of blades/chassis, number of links between each router on the green network
77   boost::split(tmp, parameters[2], boost::is_any_of(","));
78   if(tmp.size() != 2) {
79     surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
80   }
81
82   this->numBladesPerChassis_=xbt_str_parse_int(tmp[0].c_str(), "Invalid number of groups: %s");
83   this->numLinksGreen_=xbt_str_parse_int(tmp[1].c_str(), "Invalid number of links for the green level: %s");
84
85
86   // The last part of topo_parameters should be the number of nodes per blade
87   this->numNodesPerBlade_ = xbt_str_parse_int(parameters[3].c_str(), "Last parameter is not the amount of nodes per blade: %s");
88   this->cluster_ = cluster;
89 }
90
91 /*
92 * Generate the cluster once every node is created
93 */
94 void AsClusterDragonfly::seal(){
95   if(this->numNodesPerBlade_ == 0) {
96     return;
97   }
98
99   this->generateRouters();
100   this->generateLinks();
101 }
102
103 DragonflyRouter::DragonflyRouter(int group, int chassis, int blade):group_(group),chassis_(chassis),blade_(blade){ }
104
105 DragonflyRouter::~DragonflyRouter(){
106   if(this->myNodes_!=nullptr)
107     xbt_free(myNodes_);
108   if(this->greenLinks_!=nullptr)
109     xbt_free(greenLinks_);
110   if(this->blackLinks_!=nullptr)
111     xbt_free(blackLinks_);
112   if(this->blueLinks_!=nullptr)
113     xbt_free(blueLinks_);
114 }
115
116
117 void AsClusterDragonfly::generateRouters() {
118   this->routers_=static_cast<DragonflyRouter**>(xbt_malloc0(this->numGroups_*this->numChassisPerGroup_*this->numBladesPerChassis_*sizeof(DragonflyRouter*)));
119
120   for(unsigned int i=0;i<this->numGroups_;i++){
121     for(unsigned int j=0;j<this->numChassisPerGroup_;j++){
122       for(unsigned int k=0;k<this->numBladesPerChassis_;k++){
123         DragonflyRouter* router = new DragonflyRouter(i,j,k);
124         this->routers_[i*this->numChassisPerGroup_*this->numBladesPerChassis_+j*this->numBladesPerChassis_+k]=router;
125       }
126     }
127   }
128 }
129
130 void AsClusterDragonfly::createLink(char* id, int numlinks, Link** linkup, Link** linkdown){
131   *linkup=nullptr;
132   *linkdown=nullptr;
133   s_sg_platf_link_cbarg_t linkTemplate;
134   memset(&linkTemplate, 0, sizeof(linkTemplate));
135   linkTemplate.bandwidth = this->cluster_->bw * numlinks;
136   linkTemplate.latency = this->cluster_->lat;
137   linkTemplate.policy = this->cluster_->sharing_policy; // sthg to do with that ?
138   linkTemplate.id = id;
139   sg_platf_new_link(&linkTemplate);
140   XBT_DEBUG("Generating link %s", id);
141   Link* link;
142   std::string tmpID;
143   if (this->cluster_->sharing_policy == SURF_LINK_FULLDUPLEX) {
144     tmpID = std::string(linkTemplate.id) + "_UP";
145     link =  Link::byName(tmpID.c_str());
146     *linkup = link; // check link?
147     tmpID = std::string(linkTemplate.id) + "_DOWN";
148     link = Link::byName(tmpID.c_str());
149     *linkdown = link; // check link ?
150   }
151   else {
152     link = Link::byName(linkTemplate.id);
153     *linkup = link;
154     *linkdown = link;
155   }
156
157   free((void*)linkTemplate.id);
158 }
159
160
161 void AsClusterDragonfly::generateLinks() {
162
163   static int uniqueId = 0;
164   char* id = nullptr;
165   Link* linkup;
166   Link *linkdown;
167
168   unsigned int numRouters = this->numGroups_*this->numChassisPerGroup_*this->numBladesPerChassis_;
169
170   if (this->cluster_->sharing_policy == SURF_LINK_FULLDUPLEX)
171     numLinksperLink_=2;
172
173   //Links from routers to their local nodes.
174   for(unsigned int i=0; i<numRouters;i++){
175   //allocate structures
176     this->routers_[i]->myNodes_=static_cast<Link**>(xbt_malloc0(numLinksperLink_*this->numNodesPerBlade_*sizeof(Link*)));
177     this->routers_[i]->greenLinks_=static_cast<Link**>(xbt_malloc0(this->numBladesPerChassis_*sizeof(Link*)));
178     this->routers_[i]->blackLinks_=static_cast<Link**>(xbt_malloc0(this->numChassisPerGroup_*sizeof(Link*)));
179
180     for(unsigned int j=0; j< numLinksperLink_*this->numNodesPerBlade_; j+=numLinksperLink_){
181       id = bprintf("local_link_from_router_%d_to_node_%d_%d", i, j/numLinksperLink_, uniqueId);
182       this->createLink(id, 1, &linkup, &linkdown);
183       if (this->cluster_->sharing_policy == SURF_LINK_FULLDUPLEX) {
184         this->routers_[i]->myNodes_[j] = linkup; 
185         this->routers_[i]->myNodes_[j+1] = linkdown; 
186       }
187       else {
188         this->routers_[i]->myNodes_[j] = linkup;
189       }
190       uniqueId++;
191     }
192   }
193
194   //Green links from routers to same chassis routers - alltoall
195   for(unsigned int i=0; i<this->numGroups_*this->numChassisPerGroup_;i++){
196     for(unsigned int j=0; j<this->numBladesPerChassis_;j++){
197       for(unsigned int k=j+1;k<this->numBladesPerChassis_;k++){
198         id = bprintf("green_link_in_chassis_%d_between_routers_%d_and_%d_%d", i%numChassisPerGroup_, j, k, uniqueId);
199         this->createLink(id, this->numLinksGreen_, &linkup, &linkdown);
200         this->routers_[i*numBladesPerChassis_+j]->greenLinks_[k] = linkup;
201         this->routers_[i*numBladesPerChassis_+k]->greenLinks_[j] = linkdown; 
202         uniqueId++;
203       }
204     }
205   }
206
207   //Black links from routers to same group routers - alltoall
208   for(unsigned int i=0; i<this->numGroups_;i++){
209     for(unsigned int j=0; j<this->numChassisPerGroup_;j++){
210       for(unsigned int k=j+1;k<this->numChassisPerGroup_;k++){
211         for(unsigned int l=0;l<this->numBladesPerChassis_;l++){
212           id = bprintf("black_link_in_group_%d_between_chassis_%d_and_%d_blade_%d_%d", i, j, k,l, uniqueId);
213           this->createLink(id, this->numLinksBlack_,&linkup, &linkdown);
214           this->routers_[i*numBladesPerChassis_*numChassisPerGroup_+j*numBladesPerChassis_+l]->blackLinks_[k] = linkup;
215           this->routers_[i*numBladesPerChassis_*numChassisPerGroup_+k*numBladesPerChassis_+l]->blackLinks_[j] = linkdown; 
216           uniqueId++;
217         }
218       }
219     }
220   }
221
222
223   //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.
224 //FIXME: in reality blue links may be attached to several different routers
225   for(unsigned int i=0; i<this->numGroups_;i++){
226     for(unsigned int j=i+1; j<this->numGroups_;j++){
227       unsigned int routernumi=i*numBladesPerChassis_*numChassisPerGroup_+j;
228       unsigned int routernumj=j*numBladesPerChassis_*numChassisPerGroup_+i;
229       this->routers_[routernumi]->blueLinks_=static_cast<Link**>(xbt_malloc0(sizeof(Link*)));
230       this->routers_[routernumj]->blueLinks_=static_cast<Link**>(xbt_malloc0(sizeof(Link*)));
231         id = bprintf("blue_link_between_group_%d_and_%d_routers_%d_and_%d_%d", i, j, routernumi,routernumj, uniqueId);
232         this->createLink(id, this->numLinksBlue_, &linkup, &linkdown);
233         this->routers_[routernumi]->blueLinks_[0] = linkup;
234         this->routers_[routernumj]->blueLinks_[0] = linkdown; 
235         uniqueId++;
236     }
237   }
238 }
239
240 void AsClusterDragonfly::getLocalRoute(NetCard* src, NetCard* dst, sg_platf_route_cbarg_t route, double* latency)
241 {
242   //Minimal routing version.
243   // TODO : non-minimal random one, and adaptive ?
244
245   if (dst->isRouter() || src->isRouter())
246     return;
247
248   XBT_VERB("dragonfly getLocalRout from '%s'[%d] to '%s'[%d]", src->name().c_str(), src->id(), dst->name().c_str(),
249            dst->id());
250
251   if ((src->id() == dst->id()) && hasLoopback_) {
252     std::pair<Link*, Link*> info = privateLinks_.at(src->id() * linkCountPerNode_);
253
254     route->link_list->push_back(info.first);
255     if (latency)
256       *latency += info.first->latency();
257     return;
258   }
259
260   unsigned int *myCoords = rankId_to_coords(src->id());
261   unsigned int *targetCoords = rankId_to_coords(dst->id());
262   XBT_DEBUG("src : %u group, %u chassis, %u blade, %u node", myCoords[0], myCoords[1], myCoords[2], myCoords[3]);
263   XBT_DEBUG("dst : %u group, %u chassis, %u blade, %u node", targetCoords[0], targetCoords[1], targetCoords[2], targetCoords[3]);
264
265   DragonflyRouter* myRouter = routers_[myCoords[0]*(numChassisPerGroup_*numBladesPerChassis_)+myCoords[1] * numBladesPerChassis_+myCoords[2]];
266   DragonflyRouter* targetRouter = routers_[targetCoords[0]*(numChassisPerGroup_*numBladesPerChassis_)+targetCoords[1] *numBladesPerChassis_ +targetCoords[2]];
267   DragonflyRouter* currentRouter=myRouter;
268
269   //node->router local link
270   route->link_list->push_back(myRouter->myNodes_[myCoords[3]*numLinksperLink_]);
271   if (latency)
272     *latency += myRouter->myNodes_[myCoords[3] * numLinksperLink_]->latency();
273
274   if (hasLimiter_) {    // limiter for sender
275     std::pair<Link*, Link*> info = privateLinks_.at(src->id() * linkCountPerNode_ + hasLoopback_);
276     route->link_list->push_back(info.first);
277   }
278
279   if(targetRouter!=myRouter){
280
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]]);
287         if (latency)
288           *latency += currentRouter->greenLinks_[targetCoords[0]]->latency();
289         currentRouter=routers_[myCoords[0]*(numChassisPerGroup_*numBladesPerChassis_)+myCoords[1] * numBladesPerChassis_+targetCoords[0]];
290       }
291
292       if(currentRouter->chassis_!=0){
293         //go to the first chassis of our group
294         route->link_list->push_back(currentRouter->blackLinks_[0]);
295         if (latency)
296           *latency += currentRouter->blackLinks_[0]->latency();
297         currentRouter=routers_[myCoords[0]*(numChassisPerGroup_*numBladesPerChassis_)+targetCoords[0]];
298       }
299
300       //go to destination group - the only optical hop 
301       route->link_list->push_back(currentRouter->blueLinks_[0]);
302       if (latency)
303         *latency += currentRouter->blueLinks_[0]->latency();
304       currentRouter=routers_[targetCoords[0]*(numChassisPerGroup_*numBladesPerChassis_)+myCoords[0]];
305     }
306
307     
308     //same group, but same blade ?
309     if(targetRouter->blade_ != currentRouter->blade_){
310       route->link_list->push_back(currentRouter->greenLinks_[targetCoords[2]]);
311       if (latency)
312         *latency += currentRouter->greenLinks_[targetCoords[2]]->latency();
313       currentRouter=routers_[targetCoords[0]*(numChassisPerGroup_*numBladesPerChassis_)+targetCoords[2]];
314     }
315
316     //same blade, but same chassis ?
317     if(targetRouter->chassis_ != currentRouter->chassis_){
318       route->link_list->push_back(currentRouter->blackLinks_[targetCoords[1]]);
319       if (latency)
320         *latency += currentRouter->blackLinks_[targetCoords[1]]->latency();
321       currentRouter=routers_[targetCoords[0]*(numChassisPerGroup_*numBladesPerChassis_)+targetCoords[1]*numBladesPerChassis_+targetCoords[2]];
322     }
323   }
324
325   if (hasLimiter_) {    // limiter for receiver
326     std::pair<Link*, Link*> info = privateLinks_.at(dst->id() * linkCountPerNode_ + hasLoopback_);
327     route->link_list->push_back(info.first);
328   }
329
330   //router->node local link
331   route->link_list->push_back(targetRouter->myNodes_[targetCoords[3]*numLinksperLink_+numLinksperLink_-1]);
332   if (latency)
333     *latency += targetRouter->myNodes_[targetCoords[3] * numLinksperLink_ + numLinksperLink_ - 1]->latency();
334
335   xbt_free(myCoords);
336   xbt_free(targetCoords);
337 }
338 }}} // namespace