Logo AND Algorithmique Numérique Distribuée

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