From e76d8df246eac6bf94729139afc07bd21506379d Mon Sep 17 00:00:00 2001 From: Patrick Niklaus Date: Fri, 24 Apr 2015 15:31:26 +0200 Subject: [PATCH] Fix tools to build with new graph reader interface --- CMakeLists.txt | 4 +- tools/components.cpp | 147 ++++++++++++++--------------- tools/graph_compare.cpp | 199 ---------------------------------------- 3 files changed, 68 insertions(+), 282 deletions(-) delete mode 100644 tools/graph_compare.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index dd862d8dc..6fbf409e7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -328,12 +328,10 @@ if(WITH_TOOLS OR BUILD_TOOLS) if(UNIX AND NOT APPLE) target_link_libraries(osrm-unlock-all rt) endif() - add_executable(osrm-check-hsgr tools/check-hsgr.cpp $ $ $) + add_executable(osrm-check-hsgr tools/check-hsgr.cpp $ $ $ $) target_link_libraries(osrm-check-hsgr ${Boost_LIBRARIES}) add_executable(osrm-springclean tools/springclean.cpp $ $ $ $) target_link_libraries(osrm-springclean ${Boost_LIBRARIES}) - add_executable(osrm-graph-compare tools/graph_compare.cpp $ $ $ $ $ $ $) - target_link_libraries(osrm-graph-compare ${Boost_LIBRARIES} ${TBB_LIBRARIES}) install(TARGETS osrm-cli DESTINATION bin) install(TARGETS osrm-io-benchmark DESTINATION bin) diff --git a/tools/components.cpp b/tools/components.cpp index 7ac64f8d9..f35d7196d 100644 --- a/tools/components.cpp +++ b/tools/components.cpp @@ -76,12 +76,73 @@ void DeleteFileIfExists(const std::string &file_name) } } +void LoadRestrictions(const char* path, + std::unique_ptr> ext_to_int_id_map, + std::vector& restriction_list) +{ + std::ifstream input_stream(path, std::ios::binary); + if (!input_stream.is_open()) + { + throw osrm::exception("Cannot open restriction file"); + } + loadRestrictionsFromFile(input_stream, *ext_to_int_id_map, restriction_list); +} + +std::size_t LoadGraph(const char* path, + std::vector& coordinate_list, + std::vector& barrier_node_list, + std::unordered_map& ext_to_int_id_map, + std::vector& graph_edge_list) +{ + std::ifstream input_stream(path, std::ifstream::in | std::ifstream::binary); + if (!input_stream.is_open()) + { + throw osrm::exception("Cannot open osrm file"); + } + + // load graph data + std::vector edge_list; + std::vector traffic_light_node_list; + + auto number_of_nodes = loadNodesFromFile(input_stream, barrier_node_list, + traffic_light_node_list, + coordinate_list, + ext_to_int_id_map); + + auto number_of_edges = loadEdgesFromFile(input_stream, ext_to_int_id_map, edge_list); + + traffic_light_node_list.clear(); + traffic_light_node_list.shrink_to_fit(); + + // Building an node-based graph + for (const auto &input_edge : edge_list) + { + if (input_edge.source == input_edge.target) + { + continue; + } + + if (input_edge.forward) + { + graph_edge_list.emplace_back(input_edge.source, input_edge.target, + (std::max)(input_edge.weight, 1), input_edge.name_id); + } + if (input_edge.backward) + { + graph_edge_list.emplace_back(input_edge.target, input_edge.source, + (std::max)(input_edge.weight, 1), input_edge.name_id); + } + } + + return number_of_nodes; +} + int main(int argc, char *argv[]) { std::vector coordinate_list; std::vector restriction_list; - std::vector bollard_node_list; - std::vector traffic_lights_list; + std::vector barrier_node_list; + auto ext_to_int_id_map = osrm::make_unique>(); LogPolicy::GetInstance().Unmute(); try @@ -95,83 +156,10 @@ int main(int argc, char *argv[]) } SimpleLogger().Write() << "Using restrictions from file: " << argv[2]; - std::ifstream restriction_ifstream(argv[2], std::ios::binary); - const FingerPrint fingerprint_orig; - FingerPrint fingerprint_loaded; - restriction_ifstream.read(reinterpret_cast(&fingerprint_loaded), - sizeof(FingerPrint)); - // check fingerprint and warn if necessary - if (!fingerprint_loaded.TestGraphUtil(fingerprint_orig)) - { - SimpleLogger().Write(logWARNING) << argv[2] << " was prepared with a different build. " - "Reprocess to get rid of this warning."; - } - - if (!restriction_ifstream.good()) - { - throw osrm::exception("Could not access files"); - } - uint32_t usable_restrictions = 0; - restriction_ifstream.read(reinterpret_cast(&usable_restrictions), sizeof(uint32_t)); - restriction_list.resize(usable_restrictions); - - // load restrictions - if (usable_restrictions > 0) - { - restriction_ifstream.read(reinterpret_cast(&restriction_list[0]), - usable_restrictions * sizeof(TurnRestriction)); - } - restriction_ifstream.close(); - - std::ifstream input_stream(argv[1], std::ifstream::in | std::ifstream::binary); - if (!input_stream.is_open()) - { - throw osrm::exception("Cannot open osrm file"); - } - - // load graph data - std::vector edge_list; - const NodeID number_of_nodes = - readBinaryOSRMGraphFromStream(input_stream, edge_list, bollard_node_list, - traffic_lights_list, &coordinate_list, restriction_list); - input_stream.close(); - - BOOST_ASSERT_MSG(restriction_list.size() == usable_restrictions, - "size of restriction_list changed"); - - SimpleLogger().Write() << restriction_list.size() << " restrictions, " - << bollard_node_list.size() << " bollard nodes, " - << traffic_lights_list.size() << " traffic lights"; - - traffic_lights_list.clear(); - traffic_lights_list.shrink_to_fit(); - - // Building an node-based graph std::vector graph_edge_list; -// DeallocatingVector graph_edge_list; - for (const auto &input_edge : edge_list) - { - if (input_edge.source == input_edge.target) - { - continue; - } - - if (input_edge.forward) - { - graph_edge_list.emplace_back(input_edge.source, input_edge.target, - (std::max)(input_edge.weight, 1), input_edge.name_id); - } - if (input_edge.backward) - { - graph_edge_list.emplace_back(input_edge.target, input_edge.source, - (std::max)(input_edge.weight, 1), input_edge.name_id); - } - } - edge_list.clear(); - edge_list.shrink_to_fit(); - BOOST_ASSERT_MSG(0 == edge_list.size() && 0 == edge_list.capacity(), - "input edge vector not properly deallocated"); + auto number_of_nodes = LoadGraph(argv[1], coordinate_list, barrier_node_list, *ext_to_int_id_map, graph_edge_list); + LoadRestrictions(argv[2], std::move(ext_to_int_id_map), restriction_list); tbb::parallel_sort(graph_edge_list.begin(), graph_edge_list.end()); const auto graph = std::make_shared(number_of_nodes, graph_edge_list); @@ -181,9 +169,8 @@ int main(int argc, char *argv[]) SimpleLogger().Write() << "Starting SCC graph traversal"; RestrictionMap restriction_map(restriction_list); - auto tarjan = osrm::make_unique>(graph, - restriction_map, - bollard_node_list); + auto tarjan = osrm::make_unique>(graph, restriction_map, + barrier_node_list); tarjan->run(); SimpleLogger().Write() << "identified: " << tarjan->get_number_of_components() << " many components"; diff --git a/tools/graph_compare.cpp b/tools/graph_compare.cpp deleted file mode 100644 index 8adc06e78..000000000 --- a/tools/graph_compare.cpp +++ /dev/null @@ -1,199 +0,0 @@ -#include "../data_structures/dynamic_graph.hpp" -#include "../data_structures/import_edge.hpp" -#include "../data_structures/query_node.hpp" -#include "../data_structures/restriction.hpp" -#include "../data_structures/static_graph.hpp" -#include "../util/fingerprint.hpp" -#include "../util/graph_loader.hpp" -#include "../util/integer_range.hpp" -#include "../util/make_unique.hpp" -#include "../util/osrm_exception.hpp" -#include "../util/simple_logger.hpp" - -#include "../typedefs.h" - -#include -#include - -struct TarjanEdgeData -{ - TarjanEdgeData() : distance(INVALID_EDGE_WEIGHT), name_id(INVALID_NAMEID) {} - TarjanEdgeData(unsigned distance, unsigned name_id) : distance(distance), name_id(name_id) {} - unsigned distance; - unsigned name_id; -}; - -using StaticTestGraph = StaticGraph; -using DynamicTestGraph = StaticGraph; -using StaticEdge = StaticTestGraph::InputEdge; -using DynamicEdge = DynamicTestGraph::InputEdge; - -int main(int argc, char *argv[]) -{ - std::vector coordinate_list; - std::vector restriction_list; - std::vector bollard_node_list; - std::vector traffic_lights_list; - - LogPolicy::GetInstance().Unmute(); - try - { - // enable logging - if (argc < 3) - { - SimpleLogger().Write(logWARNING) << "usage:\n" << argv[0] - << " "; - return -1; - } - - SimpleLogger().Write() << "Using restrictions from file: " << argv[2]; - std::ifstream restriction_ifstream(argv[2], std::ios::binary); - const FingerPrint fingerprint_orig; - FingerPrint fingerprint_loaded; - restriction_ifstream.read(reinterpret_cast(&fingerprint_loaded), - sizeof(FingerPrint)); - - // check fingerprint and warn if necessary - if (!fingerprint_loaded.TestGraphUtil(fingerprint_orig)) - { - SimpleLogger().Write(logWARNING) << argv[2] << " was prepared with a different build. " - "Reprocess to get rid of this warning."; - } - - if (!restriction_ifstream.good()) - { - throw osrm::exception("Could not access files"); - } - uint32_t usable_restrictions = 0; - restriction_ifstream.read(reinterpret_cast(&usable_restrictions), sizeof(uint32_t)); - restriction_list.resize(usable_restrictions); - - // load restrictions - if (usable_restrictions > 0) - { - restriction_ifstream.read(reinterpret_cast(&restriction_list[0]), - usable_restrictions * sizeof(TurnRestriction)); - } - restriction_ifstream.close(); - - std::ifstream input_stream(argv[1], std::ifstream::in | std::ifstream::binary); - if (!input_stream.is_open()) - { - throw osrm::exception("Cannot open osrm file"); - } - - // load graph data - std::vector edge_list; - const NodeID number_of_nodes = - readBinaryOSRMGraphFromStream(input_stream, edge_list, bollard_node_list, - traffic_lights_list, &coordinate_list, restriction_list); - input_stream.close(); - - BOOST_ASSERT_MSG(restriction_list.size() == usable_restrictions, - "size of restriction_list changed"); - - SimpleLogger().Write() << restriction_list.size() << " restrictions, " - << bollard_node_list.size() << " bollard nodes, " - << traffic_lights_list.size() << " traffic lights"; - - traffic_lights_list.clear(); - traffic_lights_list.shrink_to_fit(); - - // Building an node-based graph - std::vector static_graph_edge_list; - std::vector dynamic_graph_edge_list; - for (const auto &input_edge : edge_list) - { - if (input_edge.source == input_edge.target) - { - continue; - } - - if (input_edge.forward) - { - static_graph_edge_list.emplace_back(input_edge.source, input_edge.target, - (std::max)(input_edge.weight, 1), - input_edge.name_id); - dynamic_graph_edge_list.emplace_back(input_edge.source, input_edge.target, - (std::max)(input_edge.weight, 1), - input_edge.name_id); - } - if (input_edge.backward) - { - dynamic_graph_edge_list.emplace_back(input_edge.target, input_edge.source, - (std::max)(input_edge.weight, 1), - input_edge.name_id); - static_graph_edge_list.emplace_back(input_edge.target, input_edge.source, - (std::max)(input_edge.weight, 1), - input_edge.name_id); - } - } - edge_list.clear(); - edge_list.shrink_to_fit(); - BOOST_ASSERT_MSG(0 == edge_list.size() && 0 == edge_list.capacity(), - "input edge vector not properly deallocated"); - - tbb::parallel_sort(static_graph_edge_list.begin(), static_graph_edge_list.end()); - tbb::parallel_sort(dynamic_graph_edge_list.begin(), dynamic_graph_edge_list.end()); - - auto static_graph = - osrm::make_unique(number_of_nodes, static_graph_edge_list); - auto dynamic_graph = - osrm::make_unique(number_of_nodes, dynamic_graph_edge_list); - - SimpleLogger().Write() << "Starting static/dynamic graph comparison"; - - BOOST_ASSERT(static_graph->GetNumberOfNodes() == dynamic_graph->GetNumberOfNodes()); - BOOST_ASSERT(static_graph->GetNumberOfEdges() == dynamic_graph->GetNumberOfEdges()); - for (const auto node : osrm::irange(0u, static_graph->GetNumberOfNodes())) - { - const auto static_range = static_graph->GetAdjacentEdgeRange(node); - const auto dynamic_range = dynamic_graph->GetAdjacentEdgeRange(node); - SimpleLogger().Write() << "checking node " << node << "/" - << static_graph->GetNumberOfNodes(); - - BOOST_ASSERT(static_range.size() == dynamic_range.size()); - const auto static_begin = static_graph->BeginEdges(node); - const auto dynamic_begin = dynamic_graph->BeginEdges(node); - - // check raw interface - for (const auto i : osrm::irange(0u, static_range.size())) - { - const auto static_target = static_graph->GetTarget(static_begin + i); - const auto dynamic_target = dynamic_graph->GetTarget(dynamic_begin + i); - BOOST_ASSERT(static_target == dynamic_target); - - const auto static_data = static_graph->GetEdgeData(static_begin + i); - const auto dynamic_data = dynamic_graph->GetEdgeData(dynamic_begin + i); - - BOOST_ASSERT(static_data.distance == dynamic_data.distance); - BOOST_ASSERT(static_data.name_id == dynamic_data.name_id); - } - - // check range interface - std::vector static_target_ids, dynamic_target_ids; - std::vector static_edge_data, dynamic_edge_data; - for (const auto static_id : static_range) - { - static_target_ids.push_back(static_graph->GetTarget(static_id)); - static_edge_data.push_back(static_graph->GetEdgeData(static_id)); - } - for (const auto dynamic_id : dynamic_range) - { - dynamic_target_ids.push_back(dynamic_graph->GetTarget(dynamic_id)); - dynamic_edge_data.push_back(dynamic_graph->GetEdgeData(dynamic_id)); - } - BOOST_ASSERT(static_target_ids.size() == dynamic_target_ids.size()); - BOOST_ASSERT(std::equal(std::begin(static_target_ids), std::end(static_target_ids), - std::begin(dynamic_target_ids))); - } - - SimpleLogger().Write() << "Graph comparison finished successfully"; - } - catch (const std::exception &e) - { - SimpleLogger().Write(logWARNING) << "[exception] " << e.what(); - } - - return 0; -}