diff --git a/src/core/benchmark/fast_cal.cc b/src/core/benchmark/fast_cal.cc index fe2a47d..4d92d2a 100644 --- a/src/core/benchmark/fast_cal.cc +++ b/src/core/benchmark/fast_cal.cc @@ -15,8 +15,8 @@ static void FastCalBenchmark(benchmark::State &state) { for (auto _ : state) { // auto fc = FastCal(code); // benchmark::DoNotOptimize(fc.demo()); -// benchmark::DoNotOptimize(FastCal_demo(code)); - auto tmp = klotski::cases::Group_extend(code); + benchmark::DoNotOptimize(FastCal_demo(code)); +// auto tmp = klotski::cases::Group_extend(code); } } diff --git a/src/core/fast_cal/internal/demo.cc b/src/core/fast_cal/internal/demo.cc index 33df763..4e464c3 100644 --- a/src/core/fast_cal/internal/demo.cc +++ b/src/core/fast_cal/internal/demo.cc @@ -34,11 +34,16 @@ struct data_t { template class MyQueue { public: +// MyQueue() = default; + explicit MyQueue(size_t reserve) { -// vec_.reserve(reserve); vec_.resize(reserve); } +// void reserve(size_t size) { +// vec_.resize(size); +// } + void emplace_back(T item) { // vec_.emplace_back(item); vec_[iter_] = item; @@ -57,7 +62,7 @@ public: return offset_ == iter_; } -private: +//private: size_t iter_ {0}; size_t offset_ {0}; std::vector vec_ {}; @@ -66,9 +71,11 @@ private: class FCDemo { public: explicit FCDemo(RawCode raw_code) : codes_(GroupUnion::from_raw_code(raw_code).max_group_size()) { + // TODO: build codes_ with reserve size auto reserve = GroupUnion::from_raw_code(raw_code).max_group_size(); // codes_.reserve(reserve); - cases_.reserve(286730); +// cases_.reserve(static_cast(reserve * 1.56)); + cases_.reserve(static_cast(25955 * 1.56)); codes_.emplace_back(raw_code.unwrap()); cases_.emplace(raw_code, data_t {0, 0}); // without mask } @@ -78,13 +85,11 @@ public: auto core = MaskMover([this, &result](uint64_t code, uint64_t mask) { if (const auto match = cases_.find(code); match != cases_.end()) { match->second.mask |= mask; // update mask -// match.value().mask |= mask; // update mask return; } cases_.emplace(code, data_t { .mask = mask, .back = codes_.front(), -// .back = codes_[offset_].unwrap(), }); codes_.emplace_back(code); @@ -99,56 +104,68 @@ public: codes_.pop(); if (result != 0) { -// std::cout << cases_.size() << std::endl; -// std::cout << cases_.load_factor() << std::endl; return RawCode::unsafe_create(result); } - } + return RawCode::unsafe_create(0); + } - std::cout << cases_.load_factor() << std::endl; + std::vector DoCalMulti() { +// uint64_t result = 0; + bool stop_flag = false; + std::vector results {}; - return RawCode::unsafe_create(0); + auto core = MaskMover([this, &stop_flag, &results](uint64_t code, uint64_t mask) { + if (const auto match = cases_.find(code); match != cases_.end()) { + match->second.mask |= mask; // update mask + return; + } + cases_.emplace(code, data_t { + .mask = mask, + .back = codes_.front(), + }); + codes_.emplace_back(code); -// while (offset_ != codes_.size()) { -// auto curr = codes_[offset_].unwrap(); -// core.next_cases(curr, cases_.find(curr)->second.mask); -// ++offset_; -// -// if (result != 0) { -// break; -// } -// } + if (((code >> 39) & 0b111) == 0b100) { + stop_flag = true; +// result = code; + results.emplace_back(RawCode::unsafe_create(code)); + } + }); + + size_t layer_end = 1; -// std::vector path; -// auto code = result; -// while (true) { -// if (code == 0) { -// break; + while (!codes_.empty()) { + auto curr = codes_.front(); + core.next_cases(curr, cases_.find(curr)->second.mask); + codes_.pop(); + + if (codes_.offset_ == layer_end) { +// std::cout << "layer: " << codes_.offset_ << std::endl; + layer_end = codes_.iter_; + if (stop_flag) { + return results; + } + } + +// if (result != 0) { +// return RawCode::unsafe_create(result); // } -// path.emplace_back(RawCode::unsafe_create(code)); -// code = cases_.find(code)->second.back; -// } -// std::reverse(path.begin(), path.end()); -// for (auto step : path) { -// std::cout << step << std::endl; -// } -// std::cout << path.size() << std::endl; - -// return codes_[offset_]; + } + return {}; } private: -// uint64_t offset_ {0}; MyQueue codes_; -// absl::flat_hash_map cases_; // -// robin_hood::unordered_map cases_; -// ankerl::unordered_dense::map cases_; -// tsl::robin_map cases_; phmap::flat_hash_map cases_; }; RawCode FastCal_demo(RawCode raw_code) { FCDemo fc {raw_code}; - return fc.DoCal(); +// return fc.DoCal(); + auto tmp = fc.DoCalMulti(); +// for (auto code : tmp) { +// std::cout << code << std::endl; +// } + return tmp[0]; } diff --git a/src/core/group/internal/extend.cc b/src/core/group/internal/extend.cc index 56ee6c4..9b396fb 100644 --- a/src/core/group/internal/extend.cc +++ b/src/core/group/internal/extend.cc @@ -13,6 +13,8 @@ using klotski::cases::RangesUnion; using klotski::mover::MaskMover; using klotski::cases::GroupUnion; +// TODO: maybe we can perf with mirror cases + std::vector klotski::cases::Group_extend(RawCode raw_code, uint32_t reserve) { std::vector codes; // absl::flat_hash_map cases; // diff --git a/src/core/main.cc b/src/core/main.cc index aea5ca5..6c89039 100644 --- a/src/core/main.cc +++ b/src/core/main.cc @@ -46,39 +46,39 @@ int main() { const auto start = std::chrono::system_clock::now(); -// auto code = CommonCode::unsafe_create(0x1A9BF0C00).to_raw_code(); -// for (int i = 0; i < 100; ++i) { + auto code = CommonCode::unsafe_create(0x1A9BF0C00).to_raw_code(); + for (int i = 0; i < 100; ++i) { // FastCal fc {code}; // fc.solve(); // klotski::cases::Group_extend(code); -// FastCal_demo(code); -// break; -// } - - for (uint32_t type_id = 0; type_id < TYPE_ID_LIMIT; ++type_id) { - auto group_union = GroupUnion::unsafe_create(type_id); - for (uint32_t pattern_id = 0; pattern_id < group_union.pattern_num(); ++pattern_id) { - std::cout << std::format("[{}, {}]\n", type_id, pattern_id); - auto seed = CommonCode::unsafe_create(PATTERN_DATA[PATTERN_OFFSET[type_id] + pattern_id] >> 23); - - double coff = 1.0; - double last_val = -1; - while (true) { - auto val = Group_load_factor(seed.to_raw_code(), coff); - if (int(val * 1000) != int(last_val * 1000)) { - std::cout << std::format("{:.2f}, {:.6f}\n", coff, val); - last_val = val; - } - if (coff >= 2.0) { - break; - } - coff += 0.01; - } - std::cout << std::endl; - } - + FastCal_demo(code); + break; } +// for (uint32_t type_id = 0; type_id < TYPE_ID_LIMIT; ++type_id) { +// auto group_union = GroupUnion::unsafe_create(type_id); +// for (uint32_t pattern_id = 0; pattern_id < group_union.pattern_num(); ++pattern_id) { +// std::cout << std::format("[{}, {}]\n", type_id, pattern_id); +// auto seed = CommonCode::unsafe_create(PATTERN_DATA[PATTERN_OFFSET[type_id] + pattern_id] >> 23); +// +// double coff = 1.0; +// double last_val = -1; +// while (true) { +// auto val = Group_load_factor(seed.to_raw_code(), coff); +// if (int(val * 1000) != int(last_val * 1000)) { +// std::cout << std::format("{:.2f}, {:.6f}\n", coff, val); +// last_val = val; +// } +// if (coff >= 2.0) { +// break; +// } +// coff += 0.01; +// } +// std::cout << std::endl; +// } +// +// } + // std::cout << Group_load_factor(code, 0.5) << std::endl; // std::cout << Group_load_factor(code, 0.8) << std::endl; // std::cout << Group_load_factor(code, 1.0) << std::endl;