Add duration values to overlay graph

This commit is contained in:
Michael Krasnyk 2017-06-29 00:20:04 +02:00 committed by Patrick Niklaus
parent 5a29e29535
commit b910ab9bcb
7 changed files with 99 additions and 41 deletions

View File

@ -20,6 +20,7 @@ class CellCustomizer
struct HeapData
{
bool from_clique;
EdgeDuration duration;
};
public:
@ -41,31 +42,41 @@ class CellCustomizer
{
std::unordered_set<NodeID> destinations_set(destinations.begin(), destinations.end());
heap.Clear();
heap.Insert(source, 0, {false});
heap.Insert(source, 0, {false, 0});
// explore search space
while (!heap.Empty() && !destinations_set.empty())
{
const NodeID node = heap.DeleteMin();
const EdgeWeight weight = heap.GetKey(node);
const EdgeDuration duration = heap.GetData(node).duration;
if (level == 1)
RelaxNode<true>(graph, cells, heap, level, node, weight);
RelaxNode<true>(graph, cells, heap, level, node, weight, duration);
else
RelaxNode<false>(graph, cells, heap, level, node, weight);
RelaxNode<false>(graph, cells, heap, level, node, weight, duration);
destinations_set.erase(node);
}
// fill a map of destination nodes to placeholder pointers
auto destination_iter = destinations.begin();
for (auto &weight : cell.GetOutWeight(source))
auto weights = cell.GetOutWeight(source);
auto durations = cell.GetOutDuration(source);
for (auto &destination : destinations)
{
BOOST_ASSERT(destination_iter != destinations.end());
const auto destination = *destination_iter++;
weight =
heap.WasInserted(destination) ? heap.GetKey(destination) : INVALID_EDGE_WEIGHT;
BOOST_ASSERT(!weights.empty());
BOOST_ASSERT(!durations.empty());
const bool inserted = heap.WasInserted(destination);
weights.front() = inserted ? heap.GetKey(destination) : INVALID_EDGE_WEIGHT;
durations.front() =
inserted ? heap.GetData(destination).duration : MAXIMAL_EDGE_DURATION;
weights.advance_begin(1);
durations.advance_begin(1);
}
BOOST_ASSERT(weights.empty());
BOOST_ASSERT(durations.empty());
}
}
@ -94,7 +105,8 @@ class CellCustomizer
Heap &heap,
LevelID level,
NodeID node,
EdgeWeight weight) const
EdgeWeight weight,
EdgeDuration duration) const
{
BOOST_ASSERT(heap.WasInserted(node));
@ -113,24 +125,26 @@ class CellCustomizer
auto subcell_id = partition.GetCell(level - 1, node);
auto subcell = cells.GetCell(level - 1, subcell_id);
auto subcell_destination = subcell.GetDestinationNodes().begin();
auto subcell_duration = subcell.GetOutDuration(node).begin();
for (auto subcell_weight : subcell.GetOutWeight(node))
{
if (subcell_weight != INVALID_EDGE_WEIGHT)
{
const NodeID to = *subcell_destination;
const EdgeWeight to_weight = subcell_weight + weight;
const EdgeWeight to_weight = weight + subcell_weight;
if (!heap.WasInserted(to))
{
heap.Insert(to, to_weight, {true});
heap.Insert(to, to_weight, {true, duration + *subcell_duration});
}
else if (to_weight < heap.GetKey(to))
{
heap.DecreaseKey(to, to_weight);
heap.GetData(to).from_clique = true;
heap.GetData(to) = {true, duration + *subcell_duration};
}
}
++subcell_destination;
++subcell_duration;
}
}
}
@ -144,15 +158,15 @@ class CellCustomizer
(first_level ||
partition.GetCell(level - 1, node) != partition.GetCell(level - 1, to)))
{
const EdgeWeight to_weight = data.weight + weight;
const EdgeWeight to_weight = weight + data.weight;
if (!heap.WasInserted(to))
{
heap.Insert(to, to_weight, {false});
heap.Insert(to, to_weight, {false, duration + data.duration});
}
else if (to_weight < heap.GetKey(to))
{
heap.DecreaseKey(to, to_weight);
heap.GetData(to).from_clique = false;
heap.GetData(to) = {false, duration + data.duration};
}
}
}

View File

@ -964,6 +964,8 @@ template <> class ContiguousInternalMemoryAlgorithmDataFacade<MLD> : public Algo
auto mld_cell_weights_ptr = data_layout.GetBlockPtr<EdgeWeight>(
memory_block, storage::DataLayout::MLD_CELL_WEIGHTS);
auto mld_cell_durations_ptr = data_layout.GetBlockPtr<EdgeDuration>(
memory_block, storage::DataLayout::MLD_CELL_DURATIONS);
auto mld_source_boundary_ptr = data_layout.GetBlockPtr<NodeID>(
memory_block, storage::DataLayout::MLD_CELL_SOURCE_BOUNDARY);
auto mld_destination_boundary_ptr = data_layout.GetBlockPtr<NodeID>(
@ -975,6 +977,8 @@ template <> class ContiguousInternalMemoryAlgorithmDataFacade<MLD> : public Algo
auto weight_entries_count =
data_layout.GetBlockEntries(storage::DataLayout::MLD_CELL_WEIGHTS);
auto duration_entries_count =
data_layout.GetBlockEntries(storage::DataLayout::MLD_CELL_DURATIONS);
auto source_boundary_entries_count =
data_layout.GetBlockEntries(storage::DataLayout::MLD_CELL_SOURCE_BOUNDARY);
auto destination_boundary_entries_count =
@ -983,7 +987,11 @@ template <> class ContiguousInternalMemoryAlgorithmDataFacade<MLD> : public Algo
auto cell_level_offsets_entries_count =
data_layout.GetBlockEntries(storage::DataLayout::MLD_CELL_LEVEL_OFFSETS);
BOOST_ASSERT(weight_entries_count == duration_entries_count);
util::vector_view<EdgeWeight> weights(mld_cell_weights_ptr, weight_entries_count);
util::vector_view<EdgeDuration> durations(mld_cell_durations_ptr,
duration_entries_count);
util::vector_view<NodeID> source_boundary(mld_source_boundary_ptr,
source_boundary_entries_count);
util::vector_view<NodeID> destination_boundary(mld_destination_boundary_ptr,
@ -994,6 +1002,7 @@ template <> class ContiguousInternalMemoryAlgorithmDataFacade<MLD> : public Algo
cell_level_offsets_entries_count);
mld_cell_storage = partition::CellStorageView{std::move(weights),
std::move(durations),
std::move(source_boundary),
std::move(destination_boundary),
std::move(cells),

View File

@ -46,18 +46,18 @@ namespace detail
template <storage::Ownership Ownership> class CellStorageImpl
{
public:
using WeightOffset = std::uint32_t;
using ValueOffset = std::uint32_t;
using BoundaryOffset = std::uint32_t;
using BoundarySize = std::uint32_t;
using SourceIndex = std::uint32_t;
using DestinationIndex = std::uint32_t;
static constexpr auto INVALID_WEIGHT_OFFSET = std::numeric_limits<WeightOffset>::max();
static constexpr auto INVALID_VALUE_OFFSET = std::numeric_limits<ValueOffset>::max();
static constexpr auto INVALID_BOUNDARY_OFFSET = std::numeric_limits<BoundaryOffset>::max();
struct CellData
{
WeightOffset weight_offset = INVALID_WEIGHT_OFFSET;
ValueOffset value_offset = INVALID_VALUE_OFFSET;
BoundaryOffset source_boundary_offset = INVALID_BOUNDARY_OFFSET;
BoundaryOffset destination_boundary_offset = INVALID_BOUNDARY_OFFSET;
BoundarySize num_source_nodes = 0;
@ -69,15 +69,16 @@ template <storage::Ownership Ownership> class CellStorageImpl
// Implementation of the cell view. We need a template parameter here
// because we need to derive a read-only and read-write view from this.
template <typename WeightValueT> class CellImpl
template <typename WeightValueT, typename DurationValueT> class CellImpl
{
private:
using WeightPtrT = WeightValueT *;
using WeightRefT = WeightValueT &;
using DurationPtrT = DurationValueT *;
BoundarySize num_source_nodes;
BoundarySize num_destination_nodes;
WeightPtrT const weights;
DurationPtrT const durations;
const NodeID *const source_boundary;
const NodeID *const destination_boundary;
@ -123,20 +124,19 @@ template <storage::Ownership Ownership> class CellStorageImpl
const std::size_t stride;
};
public:
auto GetOutWeight(NodeID node) const
template <typename ValuePtr> auto GetOutRange(const ValuePtr ptr, const NodeID node) const
{
auto iter = std::find(source_boundary, source_boundary + num_source_nodes, node);
if (iter == source_boundary + num_source_nodes)
return boost::make_iterator_range(weights, weights);
return boost::make_iterator_range(ptr, ptr);
auto row = std::distance(source_boundary, iter);
auto begin = weights + num_destination_nodes * row;
auto begin = ptr + num_destination_nodes * row;
auto end = begin + num_destination_nodes;
return boost::make_iterator_range(begin, end);
}
auto GetInWeight(NodeID node) const
template <typename ValuePtr> auto GetInRange(const ValuePtr ptr, const NodeID node) const
{
auto iter =
std::find(destination_boundary, destination_boundary + num_destination_nodes, node);
@ -144,12 +144,21 @@ template <storage::Ownership Ownership> class CellStorageImpl
return boost::make_iterator_range(ColumnIterator{}, ColumnIterator{});
auto column = std::distance(destination_boundary, iter);
auto begin = ColumnIterator{weights + column, num_destination_nodes};
auto end = ColumnIterator{weights + column + num_source_nodes * num_destination_nodes,
auto begin = ColumnIterator{ptr + column, num_destination_nodes};
auto end = ColumnIterator{ptr + column + num_source_nodes * num_destination_nodes,
num_destination_nodes};
return boost::make_iterator_range(begin, end);
}
public:
auto GetOutWeight(NodeID node) const { return GetOutRange(weights, node); }
auto GetInWeight(NodeID node) const { return GetInRange(weights, node); }
auto GetOutDuration(NodeID node) const { return GetOutRange(durations, node); }
auto GetInDuration(NodeID node) const { return GetInRange(durations, node); }
auto GetSourceNodes() const
{
return boost::make_iterator_range(source_boundary, source_boundary + num_source_nodes);
@ -162,16 +171,19 @@ template <storage::Ownership Ownership> class CellStorageImpl
}
CellImpl(const CellData &data,
WeightPtrT const all_weight,
WeightPtrT const all_weights,
DurationPtrT const all_durations,
const NodeID *const all_sources,
const NodeID *const all_destinations)
: num_source_nodes{data.num_source_nodes},
num_destination_nodes{data.num_destination_nodes},
weights{all_weight + data.weight_offset},
weights{all_weights + data.value_offset},
durations{all_durations + data.value_offset},
source_boundary{all_sources + data.source_boundary_offset},
destination_boundary{all_destinations + data.destination_boundary_offset}
{
BOOST_ASSERT(all_weight != nullptr);
BOOST_ASSERT(all_weights != nullptr);
BOOST_ASSERT(all_durations != nullptr);
BOOST_ASSERT(num_source_nodes == 0 || all_sources != nullptr);
BOOST_ASSERT(num_destination_nodes == 0 || all_destinations != nullptr);
}
@ -180,8 +192,8 @@ template <storage::Ownership Ownership> class CellStorageImpl
std::size_t LevelIDToIndex(LevelID level) const { return level - 1; }
public:
using Cell = CellImpl<EdgeWeight>;
using ConstCell = CellImpl<const EdgeWeight>;
using Cell = CellImpl<EdgeWeight, EdgeDuration>;
using ConstCell = CellImpl<const EdgeWeight, const EdgeDuration>;
CellStorageImpl() {}
@ -304,24 +316,27 @@ template <storage::Ownership Ownership> class CellStorageImpl
<< " Number of unconnected nodes is " << number_of_unconneced;
}
// Set weight offsets and calculate total storage size
WeightOffset weight_offset = 0;
// Set cell values offsets and calculate total storage size
ValueOffset value_offset = 0;
for (auto &cell : cells)
{
cell.weight_offset = weight_offset;
weight_offset += cell.num_source_nodes * cell.num_destination_nodes;
cell.value_offset = value_offset;
value_offset += cell.num_source_nodes * cell.num_destination_nodes;
}
weights.resize(weight_offset + 1, INVALID_EDGE_WEIGHT);
weights.resize(value_offset + 1, INVALID_EDGE_WEIGHT);
durations.resize(value_offset + 1, MAXIMAL_EDGE_DURATION);
}
template <typename = std::enable_if<Ownership == storage::Ownership::View>>
CellStorageImpl(Vector<EdgeWeight> weights_,
Vector<EdgeDuration> durations_,
Vector<NodeID> source_boundary_,
Vector<NodeID> destination_boundary_,
Vector<CellData> cells_,
Vector<std::uint64_t> level_to_cell_offset_)
: weights(std::move(weights_)), source_boundary(std::move(source_boundary_)),
: weights(std::move(weights_)), durations(std::move(durations_)),
source_boundary(std::move(source_boundary_)),
destination_boundary(std::move(destination_boundary_)), cells(std::move(cells_)),
level_to_cell_offset(std::move(level_to_cell_offset_))
{
@ -336,6 +351,7 @@ template <storage::Ownership Ownership> class CellStorageImpl
BOOST_ASSERT(cell_index < cells.size());
return ConstCell{cells[cell_index],
weights.data(),
durations.data(),
source_boundary.empty() ? nullptr : source_boundary.data(),
destination_boundary.empty() ? nullptr : destination_boundary.data()};
}
@ -348,8 +364,11 @@ template <storage::Ownership Ownership> class CellStorageImpl
const auto offset = level_to_cell_offset[level_index];
const auto cell_index = offset + id;
BOOST_ASSERT(cell_index < cells.size());
return Cell{
cells[cell_index], weights.data(), source_boundary.data(), destination_boundary.data()};
return Cell{cells[cell_index],
weights.data(),
durations.data(),
source_boundary.data(),
destination_boundary.data()};
}
friend void serialization::read<Ownership>(storage::io::FileReader &reader,
@ -359,6 +378,7 @@ template <storage::Ownership Ownership> class CellStorageImpl
private:
Vector<EdgeWeight> weights;
Vector<EdgeDuration> durations;
Vector<NodeID> source_boundary;
Vector<NodeID> destination_boundary;
Vector<CellData> cells;

View File

@ -55,6 +55,7 @@ template <storage::Ownership Ownership>
inline void read(storage::io::FileReader &reader, detail::CellStorageImpl<Ownership> &storage)
{
storage::serialization::read(reader, storage.weights);
storage::serialization::read(reader, storage.durations);
storage::serialization::read(reader, storage.source_boundary);
storage::serialization::read(reader, storage.destination_boundary);
storage::serialization::read(reader, storage.cells);
@ -66,6 +67,7 @@ inline void write(storage::io::FileWriter &writer,
const detail::CellStorageImpl<Ownership> &storage)
{
storage::serialization::write(writer, storage.weights);
storage::serialization::write(writer, storage.durations);
storage::serialization::write(writer, storage.source_boundary);
storage::serialization::write(writer, storage.destination_boundary);
storage::serialization::write(writer, storage.cells);

View File

@ -61,6 +61,7 @@ const constexpr char *block_id_to_name[] = {"NAME_CHAR_DATA",
"MLD_PARTITION",
"MLD_CELL_TO_CHILDREN",
"MLD_CELL_WEIGHTS",
"MLD_CELL_DURATIONS",
"MLD_CELL_SOURCE_BOUNDARY",
"MLD_CELL_DESTINATION_BOUNDARY",
"MLD_CELLS",
@ -116,6 +117,7 @@ struct DataLayout
MLD_PARTITION,
MLD_CELL_TO_CHILDREN,
MLD_CELL_WEIGHTS,
MLD_CELL_DURATIONS,
MLD_CELL_SOURCE_BOUNDARY,
MLD_CELL_DESTINATION_BOUNDARY,
MLD_CELLS,

View File

@ -115,5 +115,6 @@ void SearchEngineData<MLD>::InitializeOrClearFirstThreadLocalStorage(unsigned nu
reverse_heap_1.reset(new QueryHeap(number_of_nodes));
}
}
}
}

View File

@ -446,6 +446,8 @@ void Storage::PopulateLayout(DataLayout &layout)
const auto weights_count = reader.ReadVectorSize<EdgeWeight>();
layout.SetBlockSize<EdgeWeight>(DataLayout::MLD_CELL_WEIGHTS, weights_count);
const auto durations_count = reader.ReadVectorSize<EdgeDuration>();
layout.SetBlockSize<EdgeDuration>(DataLayout::MLD_CELL_DURATIONS, durations_count);
const auto source_node_count = reader.ReadVectorSize<NodeID>();
layout.SetBlockSize<NodeID>(DataLayout::MLD_CELL_SOURCE_BOUNDARY, source_node_count);
const auto destination_node_count = reader.ReadVectorSize<NodeID>();
@ -461,6 +463,7 @@ void Storage::PopulateLayout(DataLayout &layout)
else
{
layout.SetBlockSize<char>(DataLayout::MLD_CELL_WEIGHTS, 0);
layout.SetBlockSize<char>(DataLayout::MLD_CELL_DURATIONS, 0);
layout.SetBlockSize<char>(DataLayout::MLD_CELL_SOURCE_BOUNDARY, 0);
layout.SetBlockSize<char>(DataLayout::MLD_CELL_DESTINATION_BOUNDARY, 0);
layout.SetBlockSize<char>(DataLayout::MLD_CELLS, 0);
@ -884,6 +887,8 @@ void Storage::PopulateData(const DataLayout &layout, char *memory_ptr)
auto mld_cell_weights_ptr = layout.GetBlockPtr<EdgeWeight, true>(
memory_ptr, storage::DataLayout::MLD_CELL_WEIGHTS);
auto mld_cell_duration_ptr = layout.GetBlockPtr<EdgeDuration, true>(
memory_ptr, storage::DataLayout::MLD_CELL_DURATIONS);
auto mld_source_boundary_ptr = layout.GetBlockPtr<NodeID, true>(
memory_ptr, storage::DataLayout::MLD_CELL_SOURCE_BOUNDARY);
auto mld_destination_boundary_ptr = layout.GetBlockPtr<NodeID, true>(
@ -895,6 +900,8 @@ void Storage::PopulateData(const DataLayout &layout, char *memory_ptr)
auto weight_entries_count =
layout.GetBlockEntries(storage::DataLayout::MLD_CELL_WEIGHTS);
auto duration_entries_count =
layout.GetBlockEntries(storage::DataLayout::MLD_CELL_DURATIONS);
auto source_boundary_entries_count =
layout.GetBlockEntries(storage::DataLayout::MLD_CELL_SOURCE_BOUNDARY);
auto destination_boundary_entries_count =
@ -904,6 +911,8 @@ void Storage::PopulateData(const DataLayout &layout, char *memory_ptr)
layout.GetBlockEntries(storage::DataLayout::MLD_CELL_LEVEL_OFFSETS);
util::vector_view<EdgeWeight> weights(mld_cell_weights_ptr, weight_entries_count);
util::vector_view<EdgeDuration> durations(mld_cell_duration_ptr,
duration_entries_count);
util::vector_view<NodeID> source_boundary(mld_source_boundary_ptr,
source_boundary_entries_count);
util::vector_view<NodeID> destination_boundary(mld_destination_boundary_ptr,
@ -914,6 +923,7 @@ void Storage::PopulateData(const DataLayout &layout, char *memory_ptr)
cell_level_offsets_entries_count);
partition::CellStorageView storage{std::move(weights),
std::move(durations),
std::move(source_boundary),
std::move(destination_boundary),
std::move(cells),