From 0f5d99b1d5c70111c498829af667919dbec43810 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 9 Apr 2023 11:08:10 +0800 Subject: [PATCH] feat: group search benchmark --- src/klotski_core/CMakeLists.txt | 3 - src/klotski_core/ffi/tmain.cc | 29 +++++---- src/klotski_core/group/CMakeLists.txt | 4 -- src/klotski_core/group/group.cc | 94 +++++++++++++++++---------- 4 files changed, 78 insertions(+), 52 deletions(-) diff --git a/src/klotski_core/CMakeLists.txt b/src/klotski_core/CMakeLists.txt index 8760e69..1c9dd2b 100644 --- a/src/klotski_core/CMakeLists.txt +++ b/src/klotski_core/CMakeLists.txt @@ -106,8 +106,6 @@ include_directories(group) find_package(absl REQUIRED) -find_package(unordered_dense CONFIG REQUIRED) - ################################################################################ add_subdirectory(utils) @@ -158,6 +156,5 @@ else() 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 1f4f0e7..066008f 100644 --- a/src/klotski_core/ffi/tmain.cc +++ b/src/klotski_core/ffi/tmain.cc @@ -39,23 +39,30 @@ void tmain() { // } // } - 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 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; - } +// 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 << Group::demo(RawCode::from_common_code(0x1A9BF0C00)) << std::endl; + +// auto tmp = RawCode::from_common_code(0x1A9BF0C00); +// for (int i = 0; i < 100; ++i) { +// Group::demo(tmp); +// } + std::cout << ((clock() - start) * 1000 / CLOCKS_PER_SEC) << "ms" << std::endl; return; @@ -162,7 +169,7 @@ void tmain() { } - std::cout << ((clock() - start) * 1000 / CLOCKS_PER_SEC) << "ms" << std::endl; +// std::cout << ((clock() - start) * 1000 / CLOCKS_PER_SEC) << "ms" << std::endl; // std::sort(next_size.begin(), next_size.end()); // std::cout << next_size[0] << std::endl; diff --git a/src/klotski_core/group/CMakeLists.txt b/src/klotski_core/group/CMakeLists.txt index da26c26..2ba2948 100644 --- a/src/klotski_core/group/CMakeLists.txt +++ b/src/klotski_core/group/CMakeLists.txt @@ -1,7 +1,3 @@ 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 8dea2d4..bf446af 100644 --- a/src/klotski_core/group/group.cc +++ b/src/klotski_core/group/group.cc @@ -1,10 +1,6 @@ #include #include -//#include -//#include -//#include -//#include #include "absl/container/flat_hash_map.h" #include "group.h" @@ -53,55 +49,85 @@ 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; +// struct group_cal_t { +// uint64_t code; +// uint64_t mask; +// }; +// std::queue cache; +// std::queue cache; +// std::queue> cache; +// +// absl::flat_hash_map cases; +// absl::flat_hash_map cases; +// absl::flat_hash_map> cases; -// std::map cases; -// std::unordered_map cases; -// boost::unordered_map cases; -// ankerl::unordered_dense::map cases; - absl::flat_hash_map cases; + std::queue cache; + std::queue cache_; + 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); +// cache.emplace(&cases.emplace(seed.unwrap(), group_cal_t { +// .code = seed.unwrap(), +// .mask = 0, +// }).first->second); +// cases.emplace(seed.unwrap(), 0); +// cache.emplace(seed.unwrap()); +// cache.emplace(seed.unwrap(), 0); +// cases.emplace(seed.unwrap(), std::make_unique(group_cal_t { +// .code = seed.unwrap(), +// .mask = 0, +// })); + + cases.emplace(seed.unwrap(), 0); + cache.emplace(seed.unwrap()); + cache_.emplace(0); auto core = Core( - [&cases, &cache](auto &&code, auto &&mask) { + [&cases, &cache, &cache_](auto &&code, auto &&mask) { auto current = cases.find(code); if (current != cases.end()) { // find existed case - current->second.mask |= mask; // update mask info + +// current->second.mask |= mask; // update mask info +// current->second |= mask; +// current->second->mask |= mask; + + current->second |= mask; // update mask info return; } - cache.emplace(&cases.emplace(code, group_cal_t { - .code = code, - .mask = mask, - }).first->second); +// cache.emplace(&cases.emplace(code, group_cal_t { +// .code = code, +// .mask = mask, +// }).first->second); +// cases.emplace(code, mask); +// cache.emplace(code); +// cache.emplace(code, mask); +// cases.emplace(code, std::make_unique(group_cal_t { +// .code = code, +// .mask = mask, +// })); + + cases.emplace(code, mask); + cache.emplace(code); + cache_.emplace(mask); + } ); while (!cache.empty()) { - core.next_cases(cache.front()->code, cache.front()->mask); +// core.next_cases(cache.front()->code, cache.front()->mask); +// core.next_cases(cache.front(), cases.find(cache.front())->second); +// core.next_cases(cache.front().first, cache.front().second); +// core.next_cases(cache.front(), cases.find(cache.front())->second->mask); +// core.next_cases(cache.front(), cases.find(cache.front())->second); + core.next_cases(cache.front(), cache_.front()); + cache.pop(); + cache_.pop(); } // std::cout << cases.size() << std::endl;