Refactor file writing in OSRM contract

This commit is contained in:
Patrick Niklaus
2017-06-15 14:52:14 +00:00
committed by Patrick Niklaus
parent 9922c0f4f7
commit 97592e5bc3
9 changed files with 212 additions and 94 deletions
-7
View File
@@ -65,16 +65,9 @@ class Contractor
std::vector<EdgeWeight> &&node_weights,
std::vector<bool> &is_core_node,
std::vector<float> &inout_node_levels) const;
void WriteCoreNodeMarker(std::vector<bool> &&is_core_node) const;
void WriteContractedGraph(unsigned number_of_edge_based_nodes,
util::DeallocatingVector<QueryEdge> contracted_edge_list);
private:
ContractorConfig config;
EdgeID LoadEdgeExpandedGraph(const ContractorConfig &config,
std::vector<extractor::EdgeBasedEdge> &edge_based_edge_list,
std::vector<EdgeWeight> &node_weights);
};
}
}
+21
View File
@@ -14,6 +14,27 @@ namespace contractor
{
namespace files
{
// reads .osrm.core
template <typename CoreVectorT>
void readCoreMarker(const boost::filesystem::path &path, CoreVectorT &is_core_node)
{
static_assert(util::is_view_or_vector<bool, CoreVectorT>::value,
"is_core_node must be a vector");
storage::io::FileReader reader(path, storage::io::FileReader::VerifyFingerprint);
storage::serialization::read(reader, is_core_node);
}
// writes .osrm.core
template <typename CoreVectorT>
void writeCoreMarker(const boost::filesystem::path &path, const CoreVectorT &is_core_node)
{
static_assert(util::is_view_or_vector<bool, CoreVectorT>::value,
"is_core_node must be a vector");
storage::io::FileWriter writer(path, storage::io::FileWriter::GenerateFingerprint);
storage::serialization::write(writer, is_core_node);
}
// reads .osrm.hsgr file
template <typename QueryGraphT>
+14 -5
View File
@@ -91,8 +91,8 @@ class GraphContractor
GraphContractor(int nodes,
std::vector<ContractorEdge> edges,
std::vector<float> &&node_levels_,
std::vector<EdgeWeight> &&node_weights_);
std::vector<float> node_levels_,
std::vector<EdgeWeight> node_weights_);
/* Flush all data from the contraction to disc and reorder stuff for better locality */
void FlushDataAndRebuildContractorGraph(ThreadDataContainer &thread_data_list,
@@ -101,12 +101,14 @@ class GraphContractor
void Run(double core_factor = 1.0);
void GetCoreMarker(std::vector<bool> &out_is_core_node);
std::vector<bool> GetCoreMarker();
void GetNodeLevels(std::vector<float> &out_node_levels);
std::vector<float> GetNodeLevels();
template <class Edge> inline void GetEdges(util::DeallocatingVector<Edge> &edges)
template <class Edge> inline util::DeallocatingVector<Edge> GetEdges()
{
util::DeallocatingVector<Edge> edges;
util::UnbufferedLog log;
log << "Getting edges of minimized graph ";
util::Percent p(log, contractor_graph->GetNumberOfNodes());
@@ -161,6 +163,13 @@ class GraphContractor
edges.append(external_edge_list.begin(), external_edge_list.end());
external_edge_list.clear();
// sort and remove duplicates
tbb::parallel_sort(edges.begin(), edges.end());
auto new_end = std::unique(edges.begin(), edges.end());
edges.resize(new_end - edges.begin());
return edges;
}
private:
+41 -1
View File
@@ -104,7 +104,47 @@ template <typename T> void write(io::FileWriter &writer, const util::vector_view
{
const auto count = data.size();
writer.WriteElementCount64(count);
return writer.WriteFrom(data.data(), count);
writer.WriteFrom(data.data(), count);
}
template <> inline void read<bool>(io::FileReader &reader, util::vector_view<bool> &data)
{
const auto count = reader.ReadElementCount64();
BOOST_ASSERT(data.size() == count);
for (const auto index : util::irange<std::uint64_t>(0, count))
{
data[index] = reader.ReadOne<bool>();
}
}
template <> inline void write<bool>(io::FileWriter &writer, const util::vector_view<bool> &data)
{
const auto count = data.size();
writer.WriteElementCount64(count);
for (const auto index : util::irange<std::uint64_t>(0, count))
{
writer.WriteOne<bool>(data[index]);
}
}
template <> inline void read<bool>(io::FileReader &reader, std::vector<bool> &data)
{
const auto count = reader.ReadElementCount64();
BOOST_ASSERT(data.size() == count);
for (const auto index : util::irange<std::uint64_t>(0, count))
{
data[index] = reader.ReadOne<bool>();
}
}
template <> inline void write<bool>(io::FileWriter &writer, const std::vector<bool> &data)
{
const auto count = data.size();
writer.WriteElementCount64(count);
for (const auto index : util::irange<std::uint64_t>(0, count))
{
writer.WriteOne<bool>(data[index]);
}
}
}
}
+44 -2
View File
@@ -143,8 +143,33 @@ template <> class vector_view<bool>
unsigned *m_ptr;
std::size_t m_size;
static constexpr std::size_t UNSIGNED_BITS = CHAR_BIT * sizeof(unsigned);
public:
using value_type = bool;
struct reference
{
reference &operator=(bool value)
{
*m_ptr = (*m_ptr & ~mask) | (static_cast<unsigned>(value) * mask);
return *this;
}
operator bool() const { return (*m_ptr) & mask; }
bool operator==(const reference &other) const
{
return other.m_ptr == m_ptr && other.mask == mask;
}
friend std::ostream &operator<<(std::ostream &os, const reference &rhs)
{
return os << static_cast<bool>(rhs);
}
unsigned *m_ptr;
const unsigned mask;
};
vector_view() : m_ptr(nullptr), m_size(0) {}
@@ -153,8 +178,8 @@ template <> class vector_view<bool>
bool at(const std::size_t index) const
{
BOOST_ASSERT_MSG(index < m_size, "invalid size");
const std::size_t bucket = index / (CHAR_BIT * sizeof(unsigned));
const unsigned offset = index % (CHAR_BIT * sizeof(unsigned));
const std::size_t bucket = index / UNSIGNED_BITS;
const unsigned offset = index % UNSIGNED_BITS;
return m_ptr[bucket] & (1u << offset);
}
@@ -166,6 +191,14 @@ template <> class vector_view<bool>
bool operator[](const unsigned index) const { return at(index); }
reference operator[](const unsigned index)
{
BOOST_ASSERT(index < m_size);
const std::size_t bucket = index / UNSIGNED_BITS;
const unsigned offset = index % UNSIGNED_BITS;
return reference{m_ptr + bucket, 1u << offset};
}
template <typename T> friend void swap(vector_view<T> &, vector_view<T> &) noexcept;
};
@@ -186,6 +219,15 @@ template <typename DataT, storage::Ownership Ownership>
using ViewOrVector = typename std::conditional<Ownership == storage::Ownership::View,
vector_view<DataT>,
InternalOrExternalVector<DataT, Ownership>>::type;
// We can use this for compile time assertions
template <typename ValueT, typename VectorT>
struct is_view_or_vector
: std::integral_constant<bool,
std::is_same<std::vector<ValueT>, VectorT>::value ||
std::is_same<util::vector_view<ValueT>, VectorT>::value>
{
};
}
}