Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
I'm glad: this prototype was not used
[simgrid.git] / src / surf / surf_routing_dijkstra.cpp
index 1e9a319..fcaac99 100644 (file)
@@ -4,7 +4,6 @@
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
-#include "src/surf/surf_routing_private.hpp"
 #include "src/surf/surf_routing_dijkstra.hpp"
 #include "src/surf/network_interface.hpp"
 
@@ -31,7 +30,7 @@ static void graph_edge_data_free(void *e) // FIXME: useless code duplication
 {
   sg_platf_route_cbarg_t e_route = (sg_platf_route_cbarg_t) e;
   if (e_route) {
-    xbt_dynar_free(&(e_route->link_list));
+    delete e_route->link_list;
     xbt_free(e_route);
   }
 }
@@ -42,6 +41,9 @@ namespace simgrid {
 namespace surf {
 void AsDijkstra::Seal()
 {
+  xbt_node_t node = NULL;
+  unsigned int cursor2, cursor;
+
   /* Create the topology graph */
   if(!routeGraph_)
     routeGraph_ = xbt_graph_new_graph(1, NULL);
@@ -49,17 +51,33 @@ void AsDijkstra::Seal()
     graphNodeMap_ = xbt_dict_new_homogeneous(&graph_node_map_elem_free);
 
   /* Add the loopback if needed */
-  if (routing_platf->loopback_ && hierarchy_ == SURF_ROUTING_BASE)
-    addLoopback();
+  if (routing_platf->loopback_ && hierarchy_ == SURF_ROUTING_BASE) {
+    xbt_dynar_foreach(xbt_graph_get_nodes(routeGraph_), cursor, node) {
+      xbt_edge_t edge = NULL;
+
+      bool found = false;
+      xbt_dynar_foreach(xbt_graph_node_get_outedges(node), cursor2, edge) {
+        if (xbt_graph_edge_get_target(edge) == node) {
+          found = true;
+          break;
+        }
+      }
+
+      if (!found) {
+        sg_platf_route_cbarg_t e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
+        e_route->link_list = new std::vector<Link*>();
+        e_route->link_list->push_back(routing_platf->loopback_);
+        xbt_graph_new_edge(routeGraph_, node, node, e_route);
+      }
+    }
+  }
 
   /* initialize graph indexes in nodes after graph has been built */
   xbt_dynar_t nodes = xbt_graph_get_nodes(routeGraph_);
 
-  xbt_node_t node = NULL;
-  unsigned int cursor2;
-  xbt_dynar_foreach(nodes, cursor2, node) {
+  xbt_dynar_foreach(nodes, cursor, node) {
     graph_node_data_t data = (graph_node_data_t) xbt_graph_node_get_data(node);
-    data->graph_id = cursor2;
+    data->graph_id = cursor;
   }
 }
 
@@ -76,19 +94,14 @@ xbt_node_t AsDijkstra::routeGraphNewNode(int id, int graph_id)
 
   elm = xbt_new0(struct graph_node_map_element, 1);
   elm->node = node;
-  xbt_dict_set_ext(graphNodeMap_, (char *) (&id), sizeof(int),
-      (xbt_dictelm_t) elm, NULL);
+  xbt_dict_set_ext(graphNodeMap_, (char *) (&id), sizeof(int), (xbt_dictelm_t) elm, NULL);
 
   return node;
 }
 
 graph_node_map_element_t AsDijkstra::nodeMapSearch(int id)
 {
-  graph_node_map_element_t elm = (graph_node_map_element_t)
-          xbt_dict_get_or_null_ext(graphNodeMap_,
-              (char *) (&id),
-              sizeof(int));
-  return elm;
+  return (graph_node_map_element_t)xbt_dict_get_or_null_ext(graphNodeMap_, (char *) (&id), sizeof(int));
 }
 
 /* Parsing */
@@ -99,15 +112,8 @@ void AsDijkstra::newRoute(int src_id, int dst_id, sg_platf_route_cbarg_t e_route
   xbt_node_t src = NULL;
   xbt_node_t dst = NULL;
 
-  graph_node_map_element_t src_elm = (graph_node_map_element_t)
-          xbt_dict_get_or_null_ext(graphNodeMap_,
-              (char *) (&src_id),
-              sizeof(int));
-  graph_node_map_element_t dst_elm = (graph_node_map_element_t)
-          xbt_dict_get_or_null_ext(graphNodeMap_,
-              (char *) (&dst_id),
-              sizeof(int));
-
+  graph_node_map_element_t src_elm = nodeMapSearch(src_id);
+  graph_node_map_element_t dst_elm = nodeMapSearch(dst_id);
 
   if (src_elm)
     src = src_elm->node;
@@ -132,52 +138,22 @@ void AsDijkstra::newRoute(int src_id, int dst_id, sg_platf_route_cbarg_t e_route
   xbt_graph_new_edge(routeGraph_, src, dst, e_route);
 }
 
-void AsDijkstra::addLoopback() {
-  xbt_dynar_t nodes = xbt_graph_get_nodes(routeGraph_);
-
-  xbt_node_t node = NULL;
-  unsigned int cursor2;
-  xbt_dynar_foreach(nodes, cursor2, node) {
-    xbt_dynar_t out_edges = xbt_graph_node_get_outedges(node);
-    xbt_edge_t edge = NULL;
-    unsigned int cursor;
-
-    int found = 0;
-    xbt_dynar_foreach(out_edges, cursor, edge) {
-      xbt_node_t other_node = xbt_graph_edge_get_target(edge);
-      if (other_node == node) {
-        found = 1;
-        break;
-      }
-    }
-
-    if (!found) {
-      sg_platf_route_cbarg_t e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
-      e_route->link_list = xbt_dynar_new(sizeof(Link*), NULL);
-      xbt_dynar_push(e_route->link_list, &routing_platf->loopback_);
-      xbt_graph_new_edge(routeGraph_, node, node, e_route);
-    }
-  }
-}
-
 xbt_dynar_t AsDijkstra::getOneLinkRoutes()
 {
   xbt_dynar_t ret = xbt_dynar_new(sizeof(Onelink*), xbt_free_f);
   sg_platf_route_cbarg_t route = xbt_new0(s_sg_platf_route_cbarg_t,1);
-  route->link_list = xbt_dynar_new(sizeof(Link*),NULL);
+  route->link_list = new std::vector<Link*>();
 
-  int src,dst;
-  NetCard *src_elm, *dst_elm;
   int table_size = (int)xbt_dynar_length(vertices_);
-  for(src=0; src < table_size; src++) {
-    for(dst=0; dst< table_size; dst++) {
-      xbt_dynar_reset(route->link_list);
-      src_elm = xbt_dynar_get_as(vertices_, src, NetCard*);
-      dst_elm = xbt_dynar_get_as(vertices_, dst, NetCard*);
+  for(int src=0; src < table_size; src++) {
+    for(int dst=0; dst< table_size; dst++) {
+      route->link_list->clear();
+      NetCard *src_elm = xbt_dynar_get_as(vertices_, src, NetCard*);
+      NetCard *dst_elm = xbt_dynar_get_as(vertices_, dst, NetCard*);
       this->getRouteAndLatency(src_elm, dst_elm,route, NULL);
 
-      if (xbt_dynar_length(route->link_list) == 1) {
-        void *link = *(void **) xbt_dynar_get_ptr(route->link_list, 0);
+      if (route->link_list->size() == 1) {
+        Link *link = route->link_list->at(0);
         Onelink *onelink;
         if (hierarchy_ == SURF_ROUTING_BASE)
           onelink = new Onelink(link, src_elm, dst_elm);
@@ -194,9 +170,6 @@ xbt_dynar_t AsDijkstra::getOneLinkRoutes()
 
 void AsDijkstra::getRouteAndLatency(NetCard *src, NetCard *dst, sg_platf_route_cbarg_t route, double *lat)
 {
-
-  /* set utils vars */
-
   getRouteCheckParams(src, dst);
   int src_id = src->id();
   int dst_id = dst->id();
@@ -204,8 +177,6 @@ void AsDijkstra::getRouteAndLatency(NetCard *src, NetCard *dst, sg_platf_route_c
   int *pred_arr = NULL;
   sg_platf_route_cbarg_t e_route;
   int size = 0;
-  unsigned int cpt;
-  void *link;
   xbt_dynar_t nodes = xbt_graph_get_nodes(routeGraph_);
 
   /* Use the graph_node id mapping set to quickly find the nodes */
@@ -227,8 +198,8 @@ void AsDijkstra::getRouteAndLatency(NetCard *src, NetCard *dst, sg_platf_route_c
 
     e_route = (sg_platf_route_cbarg_t) xbt_graph_edge_get_data(edge);
 
-    xbt_dynar_foreach(e_route->link_list, cpt, link) {
-      xbt_dynar_unshift(route->link_list, &link);
+    for (auto link: *e_route->link_list) {
+      route->link_list->insert(route->link_list->begin(), link);
       if (lat)
         *lat += static_cast<Link*>(link)->getLatency();
     }
@@ -237,8 +208,7 @@ void AsDijkstra::getRouteAndLatency(NetCard *src, NetCard *dst, sg_platf_route_c
 
   route_cache_element_t elm = NULL;
   if (routeCache_) {  /* cache mode  */
-    elm = (route_cache_element_t)
-            xbt_dict_get_or_null_ext(routeCache_, (char *) (&src_id), sizeof(int));
+    elm = (route_cache_element_t) xbt_dict_get_or_null_ext(routeCache_, (char *) (&src_id), sizeof(int));
   }
 
   if (elm) {                    /* cached mode and cache hit */
@@ -279,7 +249,7 @@ void AsDijkstra::getRouteAndLatency(NetCard *src, NetCard *dst, sg_platf_route_c
         graph_node_data_t data = (graph_node_data_t) xbt_graph_node_get_data(u_node);
         int u_id = data->graph_id;
         sg_platf_route_cbarg_t tmp_e_route = (sg_platf_route_cbarg_t) xbt_graph_edge_get_data(edge);
-        int cost_v_u = (tmp_e_route->link_list)->used;    /* count of links, old model assume 1 */
+        int cost_v_u = tmp_e_route->link_list->size();    /* count of links, old model assume 1 */
 
         if (cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
           pred_arr[u_id] = *v_id;
@@ -319,24 +289,21 @@ void AsDijkstra::getRouteAndLatency(NetCard *src, NetCard *dst, sg_platf_route_c
     if (v == dst_node_id)
       first_gw = gw_dst;
 
-    if (hierarchy_ == SURF_ROUTING_RECURSIVE && v != dst_node_id
-        && strcmp(gw_dst->name(), prev_gw_src->name())) {
-      xbt_dynar_t e_route_as_to_as=NULL;
+    if (hierarchy_ == SURF_ROUTING_RECURSIVE && v != dst_node_id && strcmp(gw_dst->name(), prev_gw_src->name())) {
+      std::vector<Link*> *e_route_as_to_as = new std::vector<Link*>();
 
-      routing_platf->getRouteAndLatency(gw_dst_net_elm, prev_gw_src_net_elm, &e_route_as_to_as, NULL);
-      if (edge == NULL)
-        THROWF(arg_error,0,"No route from '%s' to '%s'", src->name(), dst->name());
-      int pos = 0;
-      xbt_dynar_foreach(e_route_as_to_as, cpt, link) {
-        xbt_dynar_insert_at(route->link_list, pos, &link);
+      routing_platf->getRouteAndLatency(gw_dst_net_elm, prev_gw_src_net_elm, e_route_as_to_as, NULL);
+      auto pos = route->link_list->begin();
+      for (auto link : *e_route_as_to_as) {
+        route->link_list->insert(pos, link);
         if (lat)
-          *lat += static_cast<Link*>(link)->getLatency();
+          *lat += link->getLatency();
         pos++;
       }
     }
 
-    xbt_dynar_foreach(e_route->link_list, cpt, link) {
-      xbt_dynar_unshift(route->link_list, &link);
+    for (auto link: *e_route->link_list) {
+      route->link_list->insert(route->link_list->begin(), link);
       if (lat)
         *lat += static_cast<Link*>(link)->getLatency();
     }
@@ -370,20 +337,20 @@ AsDijkstra::~AsDijkstra()
 /* Creation routing model functions */
 
 AsDijkstra::AsDijkstra(const char*name, bool cached)
-  : AsGeneric(name)
+  : AsRoutedGraph(name)
 {
   if (cached)
     routeCache_ = xbt_dict_new_homogeneous(&route_cache_elem_free);
 }
 
-void AsDijkstra::parseRoute(sg_platf_route_cbarg_t route)
+void AsDijkstra::addRoute(sg_platf_route_cbarg_t route)
 {
   const char *srcName = route->src;
   const char *dstName = route->dst;
   NetCard *src = sg_netcard_by_name_or_null(srcName);
   NetCard *dst = sg_netcard_by_name_or_null(dstName);
 
-  parseRouteCheckParams(route);
+  addRouteCheckParams(route);
 
   /* Create the topology graph */
   if(!routeGraph_)
@@ -420,7 +387,7 @@ void AsDijkstra::parseRoute(sg_platf_route_cbarg_t route)
     sg_platf_route_cbarg_t link_route_back = newExtendedRoute(hierarchy_, route, 0);
     newRoute(dst->id(), src->id(), link_route_back);
   }
-  xbt_dynar_free(&route->link_list);
+  delete route->link_list;
 }
 
 }