wip
This commit is contained in:
parent
f18791a71b
commit
81d128b100
@ -116,7 +116,6 @@ class CellCustomizer
|
|||||||
const std::vector<bool> &allowed_nodes,
|
const std::vector<bool> &allowed_nodes,
|
||||||
CellMetric &metric) const
|
CellMetric &metric) const
|
||||||
{
|
{
|
||||||
// std::cerr << "Customizing cells\n";
|
|
||||||
const auto number_of_nodes = graph.GetNumberOfNodes();
|
const auto number_of_nodes = graph.GetNumberOfNodes();
|
||||||
HeapPtr heaps([number_of_nodes]{
|
HeapPtr heaps([number_of_nodes]{
|
||||||
return Heap{number_of_nodes};
|
return Heap{number_of_nodes};
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#include <array>
|
#include <array>
|
||||||
#include <bit>
|
#include <bit>
|
||||||
#include <boost/assert.hpp>
|
#include <boost/assert.hpp>
|
||||||
|
#include <cstddef>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
@ -14,40 +15,42 @@
|
|||||||
namespace osrm::util
|
namespace osrm::util
|
||||||
{
|
{
|
||||||
|
|
||||||
class MemoryManager
|
class MemoryPool
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
constexpr static size_t MIN_ITEMS_IN_BLOCK = 1024;
|
constexpr static size_t MIN_CHUNK_SIZE_BYTES = 4096;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static std::shared_ptr<MemoryManager> instance()
|
static std::shared_ptr<MemoryPool> instance()
|
||||||
{
|
{
|
||||||
static thread_local std::shared_ptr<MemoryManager> instance;
|
static thread_local std::shared_ptr<MemoryPool> instance;
|
||||||
if (!instance)
|
if (!instance)
|
||||||
{
|
{
|
||||||
instance = std::shared_ptr<MemoryManager>(new MemoryManager());
|
instance = std::shared_ptr<MemoryPool>(new MemoryPool());
|
||||||
}
|
}
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: alignment!!!
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T *allocate(std::size_t n)
|
T *allocate(std::size_t items_count)
|
||||||
{
|
{
|
||||||
size_t free_list_index = get_next_power_of_two_exponent(n * sizeof(T));
|
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];
|
auto &free_list = free_lists_[free_list_index];
|
||||||
const auto items_in_block = 1u << free_list_index;
|
|
||||||
if (free_list.empty())
|
if (free_list.empty())
|
||||||
{
|
{
|
||||||
// Check if there is space in current block
|
size_t block_size_in_bytes = 1u << free_list_index;
|
||||||
if (current_block_left_items_ < items_in_block)
|
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_block<T>(items_in_block);
|
allocate_chunk(block_size_in_bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
free_list.push_back(current_block_ptr_);
|
free_list.push_back(current_chunk_ptr_);
|
||||||
current_block_left_items_ -= items_in_block;
|
current_chunk_left_bytes_ -= block_size_in_bytes;
|
||||||
current_block_ptr_ += items_in_block * sizeof(T);
|
current_chunk_ptr_ += block_size_in_bytes;
|
||||||
}
|
}
|
||||||
auto ptr = static_cast<T*>(free_list.back());
|
auto ptr = static_cast<T*>(free_list.back());
|
||||||
free_list.pop_back();
|
free_list.pop_back();
|
||||||
@ -61,49 +64,54 @@ public:
|
|||||||
free_lists_[free_list_index].push_back(p);
|
free_lists_[free_list_index].push_back(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
~MemoryManager()
|
~MemoryPool()
|
||||||
{
|
{
|
||||||
std::cerr << "~MemoryManager()" << std::endl;
|
for (auto chunk : chunks_)
|
||||||
for (auto block : blocks_)
|
|
||||||
{
|
{
|
||||||
std::free(block);
|
// NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
|
||||||
|
std::free(chunk);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
MemoryManager() = default;
|
MemoryPool() = default;
|
||||||
MemoryManager(const MemoryManager &) = delete;
|
MemoryPool(const MemoryPool &) = delete;
|
||||||
MemoryManager &operator=(const MemoryManager &) = delete;
|
MemoryPool &operator=(const MemoryPool &) = delete;
|
||||||
|
|
||||||
size_t get_next_power_of_two_exponent(size_t n) const
|
inline size_t get_next_power_of_two_exponent(size_t n) const
|
||||||
{
|
{
|
||||||
BOOST_ASSERT(n > 0);
|
BOOST_ASSERT(n > 0);
|
||||||
return (sizeof(size_t) * 8) - std::countl_zero(n - 1);
|
return (sizeof(size_t) * 8) - std::countl_zero(n - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
inline size_t align_up(size_t n, size_t alignment)
|
||||||
void allocate_block(size_t items_in_block)
|
|
||||||
{
|
{
|
||||||
items_in_block = std::max(items_in_block, MIN_ITEMS_IN_BLOCK);
|
return (n + alignment - 1) & ~(alignment - 1);
|
||||||
|
}
|
||||||
|
|
||||||
size_t block_size = items_in_block * sizeof(T);
|
inline void* align_pointer(void* ptr, size_t alignment)
|
||||||
void *block = std::malloc(block_size);
|
{
|
||||||
if (!block)
|
return reinterpret_cast<void*>(align_up(reinterpret_cast<uintptr_t>(ptr), alignment));
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
throw std::bad_alloc();
|
||||||
}
|
}
|
||||||
total_allocated_ += block_size;
|
chunks_.push_back(chunk);
|
||||||
blocks_.push_back(block);
|
current_chunk_ptr_ = static_cast<uint8_t*>(chunk);
|
||||||
current_block_ptr_ = static_cast<uint8_t*>(block);
|
current_chunk_left_bytes_ = chunk_size;
|
||||||
current_block_left_items_ = items_in_block;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::array<std::vector<void *>, 32> free_lists_;
|
std::array<std::vector<void *>, 32> free_lists_;
|
||||||
std::vector<void *> blocks_;
|
std::vector<void *> chunks_;
|
||||||
uint8_t *current_block_ptr_ = nullptr;
|
uint8_t *current_chunk_ptr_ = nullptr;
|
||||||
size_t current_block_left_items_ = 0;
|
size_t current_chunk_left_bytes_ = 0;
|
||||||
|
|
||||||
size_t total_allocated_ = 0;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@ -112,10 +120,10 @@ class PoolAllocator
|
|||||||
public:
|
public:
|
||||||
using value_type = T;
|
using value_type = T;
|
||||||
|
|
||||||
PoolAllocator() noexcept : pool(MemoryManager::instance()) {};
|
PoolAllocator() noexcept : pool(MemoryPool::instance()) {};
|
||||||
|
|
||||||
template <typename U>
|
template <typename U>
|
||||||
PoolAllocator(const PoolAllocator<U> &) noexcept : pool(MemoryManager::instance()) {}
|
PoolAllocator(const PoolAllocator<U> &) noexcept : pool(MemoryPool::instance()) {}
|
||||||
|
|
||||||
template <typename U>
|
template <typename U>
|
||||||
struct rebind
|
struct rebind
|
||||||
@ -133,17 +141,13 @@ public:
|
|||||||
pool->deallocate<T>(p, n);
|
pool->deallocate<T>(p, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
~PoolAllocator() {
|
|
||||||
std::cerr << "~PoolAllocator()" << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
PoolAllocator(const PoolAllocator &) = default;
|
PoolAllocator(const PoolAllocator &) = default;
|
||||||
PoolAllocator &operator=(const PoolAllocator &) = default;
|
PoolAllocator &operator=(const PoolAllocator &) = default;
|
||||||
PoolAllocator(PoolAllocator &&) noexcept = default;
|
PoolAllocator(PoolAllocator &&) noexcept = default;
|
||||||
PoolAllocator &operator=(PoolAllocator &&) noexcept = default;
|
PoolAllocator &operator=(PoolAllocator &&) noexcept = default;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<MemoryManager> pool;
|
std::shared_ptr<MemoryPool> pool;
|
||||||
};
|
};
|
||||||
template <typename T, typename U>
|
template <typename T, typename U>
|
||||||
bool operator==(const PoolAllocator<T> &, const PoolAllocator<U> &)
|
bool operator==(const PoolAllocator<T> &, const PoolAllocator<U> &)
|
||||||
|
@ -36,10 +36,6 @@ template <typename NodeID, typename Key> class UnorderedMapStorage
|
|||||||
public:
|
public:
|
||||||
explicit UnorderedMapStorage(std::size_t) { nodes.rehash(1000); }
|
explicit UnorderedMapStorage(std::size_t) { nodes.rehash(1000); }
|
||||||
|
|
||||||
~UnorderedMapStorage() {
|
|
||||||
std::cerr << "~UnorderedMapStorage()" << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
Key &operator[](const NodeID node) { return nodes[node]; }
|
Key &operator[](const NodeID node) { return nodes[node]; }
|
||||||
|
|
||||||
Key peek_index(const NodeID node) const
|
Key peek_index(const NodeID node) const
|
||||||
|
@ -1,5 +0,0 @@
|
|||||||
#include "util/pool_allocator.hpp"
|
|
||||||
|
|
||||||
namespace osrm::util
|
|
||||||
{
|
|
||||||
} // namespace osrm::util
|
|
@ -65,4 +65,4 @@ benchmark: data $(DATA_NAME).requests
|
|||||||
checksum:
|
checksum:
|
||||||
$(MD5SUM) $(DATA_NAME).osm.pbf $(DATA_NAME).poly > data.md5sum
|
$(MD5SUM) $(DATA_NAME).osm.pbf $(DATA_NAME).poly > data.md5sum
|
||||||
|
|
||||||
.PHONY: clean checksum benchmark data
|
.PHONY: clean checksum benchmark data
|
||||||
|
@ -85,4 +85,33 @@ BOOST_AUTO_TEST_CASE(unordered_map)
|
|||||||
BOOST_CHECK_EQUAL(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()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
Loading…
Reference in New Issue
Block a user