From: Arnaud Giersch Date: Fri, 13 Oct 2017 08:59:25 +0000 (+0200) Subject: Repair Dijkstra's cache mode. X-Git-Tag: v3.18~478^2 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/f7e8dd2137f34f77c8d74a71b691e54a772a84e3 Repair Dijkstra's cache mode. --- diff --git a/src/kernel/routing/DijkstraZone.cpp b/src/kernel/routing/DijkstraZone.cpp index 4c2d62c2bb..c6f52a0814 100644 --- a/src/kernel/routing/DijkstraZone.cpp +++ b/src/kernel/routing/DijkstraZone.cpp @@ -132,8 +132,6 @@ void DijkstraZone::getLocalRoute(NetPoint* src, NetPoint* dst, sg_platf_route_cb int src_id = src->id(); int dst_id = dst->id(); - int* pred_arr = nullptr; - int size = 0; xbt_dynar_t nodes = xbt_graph_get_nodes(routeGraph_); /* Use the graph_node id mapping set to quickly find the nodes */ @@ -162,19 +160,13 @@ void DijkstraZone::getLocalRoute(NetPoint* src, NetPoint* dst, sg_platf_route_cb } } - route_cache_element_t elm = nullptr; - if (not routeCache_.empty()) { /* cache mode */ - auto it = routeCache_.find(src_id); - elm = (it == routeCache_.end()) ? nullptr : it->second; - } - - if (elm) { /* cached mode and cache hit */ - pred_arr = elm->pred_arr; - } else { /* not cached mode, or cache miss */ + auto elm = routeCache_.emplace(src_id, std::vector()); + std::vector& pred_arr = elm.first->second; + if (elm.second) { /* new element was inserted (not cached mode, or cache miss) */ int nr_nodes = xbt_dynar_length(nodes); - double* cost_arr = new double[nr_nodes]; /* link cost from src to other hosts */ - pred_arr = new int[nr_nodes]; /* predecessors in path from src */ + std::vector cost_arr(nr_nodes); /* link cost from src to other hosts */ + pred_arr.resize(nr_nodes); /* predecessors in path from src */ xbt_heap_t pqueue = xbt_heap_new(nr_nodes, nullptr); /* initialize */ @@ -217,8 +209,6 @@ void DijkstraZone::getLocalRoute(NetPoint* src, NetPoint* dst, sg_platf_route_cb /* free item popped from pqueue */ delete v_id; } - - delete[] cost_arr; xbt_heap_free(pqueue); } @@ -265,7 +255,6 @@ void DijkstraZone::getLocalRoute(NetPoint* src, NetPoint* dst, sg_platf_route_cb if (lat) *lat += static_cast(link)->latency(); } - size++; } if (hierarchy_ == RoutingMode::recursive) { @@ -273,16 +262,8 @@ void DijkstraZone::getLocalRoute(NetPoint* src, NetPoint* dst, sg_platf_route_cb route->gw_dst = first_gw; } - if (not routeCache_.empty() && elm == nullptr) { - /* add to predecessor list of the current src-host to cache */ - elm = new s_route_cache_element_t; - elm->pred_arr = pred_arr; - elm->size = size; - routeCache_.insert({src_id, elm}); - } - - if (routeCache_.empty()) - delete[] pred_arr; + if (not cached_) + routeCache_.clear(); } DijkstraZone::~DijkstraZone() @@ -292,7 +273,7 @@ DijkstraZone::~DijkstraZone() /* Creation routing model functions */ -DijkstraZone::DijkstraZone(NetZone* father, std::string name, bool cached) : RoutedZone(father, name) +DijkstraZone::DijkstraZone(NetZone* father, std::string name, bool cached) : RoutedZone(father, name), cached_(cached) { } diff --git a/src/kernel/routing/DijkstraZone.hpp b/src/kernel/routing/DijkstraZone.hpp index 5720cd6d4f..f7fc503c5e 100644 --- a/src/kernel/routing/DijkstraZone.hpp +++ b/src/kernel/routing/DijkstraZone.hpp @@ -14,12 +14,6 @@ struct s_graph_node_data_t { }; typedef s_graph_node_data_t* graph_node_data_t; -struct s_route_cache_element_t { - int* pred_arr; - int size; -}; -typedef s_route_cache_element_t* route_cache_element_t; - namespace simgrid { namespace kernel { namespace routing { @@ -60,8 +54,9 @@ public: void addRoute(sg_platf_route_cbarg_t route) override; xbt_graph_t routeGraph_ = nullptr; /* xbt_graph */ - std::map graphNodeMap_; /* map */ - std::map routeCache_; /* use in cache mode */ + std::map graphNodeMap_; /* map */ + bool cached_; /* cache mode */ + std::map> routeCache_; /* use in cache mode */ }; } } diff --git a/src/surf/sg_platf.cpp b/src/surf/sg_platf.cpp index 6d5651723f..660c6db0e2 100644 --- a/src/surf/sg_platf.cpp +++ b/src/surf/sg_platf.cpp @@ -600,10 +600,10 @@ simgrid::s4u::NetZone* sg_platf_new_Zone_begin(ZoneCreationArgs* zone) new_zone = new simgrid::kernel::routing::FatTreeZone(current_routing, zone->id); break; case A_surfxml_AS_routing_Dijkstra: - new_zone = new simgrid::kernel::routing::DijkstraZone(current_routing, zone->id, 0); + new_zone = new simgrid::kernel::routing::DijkstraZone(current_routing, zone->id, false); break; case A_surfxml_AS_routing_DijkstraCache: - new_zone = new simgrid::kernel::routing::DijkstraZone(current_routing, zone->id, 1); + new_zone = new simgrid::kernel::routing::DijkstraZone(current_routing, zone->id, true); break; case A_surfxml_AS_routing_Floyd: new_zone = new simgrid::kernel::routing::FloydZone(current_routing, zone->id);