1 #include "surf_routing_cluster_fat_tree.hpp"
3 #include <boost/algorithm/string/split.hpp>
4 #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) {
30 if(this->levels == 0) {
33 this->nodesByLevel.resize(this->levels, 0);
34 unsigned int nodesRequired = 0;
37 for (unsigned int i = 0 ; i < this->levels ; i++) {
38 int nodesInThisLevel = 1;
40 for (unsigned int j = 0 ; j < i ; j++) {
41 nodesInThisLevel *= this->upperLevelNodesNumber[j];
44 for (unsigned int j = i+1 ; j < this->levels ; j++) {
45 nodesInThisLevel *= this->lowerLevelNodesNumber[j];
48 this->nodesByLevel[i] = nodesInThisLevel;
49 nodesRequired += nodesInThisLevel;
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());
59 // Nodes are totally ordered, by level and then by position, in this->nodes
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;
70 void AsClusterFatTree::getLevelPosition(const unsigned int level, int &position, int &size) {
71 if (level > this->levels - 1) {
78 for (unsigned int i = 0 ; i < level ; i++) {
79 tempPosition += this->nodesByLevel[i];
81 position = tempPosition;
82 size = this->nodesByLevel[level];
85 void AsClusterFatTree::addNodes(std::vector<int> const& id) {
86 for (unsigned int i = 0 ; i < id.size() ; i++) {
87 this->nodes.push_back(new FatTreeNode(id[i]));
91 void AsClusterFatTree::addLink(FatTreeNode *parent, FatTreeNode *child) {
93 if (parent->children.size() == this->nodesByLevel[parent->level] ||
94 child->parents.size() == this->nodesByLevel[child->level]) {
95 /* NB : This case should never happen, if this private function is not misused,
96 * so should we keep this test, keep it only for debug, throw an exception
97 * or get rid of it ? In all cases, anytime we get in there, code should be
100 xbt_die("I've been asked to create a link that could not possibly exist");
104 parent->children.push_back(child);
105 child->parents.push_back(parent);
107 // FatTreeLink *newLink;
109 // newLink = new FatTreeLink(parent, child, this->lowerLevelPortsNumber[parent->level]);
110 // this->links.insert(make_pair(make_pair(parent->id, child->id), newLink));
116 void AsClusterFatTree::parse_specific_arguments(sg_platf_cluster_cbarg_t
118 std::vector<string> parameters;
119 std::vector<string> tmp;
120 boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
123 // TODO : we have to check for zeros and negative numbers, or it might crash
124 if (parameters.size() != 4){
125 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
126 ", see the documentation for more informations");
127 // Well, there's no doc, yet
130 // The first parts of topo_parameters should be the levels number
131 this->levels = std::atoi(tmp[0].c_str()); // stoi() only in C++11...
133 // Then, a l-sized vector standing for the childs number by level
134 boost::split(tmp, parameters[1], boost::is_any_of(","));
135 if(tmp.size() != this->levels) {
136 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
137 ", see the documentation for more informations");
139 for(unsigned int i = 0 ; i < tmp.size() ; i++){
140 this->lowerLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
143 // Then, a l-sized vector standing for the parents number by level
144 boost::split(tmp, parameters[2], 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");
149 for(unsigned int i = 0 ; i < tmp.size() ; i++){
150 this->upperLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
153 // Finally, a l-sized vector standing for the ports number with the lower level
154 boost::split(tmp, parameters[3], 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");
160 for(unsigned int i = 0 ; i < tmp.size() ; i++){
161 this->lowerLevelPortsNumber.push_back(std::atoi(tmp[i].c_str()));
166 void AsClusterFatTree::generateDotFile(const string& filename) const {
168 /* Maybe should we get directly a char*, as open takes strings only beginning
171 file.open(filename.c_str(), ios::out | ios::trunc);
174 // That could also be greatly clarified with C++11
175 std::map<std::pair<int,int>,FatTreeLink*>::const_iterator iter;
176 file << "graph AsClusterFatTree {\n";
177 for (iter = this->links.begin() ; iter != this->links.end() ; iter++ ) {
178 for (unsigned int j = 0 ; j < iter->second->ports ; j++) {
179 file << iter->second->source->id
181 << iter->second->destination->id
189 std::cerr << "Unable to open file " << filename << std::endl;
194 FatTreeNode::FatTreeNode(int id, int level, int position) : id(id),
198 // FatTreeLink::FatTreeLink(FatTreeNode *source, FatTreeNode *destination,
199 // int ports) : source(source), destination(destination),