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 for (unsigned int j = 0 ; j < this->lowerLevelPortsNumber[level + 1] ; j++) {
57 this->addLink(cluster, currentParentNode, node->label[level + 1] +
58 j * this->lowerLevelNodesNumber[level + 1], node,
59 currentParentNode->label[level + 1] +
60 j * this->upperLevelNodesNumber[level + 1]);
65 return connectionsNumber;
69 bool AsClusterFatTree::areRelated(FatTreeNode *parent, FatTreeNode *child) {
70 if (parent->level != child->level + 1) {
74 for (unsigned int i = 0 ; i < this->levels; i++) {
75 if (parent->label[i] != child->label[i] && i != parent->level) {
82 void AsClusterFatTree::generateSwitches() {
83 this->nodesByLevel.resize(this->levels, 0);
84 unsigned int nodesRequired = 0;
86 // We take care of the number of nodes by level
87 this->nodesByLevel[0] = 1;
88 for (unsigned int i = 0 ; i < this->levels ; i++) {
89 this->nodesByLevel[0] *= this->lowerLevelNodesNumber[i];
93 if(this->nodesByLevel[0] < this->nodes.size()) {
94 surf_parse_error("There is not enough nodes to fit to the described topology."
95 " Please check your platform description (We need %d nodes, we only got %zu)",
96 this->nodesByLevel[0], this->nodes.size());
100 for (unsigned int i = 0 ; i < this->levels ; i++) {
101 int nodesInThisLevel = 1;
103 for (unsigned int j = 0 ; j <= i ; j++) {
104 nodesInThisLevel *= this->upperLevelNodesNumber[j];
107 for (unsigned int j = i+1 ; j < this->levels ; j++) {
108 nodesInThisLevel *= this->lowerLevelNodesNumber[j];
111 this->nodesByLevel[i+1] = nodesInThisLevel;
112 nodesRequired += nodesInThisLevel;
116 // If we have to many compute nodes, we ditch them
117 if (this->nodesByLevel[0] > this->nodes.size()) {
118 for (unsigned int i = this->nodesByLevel[0] ; i < this->nodes.size() ; i++) {
119 delete this->nodes[i];
121 this->nodes.resize(this->nodesByLevel[0]);
124 // We create the switches
126 for (unsigned int i = 0 ; i < this->levels ; i++) {
127 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
128 FatTreeNode* newNode;
129 newNode = new FatTreeNode(--k, i + 1, j);
130 newNode->children.resize(this->lowerLevelNodesNumber[i] *
131 this->lowerLevelPortsNumber[i]);
132 if (i != this->levels - 1) {
133 newNode->parents.resize(this->upperLevelNodesNumber[i + 1]);
135 this->nodes.push_back(newNode);
140 void AsClusterFatTree::generateLabels() {
141 // TODO : check if nodesByLevel and nodes are filled
142 for (unsigned int i = 0 ; i < this->levels ; i++) {
143 std::vector<int> maxLabel(this->nodesByLevel[i]);
144 std::vector<int> currentLabel(this->nodesByLevel[i], 0);
146 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
147 maxLabel[j] = j > i ?
148 this->lowerLevelNodesNumber[i] : this->upperLevelNodesNumber[i];
151 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
152 this->nodes[k]->label.assign(currentLabel.begin(), currentLabel.end());
157 int pos = currentLabel.size() - 1;
158 remainder = ++currentLabel[pos] / maxLabel[pos];
159 currentLabel[pos] = currentLabel[pos] % maxLabel[pos];
162 while(remainder != 0);
169 int AsClusterFatTree::getLevelPosition(const unsigned int level) {
170 if (level > this->levels - 1) {
171 // Well, that should never happen. Maybe should we throw instead.
174 int tempPosition = 0;
176 for (unsigned int i = 0 ; i < level ; i++) {
177 tempPosition += this->nodesByLevel[i];
182 void AsClusterFatTree::addComputeNodes(std::vector<int> const& id) {
183 FatTreeNode* newNode;
184 for (size_t i = 0 ; i < id.size() ; i++) {
185 newNode = new FatTreeNode(id[i], 0, i);
186 newNode->parents.resize(this->upperLevelNodesNumber[0] * this->lowerLevelPortsNumber[i]);
187 this->nodes.push_back(newNode);
191 void AsClusterFatTree::addLink(sg_platf_cluster_cbarg_t cluster,
192 FatTreeNode *parent, unsigned int parentPort,
193 FatTreeNode *child, unsigned int childPort) {
194 using std::make_pair;
197 FatTreeLink *newLink;
198 newLink = new FatTreeLink(cluster, parent, child);
200 parent->children[parentPort] = newLink;
201 child->parents[childPort] = newLink;
203 this->links.insert(make_pair(make_pair(parent->id, child->id), newLink));
209 void AsClusterFatTree::parse_specific_arguments(sg_platf_cluster_cbarg_t
211 std::vector<string> parameters;
212 std::vector<string> tmp;
213 boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
216 // TODO : we have to check for zeros and negative numbers, or it might crash
217 if (parameters.size() != 4){
218 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
219 ", see the documentation for more informations");
220 // Well, there's no doc, yet
223 // The first parts of topo_parameters should be the levels number
224 this->levels = std::atoi(tmp[0].c_str()); // stoi() only in C++11...
226 // Then, a l-sized vector standing for the childs number by level
227 boost::split(tmp, parameters[1], 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->lowerLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
236 // Then, a l-sized vector standing for the parents number by level
237 boost::split(tmp, parameters[2], 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");
242 for(size_t i = 0 ; i < tmp.size() ; i++){
243 this->upperLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
246 // Finally, a l-sized vector standing for the ports number with the lower level
247 boost::split(tmp, parameters[3], boost::is_any_of(","));
248 if(tmp.size() != this->levels) {
249 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
250 ", see the documentation for more informations");
253 for(size_t i = 0 ; i < tmp.size() ; i++){
254 this->lowerLevelPortsNumber.push_back(std::atoi(tmp[i].c_str()));
259 void AsClusterFatTree::generateDotFile(const string& filename) const {
261 /* Maybe should we get directly a char*, as open takes strings only beginning
264 file.open(filename.c_str(), ios::out | ios::trunc);
267 // That could also be greatly clarified with C++11
268 std::map<std::pair<int,int>,FatTreeLink*>::const_iterator iter;
269 file << "graph AsClusterFatTree {\n";
270 for (iter = this->links.begin() ; iter != this->links.end() ; iter++ ) {
271 file << iter->second->source->id
273 << iter->second->destination->id
280 std::cerr << "Unable to open file " << filename << std::endl;
285 FatTreeNode::FatTreeNode(int id, int level, int position) : id(id),
289 FatTreeLink::FatTreeLink(sg_platf_cluster_cbarg_t cluster, FatTreeNode *source,
290 FatTreeNode *destination) : source(source),
291 destination(destination) {
292 static int uniqueId = 0;
293 s_sg_platf_link_cbarg_t linkTemplate;
294 linkTemplate.bandwidth = cluster->bw;
295 linkTemplate.latency = cluster->lat;
296 linkTemplate.state = SURF_RESOURCE_ON;
297 linkTemplate.policy = cluster->sharing_policy; // Maybe should we do sthg with that ?
301 linkTemplate.id = bprintf("link_from_%d_to_%d_%d_UP", source->id, destination->id, uniqueId);
302 sg_platf_new_link(&linkTemplate);
303 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, linkTemplate.id, SURF_LINK_LEVEL);
304 this->linkUp = link; // check link?
305 linkTemplate.id = bprintf("link_from_%d_to_%d_%d_DOWN", source->id, destination->id, uniqueId);
306 sg_platf_new_link(&linkTemplate);
307 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, linkTemplate.id, SURF_LINK_LEVEL);
308 this->linkDown = link; // check link ?