From 0e46dd8609bb4ee99f469b5586417e2045dfe7af Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 9 Apr 2023 10:11:56 +0800 Subject: [PATCH] feat: multiple hashmap benchmarks --- src/klotski_core/CMakeLists.txt | 9 ++++ src/klotski_core/ffi/tmain.cc | 32 +++++++++++--- src/klotski_core/group/CMakeLists.txt | 4 ++ src/klotski_core/group/group.cc | 64 +++++++++++++++++++++++++++ src/klotski_core/group/group.h | 2 + 5 files changed, 104 insertions(+), 7 deletions(-) diff --git a/src/klotski_core/CMakeLists.txt b/src/klotski_core/CMakeLists.txt index 216e42f..8760e69 100644 --- a/src/klotski_core/CMakeLists.txt +++ b/src/klotski_core/CMakeLists.txt @@ -104,6 +104,12 @@ include_directories(group) ################################################################################ +find_package(absl REQUIRED) + +find_package(unordered_dense CONFIG REQUIRED) + +################################################################################ + add_subdirectory(utils) add_subdirectory(all_cases) @@ -151,4 +157,7 @@ else() add_library(klotski STATIC ${OBJS}) endif() +target_link_libraries(klotski absl::flat_hash_map) +target_link_libraries(klotski unordered_dense::unordered_dense) + ################################################################################ diff --git a/src/klotski_core/ffi/tmain.cc b/src/klotski_core/ffi/tmain.cc index d860c33..1f4f0e7 100644 --- a/src/klotski_core/ffi/tmain.cc +++ b/src/klotski_core/ffi/tmain.cc @@ -31,14 +31,32 @@ void tmain() { // Group::block_num(CommonCode(0x4FEA13400)); // Group::block_num(RawCode::from_common_code(0x4FEA13400)); - for (auto &&common_code : AllCases::release()) { - auto s1 = Group::block_num(common_code); - auto s2 = Group::block_num(common_code.to_raw_code()); - if (s1.n_1x1 != s2.n_1x1 || s1.n_1x2 != s2.n_1x2 || s1.n_2x1 != s2.n_2x1) { - std::cout << "fuck" << std::endl; - } +// for (auto &&common_code : AllCases::release()) { +// auto s1 = Group::block_num(common_code); +// auto s2 = Group::block_num(common_code.to_raw_code()); +// if (s1.n_1x1 != s2.n_1x1 || s1.n_1x2 != s2.n_1x2 || s1.n_2x1 != s2.n_2x1) { +// std::cout << "fuck" << std::endl; +// } +// } + + auto cases = std::vector(); + auto common_codes = AllCases::release(); + for (uint64_t i = 0; i < common_codes.size(); i += 293345 * 2) { + cases.emplace_back(common_codes[i].to_raw_code()); } + std::cout << "select " << cases.size() << " cases" << std::endl; + + auto start = clock(); + + for (auto &&raw_code : cases) { + std::cout << Group::demo(raw_code) << std::endl; + } + +// Group::demo(RawCode::from_common_code(0x1A9BF0C00)); +// Group::demo(RawCode::from_common_code(0x4FEA13400)); + + std::cout << ((clock() - start) * 1000 / CLOCKS_PER_SEC) << "ms" << std::endl; return; @@ -136,7 +154,7 @@ void tmain() { std::cout << "start search" << std::endl; - auto start = clock(); +// auto start = clock(); for (const auto &c : all_cases) { diff --git a/src/klotski_core/group/CMakeLists.txt b/src/klotski_core/group/CMakeLists.txt index 2ba2948..da26c26 100644 --- a/src/klotski_core/group/CMakeLists.txt +++ b/src/klotski_core/group/CMakeLists.txt @@ -1,3 +1,7 @@ cmake_minimum_required(VERSION 3.0) +find_package(unordered_dense CONFIG REQUIRED) + add_library(group OBJECT group.cc) + +target_link_libraries(group unordered_dense::unordered_dense) diff --git a/src/klotski_core/group/group.cc b/src/klotski_core/group/group.cc index 8e03ad8..8dea2d4 100644 --- a/src/klotski_core/group/group.cc +++ b/src/klotski_core/group/group.cc @@ -1,6 +1,15 @@ +#include #include + +//#include +//#include +//#include +//#include +#include "absl/container/flat_hash_map.h" + #include "group.h" #include "common.h" +#include "core.h" namespace klotski { @@ -44,6 +53,61 @@ Group::block_num_t Group::block_num(const CommonCode &common_code) { return result; } +//struct custom_hash_simple { +// +// using is_avalanching = void; +// +// auto operator()(uint64_t const& x) const noexcept -> uint64_t { +// return ankerl::unordered_dense::detail::wyhash::hash(static_cast(x)); +// } +//}; + +uint32_t Group::demo(const RawCode &seed) { + struct group_cal_t { + uint64_t code; + uint64_t mask; + }; + + std::queue cache; + +// std::map cases; +// std::unordered_map cases; +// boost::unordered_map cases; +// ankerl::unordered_dense::map cases; + absl::flat_hash_map cases; + + cases.reserve(65535 * 8); + + cache.emplace(&cases.emplace(seed.unwrap(), group_cal_t { + .code = seed.unwrap(), + .mask = 0, + }).first->second); + + auto core = Core( + [&cases, &cache](auto &&code, auto &&mask) { + auto current = cases.find(code); + + if (current != cases.end()) { // find existed case + current->second.mask |= mask; // update mask info + return; + } + + cache.emplace(&cases.emplace(code, group_cal_t { + .code = code, + .mask = mask, + }).first->second); + } + ); + + while (!cache.empty()) { + core.next_cases(cache.front()->code, cache.front()->mask); + cache.pop(); + } + +// std::cout << cases.size() << std::endl; + + return cases.size(); +} } // namespace klotski diff --git a/src/klotski_core/group/group.h b/src/klotski_core/group/group.h index 2a6cce5..9a3e184 100644 --- a/src/klotski_core/group/group.h +++ b/src/klotski_core/group/group.h @@ -18,6 +18,8 @@ public: static block_num_t block_num(const RawCode &raw_code); static block_num_t block_num(const CommonCode &common_code); + static uint32_t demo(const RawCode &seed); + }; }