Skip to content

Commit

Permalink
Add support for custom pointers. (#258)
Browse files Browse the repository at this point in the history
* Add support for custom pointers.

* Add comment

* lowercase `OpenMP` in `CMakeLists.txt`
  • Loading branch information
greg7mdp authored Nov 2, 2024
1 parent 2d0b499 commit debe606
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 11 deletions.
12 changes: 9 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ if (PHMAP_BUILD_EXAMPLES)
# -------------------------------------------------------------
find_package(OpenMP)
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)
target_include_directories(ex_llil4map PRIVATE ${Boost_INCLUDE_DIRS})
target_compile_features(ex_llil4map PUBLIC cxx_std_20)
Expand All @@ -219,11 +219,17 @@ if (PHMAP_BUILD_EXAMPLES)
target_link_libraries(ex_llil4map PRIVATE OpenMP::OpenMP_CXX)
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)
target_include_directories(ex_llil PRIVATE ${Boost_INCLUDE_DIRS})
target_compile_features(ex_llil PUBLIC cxx_std_20)

file(COPY examples/llil_utils DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
target_link_libraries(ex_llil Threads::Threads)
endif()

target_link_libraries(ex_knucleotide Threads::Threads)
Expand Down
64 changes: 64 additions & 0 deletions examples/custom_pointer.cc
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 0 additions & 1 deletion examples/llil.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@

#include <utility>
#include <iterator>
#include <execution>
#include <algorithm>
#include <filesystem>

Expand Down
7 changes: 1 addition & 6 deletions parallel_hashmap/phmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -926,12 +926,7 @@ class raw_hash_set
using IsDecomposable = IsDecomposable<void, PolicyTraits, Hash, Eq, Ts...>;

public:
static_assert(std::is_same<pointer, value_type*>::value,
"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
class iterator
{
friend class raw_hash_set;

Expand Down
2 changes: 1 addition & 1 deletion parallel_hashmap/phmap_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -4189,7 +4189,7 @@ void* Allocate(Alloc* alloc, size_t n) {
using A = typename phmap::allocator_traits<Alloc>::template rebind_alloc<M>;
using AT = typename phmap::allocator_traits<Alloc>::template rebind_traits<M>;
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 &&
"allocator does not respect alignment");
return p;
Expand Down

0 comments on commit debe606

Please sign in to comment.