mirror of
https://github.com/greg7mdp/parallel-hashmap.git
synced 2026-08-29 08:34:39 +08:00
Add support for custom pointers. (#258)
* Add support for custom pointers. * Add comment * lowercase `OpenMP` in `CMakeLists.txt`
This commit is contained in:
committed by
GitHub
parent
2d0b499273
commit
debe606647
Vendored
+9
-3
@@ -208,7 +208,7 @@ if (PHMAP_BUILD_EXAMPLES)
|
|||||||
# -------------------------------------------------------------
|
# -------------------------------------------------------------
|
||||||
find_package(OpenMP)
|
find_package(OpenMP)
|
||||||
find_package(Boost 1.70.0)
|
find_package(Boost 1.70.0)
|
||||||
if (OPENMP_FOUND AND Boost_FOUND AND "cxx_std_20" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
|
if (OpenMP_FOUND AND Boost_FOUND AND "cxx_std_20" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
|
||||||
add_executable(ex_llil4map examples/llil4map.cc phmap.natvis)
|
add_executable(ex_llil4map examples/llil4map.cc phmap.natvis)
|
||||||
target_include_directories(ex_llil4map PRIVATE ${Boost_INCLUDE_DIRS})
|
target_include_directories(ex_llil4map PRIVATE ${Boost_INCLUDE_DIRS})
|
||||||
target_compile_features(ex_llil4map PUBLIC cxx_std_20)
|
target_compile_features(ex_llil4map PUBLIC cxx_std_20)
|
||||||
@@ -219,11 +219,17 @@ if (PHMAP_BUILD_EXAMPLES)
|
|||||||
target_link_libraries(ex_llil4map PRIVATE OpenMP::OpenMP_CXX)
|
target_link_libraries(ex_llil4map PRIVATE OpenMP::OpenMP_CXX)
|
||||||
target_compile_options(ex_llil4map PRIVATE "${OpenMP_CXX_FLAGS}")
|
target_compile_options(ex_llil4map PRIVATE "${OpenMP_CXX_FLAGS}")
|
||||||
|
|
||||||
|
file(COPY examples/llil_utils DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if (Boost_FOUND)
|
||||||
|
add_executable(ex_custom_pointer examples/custom_pointer.cc phmap.natvis)
|
||||||
|
target_include_directories(ex_custom_pointer PRIVATE ${Boost_INCLUDE_DIRS})
|
||||||
|
|
||||||
add_executable(ex_llil examples/llil.cc phmap.natvis)
|
add_executable(ex_llil examples/llil.cc phmap.natvis)
|
||||||
target_include_directories(ex_llil PRIVATE ${Boost_INCLUDE_DIRS})
|
target_include_directories(ex_llil PRIVATE ${Boost_INCLUDE_DIRS})
|
||||||
target_compile_features(ex_llil PUBLIC cxx_std_20)
|
target_compile_features(ex_llil PUBLIC cxx_std_20)
|
||||||
|
target_link_libraries(ex_llil Threads::Threads)
|
||||||
file(COPY examples/llil_utils DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
|
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
target_link_libraries(ex_knucleotide Threads::Threads)
|
target_link_libraries(ex_knucleotide Threads::Threads)
|
||||||
|
|||||||
@@ -0,0 +1,64 @@
|
|||||||
|
#include <boost/interprocess/managed_mapped_file.hpp>
|
||||||
|
#include <scoped_allocator>
|
||||||
|
#include <parallel_hashmap/phmap.h>
|
||||||
|
#include <vector>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using mmap_file_t = boost::interprocess::managed_mapped_file;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
using bi_alloc_t = boost::interprocess::allocator<T, boost::interprocess::managed_mapped_file::segment_manager>;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
using scoped_alloc_t = std::scoped_allocator_adaptor<T>;
|
||||||
|
|
||||||
|
void simple_map() {
|
||||||
|
struct LatpLon {
|
||||||
|
int32_t latp;
|
||||||
|
int32_t lon;
|
||||||
|
};
|
||||||
|
|
||||||
|
using nodestore_pair_t = std::pair<const uint64_t, LatpLon>;
|
||||||
|
using map_t = phmap::flat_hash_map<const uint64_t, LatpLon, std::hash<uint64_t>, std::equal_to<uint64_t>,
|
||||||
|
bi_alloc_t<nodestore_pair_t>>;
|
||||||
|
|
||||||
|
auto mmap_file =
|
||||||
|
boost::interprocess::managed_mapped_file(boost::interprocess::open_or_create, "map_iv.dat", 1000000);
|
||||||
|
map_t* map = mmap_file.find_or_construct<map_t>("node_store")(mmap_file.get_segment_manager());
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < 1000; ++i) {
|
||||||
|
LatpLon p = {10, 10};
|
||||||
|
map->emplace(i, p);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << map->at(10).latp << " " << map->at(10).lon << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void scoped_map() {
|
||||||
|
using way_t = std::vector<uint64_t, bi_alloc_t<uint64_t>>;
|
||||||
|
using waystore_pair_t = std::pair<const uint64_t, way_t>;
|
||||||
|
using map_t = phmap::flat_hash_map<const uint64_t, way_t, std::hash<uint64_t>, std::equal_to<uint64_t>,
|
||||||
|
std::scoped_allocator_adaptor<bi_alloc_t<waystore_pair_t>>>;
|
||||||
|
|
||||||
|
auto mmap_file =
|
||||||
|
boost::interprocess::managed_mapped_file(boost::interprocess::open_or_create, "map_iv.dat", 1000000);
|
||||||
|
map_t* map = mmap_file.find_or_construct<map_t>("ways_store")(mmap_file.get_segment_manager());
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < 1000; ++i) {
|
||||||
|
std::vector<uint64_t> init = {1, 2, 3, 4};
|
||||||
|
map->emplace(std::piecewise_construct, std::forward_as_tuple(i), std::forward_as_tuple(init.begin(), init.end()));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << map->at(10).size() << std::endl;
|
||||||
|
for (auto const& i : map->at(10))
|
||||||
|
std::cout << i << " ";
|
||||||
|
std::cout << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
simple_map();
|
||||||
|
scoped_map();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -63,7 +63,6 @@
|
|||||||
|
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
#include <execution>
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
|
||||||
|
|||||||
Vendored
+1
-6
@@ -926,12 +926,7 @@ private:
|
|||||||
using IsDecomposable = IsDecomposable<void, PolicyTraits, Hash, Eq, Ts...>;
|
using IsDecomposable = IsDecomposable<void, PolicyTraits, Hash, Eq, Ts...>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static_assert(std::is_same<pointer, value_type*>::value,
|
class iterator
|
||||||
"Allocators with custom pointer types are not supported");
|
|
||||||
static_assert(std::is_same<const_pointer, const value_type*>::value,
|
|
||||||
"Allocators with custom pointer types are not supported");
|
|
||||||
|
|
||||||
class iterator
|
|
||||||
{
|
{
|
||||||
friend class raw_hash_set;
|
friend class raw_hash_set;
|
||||||
|
|
||||||
|
|||||||
Vendored
+1
-1
@@ -4189,7 +4189,7 @@ void* Allocate(Alloc* alloc, size_t n) {
|
|||||||
using A = typename phmap::allocator_traits<Alloc>::template rebind_alloc<M>;
|
using A = typename phmap::allocator_traits<Alloc>::template rebind_alloc<M>;
|
||||||
using AT = typename phmap::allocator_traits<Alloc>::template rebind_traits<M>;
|
using AT = typename phmap::allocator_traits<Alloc>::template rebind_traits<M>;
|
||||||
A mem_alloc(*alloc);
|
A mem_alloc(*alloc);
|
||||||
void* p = AT::allocate(mem_alloc, (n + sizeof(M) - 1) / sizeof(M));
|
void* p = &*AT::allocate(mem_alloc, (n + sizeof(M) - 1) / sizeof(M)); // `&*` to support custom pointers such as boost offset_ptr.
|
||||||
assert(reinterpret_cast<uintptr_t>(p) % Alignment == 0 &&
|
assert(reinterpret_cast<uintptr_t>(p) % Alignment == 0 &&
|
||||||
"allocator does not respect alignment");
|
"allocator does not respect alignment");
|
||||||
return p;
|
return p;
|
||||||
|
|||||||
Reference in New Issue
Block a user