1 #include "surf_routing_cluster_fat_tree.hpp"
4 #include <boost/algorithm/string/split.hpp>
5 #include <boost/algorithm/string/classification.hpp>
10 AsClusterFatTree::AsClusterFatTree() : levels(0) {}
12 AsClusterFatTree::~AsClusterFatTree() {
13 for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
14 delete this->nodes[i];
18 void AsClusterFatTree::getRouteAndLatency(RoutingEdgePtr src,
20 sg_platf_route_cbarg_t into,
25 /* This function makes the assumption that parse_specific_arguments() and
26 * addNodes() have already been called
28 void AsClusterFatTree::create_links(sg_platf_cluster_cbarg_t cluster){
29 if(this->levels == 0) {
32 this->generateSwitches();
33 this->generateLabels();
35 // Nodes are totally ordered, by level and then by position, in this->nodes
36 for (unsigned int i = 0 ; i < this->levels ; i++) {
37 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
38 if(i != this->levels - 1) {
39 for(unsigned int l = 0 ; l < this->nodesByLevel[i+1] ; l++) {
40 this->connectNodeToParents(cluster, this->nodes[k]);
48 int AsClusterFatTree::connectNodeToParents(sg_platf_cluster_cbarg_t cluster,
50 FatTreeNode* currentParentNode;
51 int connectionsNumber = 0;
52 const int level = node->level;
53 currentParentNode = this->nodes[this->getLevelPosition(level + 1)];
54 for (unsigned int i = 0 ; i < this->nodesByLevel[level] ; i++ ) {
55 if(this->areRelated(currentParentNode, node)) {
56 this->addLink(cluster, currentParentNode, node->label[level + 1], node,
57 currentParentNode->label[level + 1]);
61 return connectionsNumber;
65 bool AsClusterFatTree::areRelated(FatTreeNode *parent, FatTreeNode *child) {
66 if (parent->level != child->level + 1) {
70 for (unsigned int i = 0 ; i < this->levels; i++) {
71 if (parent->label[i] != child->label[i] && i != parent->level) {
78 void AsClusterFatTree::generateSwitches() {
79 this->nodesByLevel.resize(this->levels, 0);
80 unsigned int nodesRequired = 0;
82 // We take care of the number of nodes by level
83 this->nodesByLevel[0] = 1;
84 for (unsigned int i = 0 ; i < this->levels ; i++) {
85 this->nodesByLevel[0] *= this->lowerLevelNodesNumber[i];
89 if(this->nodesByLevel[0] < this->nodes.size()) {
90 surf_parse_error("There is not enough nodes to fit to the described topology."
91 " Please check your platform description (We need %d nodes, we only got %zu)",
92 this->nodesByLevel[0], this->nodes.size());
96 for (unsigned int i = 0 ; i < this->levels ; i++) {
97 int nodesInThisLevel = 1;
99 for (unsigned int j = 0 ; j <= i ; j++) {
100 nodesInThisLevel *= this->upperLevelNodesNumber[j];
103 for (unsigned int j = i+1 ; j < this->levels ; j++) {
104 nodesInThisLevel *= this->lowerLevelNodesNumber[j];
107 this->nodesByLevel[i+1] = nodesInThisLevel;
108 nodesRequired += nodesInThisLevel;
112 // If we have to many compute nodes, we ditch them
113 if (this->nodesByLevel[0] > this->nodes.size()) {
114 for (unsigned int i = this->nodesByLevel[0] ; i < this->nodes.size() ; i++) {
115 delete this->nodes[i];
117 this->nodes.resize(this->nodesByLevel[0]);
120 // We create the switches
122 for (unsigned int i = 1 ; i < this->levels ; i++) {
123 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
124 FatTreeNode* newNode;
125 newNode = new FatTreeNode(--k, i, j);
126 newNode->children.resize(this->lowerLevelNodesNumber[i]);
127 newNode->parents.resize(this->upperLevelNodesNumber[i]);
128 this->nodes.push_back(newNode);
133 void AsClusterFatTree::generateLabels() {
134 // TODO : check if nodesByLevel and nodes are filled
135 for (unsigned int i = 0 ; i < this->levels ; i++) {
136 std::vector<int> maxLabel(this->nodesByLevel[i]);
137 std::vector<int> currentLabel(this->nodesByLevel[i], 0);
139 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
140 maxLabel[j] = j > i ?
141 this->lowerLevelNodesNumber[i] : this->upperLevelNodesNumber[i];
144 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
145 this->nodes[k]->label.assign(currentLabel.begin(), currentLabel.end());
150 int pos = currentLabel.size() - 1;
151 remainder = ++currentLabel[pos] / maxLabel[pos];
152 currentLabel[pos] = currentLabel[pos] % maxLabel[pos];
155 while(remainder != 0);
162 int AsClusterFatTree::getLevelPosition(const unsigned int level) {
163 if (level > this->levels - 1) {
164 // Well, that should never happen. Maybe should we throw instead.
167 int tempPosition = 0;
169 for (unsigned int i = 0 ; i < level ; i++) {
170 tempPosition += this->nodesByLevel[i];
175 void AsClusterFatTree::addComputeNodes(std::vector<int> const& id) {
176 for (size_t i = 0 ; i < id.size() ; i++) {
177 this->nodes.push_back(new FatTreeNode(id[i]));
181 void AsClusterFatTree::addLink(sg_platf_cluster_cbarg_t cluster,
182 FatTreeNode *parent, unsigned int parentPort,
183 FatTreeNode *child, unsigned int childPort) {
184 using std::make_pair;
187 FatTreeLink *newLink;
188 newLink = new FatTreeLink(cluster, parent, child);
190 parent->children[parentPort] = newLink;
191 child->parents[childPort] = newLink;
193 this->links.insert(make_pair(make_pair(parent->id, child->id), newLink));
199 void AsClusterFatTree::parse_specific_arguments(sg_platf_cluster_cbarg_t
201 std::vector<string> parameters;
202 std::vector<string> tmp;
203 boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
206 // TODO : we have to check for zeros and negative numbers, or it might crash
207 if (parameters.size() != 4){
208 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
209 ", see the documentation for more informations");
210 // Well, there's no doc, yet
213 // The first parts of topo_parameters should be the levels number
214 this->levels = std::atoi(tmp[0].c_str()); // stoi() only in C++11...
216 // Then, a l-sized vector standing for the childs number by level
217 boost::split(tmp, parameters[1], boost::is_any_of(","));
218 if(tmp.size() != this->levels) {
219 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
220 ", see the documentation for more informations");
222 for(size_t i = 0 ; i < tmp.size() ; i++){
223 this->lowerLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
226 // Then, a l-sized vector standing for the parents number by level
227 boost::split(tmp, parameters[2], boost::is_any_of(","));
228 if(tmp.size() != this->levels) {
229 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
230 ", see the documentation for more informations");
232 for(size_t i = 0 ; i < tmp.size() ; i++){
233 this->upperLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
236 // Finally, a l-sized vector standing for the ports number with the lower level
237 boost::split(tmp, parameters[3], boost::is_any_of(","));
238 if(tmp.size() != this->levels) {
239 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
240 ", see the documentation for more informations");
243 for(size_t i = 0 ; i < tmp.size() ; i++){
244 this->lowerLevelPortsNumber.push_back(std::atoi(tmp[i].c_str()));
249 void AsClusterFatTree::generateDotFile(const string& filename) const {
251 /* Maybe should we get directly a char*, as open takes strings only beginning
254 file.open(filename.c_str(), ios::out | ios::trunc);
257 // That could also be greatly clarified with C++11
258 std::map<std::pair<int,int>,FatTreeLink*>::const_iterator iter;
259 file << "graph AsClusterFatTree {\n";
260 for (iter = this->links.begin() ; iter != this->links.end() ; iter++ ) {
261 file << iter->second->source->id
263 << iter->second->destination->id
270 std::cerr << "Unable to open file " << filename << std::endl;
275 FatTreeNode::FatTreeNode(int id, int level, int position) : id(id),
279 FatTreeLink::FatTreeLink(sg_platf_cluster_cbarg_t cluster, FatTreeNode *source,
280 FatTreeNode *destination) : source(source),
281 destination(destination) {
282 static int uniqueId = 0;
283 s_sg_platf_link_cbarg_t linkTemplate;
284 linkTemplate.bandwidth = cluster->bw;
285 linkTemplate.latency = cluster->lat;
286 linkTemplate.state = SURF_RESOURCE_ON;
287 linkTemplate.policy = cluster->sharing_policy; // Maybe should we do sthg with that ?
291 linkTemplate.id = bprintf("link_from_%d_to_%d_%d_UP", source->id, destination->id, uniqueId);
292 sg_platf_new_link(&linkTemplate);
293 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, linkTemplate.id, SURF_LINK_LEVEL);
294 this->linkUp = link; // check link?
295 linkTemplate.id = bprintf("link_from_%d_to_%d_%d_DOWN", source->id, destination->id, uniqueId);
296 sg_platf_new_link(&linkTemplate);
297 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, linkTemplate.id, SURF_LINK_LEVEL);
298 this->linkDown = link; // check link ?