Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| eb50311d5e | |||
| 203314b1aa |
@@ -731,6 +731,8 @@ jobs:
|
||||
sudo umount ~/benchmarks | true
|
||||
rm -rf ~/benchmarks
|
||||
mkdir -p ~/benchmarks
|
||||
|
||||
for i in {1..15}; do sudo cset shield --reset && break || echo "cset shield reset attempt $i failed"; sleep 1; done
|
||||
# see https://llvm.org/docs/Benchmarking.html
|
||||
- name: Run PR Benchmarks
|
||||
run: |
|
||||
@@ -744,7 +746,9 @@ jobs:
|
||||
|
||||
sudo cset shield --exec -- ./pr/scripts/ci/run_benchmarks.sh -f ~/benchmarks -r $(pwd)/pr_results -s $(pwd)/pr -b ~/benchmarks/build -o ~/data.osm.pbf -g ~/gps_traces.csv
|
||||
sudo umount ~/benchmarks
|
||||
sudo cset shield --reset
|
||||
|
||||
for i in {1..15}; do sudo cset shield --reset && break || echo "cset shield reset attempt $i failed"; sleep 1; done
|
||||
|
||||
- name: Run Base Benchmarks
|
||||
run: |
|
||||
sudo cset shield -c 2-3 -k on
|
||||
@@ -760,9 +764,10 @@ jobs:
|
||||
cp base/src/benchmarks/portugal_to_korea.json ~/benchmarks/test/data/portugal_to_korea.json
|
||||
fi
|
||||
# we intentionally use scripts from PR branch to be able to update them and see results in the same PR
|
||||
sudo cset shield --exec -- cset shield --exec -- ./pr/scripts/ci/run_benchmarks.sh -f ~/benchmarks -r $(pwd)/base_results -s $(pwd)/pr -b ~/benchmarks/build -o ~/data.osm.pbf -g ~/gps_traces.csv
|
||||
sudo cset shield --exec -- ./pr/scripts/ci/run_benchmarks.sh -f ~/benchmarks -r $(pwd)/base_results -s $(pwd)/pr -b ~/benchmarks/build -o ~/data.osm.pbf -g ~/gps_traces.csv
|
||||
sudo umount ~/benchmarks
|
||||
sudo cset shield --reset
|
||||
|
||||
for i in {1..15}; do sudo cset shield --reset && break || echo "cset shield reset attempt $i failed"; sleep 1; done
|
||||
- name: Post Benchmark Results
|
||||
run: |
|
||||
python3 pr/scripts/ci/post_benchmark_results.py base_results pr_results
|
||||
|
||||
@@ -116,8 +116,8 @@ class CellCustomizer
|
||||
const std::vector<bool> &allowed_nodes,
|
||||
CellMetric &metric) const
|
||||
{
|
||||
const auto number_of_nodes = graph.GetNumberOfNodes();
|
||||
HeapPtr heaps([number_of_nodes] { return Heap{number_of_nodes}; });
|
||||
Heap heap_exemplar(graph.GetNumberOfNodes());
|
||||
HeapPtr heaps(heap_exemplar);
|
||||
|
||||
for (std::size_t level = 1; level < partition.GetNumberOfLevels(); ++level)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
#pragma once
|
||||
|
||||
#include <boost/assert.hpp>
|
||||
#include <functional>
|
||||
#include <limits>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace osrm::util
|
||||
{
|
||||
template <typename HeapData, int Arity, typename Comparator = std::less<HeapData>> class DAryHeap
|
||||
{
|
||||
public:
|
||||
using HeapHandle = size_t;
|
||||
|
||||
static constexpr HeapHandle INVALID_HANDLE = std::numeric_limits<size_t>::max();
|
||||
|
||||
public:
|
||||
const HeapData &top() const { return heap[0]; }
|
||||
|
||||
size_t size() const { return heap.size(); }
|
||||
|
||||
bool empty() const { return heap.empty(); }
|
||||
|
||||
const HeapData &operator[](HeapHandle handle) const { return heap[handle]; }
|
||||
|
||||
template <typename ReorderHandler>
|
||||
void emplace(HeapData &&data, ReorderHandler &&reorderHandler)
|
||||
{
|
||||
heap.emplace_back(std::forward<HeapData>(data));
|
||||
heapifyUp(heap.size() - 1, std::forward<ReorderHandler>(reorderHandler));
|
||||
}
|
||||
|
||||
template <typename ReorderHandler>
|
||||
void decrease(HeapHandle handle, HeapData &&data, ReorderHandler &&reorderHandler)
|
||||
{
|
||||
BOOST_ASSERT(handle < heap.size());
|
||||
|
||||
heap[handle] = std::forward<HeapData>(data);
|
||||
heapifyUp(handle, std::forward<ReorderHandler>(reorderHandler));
|
||||
}
|
||||
|
||||
void clear() { heap.clear(); }
|
||||
|
||||
template <typename ReorderHandler> void pop(ReorderHandler &&reorderHandler)
|
||||
{
|
||||
BOOST_ASSERT(!heap.empty());
|
||||
heap[0] = std::move(heap.back());
|
||||
heap.pop_back();
|
||||
if (!heap.empty())
|
||||
{
|
||||
heapifyDown(0, std::forward<ReorderHandler>(reorderHandler));
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
size_t parent(size_t index) { return (index - 1) / Arity; }
|
||||
|
||||
size_t kthChild(size_t index, size_t k) { return Arity * index + k + 1; }
|
||||
|
||||
template <typename ReorderHandler> void heapifyUp(size_t index, ReorderHandler &&reorderHandler)
|
||||
{
|
||||
HeapData temp = std::move(heap[index]);
|
||||
while (index > 0 && comp(temp, heap[parent(index)]))
|
||||
{
|
||||
size_t parentIndex = parent(index);
|
||||
heap[index] = std::move(heap[parentIndex]);
|
||||
reorderHandler(heap[index], index);
|
||||
index = parentIndex;
|
||||
}
|
||||
heap[index] = std::move(temp);
|
||||
reorderHandler(heap[index], index);
|
||||
}
|
||||
|
||||
template <typename ReorderHandler>
|
||||
void heapifyDown(size_t index, ReorderHandler &&reorderHandler)
|
||||
{
|
||||
HeapData temp = std::move(heap[index]);
|
||||
size_t child;
|
||||
while (kthChild(index, 0) < heap.size())
|
||||
{
|
||||
child = minChild(index);
|
||||
if (!comp(heap[child], temp))
|
||||
{
|
||||
break;
|
||||
}
|
||||
heap[index] = std::move(heap[child]);
|
||||
reorderHandler(heap[index], index);
|
||||
index = child;
|
||||
}
|
||||
heap[index] = std::move(temp);
|
||||
reorderHandler(heap[index], index);
|
||||
}
|
||||
|
||||
size_t minChild(size_t index)
|
||||
{
|
||||
size_t bestChild = kthChild(index, 0);
|
||||
for (size_t k = 1; k < Arity; ++k)
|
||||
{
|
||||
size_t pos = kthChild(index, k);
|
||||
if (pos < heap.size() && comp(heap[pos], heap[bestChild]))
|
||||
{
|
||||
bestChild = pos;
|
||||
}
|
||||
}
|
||||
return bestChild;
|
||||
}
|
||||
|
||||
private:
|
||||
Comparator comp;
|
||||
std::vector<HeapData> heap;
|
||||
};
|
||||
} // namespace osrm::util
|
||||
@@ -1,158 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <bit>
|
||||
#include <boost/assert.hpp>
|
||||
#include <cstddef>
|
||||
#include <cstdlib>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <new>
|
||||
#include <vector>
|
||||
|
||||
namespace osrm::util
|
||||
{
|
||||
|
||||
inline size_t align_up(size_t n, size_t alignment)
|
||||
{
|
||||
return (n + alignment - 1) & ~(alignment - 1);
|
||||
}
|
||||
|
||||
inline size_t get_next_power_of_two_exponent(size_t n)
|
||||
{
|
||||
BOOST_ASSERT(n > 0);
|
||||
return (sizeof(size_t) * 8) - std::countl_zero(n - 1);
|
||||
}
|
||||
|
||||
class MemoryPool
|
||||
{
|
||||
private:
|
||||
constexpr static size_t MIN_CHUNK_SIZE_BYTES = 4096;
|
||||
|
||||
public:
|
||||
static std::shared_ptr<MemoryPool> instance()
|
||||
{
|
||||
static thread_local std::shared_ptr<MemoryPool> instance;
|
||||
if (!instance)
|
||||
{
|
||||
instance = std::shared_ptr<MemoryPool>(new MemoryPool());
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
template <typename T> T *allocate(std::size_t items_count)
|
||||
{
|
||||
static_assert(alignof(T) <= alignof(std::max_align_t),
|
||||
"Type is over-aligned for this allocator.");
|
||||
|
||||
size_t free_list_index = get_next_power_of_two_exponent(items_count * sizeof(T));
|
||||
auto &free_list = free_lists_[free_list_index];
|
||||
if (free_list.empty())
|
||||
{
|
||||
size_t block_size_in_bytes = 1u << free_list_index;
|
||||
block_size_in_bytes = align_up(block_size_in_bytes, alignof(std::max_align_t));
|
||||
// check if there is space in current memory chunk
|
||||
if (current_chunk_left_bytes_ < block_size_in_bytes)
|
||||
{
|
||||
allocate_chunk(block_size_in_bytes);
|
||||
}
|
||||
|
||||
free_list.push_back(current_chunk_ptr_);
|
||||
current_chunk_left_bytes_ -= block_size_in_bytes;
|
||||
current_chunk_ptr_ += block_size_in_bytes;
|
||||
}
|
||||
auto ptr = reinterpret_cast<T *>(free_list.back());
|
||||
free_list.pop_back();
|
||||
return ptr;
|
||||
}
|
||||
|
||||
template <typename T> void deallocate(T *p, std::size_t n) noexcept
|
||||
{
|
||||
size_t free_list_index = get_next_power_of_two_exponent(n * sizeof(T));
|
||||
// NOLINTNEXTLINE(bugprone-multi-level-implicit-pointer-conversion)
|
||||
free_lists_[free_list_index].push_back(reinterpret_cast<void *>(p));
|
||||
}
|
||||
|
||||
~MemoryPool()
|
||||
{
|
||||
for (auto chunk : chunks_)
|
||||
{
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
|
||||
std::free(chunk);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
MemoryPool() = default;
|
||||
MemoryPool(const MemoryPool &) = delete;
|
||||
MemoryPool &operator=(const MemoryPool &) = delete;
|
||||
|
||||
void allocate_chunk(size_t bytes)
|
||||
{
|
||||
auto chunk_size = std::max(bytes, MIN_CHUNK_SIZE_BYTES);
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
|
||||
void *chunk = std::malloc(chunk_size);
|
||||
if (!chunk)
|
||||
{
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
chunks_.push_back(chunk);
|
||||
current_chunk_ptr_ = static_cast<uint8_t *>(chunk);
|
||||
current_chunk_left_bytes_ = chunk_size;
|
||||
}
|
||||
|
||||
// we have 64 free lists, one for each possible power of two
|
||||
std::array<std::vector<void *>, sizeof(std::size_t) * 8> free_lists_;
|
||||
|
||||
// list of allocated memory chunks, we don't free them until the pool is destroyed
|
||||
std::vector<void *> chunks_;
|
||||
|
||||
uint8_t *current_chunk_ptr_ = nullptr;
|
||||
size_t current_chunk_left_bytes_ = 0;
|
||||
};
|
||||
|
||||
template <typename T> class PoolAllocator
|
||||
{
|
||||
public:
|
||||
using value_type = T;
|
||||
|
||||
PoolAllocator() noexcept : pool(MemoryPool::instance()){};
|
||||
|
||||
template <typename U>
|
||||
PoolAllocator(const PoolAllocator<U> &) noexcept : pool(MemoryPool::instance())
|
||||
{
|
||||
}
|
||||
|
||||
template <typename U> struct rebind
|
||||
{
|
||||
using other = PoolAllocator<U>;
|
||||
};
|
||||
|
||||
T *allocate(std::size_t n) { return pool->allocate<T>(n); }
|
||||
|
||||
void deallocate(T *p, std::size_t n) noexcept { pool->deallocate<T>(p, n); }
|
||||
|
||||
PoolAllocator(const PoolAllocator &) = default;
|
||||
PoolAllocator &operator=(const PoolAllocator &) = default;
|
||||
PoolAllocator(PoolAllocator &&) noexcept = default;
|
||||
PoolAllocator &operator=(PoolAllocator &&) noexcept = default;
|
||||
|
||||
private:
|
||||
// using shared_ptr guarantees that memory pool won't be destroyed before all allocators using
|
||||
// it (important if there are static instances of PoolAllocator)
|
||||
std::shared_ptr<MemoryPool> pool;
|
||||
};
|
||||
template <typename T, typename U>
|
||||
bool operator==(const PoolAllocator<T> &, const PoolAllocator<U> &)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T, typename U>
|
||||
bool operator!=(const PoolAllocator<T> &, const PoolAllocator<U> &)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace osrm::util
|
||||
+53
-40
@@ -1,16 +1,19 @@
|
||||
#ifndef OSRM_UTIL_QUERY_HEAP_HPP
|
||||
#define OSRM_UTIL_QUERY_HEAP_HPP
|
||||
|
||||
#include "util/pool_allocator.hpp"
|
||||
#include <algorithm>
|
||||
#include <boost/assert.hpp>
|
||||
#include <boost/heap/d_ary_heap.hpp>
|
||||
|
||||
#include "d_ary_heap.hpp"
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <limits>
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace osrm::util
|
||||
{
|
||||
|
||||
@@ -55,11 +58,7 @@ template <typename NodeID, typename Key> class UnorderedMapStorage
|
||||
void Clear() { nodes.clear(); }
|
||||
|
||||
private:
|
||||
template <typename K, typename V>
|
||||
using UnorderedMap = std::
|
||||
unordered_map<K, V, std::hash<K>, std::equal_to<K>, PoolAllocator<std::pair<const K, V>>>;
|
||||
|
||||
UnorderedMap<NodeID, Key> nodes;
|
||||
std::unordered_map<NodeID, Key> nodes;
|
||||
};
|
||||
|
||||
template <typename NodeID,
|
||||
@@ -136,22 +135,17 @@ class QueryHeap
|
||||
Weight weight;
|
||||
Key index;
|
||||
|
||||
bool operator>(const HeapData &other) const
|
||||
bool operator<(const HeapData &other) const
|
||||
{
|
||||
if (weight == other.weight)
|
||||
{
|
||||
return index > other.index;
|
||||
return index < other.index;
|
||||
}
|
||||
return weight > other.weight;
|
||||
return weight < other.weight;
|
||||
}
|
||||
};
|
||||
|
||||
using HeapContainer = boost::heap::d_ary_heap<HeapData,
|
||||
boost::heap::arity<4>,
|
||||
boost::heap::mutable_<true>,
|
||||
boost::heap::compare<std::greater<HeapData>>,
|
||||
boost::heap::allocator<PoolAllocator<HeapData>>>;
|
||||
using HeapHandle = typename HeapContainer::handle_type;
|
||||
using HeapContainer = DAryHeap<HeapData, 4>;
|
||||
using HeapHandle = typename HeapContainer::HeapHandle;
|
||||
|
||||
public:
|
||||
using WeightType = Weight;
|
||||
@@ -165,9 +159,6 @@ class QueryHeap
|
||||
Data data;
|
||||
};
|
||||
|
||||
QueryHeap(const QueryHeap &other) = delete;
|
||||
QueryHeap(QueryHeap &&other) = delete;
|
||||
|
||||
template <typename... StorageArgs> explicit QueryHeap(StorageArgs... args) : node_index(args...)
|
||||
{
|
||||
Clear();
|
||||
@@ -186,11 +177,31 @@ class QueryHeap
|
||||
|
||||
void Insert(NodeID node, Weight weight, const Data &data)
|
||||
{
|
||||
checkInvariants();
|
||||
|
||||
BOOST_ASSERT(node < std::numeric_limits<NodeID>::max());
|
||||
const auto index = static_cast<Key>(inserted_nodes.size());
|
||||
const auto handle = heap.emplace(HeapData{weight, index});
|
||||
inserted_nodes.emplace_back(HeapNode{handle, node, weight, data});
|
||||
inserted_nodes.emplace_back(HeapNode{heap.size(), node, weight, data});
|
||||
|
||||
heap.emplace(HeapData{weight, index},
|
||||
[this](const auto &heapData, auto new_handle)
|
||||
{ inserted_nodes[heapData.index].handle = new_handle; });
|
||||
node_index[node] = index;
|
||||
|
||||
checkInvariants();
|
||||
}
|
||||
|
||||
void checkInvariants()
|
||||
{
|
||||
#ifndef NDEBUG
|
||||
for (size_t handle = 0; handle < heap.size(); ++handle)
|
||||
{
|
||||
auto &in_heap = heap[handle];
|
||||
auto &inserted = inserted_nodes[in_heap.index];
|
||||
BOOST_ASSERT(in_heap.weight == inserted.weight);
|
||||
BOOST_ASSERT(inserted.handle == handle);
|
||||
}
|
||||
#endif // !NDEBUG
|
||||
}
|
||||
|
||||
Data &GetData(NodeID node)
|
||||
@@ -224,16 +235,7 @@ class QueryHeap
|
||||
{
|
||||
BOOST_ASSERT(WasInserted(node));
|
||||
const Key index = node_index.peek_index(node);
|
||||
|
||||
// Use end iterator as a reliable "non-existent" handle.
|
||||
// Default-constructed handles are singular and
|
||||
// can only be checked-compared to another singular instance.
|
||||
// Behaviour investigated at https://lists.boost.org/boost-users/2017/08/87787.php,
|
||||
// eventually confirmation at https://stackoverflow.com/a/45622940/151641.
|
||||
// Corrected in https://github.com/Project-OSRM/osrm-backend/pull/4396
|
||||
auto const end_it = const_cast<HeapContainer &>(heap).end(); // non-const iterator
|
||||
auto const none_handle = heap.s_handle_from_iterator(end_it); // from non-const iterator
|
||||
return inserted_nodes[index].handle == none_handle;
|
||||
return inserted_nodes[index].handle == HeapContainer::INVALID_HANDLE;
|
||||
}
|
||||
|
||||
bool WasInserted(const NodeID node) const
|
||||
@@ -284,26 +286,30 @@ class QueryHeap
|
||||
{
|
||||
BOOST_ASSERT(!heap.empty());
|
||||
const Key removedIndex = heap.top().index;
|
||||
heap.pop();
|
||||
inserted_nodes[removedIndex].handle = heap.s_handle_from_iterator(heap.end());
|
||||
inserted_nodes[removedIndex].handle = HeapContainer::INVALID_HANDLE;
|
||||
|
||||
heap.pop([this](const auto &heapData, auto new_handle)
|
||||
{ inserted_nodes[heapData.index].handle = new_handle; });
|
||||
return inserted_nodes[removedIndex].node;
|
||||
}
|
||||
|
||||
HeapNode &DeleteMinGetHeapNode()
|
||||
{
|
||||
BOOST_ASSERT(!heap.empty());
|
||||
checkInvariants();
|
||||
const Key removedIndex = heap.top().index;
|
||||
heap.pop();
|
||||
inserted_nodes[removedIndex].handle = heap.s_handle_from_iterator(heap.end());
|
||||
inserted_nodes[removedIndex].handle = HeapContainer::INVALID_HANDLE;
|
||||
heap.pop([this](const auto &heapData, auto new_handle)
|
||||
{ inserted_nodes[heapData.index].handle = new_handle; });
|
||||
checkInvariants();
|
||||
return inserted_nodes[removedIndex];
|
||||
}
|
||||
|
||||
void DeleteAll()
|
||||
{
|
||||
auto const none_handle = heap.s_handle_from_iterator(heap.end());
|
||||
std::for_each(inserted_nodes.begin(),
|
||||
inserted_nodes.end(),
|
||||
[&none_handle](auto &node) { node.handle = none_handle; });
|
||||
[&](auto &node) { node.handle = HeapContainer::INVALID_HANDLE; });
|
||||
heap.clear();
|
||||
}
|
||||
|
||||
@@ -313,13 +319,19 @@ class QueryHeap
|
||||
const auto index = node_index.peek_index(node);
|
||||
auto &reference = inserted_nodes[index];
|
||||
reference.weight = weight;
|
||||
heap.increase(reference.handle, HeapData{weight, static_cast<Key>(index)});
|
||||
heap.decrease(reference.handle,
|
||||
HeapData{weight, static_cast<Key>(index)},
|
||||
[this](const auto &heapData, auto new_handle)
|
||||
{ inserted_nodes[heapData.index].handle = new_handle; });
|
||||
}
|
||||
|
||||
void DecreaseKey(const HeapNode &heapNode)
|
||||
{
|
||||
BOOST_ASSERT(!WasRemoved(heapNode.node));
|
||||
heap.increase(heapNode.handle, HeapData{heapNode.weight, (*heapNode.handle).index});
|
||||
heap.decrease(heapNode.handle,
|
||||
HeapData{heapNode.weight, heap[heapNode.handle].index},
|
||||
[this](const auto &heapData, auto new_handle)
|
||||
{ inserted_nodes[heapData.index].handle = new_handle; });
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -327,6 +339,7 @@ class QueryHeap
|
||||
HeapContainer heap;
|
||||
IndexStorage node_index;
|
||||
};
|
||||
|
||||
} // namespace osrm::util
|
||||
|
||||
#endif // OSRM_UTIL_QUERY_HEAP_HPP
|
||||
|
||||
@@ -222,7 +222,6 @@ oneToManySearch(SearchEngineData<Algorithm> &engine_working_data,
|
||||
std::vector<EdgeDuration> durations_table(target_indices.size(), MAXIMAL_EDGE_DURATION);
|
||||
std::vector<EdgeDistance> distances_table(calculate_distance ? target_indices.size() : 0,
|
||||
MAXIMAL_EDGE_DISTANCE);
|
||||
std::vector<NodeID> middle_nodes_table(target_indices.size(), SPECIAL_NODEID);
|
||||
|
||||
// Collect destination (source) nodes into a map
|
||||
std::unordered_multimap<NodeID, std::tuple<std::size_t, EdgeWeight, EdgeDuration, EdgeDistance>>
|
||||
@@ -307,7 +306,6 @@ oneToManySearch(SearchEngineData<Algorithm> &engine_working_data,
|
||||
weights_table[index] = path_weight;
|
||||
durations_table[index] = path_duration;
|
||||
current_distance = path_distance;
|
||||
middle_nodes_table[index] = node;
|
||||
}
|
||||
|
||||
// Remove node from destinations list
|
||||
|
||||
@@ -0,0 +1,175 @@
|
||||
|
||||
#include "util/d_ary_heap.hpp"
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
using namespace osrm::util;
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(d_ary_heap_test)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_empty_heap)
|
||||
{
|
||||
DAryHeap<int, 2> heap;
|
||||
BOOST_CHECK(heap.empty());
|
||||
BOOST_CHECK_EQUAL(heap.size(), 0);
|
||||
heap.emplace(10, [](int &, size_t) {});
|
||||
BOOST_CHECK(!heap.empty());
|
||||
BOOST_CHECK_EQUAL(heap.size(), 1);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_emplace_and_top)
|
||||
{
|
||||
DAryHeap<int, 2> heap;
|
||||
heap.emplace(10, [](int &, size_t) {});
|
||||
heap.emplace(5, [](int &, size_t) {});
|
||||
heap.emplace(8, [](int &, size_t) {});
|
||||
|
||||
BOOST_CHECK_EQUAL(heap.top(), 5);
|
||||
BOOST_CHECK_EQUAL(heap.size(), 3);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_pop)
|
||||
{
|
||||
DAryHeap<int, 2> heap;
|
||||
heap.emplace(10, [](int &, size_t) {});
|
||||
heap.emplace(5, [](int &, size_t) {});
|
||||
heap.emplace(8, [](int &, size_t) {});
|
||||
|
||||
heap.pop([](int &, size_t) {});
|
||||
BOOST_CHECK_EQUAL(heap.top(), 8);
|
||||
BOOST_CHECK_EQUAL(heap.size(), 2);
|
||||
|
||||
heap.pop([](int &, size_t) {});
|
||||
BOOST_CHECK_EQUAL(heap.top(), 10);
|
||||
BOOST_CHECK_EQUAL(heap.size(), 1);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_decrease)
|
||||
{
|
||||
struct HeapData
|
||||
{
|
||||
int key;
|
||||
int data;
|
||||
|
||||
bool operator<(const HeapData &other) const { return key < other.key; }
|
||||
};
|
||||
DAryHeap<HeapData, 2> heap;
|
||||
size_t handle = DAryHeap<HeapData, 2>::INVALID_HANDLE;
|
||||
|
||||
auto reorder_handler = [&](const HeapData &value, size_t new_handle)
|
||||
{
|
||||
if (value.data == 42)
|
||||
{
|
||||
handle = new_handle;
|
||||
}
|
||||
};
|
||||
|
||||
heap.emplace({10, 42}, reorder_handler);
|
||||
heap.emplace({5, 73}, reorder_handler);
|
||||
heap.emplace({8, 37}, reorder_handler);
|
||||
|
||||
heap.decrease(handle, {3, 42}, reorder_handler);
|
||||
BOOST_CHECK_EQUAL(heap.size(), 3);
|
||||
BOOST_CHECK_EQUAL(heap.top().key, 3);
|
||||
BOOST_CHECK_EQUAL(heap.top().data, 42);
|
||||
heap.pop(reorder_handler);
|
||||
BOOST_CHECK_EQUAL(heap.size(), 2);
|
||||
BOOST_CHECK_EQUAL(heap.top().key, 5);
|
||||
BOOST_CHECK_EQUAL(heap.top().data, 73);
|
||||
heap.pop(reorder_handler);
|
||||
BOOST_CHECK_EQUAL(heap.size(), 1);
|
||||
BOOST_CHECK_EQUAL(heap.top().key, 8);
|
||||
BOOST_CHECK_EQUAL(heap.top().data, 37);
|
||||
heap.pop(reorder_handler);
|
||||
BOOST_CHECK_EQUAL(heap.size(), 0);
|
||||
BOOST_CHECK(heap.empty());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_reorder_handler)
|
||||
{
|
||||
std::vector<int> reordered_values;
|
||||
std::vector<size_t> reordered_indices;
|
||||
auto reorder_handler = [&](int value, size_t index)
|
||||
{
|
||||
reordered_values.push_back(value);
|
||||
reordered_indices.push_back(index);
|
||||
};
|
||||
DAryHeap<int, 2> heap;
|
||||
std::vector<int> expected_reordered_values;
|
||||
std::vector<int> expected_reordered_indices;
|
||||
|
||||
heap.emplace(10, reorder_handler);
|
||||
|
||||
expected_reordered_values = {10};
|
||||
expected_reordered_indices = {0};
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(reordered_values.begin(),
|
||||
reordered_values.end(),
|
||||
expected_reordered_values.begin(),
|
||||
expected_reordered_values.end());
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(reordered_indices.begin(),
|
||||
reordered_indices.end(),
|
||||
expected_reordered_indices.begin(),
|
||||
expected_reordered_indices.end());
|
||||
|
||||
heap.emplace(5, reorder_handler);
|
||||
expected_reordered_values = {10, 10, 5};
|
||||
expected_reordered_indices = {0, 1, 0};
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(reordered_values.begin(),
|
||||
reordered_values.end(),
|
||||
expected_reordered_values.begin(),
|
||||
expected_reordered_values.end());
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(reordered_indices.begin(),
|
||||
reordered_indices.end(),
|
||||
expected_reordered_indices.begin(),
|
||||
expected_reordered_indices.end());
|
||||
|
||||
heap.emplace(8, reorder_handler);
|
||||
expected_reordered_values = {10, 10, 5, 8};
|
||||
expected_reordered_indices = {0, 1, 0, 2};
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(reordered_values.begin(),
|
||||
reordered_values.end(),
|
||||
expected_reordered_values.begin(),
|
||||
expected_reordered_values.end());
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(reordered_indices.begin(),
|
||||
reordered_indices.end(),
|
||||
expected_reordered_indices.begin(),
|
||||
expected_reordered_indices.end());
|
||||
|
||||
heap.pop(reorder_handler);
|
||||
expected_reordered_values = {10, 10, 5, 8, 8};
|
||||
expected_reordered_indices = {0, 1, 0, 2, 0};
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(reordered_values.begin(),
|
||||
reordered_values.end(),
|
||||
expected_reordered_values.begin(),
|
||||
expected_reordered_values.end());
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(reordered_indices.begin(),
|
||||
reordered_indices.end(),
|
||||
expected_reordered_indices.begin(),
|
||||
expected_reordered_indices.end());
|
||||
|
||||
heap.pop(reorder_handler);
|
||||
|
||||
expected_reordered_values = {10, 10, 5, 8, 8, 10};
|
||||
expected_reordered_indices = {0, 1, 0, 2, 0, 0};
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(reordered_values.begin(),
|
||||
reordered_values.end(),
|
||||
expected_reordered_values.begin(),
|
||||
expected_reordered_values.end());
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(reordered_indices.begin(),
|
||||
reordered_indices.end(),
|
||||
expected_reordered_indices.begin(),
|
||||
expected_reordered_indices.end());
|
||||
|
||||
heap.pop(reorder_handler);
|
||||
expected_reordered_values = {10, 10, 5, 8, 8, 10};
|
||||
expected_reordered_indices = {0, 1, 0, 2, 0, 0};
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(reordered_values.begin(),
|
||||
reordered_values.end(),
|
||||
expected_reordered_values.begin(),
|
||||
expected_reordered_values.end());
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(reordered_indices.begin(),
|
||||
reordered_indices.end(),
|
||||
expected_reordered_indices.begin(),
|
||||
expected_reordered_indices.end());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
@@ -1,158 +0,0 @@
|
||||
#include "util/pool_allocator.hpp"
|
||||
#include "util/typedefs.hpp"
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(pool_allocator)
|
||||
|
||||
using namespace osrm;
|
||||
using namespace osrm::util;
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_align_up)
|
||||
{
|
||||
BOOST_CHECK_EQUAL(align_up(5, 4), 8);
|
||||
BOOST_CHECK_EQUAL(align_up(9, 8), 16);
|
||||
BOOST_CHECK_EQUAL(align_up(17, 16), 32);
|
||||
BOOST_CHECK_EQUAL(align_up(4, 4), 4);
|
||||
BOOST_CHECK_EQUAL(align_up(8, 8), 8);
|
||||
BOOST_CHECK_EQUAL(align_up(16, 16), 16);
|
||||
BOOST_CHECK_EQUAL(align_up(32, 16), 32);
|
||||
BOOST_CHECK_EQUAL(align_up(0, 4), 0);
|
||||
BOOST_CHECK_EQUAL(align_up(0, 8), 0);
|
||||
BOOST_CHECK_EQUAL(align_up(0, 16), 0);
|
||||
BOOST_CHECK_EQUAL(align_up(1000000, 256), 1000192);
|
||||
BOOST_CHECK_EQUAL(align_up(999999, 512), 1000448);
|
||||
BOOST_CHECK_EQUAL(align_up(123456789, 1024), 123457536);
|
||||
BOOST_CHECK_EQUAL(align_up(0, 1), 0);
|
||||
BOOST_CHECK_EQUAL(align_up(5, 1), 5);
|
||||
BOOST_CHECK_EQUAL(align_up(123456, 1), 123456);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_get_next_power_of_two_exponent)
|
||||
{
|
||||
BOOST_CHECK_EQUAL(get_next_power_of_two_exponent(1), 0);
|
||||
BOOST_CHECK_EQUAL(get_next_power_of_two_exponent(2), 1);
|
||||
BOOST_CHECK_EQUAL(get_next_power_of_two_exponent(4), 2);
|
||||
BOOST_CHECK_EQUAL(get_next_power_of_two_exponent(8), 3);
|
||||
BOOST_CHECK_EQUAL(get_next_power_of_two_exponent(16), 4);
|
||||
BOOST_CHECK_EQUAL(get_next_power_of_two_exponent(3), 2);
|
||||
BOOST_CHECK_EQUAL(get_next_power_of_two_exponent(5), 3);
|
||||
BOOST_CHECK_EQUAL(get_next_power_of_two_exponent(9), 4);
|
||||
BOOST_CHECK_EQUAL(get_next_power_of_two_exponent(15), 4);
|
||||
BOOST_CHECK_EQUAL(get_next_power_of_two_exponent(17), 5);
|
||||
BOOST_CHECK_EQUAL(get_next_power_of_two_exponent(1), 0);
|
||||
BOOST_CHECK_EQUAL(get_next_power_of_two_exponent(SIZE_MAX), sizeof(size_t) * 8);
|
||||
}
|
||||
|
||||
// in many of these tests we hope on address sanitizer to alert in the case if we are doing
|
||||
// something wrong
|
||||
BOOST_AUTO_TEST_CASE(smoke)
|
||||
{
|
||||
PoolAllocator<int> pool;
|
||||
auto ptr = pool.allocate(1);
|
||||
*ptr = 42;
|
||||
BOOST_CHECK_NE(ptr, nullptr);
|
||||
pool.deallocate(ptr, 1);
|
||||
|
||||
ptr = pool.allocate(2);
|
||||
*ptr = 42;
|
||||
*(ptr + 1) = 43;
|
||||
BOOST_CHECK_NE(ptr, nullptr);
|
||||
pool.deallocate(ptr, 2);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(a_lot_of_items)
|
||||
{
|
||||
PoolAllocator<int> pool;
|
||||
auto ptr = pool.allocate(2048);
|
||||
for (int i = 0; i < 2048; ++i)
|
||||
{
|
||||
ptr[i] = i;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 2048; ++i)
|
||||
{
|
||||
BOOST_CHECK_EQUAL(ptr[i], i);
|
||||
}
|
||||
|
||||
pool.deallocate(ptr, 2048);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(copy)
|
||||
{
|
||||
PoolAllocator<int> pool;
|
||||
auto ptr = pool.allocate(1);
|
||||
*ptr = 42;
|
||||
BOOST_CHECK_NE(ptr, nullptr);
|
||||
pool.deallocate(ptr, 1);
|
||||
|
||||
PoolAllocator<int> pool2(pool);
|
||||
ptr = pool2.allocate(1);
|
||||
*ptr = 42;
|
||||
BOOST_CHECK_NE(ptr, nullptr);
|
||||
pool2.deallocate(ptr, 1);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(move)
|
||||
{
|
||||
PoolAllocator<int> pool;
|
||||
auto ptr = pool.allocate(1);
|
||||
*ptr = 42;
|
||||
BOOST_CHECK_NE(ptr, nullptr);
|
||||
pool.deallocate(ptr, 1);
|
||||
|
||||
PoolAllocator<int> pool2(std::move(pool));
|
||||
ptr = pool2.allocate(1);
|
||||
*ptr = 42;
|
||||
BOOST_CHECK_NE(ptr, nullptr);
|
||||
pool2.deallocate(ptr, 1);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(unordered_map)
|
||||
{
|
||||
std::unordered_map<int,
|
||||
int,
|
||||
std::hash<int>,
|
||||
std::equal_to<int>,
|
||||
PoolAllocator<std::pair<const int, int>>>
|
||||
map;
|
||||
map[1] = 42;
|
||||
BOOST_CHECK_EQUAL(map[1], 42);
|
||||
|
||||
map.clear();
|
||||
|
||||
map[2] = 43;
|
||||
|
||||
BOOST_CHECK_EQUAL(map[2], 43);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(alignment)
|
||||
{
|
||||
PoolAllocator<char> pool_char;
|
||||
PoolAllocator<double> pool_double;
|
||||
|
||||
auto ptr_char = pool_char.allocate(1);
|
||||
auto ptr_double = pool_double.allocate(1);
|
||||
|
||||
BOOST_CHECK_NE(ptr_double, nullptr);
|
||||
BOOST_CHECK_EQUAL(reinterpret_cast<uintptr_t>(ptr_double) % alignof(double), 0);
|
||||
BOOST_CHECK_NE(ptr_char, nullptr);
|
||||
BOOST_CHECK_EQUAL(reinterpret_cast<uintptr_t>(ptr_char) % alignof(char), 0);
|
||||
|
||||
pool_char.deallocate(ptr_char, 1);
|
||||
pool_double.deallocate(ptr_double, 1);
|
||||
|
||||
ptr_char = pool_char.allocate(2);
|
||||
ptr_double = pool_double.allocate(1);
|
||||
|
||||
BOOST_CHECK_NE(ptr_double, nullptr);
|
||||
BOOST_CHECK_EQUAL(reinterpret_cast<uintptr_t>(ptr_double) % alignof(double), 0);
|
||||
BOOST_CHECK_NE(ptr_char, nullptr);
|
||||
BOOST_CHECK_EQUAL(reinterpret_cast<uintptr_t>(ptr_char) % alignof(char), 0);
|
||||
|
||||
pool_char.deallocate(ptr_char, 2);
|
||||
pool_double.deallocate(ptr_double, 1);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
@@ -120,6 +120,28 @@ BOOST_FIXTURE_TEST_CASE_TEMPLATE(delete_all_test, T, storage_types, RandomDataFi
|
||||
BOOST_CHECK(heap.Empty());
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE_TEMPLATE(smoke_test, T, storage_types, RandomDataFixture<NUM_NODES>)
|
||||
{
|
||||
QueryHeap<TestNodeID, TestKey, TestWeight, TestData, T> heap(NUM_NODES);
|
||||
|
||||
for (unsigned idx : order)
|
||||
{
|
||||
heap.Insert(ids[idx], weights[idx], data[idx]);
|
||||
}
|
||||
|
||||
while (!heap.Empty())
|
||||
{
|
||||
auto old_weight = heap.MinKey();
|
||||
auto node = heap.GetHeapNodeIfWasInserted(heap.Min());
|
||||
BOOST_CHECK(old_weight == node->weight);
|
||||
BOOST_CHECK(node);
|
||||
node->weight = node->weight - 1;
|
||||
heap.DecreaseKey(*node);
|
||||
BOOST_CHECK(heap.MinKey() == node->weight);
|
||||
heap.DeleteMin();
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE_TEMPLATE(decrease_key_test, T, storage_types, RandomDataFixture<10>)
|
||||
{
|
||||
QueryHeap<TestNodeID, TestKey, TestWeight, TestData, T> heap(10);
|
||||
|
||||
Reference in New Issue
Block a user