Browse Source

feat: multiple hashmap benchmarks

master
Dnomd343 2 years ago
parent
commit
0e46dd8609
  1. 9
      src/klotski_core/CMakeLists.txt
  2. 32
      src/klotski_core/ffi/tmain.cc
  3. 4
      src/klotski_core/group/CMakeLists.txt
  4. 64
      src/klotski_core/group/group.cc
  5. 2
      src/klotski_core/group/group.h

9
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)
################################################################################

32
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<RawCode>();
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) {

4
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)

64
src/klotski_core/group/group.cc

@ -1,6 +1,15 @@
#include <queue>
#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 "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<uint64_t>(x));
// }
//};
uint32_t Group::demo(const RawCode &seed) {
struct group_cal_t {
uint64_t code;
uint64_t mask;
};
std::queue<group_cal_t*> cache;
// std::map<uint64_t, group_cal_t> cases;
// std::unordered_map<uint64_t, group_cal_t> cases;
// boost::unordered_map<uint64_t, group_cal_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);
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

2
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);
};
}

Loading…
Cancel
Save