Browse Source

feat: group search benchmark

legacy
Dnomd343 2 years ago
parent
commit
0f5d99b1d5
  1. 3
      src/klotski_core/CMakeLists.txt
  2. 29
      src/klotski_core/ffi/tmain.cc
  3. 4
      src/klotski_core/group/CMakeLists.txt
  4. 94
      src/klotski_core/group/group.cc

3
src/klotski_core/CMakeLists.txt

@ -106,8 +106,6 @@ include_directories(group)
find_package(absl REQUIRED) find_package(absl REQUIRED)
find_package(unordered_dense CONFIG REQUIRED)
################################################################################ ################################################################################
add_subdirectory(utils) add_subdirectory(utils)
@ -158,6 +156,5 @@ else()
endif() endif()
target_link_libraries(klotski absl::flat_hash_map) target_link_libraries(klotski absl::flat_hash_map)
target_link_libraries(klotski unordered_dense::unordered_dense)
################################################################################ ################################################################################

29
src/klotski_core/ffi/tmain.cc

@ -39,23 +39,30 @@ void tmain() {
// } // }
// } // }
auto cases = std::vector<RawCode>(); // auto cases = std::vector<RawCode>();
auto common_codes = AllCases::release(); // auto common_codes = AllCases::release();
for (uint64_t i = 0; i < common_codes.size(); i += 293345 * 2) { // for (uint64_t i = 0; i < common_codes.size(); i += 293345 * 2) {
cases.emplace_back(common_codes[i].to_raw_code()); // cases.emplace_back(common_codes[i].to_raw_code());
} // }
//
std::cout << "select " << cases.size() << " cases" << std::endl; // std::cout << "select " << cases.size() << " cases" << std::endl;
auto start = clock(); auto start = clock();
for (auto &&raw_code : cases) { // for (auto &&raw_code : cases) {
std::cout << Group::demo(raw_code) << std::endl; // std::cout << Group::demo(raw_code) << std::endl;
} // }
// Group::demo(RawCode::from_common_code(0x1A9BF0C00)); // Group::demo(RawCode::from_common_code(0x1A9BF0C00));
// Group::demo(RawCode::from_common_code(0x4FEA13400)); // 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; std::cout << ((clock() - start) * 1000 / CLOCKS_PER_SEC) << "ms" << std::endl;
return; 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::sort(next_size.begin(), next_size.end());
// std::cout << next_size[0] << std::endl; // std::cout << next_size[0] << std::endl;

4
src/klotski_core/group/CMakeLists.txt

@ -1,7 +1,3 @@
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.0)
find_package(unordered_dense CONFIG REQUIRED)
add_library(group OBJECT group.cc) add_library(group OBJECT group.cc)
target_link_libraries(group unordered_dense::unordered_dense)

94
src/klotski_core/group/group.cc

@ -1,10 +1,6 @@
#include <queue> #include <queue>
#include <iostream> #include <iostream>
//#include <map>
//#include <unordered_map>
//#include <boost/unordered_map.hpp>
//#include <ankerl/unordered_dense.h>
#include "absl/container/flat_hash_map.h" #include "absl/container/flat_hash_map.h"
#include "group.h" #include "group.h"
@ -53,55 +49,85 @@ Group::block_num_t Group::block_num(const CommonCode &common_code) {
return result; 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<uint64_t>(x));
// }
//};
uint32_t Group::demo(const RawCode &seed) { uint32_t Group::demo(const RawCode &seed) {
struct group_cal_t {
uint64_t code;
uint64_t mask;
};
std::queue<group_cal_t*> cache; // struct group_cal_t {
// uint64_t code;
// uint64_t mask;
// };
// std::queue<group_cal_t*> cache;
// std::queue<uint64_t> cache;
// std::queue<std::pair<uint64_t, uint64_t>> cache;
//
// absl::flat_hash_map<uint64_t, group_cal_t> cases;
// absl::flat_hash_map<uint64_t, uint64_t> cases;
// absl::flat_hash_map<uint64_t, std::unique_ptr<group_cal_t>> cases;
// std::map<uint64_t, group_cal_t> cases; std::queue<uint64_t> cache;
// std::unordered_map<uint64_t, group_cal_t> cases; std::queue<uint64_t> cache_;
// boost::unordered_map<uint64_t, group_cal_t> cases; absl::flat_hash_map<uint64_t, uint64_t> cases;
// ankerl::unordered_dense::map<uint64_t, group_cal_t> cases;
absl::flat_hash_map<uint64_t, group_cal_t> cases;
cases.reserve(65535 * 8); cases.reserve(65535 * 8);
cache.emplace(&cases.emplace(seed.unwrap(), group_cal_t { // cache.emplace(&cases.emplace(seed.unwrap(), group_cal_t {
.code = seed.unwrap(), // .code = seed.unwrap(),
.mask = 0, // .mask = 0,
}).first->second); // }).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>(group_cal_t {
// .code = seed.unwrap(),
// .mask = 0,
// }));
cases.emplace(seed.unwrap(), 0);
cache.emplace(seed.unwrap());
cache_.emplace(0);
auto core = Core( auto core = Core(
[&cases, &cache](auto &&code, auto &&mask) { [&cases, &cache, &cache_](auto &&code, auto &&mask) {
auto current = cases.find(code); auto current = cases.find(code);
if (current != cases.end()) { // find existed case 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; return;
} }
cache.emplace(&cases.emplace(code, group_cal_t { // cache.emplace(&cases.emplace(code, group_cal_t {
.code = code, // .code = code,
.mask = mask, // .mask = mask,
}).first->second); // }).first->second);
// cases.emplace(code, mask);
// cache.emplace(code);
// cache.emplace(code, mask);
// cases.emplace(code, std::make_unique<group_cal_t>(group_cal_t {
// .code = code,
// .mask = mask,
// }));
cases.emplace(code, mask);
cache.emplace(code);
cache_.emplace(mask);
} }
); );
while (!cache.empty()) { 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();
cache_.pop();
} }
// std::cout << cases.size() << std::endl; // std::cout << cases.size() << std::endl;

Loading…
Cancel
Save