Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally cache CH/MLD edge distances #5197

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions include/contractor/contractor_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,14 @@ struct ContractorConfig final : storage::IOConfig
// The remaining vertices form the core of the hierarchy
//(e.g. 0.8 contracts 80 percent of the hierarchy, leaving a core of 20%)
double core_factor;

// Whether to store distances for CH edges in addition to duration/weight
// Defaults to false. Setting to true will require more storage/memory,
// but avoids the need for path unpacking to learn the distance of a CH
// route (useful for faster distance results in table queries)
bool cache_distances;
};
}
}
} // namespace contractor
} // namespace osrm

#endif // EXTRACTOR_OPTIONS_HPP
7 changes: 5 additions & 2 deletions include/contractor/contractor_graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,26 @@ namespace contractor
struct ContractorEdgeData
{
ContractorEdgeData()
: weight(0), duration(0), id(0), originalEdges(0), shortcut(0), forward(0), backward(0)
: weight(0), duration(0), distance(0), id(0), originalEdges(0), shortcut(0), forward(0),
backward(0)
{
}
ContractorEdgeData(EdgeWeight weight,
EdgeWeight duration,
EdgeDistance distance,
unsigned original_edges,
unsigned id,
bool shortcut,
bool forward,
bool backward)
: weight(weight), duration(duration), id(id),
: weight(weight), duration(duration), distance(distance), id(id),
originalEdges(std::min((1u << 29) - 1u, original_edges)), shortcut(shortcut),
forward(forward), backward(backward)
{
}
EdgeWeight weight;
EdgeWeight duration;
EdgeDistance distance;
unsigned id;
unsigned originalEdges : 29;
bool shortcut : 1;
Expand Down
8 changes: 8 additions & 0 deletions include/contractor/graph_contractor_adaptors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ ContractorGraph toContractorGraph(NodeID number_of_nodes, InputEdgeContainer inp
input_edge.target,
std::max(input_edge.data.weight, 1),
input_edge.data.duration,
input_edge.data.distance,
1,
input_edge.data.turn_id,
false,
Expand All @@ -51,6 +52,7 @@ ContractorGraph toContractorGraph(NodeID number_of_nodes, InputEdgeContainer inp
input_edge.source,
std::max(input_edge.data.weight, 1),
input_edge.data.duration,
input_edge.data.distance,
1,
input_edge.data.turn_id,
false,
Expand Down Expand Up @@ -82,6 +84,7 @@ ContractorGraph toContractorGraph(NodeID number_of_nodes, InputEdgeContainer inp
forward_edge.data.originalEdges = reverse_edge.data.originalEdges = 1;
forward_edge.data.weight = reverse_edge.data.weight = INVALID_EDGE_WEIGHT;
forward_edge.data.duration = reverse_edge.data.duration = MAXIMAL_EDGE_DURATION;
forward_edge.data.distance = reverse_edge.data.distance = MAXIMAL_EDGE_DISTANCE;
// remove parallel edges
while (i < edges.size() && edges[i].source == source && edges[i].target == target)
{
Expand All @@ -90,12 +93,16 @@ ContractorGraph toContractorGraph(NodeID number_of_nodes, InputEdgeContainer inp
forward_edge.data.weight = std::min(edges[i].data.weight, forward_edge.data.weight);
forward_edge.data.duration =
std::min(edges[i].data.duration, forward_edge.data.duration);
forward_edge.data.distance =
std::min(edges[i].data.distance, forward_edge.data.distance);
}
if (edges[i].data.backward)
{
reverse_edge.data.weight = std::min(edges[i].data.weight, reverse_edge.data.weight);
reverse_edge.data.duration =
std::min(edges[i].data.duration, reverse_edge.data.duration);
reverse_edge.data.distance =
std::min(edges[i].data.distance, reverse_edge.data.distance);
}
++i;
}
Expand Down Expand Up @@ -151,6 +158,7 @@ template <class Edge, typename GraphT> inline std::vector<Edge> toEdges(GraphT g
BOOST_ASSERT_MSG(SPECIAL_NODEID != new_edge.target, "Target id invalid");
new_edge.data.weight = data.weight;
new_edge.data.duration = data.duration;
new_edge.data.distance = data.distance;
new_edge.data.shortcut = data.shortcut;
new_edge.data.turn_id = data.id;
BOOST_ASSERT_MSG(new_edge.data.turn_id != INT_MAX, // 2^31
Expand Down
15 changes: 10 additions & 5 deletions include/contractor/query_edge.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@ struct QueryEdge
struct EdgeData
{
explicit EdgeData()
: turn_id(0), shortcut(false), weight(0), duration(0), forward(false), backward(false)
: turn_id(0), shortcut(false), weight(0), duration(0), forward(false), backward(false),
distance(0)
{
}

EdgeData(const NodeID turn_id,
const bool shortcut,
const EdgeWeight weight,
const EdgeWeight duration,
const EdgeDistance distance,
const bool forward,
const bool backward)
: turn_id(turn_id), shortcut(shortcut), weight(weight), duration(duration),
forward(forward), backward(backward)
forward(forward), backward(backward), distance(distance)
{
}

Expand All @@ -40,6 +42,7 @@ struct QueryEdge
turn_id = other.id;
forward = other.forward;
backward = other.backward;
distance = other.distance;
}
// this ID is either the middle node of the shortcut, or the ID of the edge based node (node
// based edge) storing the appropriate data. If `shortcut` is set to true, we get the middle
Expand All @@ -50,6 +53,7 @@ struct QueryEdge
EdgeWeight duration : 30;
std::uint32_t forward : 1;
std::uint32_t backward : 1;
EdgeDistance distance;
} data;

QueryEdge() : source(SPECIAL_NODEID), target(SPECIAL_NODEID) {}
Expand All @@ -69,10 +73,11 @@ struct QueryEdge
return (source == right.source && target == right.target &&
data.weight == right.data.weight && data.duration == right.data.duration &&
data.shortcut == right.data.shortcut && data.forward == right.data.forward &&
data.backward == right.data.backward && data.turn_id == right.data.turn_id);
data.backward == right.data.backward && data.turn_id == right.data.turn_id &&
data.distance == right.data.distance);
}
};
}
}
} // namespace contractor
} // namespace osrm

#endif // QUERYEDGE_HPP
22 changes: 13 additions & 9 deletions include/engine/phantom_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "util/coordinate.hpp"
#include "util/typedefs.hpp"

#include <iostream>

#include <boost/assert.hpp>

namespace osrm
Expand All @@ -44,14 +46,15 @@ namespace engine
struct PhantomNode
{
PhantomNode()
: forward_segment_id{SPECIAL_SEGMENTID, false},
reverse_segment_id{SPECIAL_SEGMENTID, false}, forward_weight(INVALID_EDGE_WEIGHT),
reverse_weight(INVALID_EDGE_WEIGHT), forward_weight_offset(0), reverse_weight_offset(0),
: forward_segment_id{SPECIAL_SEGMENTID, false}, reverse_segment_id{SPECIAL_SEGMENTID,
false},
forward_weight(INVALID_EDGE_WEIGHT), reverse_weight(INVALID_EDGE_WEIGHT),
forward_weight_offset(0), reverse_weight_offset(0),
forward_distance(INVALID_EDGE_DISTANCE), reverse_distance(INVALID_EDGE_DISTANCE),
forward_distance_offset(0), reverse_distance_offset(0),
forward_duration(MAXIMAL_EDGE_DURATION), reverse_duration(MAXIMAL_EDGE_DURATION),
forward_duration_offset(0), reverse_duration_offset(0), fwd_segment_position(0),
is_valid_forward_source{false}, is_valid_forward_target{false},
forward_duration_offset(0), reverse_duration_offset(0),
fwd_segment_position(0), is_valid_forward_source{false}, is_valid_forward_target{false},
is_valid_reverse_source{false}, is_valid_reverse_target{false}, bearing(0)

{
Expand Down Expand Up @@ -109,8 +112,9 @@ struct PhantomNode

bool IsValid(const unsigned number_of_nodes) const
{
return location.IsValid() && ((forward_segment_id.id < number_of_nodes) ||
(reverse_segment_id.id < number_of_nodes)) &&
return location.IsValid() &&
((forward_segment_id.id < number_of_nodes) ||
(reverse_segment_id.id < number_of_nodes)) &&
((forward_weight != INVALID_EDGE_WEIGHT) ||
(reverse_weight != INVALID_EDGE_WEIGHT)) &&
((forward_duration != MAXIMAL_EDGE_DURATION) ||
Expand Down Expand Up @@ -234,7 +238,7 @@ struct PhantomNodes
PhantomNode source_phantom;
PhantomNode target_phantom;
};
}
}
} // namespace engine
} // namespace osrm

#endif // PHANTOM_NODES_H
11 changes: 7 additions & 4 deletions include/engine/routing_algorithms/many_to_many.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,28 @@ struct NodeBucket
unsigned from_clique_arc : 1;
EdgeWeight weight;
EdgeDuration duration;
EdgeDistance distance;

NodeBucket(NodeID middle_node,
NodeID parent_node,
bool from_clique_arc,
unsigned column_index,
EdgeWeight weight,
EdgeDuration duration)
EdgeDuration duration,
EdgeDistance distance)
: middle_node(middle_node), parent_node(parent_node), column_index(column_index),
from_clique_arc(from_clique_arc), weight(weight), duration(duration)
from_clique_arc(from_clique_arc), weight(weight), duration(duration), distance(distance)
{
}

NodeBucket(NodeID middle_node,
NodeID parent_node,
unsigned column_index,
EdgeWeight weight,
EdgeDuration duration)
EdgeDuration duration,
EdgeDistance distance)
: middle_node(middle_node), parent_node(parent_node), column_index(column_index),
from_clique_arc(false), weight(weight), duration(duration)
from_clique_arc(false), weight(weight), duration(duration), distance(distance)
{
}

Expand Down
16 changes: 12 additions & 4 deletions include/engine/routing_algorithms/routing_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,17 @@ void insertSourceInHeap(typename SearchEngineData<Algorithm>::ManyToManyQueryHea
{
heap.Insert(phantom_node.forward_segment_id.id,
-phantom_node.GetForwardWeightPlusOffset(),
{phantom_node.forward_segment_id.id, -phantom_node.GetForwardDuration()});
{phantom_node.forward_segment_id.id,
-phantom_node.GetForwardDuration(),
-phantom_node.GetForwardDistance()});
}
if (phantom_node.IsValidReverseTarget())
{
heap.Insert(phantom_node.reverse_segment_id.id,
-phantom_node.GetReverseWeightPlusOffset(),
{phantom_node.reverse_segment_id.id, -phantom_node.GetReverseDuration()});
{phantom_node.reverse_segment_id.id,
-phantom_node.GetReverseDuration(),
-phantom_node.GetReverseDistance()});
}
}

Expand All @@ -72,13 +76,17 @@ void insertTargetInHeap(typename SearchEngineData<Algorithm>::ManyToManyQueryHea
{
heap.Insert(phantom_node.forward_segment_id.id,
phantom_node.GetForwardWeightPlusOffset(),
{phantom_node.forward_segment_id.id, phantom_node.GetForwardDuration()});
{phantom_node.forward_segment_id.id,
phantom_node.GetForwardDuration(),
phantom_node.GetForwardDistance()});
}
if (phantom_node.IsValidReverseTarget())
{
heap.Insert(phantom_node.reverse_segment_id.id,
phantom_node.GetReverseWeightPlusOffset(),
{phantom_node.reverse_segment_id.id, phantom_node.GetReverseDuration()});
{phantom_node.reverse_segment_id.id,
phantom_node.GetReverseDuration(),
phantom_node.GetReverseDistance()});
}
}

Expand Down
11 changes: 8 additions & 3 deletions include/engine/routing_algorithms/routing_base_ch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,10 @@ void routingStep(const DataFacade<Algorithm> &facade,
}

template <bool UseDuration>
EdgeWeight getLoopWeight(const DataFacade<Algorithm> &facade, NodeID node)
std::tuple<EdgeWeight,EdgeDistance> getLoopWeight(const DataFacade<Algorithm> &facade, NodeID node)
{
EdgeWeight loop_weight = UseDuration ? MAXIMAL_EDGE_DURATION : INVALID_EDGE_WEIGHT;
EdgeDistance loop_distance = MAXIMAL_EDGE_DISTANCE;
for (auto edge : facade.GetAdjacentEdgeRange(node))
{
const auto &data = facade.GetEdgeData(edge);
Expand All @@ -198,11 +199,15 @@ EdgeWeight getLoopWeight(const DataFacade<Algorithm> &facade, NodeID node)
if (to == node)
{
const auto value = UseDuration ? data.duration : data.weight;
loop_weight = std::min(loop_weight, value);
if (value < loop_weight)
{
loop_weight = value;
loop_distance = data.distance;
}
}
}
}
return loop_weight;
return std::make_tuple(loop_weight, loop_distance);
}

/**
Expand Down
22 changes: 15 additions & 7 deletions include/engine/search_engine_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ struct HeapData
struct ManyToManyHeapData : HeapData
{
EdgeWeight duration;
ManyToManyHeapData(NodeID p, EdgeWeight duration) : HeapData(p), duration(duration) {}
EdgeDistance distance;
ManyToManyHeapData(NodeID p, EdgeWeight duration, EdgeDistance distance)
: HeapData(p), duration(duration), distance(distance)
{
}
};

template <> struct SearchEngineData<routing_algorithms::ch::Algorithm>
Expand Down Expand Up @@ -75,12 +79,16 @@ struct MultiLayerDijkstraHeapData
struct ManyToManyMultiLayerDijkstraHeapData : MultiLayerDijkstraHeapData
{
EdgeWeight duration;
ManyToManyMultiLayerDijkstraHeapData(NodeID p, EdgeWeight duration)
: MultiLayerDijkstraHeapData(p), duration(duration)
EdgeDistance distance;
ManyToManyMultiLayerDijkstraHeapData(NodeID p, EdgeWeight duration, EdgeDistance distance)
: MultiLayerDijkstraHeapData(p), duration(duration), distance(distance)
{
}
ManyToManyMultiLayerDijkstraHeapData(NodeID p, bool from, EdgeWeight duration)
: MultiLayerDijkstraHeapData(p, from), duration(duration)
ManyToManyMultiLayerDijkstraHeapData(NodeID p,
bool from,
EdgeWeight duration,
EdgeDistance distance)
: MultiLayerDijkstraHeapData(p, from), duration(duration), distance(distance)
{
}
};
Expand Down Expand Up @@ -112,7 +120,7 @@ template <> struct SearchEngineData<routing_algorithms::mld::Algorithm>
void InitializeOrClearManyToManyThreadLocalStorage(unsigned number_of_nodes,
unsigned number_of_boundary_nodes);
};
}
}
} // namespace engine
} // namespace osrm

#endif // SEARCH_ENGINE_DATA_HPP
4 changes: 2 additions & 2 deletions include/extractor/compressed_edge_container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class CompressedEdgeContainer
std::unordered_map<EdgeID, unsigned> m_reverse_edge_id_to_zipped_index_map;
std::unique_ptr<SegmentDataContainer> segment_data;
};
}
}
} // namespace extractor
} // namespace osrm

#endif // GEOMETRY_COMPRESSOR_HPP_
Loading