1 #include "surf_routing_cluster_fat_tree.hpp"
4 #include <boost/algorithm/string/split.hpp>
5 #include <boost/algorithm/string/classification.hpp>
11 AS_t model_fat_tree_cluster_create(void)
13 return new AsClusterFatTree();
16 AsClusterFatTree::AsClusterFatTree() : levels(0) {}
18 AsClusterFatTree::~AsClusterFatTree() {
19 for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
20 delete this->nodes[i];
24 bool AsClusterFatTree::isInSubTree(FatTreeNode *root, FatTreeNode *node) {
25 for (unsigned int i = 0 ; i < node->level ; i++) {
26 if(root->label[i] != node->label[i]) {
31 for (unsigned int i = root->level + 1 ; i < this->levels ; i++) {
32 if(root->label[i] != node->label[i]) {
39 void AsClusterFatTree::getRouteAndLatency(RoutingEdgePtr src,
41 sg_platf_route_cbarg_t into,
43 FatTreeNode *source, *destination, *currentNode;
44 std::vector<NetworkLink*> route;
45 source = this->nodes.find(src->getId())->second;
46 destination = this->nodes.find(dst->getId())->second;
53 while (!isInSubTree(currentNode, destination)) {
54 int d, k; // as in d-mod-k
55 d = destination->position;
57 for (unsigned int i = 0 ; i < currentNode->level ; i++) {
58 d /= this->upperLevelNodesNumber[i];
60 k = this->upperLevelNodesNumber[currentNode->level] *
61 this->lowerLevelNodesNumber[currentNode->level];
63 route.push_back(currentNode->parents[d]->upLink);
65 *latency += currentNode->parents[d]->upLink->getLatency();
67 currentNode = currentNode->parents[d]->upNode;
71 while(currentNode != destination) {
72 for(unsigned int i = 0 ; i < currentNode->children.size() ; i++) {
73 if(i % this->lowerLevelNodesNumber[currentNode->level] ==
74 destination->label[currentNode->level]) {
75 route.push_back(currentNode->children[i]->downLink);
77 *latency += currentNode->children[i]->downLink->getLatency();
79 currentNode = currentNode->children[i]->downNode;
84 for (unsigned int i = 0 ; i < route.size() ; i++) {
85 xbt_dynar_push_as(into->link_list, void*, route[i]);
90 /* This function makes the assumption that parse_specific_arguments() and
91 * addNodes() have already been called
93 void AsClusterFatTree::create_links(sg_platf_cluster_cbarg_t cluster){
94 if(this->levels == 0) {
97 this->generateSwitches();
98 this->generateLabels();
100 // Nodes are totally ordered, by level and then by position, in this->nodes
101 for (unsigned int i = 0 ; i < this->levels ; i++) {
102 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
103 if(i != this->levels - 1) {
104 for(unsigned int l = 0 ; l < this->nodesByLevel[i+1] ; l++) {
105 this->connectNodeToParents(cluster, this->nodes[k]);
113 int AsClusterFatTree::connectNodeToParents(sg_platf_cluster_cbarg_t cluster,
115 FatTreeNode* currentParentNode;
116 int connectionsNumber = 0;
117 const int level = node->level;
118 currentParentNode = this->nodes[this->getLevelPosition(level + 1)];
119 for (unsigned int i = 0 ; i < this->nodesByLevel[level] ; i++ ) {
120 if(this->areRelated(currentParentNode, node)) {
121 for (unsigned int j = 0 ; j < this->lowerLevelPortsNumber[level + 1] ; j++) {
122 this->addLink(cluster, currentParentNode, node->label[level + 1] +
123 j * this->lowerLevelNodesNumber[level + 1], node,
124 currentParentNode->label[level + 1] +
125 j * this->upperLevelNodesNumber[level + 1]);
130 return connectionsNumber;
134 bool AsClusterFatTree::areRelated(FatTreeNode *parent, FatTreeNode *child) {
135 if (parent->level != child->level + 1) {
139 for (unsigned int i = 0 ; i < this->levels; i++) {
140 if (parent->label[i] != child->label[i] && i != parent->level) {
147 void AsClusterFatTree::generateSwitches() {
148 this->nodesByLevel.resize(this->levels, 0);
149 unsigned int nodesRequired = 0;
151 // We take care of the number of nodes by level
152 this->nodesByLevel[0] = 1;
153 for (unsigned int i = 0 ; i < this->levels ; i++) {
154 this->nodesByLevel[0] *= this->lowerLevelNodesNumber[i];
158 if(this->nodesByLevel[0] > this->nodes.size()) {
159 surf_parse_error("There is not enough nodes to fit to the described topology."
160 " Please check your platform description (We need %d nodes, we only got %zu)",
161 this->nodesByLevel[0], this->nodes.size());
165 for (unsigned int i = 0 ; i < this->levels ; i++) {
166 int nodesInThisLevel = 1;
168 for (unsigned int j = 0 ; j <= i ; j++) {
169 nodesInThisLevel *= this->upperLevelNodesNumber[j];
172 for (unsigned int j = i+1 ; j < this->levels ; j++) {
173 nodesInThisLevel *= this->lowerLevelNodesNumber[j];
176 this->nodesByLevel[i+1] = nodesInThisLevel;
177 nodesRequired += nodesInThisLevel;
181 // If we have to many compute nodes, we ditch them
182 if (this->nodesByLevel[0] > this->nodes.size()) {
183 for (unsigned int i = this->nodesByLevel[0] ; i < this->nodes.size() ; i++) {
184 delete this->nodes[i];
188 // We create the switches
190 for (unsigned int i = 0 ; i < this->levels ; i++) {
191 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
192 FatTreeNode* newNode;
193 newNode = new FatTreeNode(--k, i + 1, j);
194 newNode->children.resize(this->lowerLevelNodesNumber[i] *
195 this->lowerLevelPortsNumber[i]);
196 if (i != this->levels - 1) {
197 newNode->parents.resize(this->upperLevelNodesNumber[i + 1]);
199 this->nodes.insert(std::make_pair(k,newNode));
204 void AsClusterFatTree::generateLabels() {
205 // TODO : check if nodesByLevel and nodes are filled
206 for (unsigned int i = 0 ; i < this->levels ; i++) {
207 std::vector<int> maxLabel(this->nodesByLevel[i]);
208 std::vector<int> currentLabel(this->nodesByLevel[i], 0);
210 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
211 maxLabel[j] = j > i ?
212 this->lowerLevelNodesNumber[i] : this->upperLevelNodesNumber[i];
215 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
216 this->nodes[k]->label.assign(currentLabel.begin(), currentLabel.end());
221 int pos = currentLabel.size() - 1;
222 remainder = ++currentLabel[pos] / maxLabel[pos];
223 currentLabel[pos] = currentLabel[pos] % maxLabel[pos];
226 while(remainder != 0);
233 int AsClusterFatTree::getLevelPosition(const unsigned int level) {
234 if (level > this->levels - 1) {
235 // Well, that should never happen. Maybe should we throw instead.
238 int tempPosition = 0;
240 for (unsigned int i = 0 ; i < level ; i++) {
241 tempPosition += this->nodesByLevel[i];
246 void AsClusterFatTree::addComputeNode(int id) {
247 using std::make_pair;
248 static int position = 0;
249 FatTreeNode* newNode;
250 newNode = new FatTreeNode(id, 0, position);
251 newNode->parents.resize(this->upperLevelNodesNumber[0] * this->lowerLevelPortsNumber[0]);
252 this->nodes.insert(make_pair(id,newNode));
255 void AsClusterFatTree::addLink(sg_platf_cluster_cbarg_t cluster,
256 FatTreeNode *parent, unsigned int parentPort,
257 FatTreeNode *child, unsigned int childPort) {
258 FatTreeLink *newLink;
259 newLink = new FatTreeLink(cluster, parent, child);
261 parent->children[parentPort] = newLink;
262 child->parents[childPort] = newLink;
264 this->links.push_back(newLink);
270 void AsClusterFatTree::parse_specific_arguments(sg_platf_cluster_cbarg_t
272 std::vector<string> parameters;
273 std::vector<string> tmp;
274 boost::split(parameters, cluster->topo_parameters, boost::is_any_of(";"));
277 // TODO : we have to check for zeros and negative numbers, or it might crash
278 if (parameters.size() != 4){
279 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
280 ", see the documentation for more informations");
281 // Well, there's no doc, yet
284 // The first parts of topo_parameters should be the levels number
285 this->levels = std::atoi(parameters[0].c_str()); // stoi() only in C++11...
287 // Then, a l-sized vector standing for the childs number by level
288 boost::split(tmp, parameters[1], boost::is_any_of(","));
289 if(tmp.size() != this->levels) {
290 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
291 ", see the documentation for more informations");
293 for(size_t i = 0 ; i < tmp.size() ; i++){
294 this->lowerLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
297 // Then, a l-sized vector standing for the parents number by level
298 boost::split(tmp, parameters[2], boost::is_any_of(","));
299 if(tmp.size() != this->levels) {
300 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
301 ", see the documentation for more informations");
303 for(size_t i = 0 ; i < tmp.size() ; i++){
304 this->upperLevelNodesNumber.push_back(std::atoi(tmp[i].c_str()));
307 // Finally, a l-sized vector standing for the ports number with the lower level
308 boost::split(tmp, parameters[3], boost::is_any_of(","));
309 if(tmp.size() != this->levels) {
310 surf_parse_error("Fat trees are defined by the levels number and 3 vectors"
311 ", see the documentation for more informations");
314 for(size_t i = 0 ; i < tmp.size() ; i++){
315 this->lowerLevelPortsNumber.push_back(std::atoi(tmp[i].c_str()));
320 void AsClusterFatTree::generateDotFile(const string& filename) const {
322 /* Maybe should we get directly a char*, as open takes strings only beginning
325 file.open(filename.c_str(), ios::out | ios::trunc);
328 // That could also be greatly clarified with C++11
329 std::vector<FatTreeLink*>::const_iterator iter;
330 file << "graph AsClusterFatTree {\n";
331 for (iter = this->links.begin() ; iter != this->links.end() ; iter++ ) {
332 file << (*iter)->downNode->id
334 << (*iter)->upNode->id
341 std::cerr << "Unable to open file " << filename << std::endl;
346 FatTreeNode::FatTreeNode(int id, int level, int position) : id(id),
350 FatTreeLink::FatTreeLink(sg_platf_cluster_cbarg_t cluster, FatTreeNode *downNode,
351 FatTreeNode *upNode) : upNode(upNode),
353 static int uniqueId = 0;
354 s_sg_platf_link_cbarg_t linkTemplate;
355 linkTemplate.bandwidth = cluster->bw;
356 linkTemplate.latency = cluster->lat;
357 linkTemplate.state = SURF_RESOURCE_ON;
358 linkTemplate.policy = cluster->sharing_policy; // Maybe should we do sthg with that ?
362 linkTemplate.id = bprintf("link_from_%d_to_%d_%d_UP", downNode->id, upNode->id, uniqueId);
363 sg_platf_new_link(&linkTemplate);
364 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, linkTemplate.id, SURF_LINK_LEVEL);
365 this->upLink = link; // check link?
366 linkTemplate.id = bprintf("link_from_%d_to_%d_%d_DOWN", downNode->id, upNode->id, uniqueId);
367 sg_platf_new_link(&linkTemplate);
368 link = (NetworkLink*) xbt_lib_get_or_null(link_lib, linkTemplate.id, SURF_LINK_LEVEL);
369 this->downLink = link; // check link ?