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_fatTree_cluster_create(void) {
12 return new AsClusterFatTree();
15 AsClusterFatTree::AsClusterFatTree() : levels(0) {}
17 AsClusterFatTree::~AsClusterFatTree() {
18 for (unsigned int i = 0 ; i < this->nodes.size() ; i++) {
19 delete this->nodes[i];
23 bool AsClusterFatTree::isInSubTree(FatTreeNode *root, FatTreeNode *node) {
24 for (unsigned int i = 0 ; i < node->level ; i++) {
25 if(root->label[i] != node->label[i]) {
30 for (unsigned int i = root->level + 1 ; i < this->levels ; i++) {
31 if(root->label[i] != node->label[i]) {
38 void AsClusterFatTree::getRouteAndLatency(RoutingEdgePtr src,
40 sg_platf_route_cbarg_t into,
42 FatTreeNode *source, *destination, *currentNode;
43 std::vector<NetworkLink*> route;
44 source = this->nodes.find(src->getId())->second;
45 destination = this->nodes.find(dst->getId())->second;
52 while (!isInSubTree(currentNode, destination)) {
53 int d, k; // as in d-mod-k
54 d = destination->position;
56 for (unsigned int i = 0 ; i < currentNode->level ; i++) {
57 d /= this->upperLevelNodesNumber[i];
59 k = this->upperLevelNodesNumber[currentNode->level] *
60 this->lowerLevelNodesNumber[currentNode->level];
62 route.push_back(currentNode->parents[d]->upLink);
64 *latency += currentNode->parents[d]->upLink->getLatency();
66 currentNode = currentNode->parents[d]->upNode;
70 while(currentNode != destination) {
71 for(unsigned int i = 0 ; i < currentNode->children.size() ; i++) {
72 if(i % this->lowerLevelNodesNumber[currentNode->level] ==
73 destination->label[currentNode->level]) {
74 route.push_back(currentNode->children[i]->downLink);
76 *latency += currentNode->children[i]->downLink->getLatency();
78 currentNode = currentNode->children[i]->downNode;
83 for (unsigned int i = 0 ; i < route.size() ; i++) {
84 xbt_dynar_push_as(into->link_list, void*, route[i]);
89 /* This function makes the assumption that parse_specific_arguments() and
90 * addNodes() have already been called
92 void AsClusterFatTree::create_links(sg_platf_cluster_cbarg_t cluster){
93 if(this->levels == 0) {
96 this->generateSwitches();
97 this->generateLabels();
99 // Nodes are totally ordered, by level and then by position, in this->nodes
100 for (unsigned int i = 0 ; i < this->levels ; i++) {
101 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
102 if(i != this->levels - 1) {
103 for(unsigned int l = 0 ; l < this->nodesByLevel[i+1] ; l++) {
104 this->connectNodeToParents(cluster, this->nodes[k]);
112 int AsClusterFatTree::connectNodeToParents(sg_platf_cluster_cbarg_t cluster,
114 FatTreeNode* currentParentNode;
115 int connectionsNumber = 0;
116 const int level = node->level;
117 currentParentNode = this->nodes[this->getLevelPosition(level + 1)];
118 for (unsigned int i = 0 ; i < this->nodesByLevel[level] ; i++ ) {
119 if(this->areRelated(currentParentNode, node)) {
120 for (unsigned int j = 0 ; j < this->lowerLevelPortsNumber[level + 1] ; j++) {
121 this->addLink(cluster, currentParentNode, node->label[level + 1] +
122 j * this->lowerLevelNodesNumber[level + 1], node,
123 currentParentNode->label[level + 1] +
124 j * this->upperLevelNodesNumber[level + 1]);
129 return connectionsNumber;
133 bool AsClusterFatTree::areRelated(FatTreeNode *parent, FatTreeNode *child) {
134 if (parent->level != child->level + 1) {
138 for (unsigned int i = 0 ; i < this->levels; i++) {
139 if (parent->label[i] != child->label[i] && i != parent->level) {
146 void AsClusterFatTree::generateSwitches() {
147 this->nodesByLevel.resize(this->levels, 0);
148 unsigned int nodesRequired = 0;
150 // We take care of the number of nodes by level
151 this->nodesByLevel[0] = 1;
152 for (unsigned int i = 0 ; i < this->levels ; i++) {
153 this->nodesByLevel[0] *= this->lowerLevelNodesNumber[i];
157 if(this->nodesByLevel[0] < this->nodes.size()) {
158 surf_parse_error("There is not enough nodes to fit to the described topology."
159 " Please check your platform description (We need %d nodes, we only got %zu)",
160 this->nodesByLevel[0], this->nodes.size());
164 for (unsigned int i = 0 ; i < this->levels ; i++) {
165 int nodesInThisLevel = 1;
167 for (unsigned int j = 0 ; j <= i ; j++) {
168 nodesInThisLevel *= this->upperLevelNodesNumber[j];
171 for (unsigned int j = i+1 ; j < this->levels ; j++) {
172 nodesInThisLevel *= this->lowerLevelNodesNumber[j];
175 this->nodesByLevel[i+1] = nodesInThisLevel;
176 nodesRequired += nodesInThisLevel;
180 // If we have to many compute nodes, we ditch them
181 if (this->nodesByLevel[0] > this->nodes.size()) {
182 for (unsigned int i = this->nodesByLevel[0] ; i < this->nodes.size() ; i++) {
183 delete this->nodes[i];
187 // We create the switches
189 for (unsigned int i = 0 ; i < this->levels ; i++) {
190 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
191 FatTreeNode* newNode;
192 newNode = new FatTreeNode(--k, i + 1, j);
193 newNode->children.resize(this->lowerLevelNodesNumber[i] *
194 this->lowerLevelPortsNumber[i]);
195 if (i != this->levels - 1) {
196 newNode->parents.resize(this->upperLevelNodesNumber[i + 1]);
198 this->nodes.insert(std::make_pair(k,newNode));
203 void AsClusterFatTree::generateLabels() {
204 // TODO : check if nodesByLevel and nodes are filled
205 for (unsigned int i = 0 ; i < this->levels ; i++) {
206 std::vector<int> maxLabel(this->nodesByLevel[i]);
207 std::vector<int> currentLabel(this->nodesByLevel[i], 0);
209 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
210 maxLabel[j] = j > i ?
211 this->lowerLevelNodesNumber[i] : this->upperLevelNodesNumber[i];
214 for (unsigned int j = 0 ; j < this->nodesByLevel[i] ; j++) {
215 this->nodes[k]->label.assign(currentLabel.begin(), currentLabel.end());
220 int pos = currentLabel.size() - 1;
221 remainder = ++currentLabel[pos] / maxLabel[pos];
222 currentLabel[pos] = currentLabel[pos] % maxLabel[pos];
225 while(remainder != 0);
232 int AsClusterFatTree::getLevelPosition(const unsigned int level) {
233 if (level > this->levels - 1) {
234 // Well, that should never happen. Maybe should we throw instead.
237 int tempPosition = 0;
239 for (unsigned int i = 0 ; i < level ; i++) {
240 tempPosition += this->nodesByLevel[i];
245 void AsClusterFatTree::addComputeNodes(std::vector<int> const& id) {
246 using std::make_pair;
247 FatTreeNode* newNode;
248 for (size_t i = 0 ; i < id.size() ; i++) {
249 newNode = new FatTreeNode(id[i], 0, i);
250 newNode->parents.resize(this->upperLevelNodesNumber[0] * this->lowerLevelPortsNumber[i]);
251 this->nodes.insert(make_pair(id[i],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(tmp[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 ?