Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
woops, fix MC builds
[simgrid.git] / src / 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/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 routing {
17
18 AsClusterDragonfly::AsClusterDragonfly(const char*name)
19   : AsCluster(name) {
20 }
21
22 AsClusterDragonfly::~AsClusterDragonfly() {
23   if(this->routers_ != nullptr){
24     for (unsigned int i=0; i<this->numGroups_*this->numChassisPerGroup_*this->numBladesPerChassis_;i++)
25         delete(routers_[i]);
26     xbt_free(routers_);
27   }
28 }
29
30 unsigned int *AsClusterDragonfly::rankId_to_coords(int rankId)
31 {
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_;
40
41   return coords;
42 }
43
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(";"));
48
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");
52   }
53
54   // Blue network : number of groups, number of links between each group
55   boost::split(tmp, parameters[0], boost::is_any_of(","));
56   if(tmp.size() != 2) {
57     surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
58   }
59
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");
62
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(","));
65   if(tmp.size() != 2) {
66     surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
67   }
68
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");
71
72
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(","));
75   if(tmp.size() != 2) {
76     surf_parse_error("Dragonfly topologies are defined by 3 levels with 2 elements each, and one with one element");
77   }
78
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");
81
82
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;
86 }
87
88 /*
89 * Generate the cluster once every node is created
90 */
91 void AsClusterDragonfly::seal(){
92   if(this->numNodesPerBlade_ == 0) {
93     return;
94   }
95
96   this->generateRouters();
97   this->generateLinks();
98 }
99
100 DragonflyRouter::DragonflyRouter(int group, int chassis, int blade):group_(group),chassis_(chassis),blade_(blade){ }
101
102 DragonflyRouter::~DragonflyRouter(){
103   if(this->myNodes_!=nullptr)
104     xbt_free(myNodes_);
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_);
111 }
112
113
114 void AsClusterDragonfly::generateRouters() {
115   this->routers_=static_cast<DragonflyRouter**>(xbt_malloc0(this->numGroups_*this->numChassisPerGroup_*this->numBladesPerChassis_*sizeof(DragonflyRouter*)));
116
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;
122       }
123     }
124   }
125 }
126
127 void AsClusterDragonfly::createLink(char* id, int numlinks, Link** linkup, Link** linkdown){
128   *linkup=nullptr;
129   *linkdown=nullptr;
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);
138   Link* link;
139   std::string tmpID;
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 ?
147   }
148   else {
149     link = Link::byName(linkTemplate.id);
150     *linkup = link;
151     *linkdown = link;
152   }
153
154   free((void*)linkTemplate.id);
155 }
156
157
158 void AsClusterDragonfly::generateLinks() {
159
160   static int uniqueId = 0;
161   char* id = nullptr;
162   Link* linkup;
163   Link *linkdown;
164
165   unsigned int numRouters = this->numGroups_*this->numChassisPerGroup_*this->numBladesPerChassis_;
166
167   if (this->cluster_->sharing_policy == SURF_LINK_FULLDUPLEX)
168     numLinksperLink_=2;
169
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*)));
176
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; 
183       }
184       else {
185         this->routers_[i]->myNodes_[j] = linkup;
186       }
187       uniqueId++;
188     }
189   }
190
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; 
199         uniqueId++;
200       }
201     }
202   }
203
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; 
213           uniqueId++;
214         }
215       }
216     }
217   }
218
219
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; 
232         uniqueId++;
233     }
234   }
235 }
236
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 ?
240
241   if (dst->isRouter() || src->isRouter())
242     return;
243
244   XBT_VERB("dragonfly_get_route_and_latency from '%s'[%d] to '%s'[%d]", src->name(), src->id(), dst->name(), dst->id());
245
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);
248
249      route->link_list->push_back(info.linkUp);
250      if (latency)
251        *latency += info.linkUp->getLatency();
252      return;
253   }
254
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]);
259
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;
263
264   //node->router local link
265   route->link_list->push_back(myRouter->myNodes_[myCoords[3]*numLinksperLink_]);
266   if(latency) {
267     *latency += myRouter->myNodes_[myCoords[3]*numLinksperLink_]->getLatency();
268   }
269
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);
274   }
275
276   if(targetRouter!=myRouter){
277
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]]);
284         if(latency) {
285           *latency += currentRouter->greenLinks_[targetCoords[0]]->getLatency();
286         }
287         currentRouter=routers_[myCoords[0]*(numChassisPerGroup_*numBladesPerChassis_)+myCoords[1] * numBladesPerChassis_+targetCoords[0]];
288       }
289
290       if(currentRouter->chassis_!=0){
291         //go to the first chassis of our group
292         route->link_list->push_back(currentRouter->blackLinks_[0]);
293         if(latency) {
294           *latency += currentRouter->blackLinks_[0]->getLatency();
295         }
296         currentRouter=routers_[myCoords[0]*(numChassisPerGroup_*numBladesPerChassis_)+targetCoords[0]];
297       }
298
299       //go to destination group - the only optical hop 
300       route->link_list->push_back(currentRouter->blueLinks_[0]);
301       if(latency) {
302         *latency += currentRouter->blueLinks_[0]->getLatency();
303       }
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]]->getLatency();
313       }
314       currentRouter=routers_[targetCoords[0]*(numChassisPerGroup_*numBladesPerChassis_)+targetCoords[2]];
315     }
316
317     //same blade, but same chassis ?
318     if(targetRouter->chassis_ != currentRouter->chassis_){
319       route->link_list->push_back(currentRouter->blackLinks_[targetCoords[1]]);
320       if(latency) {
321         *latency += currentRouter->blackLinks_[targetCoords[1]]->getLatency();
322       }
323       currentRouter=routers_[targetCoords[0]*(numChassisPerGroup_*numBladesPerChassis_)+targetCoords[1]*numBladesPerChassis_+targetCoords[2]];
324     }
325   }
326
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);
331   }
332
333   //router->node local link
334   route->link_list->push_back(targetRouter->myNodes_[targetCoords[3]*numLinksperLink_+numLinksperLink_-1]);
335   if(latency) {
336     *latency += targetRouter->myNodes_[targetCoords[3]*numLinksperLink_+numLinksperLink_-1]->getLatency();
337   }
338
339   xbt_free(myCoords);
340   xbt_free(targetCoords);
341
342   
343 }
344   }
345 }