Logo AND Algorithmique Numérique Distribuée

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