Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
allow several hosts to open the same file simultaneously
[simgrid.git] / src / surf / surf_routing_cluster_fat_tree.cpp
index 19ecc1230178db1741d526300fed86b4a6ef6bba..066cd0fdeef791f6d801b0598f75a1463b8655c7 100644 (file)
@@ -7,6 +7,12 @@
 #include <fstream>
 
 
+
+AS_t model_fat_tree_cluster_create(void)
+{
+  return new AsClusterFatTree();
+}
+
 AsClusterFatTree::AsClusterFatTree() : levels(0) {}
 
 AsClusterFatTree::~AsClusterFatTree() {
@@ -15,11 +21,70 @@ AsClusterFatTree::~AsClusterFatTree() {
   }
 }
 
+bool AsClusterFatTree::isInSubTree(FatTreeNode *root, FatTreeNode *node) {
+  for (unsigned int i = 0 ; i < node->level ; i++) {
+    if(root->label[i] != node->label[i]) {
+      return false;
+    }
+  }
+  
+  for (unsigned int i = root->level + 1 ; i < this->levels ; i++) {
+    if(root->label[i] != node->label[i]) {
+      return false;
+    }
+  }
+  return true;
+}
+
 void AsClusterFatTree::getRouteAndLatency(RoutingEdgePtr src,
                                           RoutingEdgePtr dst,
                                           sg_platf_route_cbarg_t into,
                                           double *latency) {
+  FatTreeNode *source, *destination, *currentNode;
+  std::vector<NetworkLink*> route;
+  source = this->nodes.find(src->getId())->second;
+  destination = this->nodes.find(dst->getId())->second;
+
+
+  currentNode = source;
+
+  // up part
+  while (!isInSubTree(currentNode, destination)) {
+    int d, k; // as in d-mod-k
+    d = destination->position;
+
+    for (unsigned int i = 0 ; i < currentNode->level ; i++) {
+      d /= this->upperLevelNodesNumber[i];
+    }
+     k = this->upperLevelNodesNumber[currentNode->level] *
+      this->lowerLevelNodesNumber[currentNode->level];
+     d = d % k;
+     route.push_back(currentNode->parents[d]->upLink);
+     if(latency) {
+       *latency += currentNode->parents[d]->upLink->getLatency();
+     }
+     currentNode = currentNode->parents[d]->upNode;
+  }
+  
+  // Down part
+  while(currentNode != destination) {
+    for(unsigned int i = 0 ; i < currentNode->children.size() ; i++) {
+      if(i % this->lowerLevelNodesNumber[currentNode->level] ==
+         destination->label[currentNode->level]) {
+        route.push_back(currentNode->children[i]->downLink);
+        if(latency) {
+          *latency += currentNode->children[i]->downLink->getLatency();
+        }
+        currentNode = currentNode->children[i]->downNode;
+      }
+    }
+  }
   
+  for (unsigned int i = 0 ; i < route.size() ; i++) {
+    xbt_dynar_push_as(into->link_list, void*, route[i]);
+  }
+
 }
 
 /* This function makes the assumption that parse_specific_arguments() and
@@ -90,7 +155,7 @@ void AsClusterFatTree::generateSwitches() {
   }
 
      
-  if(this->nodesByLevel[0] < this->nodes.size()) {
+  if(this->nodesByLevel[0] > this->nodes.size()) {
     surf_parse_error("There is not enough nodes to fit to the described topology."
                      " Please check your platform description (We need %d nodes, we only got %zu)",
                      this->nodesByLevel[0], this->nodes.size());
@@ -118,7 +183,6 @@ void AsClusterFatTree::generateSwitches() {
     for (unsigned int i = this->nodesByLevel[0] ; i < this->nodes.size() ; i++) {
       delete this->nodes[i];
     }
-    this->nodes.resize(this->nodesByLevel[0]);
   }
 
   // We create the switches
@@ -132,7 +196,7 @@ void AsClusterFatTree::generateSwitches() {
       if (i != this->levels - 1) {
         newNode->parents.resize(this->upperLevelNodesNumber[i + 1]);
       }
-      this->nodes.push_back(newNode);
+      this->nodes.insert(std::make_pair(k,newNode));
     }
   }
 }
@@ -179,28 +243,25 @@ int AsClusterFatTree::getLevelPosition(const unsigned  int level) {
  return tempPosition;
 }
 
-void AsClusterFatTree::addComputeNodes(std::vector<int> const& id) {
+void AsClusterFatTree::addComputeNode(int id) {
+  using std::make_pair;
+  static int position = 0;
   FatTreeNode* newNode;
-  for (size_t  i = 0 ; i < id.size() ; i++) {
-    newNode = new FatTreeNode(id[i], 0, i);
-    newNode->parents.resize(this->upperLevelNodesNumber[0] * this->lowerLevelPortsNumber[i]);
-    this->nodes.push_back(newNode);
-  }
+  newNode = new FatTreeNode(id, 0, position);
+  newNode->parents.resize(this->upperLevelNodesNumber[0] * this->lowerLevelPortsNumber[0]);
+  this->nodes.insert(make_pair(id,newNode));
 }
 
 void AsClusterFatTree::addLink(sg_platf_cluster_cbarg_t cluster, 
                                FatTreeNode *parent, unsigned int parentPort,
                                FatTreeNode *child, unsigned int childPort) {
-  using std::make_pair;
-
-
   FatTreeLink *newLink;
   newLink = new FatTreeLink(cluster, parent, child);
 
   parent->children[parentPort] = newLink;
   child->parents[childPort] = newLink;
 
-  this->links.insert(make_pair(make_pair(parent->id, child->id), newLink));
+  this->links.push_back(newLink);
 
   
 
@@ -221,7 +282,7 @@ void AsClusterFatTree::parse_specific_arguments(sg_platf_cluster_cbarg_t
   }
 
   // The first parts of topo_parameters should be the levels number
-  this->levels = std::atoi(tmp[0].c_str()); // stoi() only in C++11...
+  this->levels = std::atoi(parameters[0].c_str()); // stoi() only in C++11...
   
   // Then, a l-sized vector standing for the childs number by level
   boost::split(tmp, parameters[1], boost::is_any_of(","));
@@ -265,12 +326,12 @@ void AsClusterFatTree::generateDotFile(const string& filename) const {
   
   if(file.is_open()) {
     // That could also be greatly clarified with C++11
-    std::map<std::pair<int,int>,FatTreeLink*>::const_iterator iter;
+    std::vector<FatTreeLink*>::const_iterator iter;
     file << "graph AsClusterFatTree {\n";
     for (iter = this->links.begin() ; iter != this->links.end() ; iter++ ) {
-        file << iter->second->source->id
+      file << (*iter)->downNode->id
              << " -- "
-             << iter->second->destination->id
+           << (*iter)->upNode->id
              << ";\n";
     }
     file << "}";
@@ -286,9 +347,9 @@ FatTreeNode::FatTreeNode(int id, int level, int position) : id(id),
                                                             level(level),
                                                             position(position){}
 
-FatTreeLink::FatTreeLink(sg_platf_cluster_cbarg_t cluster, FatTreeNode *source,
-                         FatTreeNode *destination) : source(source),
-                                                     destination(destination) {
+FatTreeLink::FatTreeLink(sg_platf_cluster_cbarg_t cluster, FatTreeNode *downNode,
+                         FatTreeNode *upNode) : upNode(upNode),
+                                                downNode(downNode) {
   static int uniqueId = 0;
   s_sg_platf_link_cbarg_t linkTemplate;
   linkTemplate.bandwidth = cluster->bw;
@@ -298,14 +359,14 @@ FatTreeLink::FatTreeLink(sg_platf_cluster_cbarg_t cluster, FatTreeNode *source,
 
 
   NetworkLink* link;
-  linkTemplate.id = bprintf("link_from_%d_to_%d_%d_UP", source->id, destination->id, uniqueId);
+  linkTemplate.id = bprintf("link_from_%d_to_%d_%d_UP", downNode->id, upNode->id, uniqueId);
   sg_platf_new_link(&linkTemplate);
   link = (NetworkLink*) xbt_lib_get_or_null(link_lib, linkTemplate.id, SURF_LINK_LEVEL);
-  this->linkUp = link; // check link?
-  linkTemplate.id = bprintf("link_from_%d_to_%d_%d_DOWN", source->id, destination->id, uniqueId);
+  this->upLink = link; // check link?
+  linkTemplate.id = bprintf("link_from_%d_to_%d_%d_DOWN", downNode->id, upNode->id, uniqueId);
   sg_platf_new_link(&linkTemplate);
   link = (NetworkLink*) xbt_lib_get_or_null(link_lib, linkTemplate.id, SURF_LINK_LEVEL);
-  this->linkDown = link; // check link ?
+  this->downLink = link; // check link ?
 
   uniqueId++;