More refactor

This commit is contained in:
Patrick Niklaus 2018-03-26 10:46:07 +00:00 committed by Patrick Niklaus
parent 9e97748700
commit b3ef2a0383
2 changed files with 47 additions and 34 deletions

View File

@ -39,24 +39,12 @@ class DataLayout
inline uint64_t GetBlockEntries(const std::string &name) const inline uint64_t GetBlockEntries(const std::string &name) const
{ {
auto iter = blocks.find(name); return GetBlock(name).num_entries;
if (iter == blocks.end())
{
throw util::exception("Could not find block " + name);
}
return iter->second.num_entries;
} }
inline uint64_t GetBlockSize(const std::string &name) const inline uint64_t GetBlockSize(const std::string &name) const
{ {
auto iter = blocks.find(name); return GetBlock(name).byte_size;
if (iter == blocks.end())
{
throw util::exception("Could not find block " + name);
}
return iter->second.byte_size;
} }
inline uint64_t GetSizeOfLayout() const inline uint64_t GetSizeOfLayout() const
@ -80,6 +68,7 @@ class DataLayout
{ {
char *start_canary_ptr = ptr - sizeof(CANARY); char *start_canary_ptr = ptr - sizeof(CANARY);
char *end_canary_ptr = ptr + GetBlockSize(name); char *end_canary_ptr = ptr + GetBlockSize(name);
std::cout << name << ": " << (long) (start_canary_ptr-shared_memory) << " -> " << (long) (end_canary_ptr-shared_memory) << std::endl;
std::copy(CANARY, CANARY + sizeof(CANARY), start_canary_ptr); std::copy(CANARY, CANARY + sizeof(CANARY), start_canary_ptr);
std::copy(CANARY, CANARY + sizeof(CANARY), end_canary_ptr); std::copy(CANARY, CANARY + sizeof(CANARY), end_canary_ptr);
} }
@ -103,10 +92,34 @@ class DataLayout
return (T *)ptr; return (T *)ptr;
} }
template<typename OutIter>
void List(const std::string& name_prefix, OutIter out) const
{
for (const auto& pair : blocks)
{
// check if string begins with the name prefix
if (pair.first.find(name_prefix) == 0)
{
*out++ = pair.first;
}
}
}
private: private:
friend void serialization::read(io::BufferReader &reader, DataLayout &layout); friend void serialization::read(io::BufferReader &reader, DataLayout &layout);
friend void serialization::write(io::BufferWriter &writer, const DataLayout &layout); friend void serialization::write(io::BufferWriter &writer, const DataLayout &layout);
const Block& GetBlock(const std::string& name) const
{
auto iter = blocks.find(name);
if (iter == blocks.end())
{
throw util::exception("Could not find block " + name);
}
return iter->second;
}
// Fit aligned storage in buffer to 64 bytes to conform with AVX 512 types // Fit aligned storage in buffer to 64 bytes to conform with AVX 512 types
inline void *align(void *&ptr) const noexcept inline void *align(void *&ptr) const noexcept
{ {
@ -117,11 +130,17 @@ class DataLayout
inline void *GetAlignedBlockPtr(void *ptr, const std::string &name) const inline void *GetAlignedBlockPtr(void *ptr, const std::string &name) const
{ {
for (auto iter = blocks.begin(); iter != blocks.end() && iter->first != name; ++iter) auto block_iter = blocks.find(name);
if (block_iter == blocks.end())
{
throw util::exception("Could not find block " + name);
}
for (auto iter = blocks.begin(); iter != block_iter; ++iter)
{ {
ptr = static_cast<char *>(ptr) + sizeof(CANARY); ptr = static_cast<char *>(ptr) + sizeof(CANARY);
ptr = align(ptr); ptr = align(ptr);
ptr = static_cast<char *>(ptr) + GetBlockSize(name); ptr = static_cast<char *>(ptr) + iter->second.byte_size;
ptr = static_cast<char *>(ptr) + sizeof(CANARY); ptr = static_cast<char *>(ptr) + sizeof(CANARY);
} }
@ -130,12 +149,6 @@ class DataLayout
return ptr; return ptr;
} }
template <typename T> inline T *GetBlockEnd(char *shared_memory, const std::string &name) const
{
auto begin = GetBlockPtr<T>(shared_memory, name);
return begin + GetBlockEntries(name);
}
static constexpr std::size_t BLOCK_ALIGNMENT = 64; static constexpr std::size_t BLOCK_ALIGNMENT = 64;
std::map<std::string, Block> blocks; std::map<std::string, Block> blocks;
}; };

View File

@ -170,7 +170,7 @@ int Storage::Run(int max_wait)
// Copy memory layout to shared memory and populate data // Copy memory layout to shared memory and populate data
char *shared_memory_ptr = static_cast<char *>(data_memory->Ptr()); char *shared_memory_ptr = static_cast<char *>(data_memory->Ptr());
memcpy(shared_memory_ptr, encoded_layout.data(), encoded_layout.size()); std::copy_n(encoded_layout.data(), encoded_layout.size(), shared_memory_ptr);
PopulateData(layout, shared_memory_ptr + encoded_layout.size()); PopulateData(layout, shared_memory_ptr + encoded_layout.size());
{ // Lock for write access shared region mutex { // Lock for write access shared region mutex
@ -304,8 +304,8 @@ void Storage::PopulateData(const DataLayout &layout, char *memory_ptr)
0); 0);
const auto absolute_file_index_path = const auto absolute_file_index_path =
boost::filesystem::absolute(config.GetPath(".osrm.fileIndex")).string(); boost::filesystem::absolute(config.GetPath(".osrm.fileIndex")).string();
BOOST_ASSERT(static_cast<std::size_t>(layout.GetBlockSize("/common/rtree/file_index_path")) >= BOOST_ASSERT(static_cast<std::size_t>(layout.GetBlockSize(
absolute_file_index_path.size()); "/common/rtree/file_index_path")) >= absolute_file_index_path.size());
std::copy( std::copy(
absolute_file_index_path.begin(), absolute_file_index_path.end(), file_index_path_ptr); absolute_file_index_path.begin(), absolute_file_index_path.end(), file_index_path_ptr);
} }
@ -386,8 +386,8 @@ void Storage::PopulateData(const DataLayout &layout, char *memory_ptr)
turn_instruction_list_ptr, turn_instruction_list_ptr,
layout.GetBlockEntries("/common/turn_data/turn_instructions")); layout.GetBlockEntries("/common/turn_data/turn_instructions"));
const auto entry_class_id_list_ptr = layout.GetBlockPtr<EntryClassID, true>( const auto entry_class_id_list_ptr =
memory_ptr, "/common/turn_data/entry_class_ids"); layout.GetBlockPtr<EntryClassID, true>(memory_ptr, "/common/turn_data/entry_class_ids");
util::vector_view<EntryClassID> entry_class_ids( util::vector_view<EntryClassID> entry_class_ids(
entry_class_id_list_ptr, layout.GetBlockEntries("/common/turn_data/entry_class_ids")); entry_class_id_list_ptr, layout.GetBlockEntries("/common/turn_data/entry_class_ids"));
@ -620,13 +620,13 @@ void Storage::PopulateData(const DataLayout &layout, char *memory_ptr)
graph_edges_ptr, layout.GetBlockEntries("/ch/contracted_graph/edge_array")); graph_edges_ptr, layout.GetBlockEntries("/ch/contracted_graph/edge_array"));
std::vector<util::vector_view<bool>> edge_filter; std::vector<util::vector_view<bool>> edge_filter;
for (auto index : util::irange<std::size_t>(0, NUM_METRICS)) layout.List(
{ "/ch/edge_filter", boost::make_function_output_iterator([&](const auto &name) {
auto block_id = "/ch/edge_filter/" + std::to_string(index); auto data_ptr =
auto data_ptr = layout.GetBlockPtr<util::vector_view<bool>::Word, true>(memory_ptr, block_id); layout.GetBlockPtr<util::vector_view<bool>::Word, true>(memory_ptr, name);
auto num_entries = layout.GetBlockEntries(block_id); auto num_entries = layout.GetBlockEntries(name);
edge_filter.emplace_back(data_ptr, num_entries); edge_filter.emplace_back(data_ptr, num_entries);
} }));
std::uint32_t graph_connectivity_checksum = 0; std::uint32_t graph_connectivity_checksum = 0;
contractor::QueryGraphView graph_view(std::move(node_list), std::move(edge_list)); contractor::QueryGraphView graph_view(std::move(node_list), std::move(edge_list));