Browse Source

feat: benchmark suites for Ranges

master
Dnomd343 4 weeks ago
parent
commit
aa6d61432b
  1. 5
      src/core/CMakeLists.txt
  2. 38
      src/core/benchmark/ranges.cc
  3. 16
      src/core/ranges/internal/derive.cc
  4. 38
      src/core/ranges/internal/ranges.cc

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

38
src/core/benchmark/ranges.cc

@ -0,0 +1,38 @@
#include <benchmark/benchmark.h>
#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();

16
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;

38
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<CommonCode> RangesUnion::codes() const {
std::vector<CommonCode> 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<CommonCode> RangesUnion::codes() const {
constexpr auto heads = std::to_array<uint64_t>({
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<CommonCode> 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;
}

Loading…
Cancel
Save