From aa6d61432b05c0908bfed96d4d2a4951420e4528 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 8 Jun 2024 12:56:10 +0800 Subject: [PATCH] feat: benchmark suites for Ranges --- src/core/CMakeLists.txt | 5 +++- src/core/benchmark/ranges.cc | 38 ++++++++++++++++++++++++++++++ src/core/ranges/internal/derive.cc | 16 ++++--------- src/core/ranges/internal/ranges.cc | 38 +++++++++++++++--------------- 4 files changed, 66 insertions(+), 31 deletions(-) create mode 100644 src/core/benchmark/ranges.cc diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 7fdc158..e27515d 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -6,7 +6,6 @@ set(CMAKE_CXX_STANDARD 23) set(KLOTSKI_CORE_SRC all_cases/internal/basic_ranges.cc all_cases/internal/all_cases.cc -# all_cases/internal/derive.cc common_code/internal/common_code.cc common_code/internal/serialize.cc @@ -52,3 +51,7 @@ target_link_libraries(group_benchmark PRIVATE klotski::core benchmark::benchmark add_executable(bm_all_cases benchmark/all_cases.cc) target_compile_options(bm_all_cases PRIVATE -fno-rtti -fno-exceptions -fno-access-control) target_link_libraries(bm_all_cases PRIVATE klotski::core benchmark::benchmark_main bs::thread_pool) + +add_executable(bm_ranges benchmark/ranges.cc) +target_compile_options(bm_ranges PRIVATE -fno-rtti -fno-exceptions) +target_link_libraries(bm_ranges PRIVATE klotski::core benchmark::benchmark_main) diff --git a/src/core/benchmark/ranges.cc b/src/core/benchmark/ranges.cc new file mode 100644 index 0000000..42a54f9 --- /dev/null +++ b/src/core/benchmark/ranges.cc @@ -0,0 +1,38 @@ +#include + +#include "group/group.h" +#include "ranges/ranges.h" +#include "all_cases/all_cases.h" + +using klotski::cases::AllCases; + +static void SpawnRanges(benchmark::State &state) { + // constexpr auto nums = target_nums(); + + for (auto _ : state) { + + klotski::cases::Ranges kk {}; + kk.reserve(7311921); + + // for (uint32_t type_id = 0; type_id < klotski::cases::TYPE_ID_LIMIT; ++type_id) { + for (auto [n, n_2x1, n_1x1] : klotski::cases::BLOCK_NUM) { + kk.spawn(n, n_2x1, n_1x1); + } + // } + } + +} + +static void RangesUnionExport(benchmark::State &state) { + auto &all_cases = AllCases::instance().fetch(); + for (auto _ : state) { + auto codes = all_cases.codes(); + benchmark::DoNotOptimize(codes.size()); + } +} + +BENCHMARK(SpawnRanges)->Unit(benchmark::kMillisecond); + +// BENCHMARK(RangesUnionExport)->Unit(benchmark::kMillisecond); + +BENCHMARK_MAIN(); diff --git a/src/core/ranges/internal/derive.cc b/src/core/ranges/internal/derive.cc index a717469..4b54cdb 100644 --- a/src/core/ranges/internal/derive.cc +++ b/src/core/ranges/internal/derive.cc @@ -32,23 +32,17 @@ int Ranges::check(const int head, uint32_t range) { } void Ranges::derive(const int head, Ranges &output) const { - - uint32_t last_val = range_reverse(this->back()); - + const uint32_t max_val = range_reverse(this->back()); for (uint32_t index = 0; index < size(); ++index) { if (const auto offset = check(head, (*this)[index])) { // invalid case - uint32_t tmp = 1U << (32 - offset * 2); // distance to next possible range /// !! <- broken /// ( xx xx xx ) xx xx xx ... [reversed range] /// +1 00 00 00 ... (delta) - tmp += range_reverse((*this)[index]) & ~(tmp - 1); - - auto min_next = tmp; - - if (min_next > last_val) { - break; + const uint32_t delta = 1U << (32 - offset * 2); // distance to next possible range + const auto min_next = delta + range_reverse((*this)[index]) & ~(delta - 1); + if (min_next > max_val) { + break; // index has overflowed } - while (range_reverse((*this)[++index]) < min_next) {} // located next range --index; continue; diff --git a/src/core/ranges/internal/ranges.cc b/src/core/ranges/internal/ranges.cc index 9fd14a5..29ae229 100644 --- a/src/core/ranges/internal/ranges.cc +++ b/src/core/ranges/internal/ranges.cc @@ -1,35 +1,35 @@ -#pragma once - +#include "ranges/ranges.h" #include "common_code/common_code.h" using klotski::cases::Ranges; using klotski::codec::CommonCode; using klotski::cases::RangesUnion; -inline void Ranges::reverse() { +void Ranges::reverse() { for (auto &x : *this) { x = range_reverse(x); } } -inline std::vector RangesUnion::codes() const { - std::vector codes; - - codes.reserve(0); // TODO: cal sum - - for (uint64_t head = 0; head < 16; ++head) { - - if (head % 4 == 3) { - continue; - } - - for (auto range : (*this)[head]) { - auto kk = head << 32 | range; - codes.emplace_back(CommonCode::unsafe_create(kk)); +std::vector RangesUnion::codes() const { + constexpr auto heads = std::to_array({ + 0x0, 0x1, 0x2, + 0x4, 0x5, 0x6, + 0x8, 0x9, 0xA, + 0xC, 0xD, 0xE, + }); + + size_type size = 0; + for (const auto head : heads) { + size += (*this)[head].size(); + } + std::vector codes; + codes.reserve(size); + for (const auto head : heads) { + for (const auto range : (*this)[head]) { + codes.emplace_back(CommonCode::unsafe_create(head << 32 | range)); } - } - return codes; }