Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge with master
[simgrid.git] / src / surf / surf_routing_cluster_fat_tree.cpp
1 #include "surf_routing_cluster_fat_tree.hpp"
2 #include "xbt/lib.h"
3
4 #include <boost/algorithm/string/split.hpp>
5 #include <boost/algorithm/string/classification.hpp>
6 #include <iostream>
7 #include <fstream>
8
9
10 AsClusterFatTree::AsClusterFatTree() : levels(0) {}
11
12 AsClusterFatTree::~AsClusterFatTree() {
13   for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
14     delete this->nodes[i];
15   }
16 }
17
18 void AsClusterFatTree::getRouteAndLatency(RoutingEdgePtr src,
19                                           RoutingEdgePtr dst,
20                                           sg_platf_route_cbarg_t into,
21                                           double *latency) {
22   // TODO
23 }
24
25 /* This function makes the assumption that parse_specific_arguments() and
26  * addNodes() have already been called
27  */
28 void AsClusterFatTree::create_links(sg_platf_cluster_cbarg_t cluster) {
29
30   if(this->levels == 0) {
31     return;
32   }
33   this->nodesByLevel.resize(this->levels, 0);
34   unsigned int nodesRequired = 0;
35
36
37   for (unsigned int i = 0 ; i < this->levels ; i++) {
38     int nodesInThisLevel = 1;
39       
40     for (unsigned int j = 0 ;  j < i ; j++) {
41       nodesInThisLevel *= this->upperLevelNodesNumber[j];
42     }
43       
44     for (unsigned int j = i+1 ; j < this->levels ; j++) {
45       nodesInThisLevel *= this->lowerLevelNodesNumber[j];
46     }
47
48     this->nodesByLevel[i] = nodesInThisLevel;
49     nodesRequired += nodesInThisLevel;
50   }
51    
52   if(nodesRequired > this->nodes.size()) {
53     surf_parse_error("There is not enough nodes to fit to the described topology."
54                      " Please check your platform description (We need %d nodes, we only got %lu)",
55                      nodesRequired, this->nodes.size());
56     return;
57   }
58
59   // Nodes are totally ordered, by level and then by position, in this->nodes
60   int k = 0;
61   for (unsigned int i = 0 ; i < this->levels ; i++) {
62     for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
63       this->nodes[k]->level = i;
64       this->nodes[k]->position = j;
65       if(i != 0) {
66         int position, size;
67         this->getLevelPosition(i - 1, &position, &size); // TODO : check position and size ?
68         /* We create the connexions between this nodes and all its parents
69          */
70         for (unsigned int l = this->upperLevelNodesNumber[i] * j ;
71              l < this->upperLevelNodesNumber[i] * (j + 1) ; l++)
72           this->addLink(cluster, this->nodes[position + l], this->nodes[k]);
73       }
74       k++;
75     }
76   }
77 }
78
79 void AsClusterFatTree::getLevelPosition(const unsigned  int level, int *position, int *size) {
80   if (level > this->levels - 1) {
81     *position = -1;
82     *size =  -1;
83     return;
84   }
85   int tempPosition = 0;
86
87   for (unsigned int i = 0 ; i < level ; i++) {
88     tempPosition += this->nodesByLevel[i];
89   }
90   *position = tempPosition;
91   *size = this->nodesByLevel[level];
92 }
93
94 void AsClusterFatTree::addNodes(std::vector<int> const& id) {
95   for (unsigned int  i = 0 ; i < id.size() ; i++) {
96     this->nodes.push_back(new FatTreeNode(id[i]));
97   }
98 }
99
100 void AsClusterFatTree::addLink(sg_platf_cluster_cbarg_t cluster, FatTreeNode *parent,
101                                FatTreeNode *child) {
102   using std::make_pair;
103   if (parent->children.size() == this->nodesByLevel[parent->level] ||
104       child->parents.size()   == this->nodesByLevel[child->level]) {
105     /* NB : This case should never happen, if this private function is not misused,
106      * so should we keep this test, keep it only for debug, throw an exception
107      * or get rid of it ? In all cases, anytime we get in there, code should be
108      * fixed
109      */
110     xbt_die("I've been asked to create a link that could not possibly exist");
111     return;
112   }
113
114   parent->children.push_back(child);
115   child->parents.push_back(parent);
116
117   FatTreeLink *newLink;
118
119   newLink = new FatTreeLink(cluster, parent, child, this->lowerLevelPortsNumber[parent->level]);
120    this->links.insert(make_pair(make_pair(parent->id, child->id), newLink));
121
122   
123
124 }
125
126 void AsClusterFatTree::parse_specific_arguments(sg_platf_cluster_cbarg_t 
127                                                 cluster) {
128   std::vector<string> parameters;
129   std::vector<string> tmp;
130   boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
131  
132
133   // TODO : we have to check for zeros and negative numbers, or it might crash
134   if (parameters.size() != 4){
135     surf_parse_error("Fat trees are defined by the levels number and 3 vectors" 
136                      ", see the documentation for more informations");
137     // Well, there's no doc, yet
138   }
139
140   // The first parts of topo_parameters should be the levels number
141   this->levels = std::atoi(tmp[0].c_str()); // stoi() only in C++11...
142   
143   // Then, a l-sized vector standing for the childs number by level
144   boost::split(tmp, parameters[1], boost::is_any_of(","));
145   if(tmp.size() != this->levels) {
146     surf_parse_error("Fat trees are defined by the levels number and 3 vectors" 
147                      ", see the documentation for more informations"); 
148   }
149   for(unsigned int i = 0 ; i < tmp.size() ; i++){
150     this->lowerLevelNodesNumber.push_back(std::atoi(tmp[i].c_str())); 
151   }
152   
153   // Then, a l-sized vector standing for the parents number by level
154   boost::split(tmp, parameters[2], boost::is_any_of(","));
155   if(tmp.size() != this->levels) {
156     surf_parse_error("Fat trees are defined by the levels number and 3 vectors" 
157                      ", see the documentation for more informations"); 
158   }
159   for(unsigned int i = 0 ; i < tmp.size() ; i++){
160     this->upperLevelNodesNumber.push_back(std::atoi(tmp[i].c_str())); 
161   }
162   
163   // Finally, a l-sized vector standing for the ports number with the lower level
164   boost::split(tmp, parameters[3], boost::is_any_of(","));
165   if(tmp.size() != this->levels) {
166     surf_parse_error("Fat trees are defined by the levels number and 3 vectors" 
167                      ", see the documentation for more informations"); 
168     
169   }
170   for(unsigned int i = 0 ; i < tmp.size() ; i++){
171     this->lowerLevelPortsNumber.push_back(std::atoi(tmp[i].c_str())); 
172   }
173 }
174
175
176 void AsClusterFatTree::generateDotFile(const string& filename) const {
177   ofstream file;
178   /* Maybe should we get directly a char*, as open takes strings only beginning
179    * with C++11...
180    */
181   file.open(filename.c_str(), ios::out | ios::trunc); 
182   
183   if(file.is_open()) {
184     // That could also be greatly clarified with C++11
185     std::map<std::pair<int,int>,FatTreeLink*>::const_iterator iter;
186     file << "graph AsClusterFatTree {\n";
187     for (iter = this->links.begin() ; iter != this->links.end() ; iter++ ) {
188       for (unsigned int j = 0 ; j < iter->second->ports ; j++) {
189         file << iter->second->source->id
190              << " -- "
191              << iter->second->destination->id
192              << ";\n";
193       }
194     }
195     file << "}";
196     file.close();
197   }
198   else {
199     std::cerr << "Unable to open file " << filename << std::endl;
200     return;
201   }
202 }
203
204 FatTreeNode::FatTreeNode(int id, int level, int position) : id(id),
205                                                             level(level),
206                                                             position(position){}
207
208 FatTreeLink::FatTreeLink(sg_platf_cluster_cbarg_t cluster, FatTreeNode *source,
209                          FatTreeNode *destination,
210                          unsigned int ports) : ports(ports), source(source),
211                                                destination(destination) {
212   s_sg_platf_link_cbarg_t linkTemplate;
213   linkTemplate.bandwidth = cluster->bw;
214   linkTemplate.latency = cluster->lat;
215   linkTemplate.state = SURF_RESOURCE_ON;
216   linkTemplate.policy = cluster->sharing_policy; // Maybe should we do sthg with that ?
217
218   for(unsigned int i = 0 ; i < ports ; i++) {
219     NetworkLink* link;
220     linkTemplate.id = bprintf("link_from_%d_to_%d_%d_UP", source->id, destination->id, i);
221     sg_platf_new_link(&linkTemplate);
222     link = (NetworkLink*) xbt_lib_get_or_null(link_lib, linkTemplate.id, SURF_LINK_LEVEL);
223     this->linksUp.push_back(link); // check link?
224     linkTemplate.id = bprintf("link_from_%d_to_%d_%d_DOWN", source->id, destination->id, i);
225     sg_platf_new_link(&linkTemplate);
226     link = (NetworkLink*) xbt_lib_get_or_null(link_lib, linkTemplate.id, SURF_LINK_LEVEL);
227     this->linksDown.push_back(link); // check link ?
228   }
229 }