Logo AND Algorithmique Numérique Distribuée

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