Implement Parallel Spatial-Ordering/Cut Selection
Extends explanation for recursive bisection ids Cleans up Bisection State Removes license boilerplate from partitioner config Sorts Spatially and picks Sources and Sinks Uses sets for sources and sinks for now; see how large they will get Runs n cuts in parallel changing the slope and uses the best Clarifies balance <-> ratio naming
This commit is contained in:
committed by
Patrick Niklaus
parent
db7adfa77b
commit
dd60ae31ae
@@ -31,7 +31,11 @@ namespace partition
|
||||
class DinicMaxFlow
|
||||
{
|
||||
public:
|
||||
using PartitionResult = std::vector<bool>;
|
||||
using PartitionResult = struct
|
||||
{
|
||||
std::size_t num_edges;
|
||||
std::vector<bool> flags;
|
||||
};
|
||||
using SourceSinkNodes = std::set<NodeID>;
|
||||
using LevelGraph = std::unordered_map<NodeID, std::uint32_t>;
|
||||
using FlowEdges = std::unordered_set<std::pair<NodeID, NodeID>>;
|
||||
|
||||
@@ -81,6 +81,9 @@ class GraphView
|
||||
|
||||
NodeID GetTarget(const EdgeID eid) const;
|
||||
|
||||
const BisectionNode &GetNode(const NodeID nid) const;
|
||||
const BisectionEdge &GetEdge(const EdgeID eid) const;
|
||||
|
||||
private:
|
||||
const BisectionGraph &bisection_graph;
|
||||
const RecursiveBisectionState &bisection_state;
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
#define OSRM_PARTITION_INERTIAL_FLOW_HPP_
|
||||
|
||||
#include "partition/graph_view.hpp"
|
||||
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
namespace osrm
|
||||
@@ -15,7 +17,24 @@ class InertialFlow
|
||||
InertialFlow(const GraphView &view);
|
||||
|
||||
std::vector<bool> ComputePartition(const double balance, const double source_sink_rate);
|
||||
|
||||
private:
|
||||
// Spatially ordered sources and sink ids.
|
||||
// The node ids refer to nodes in the GraphView.
|
||||
struct SpatialOrder
|
||||
{
|
||||
std::unordered_set<NodeID> sources;
|
||||
std::unordered_set<NodeID> sinks;
|
||||
};
|
||||
|
||||
// Creates a spatial order of n * sources "first" and n * sink "last" node ids.
|
||||
// The slope determines the spatial order for sorting node coordinates.
|
||||
SpatialOrder MakeSpatialOrder(double ratio, double slope) const;
|
||||
|
||||
// Makes n cuts with different spatial orders and returns the best.
|
||||
MinCut bestMinCut(std::size_t n, double ratio) const;
|
||||
|
||||
// The subgraph to partition into two parts.
|
||||
const GraphView &view;
|
||||
};
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace partition
|
||||
|
||||
struct PartitionConfig
|
||||
{
|
||||
PartitionConfig() noexcept : requested_num_threads(0) {}
|
||||
PartitionConfig() : requested_num_threads(0) {}
|
||||
|
||||
void UseDefaults()
|
||||
{
|
||||
|
||||
@@ -41,10 +41,26 @@ namespace partition
|
||||
//
|
||||
// bisection-ids: [00,10,01,10,00,11,01,11,00,10]
|
||||
|
||||
/* Written out in a recursive tree form:
|
||||
|
||||
ids: [0,1,2,3,4,5,6,7,8,9]
|
||||
mask: [0,1,0,1,0,1,0,1,0,1]
|
||||
/ \
|
||||
ids: [0,2,4,6,8] [1,3,5,7,9]
|
||||
mask: [0,1,0,1,0] [0,0,1,1,0]
|
||||
/ \ / \
|
||||
ids: [0,4,8] [2,6] [1,3,9] [5,7]
|
||||
|
||||
The bisection ids then trace the path (left: 0, right: 1) through the tree:
|
||||
|
||||
ids: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
|
||||
path: [00, 10, 01, 10, 00, 11, 01, 11, 00, 10]
|
||||
|
||||
*/
|
||||
class RecursiveBisectionState
|
||||
{
|
||||
public:
|
||||
// the ID in the partition arr
|
||||
// The ID in the partition array
|
||||
using BisectionID = std::uint32_t;
|
||||
using IDIterator = std::vector<NodeID>::const_iterator;
|
||||
|
||||
@@ -53,7 +69,8 @@ class RecursiveBisectionState
|
||||
|
||||
BisectionID GetBisectionID(const NodeID nid) const;
|
||||
|
||||
// returns the center of the bisection
|
||||
// Bisects the node id array's sub-range based on the partition mask.
|
||||
// Returns: partition point of the bisection: iterator to the second group's first element.
|
||||
IDIterator ApplyBisection(const IDIterator begin,
|
||||
const IDIterator end,
|
||||
const std::vector<bool> &partition);
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
#ifndef OSRM_REORDER_FIRST_LAST_HPP
|
||||
#define OSRM_REORDER_FIRST_LAST_HPP
|
||||
|
||||
#include <boost/assert.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <iterator>
|
||||
|
||||
namespace osrm
|
||||
{
|
||||
namespace partition
|
||||
{
|
||||
|
||||
// Reorders the first n elements in the range to satisfy the comparator,
|
||||
// and the last n elements to satisfy the comparator with arguments flipped.
|
||||
// Note: no guarantees to the element's ordering inside the reordered ranges.
|
||||
template <typename RandomIt, typename Comparator>
|
||||
void reorderFirstLast(RandomIt first, RandomIt last, std::size_t n, Comparator comp)
|
||||
{
|
||||
BOOST_ASSERT_MSG(n <= (last - first) / std::size_t{2}, "overlapping subranges not allowed");
|
||||
|
||||
if (n == 0 or (last - first < 2))
|
||||
return;
|
||||
|
||||
// Reorder first n: guarantees that the predicate holds for the first elements.
|
||||
std::nth_element(first, first + (n - 1), last, comp);
|
||||
|
||||
// Reorder last n: guarantees that the flipped predicate holds for the last k elements.
|
||||
// We reorder from the end backwards up to the end of the already reordered range.
|
||||
// We can not use std::not2, since then e.g. std::less<> would lose its irreflexive
|
||||
// requirements.
|
||||
std::reverse_iterator<RandomIt> rfirst{last}, rlast{first + n};
|
||||
|
||||
const auto flipped = [](auto fn) {
|
||||
return [fn](auto &&lhs, auto &&rhs) {
|
||||
return fn(std::forward<decltype(lhs)>(rhs), std::forward<decltype(rhs)>(lhs));
|
||||
};
|
||||
};
|
||||
|
||||
std::nth_element(rfirst, rfirst + (n - 1), rlast, flipped(comp));
|
||||
}
|
||||
|
||||
template <typename RandomAccessRange, typename Compare>
|
||||
void reorderFirstLast(RandomAccessRange &rng, std::size_t n, Compare comp)
|
||||
{
|
||||
using std::begin;
|
||||
using std::end;
|
||||
return reorderFirstLast(begin(rng), end(rng), n, comp);
|
||||
}
|
||||
|
||||
} // ns partition
|
||||
} // ns osrm
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user