mirror of https://github.com/dnomd343/klotski.git
Dnomd343
3 months ago
6 changed files with 191 additions and 89 deletions
@ -1,22 +0,0 @@ |
|||
#include <gtest/gtest.h> |
|||
|
|||
#include "cases.h" |
|||
|
|||
using klotski::cases::TYPE_ID_LIMIT; |
|||
|
|||
void type_id_parallel(std::function<void(uint32_t type_id)> &&func) { |
|||
|
|||
BS::thread_pool pool; |
|||
|
|||
// TODO: using `detach_sequence`
|
|||
for (uint32_t type_id = 0; type_id < TYPE_ID_LIMIT; ++type_id) { |
|||
|
|||
pool.detach_task([type_id, func = std::move(func)]() { |
|||
func(type_id); |
|||
}); |
|||
|
|||
} |
|||
|
|||
pool.wait(); |
|||
|
|||
} |
@ -0,0 +1,86 @@ |
|||
#include <BS_thread_pool.hpp> |
|||
|
|||
#include <iostream> |
|||
|
|||
#include "group/group.h" |
|||
#include "helper/parallel.h" |
|||
#include "all_cases/all_cases.h" |
|||
|
|||
using klotski::cases::AllCases; |
|||
using klotski::cases::TYPE_ID_LIMIT; |
|||
using klotski::cases::ALL_CASES_NUM_; |
|||
|
|||
void helper::type_id_parallel(std::function<void(uint32_t type_id)> &&func) { |
|||
|
|||
BS::thread_pool pool; |
|||
|
|||
// TODO: using `detach_sequence`
|
|||
for (uint32_t type_id = 0; type_id < TYPE_ID_LIMIT; ++type_id) { |
|||
|
|||
pool.detach_task([type_id, &func]() { |
|||
func(type_id); |
|||
}); |
|||
|
|||
} |
|||
|
|||
pool.wait(); |
|||
|
|||
} |
|||
|
|||
void helper::group_union_parallel(std::function<void(GroupUnion)> &&func) { |
|||
type_id_parallel([&func](uint32_t type_id) { |
|||
func(GroupUnion::unsafe_create(type_id)); |
|||
}); |
|||
} |
|||
|
|||
void helper::common_code_parallel(std::function<void(std::span<CommonCode>)> &&func) { |
|||
|
|||
static auto codes = AllCases::instance().fetch().codes(); |
|||
|
|||
BS::thread_pool pool; |
|||
|
|||
// TODO: enhance performance
|
|||
|
|||
pool.detach_blocks((uint64_t)0, codes.size(), [&func](auto start, auto end) { |
|||
|
|||
func(std::span<CommonCode> {codes.data() + start, end - start}); |
|||
|
|||
}, 16); |
|||
|
|||
pool.wait(); |
|||
|
|||
} |
|||
|
|||
void helper::raw_code_parallel(std::function<void(std::span<RawCode>)> &&func) { |
|||
|
|||
auto common_codes = AllCases::instance().fetch().codes(); |
|||
static auto codes = std::vector<RawCode> {common_codes.begin(), common_codes.end()}; |
|||
|
|||
BS::thread_pool pool; |
|||
pool.detach_blocks((uint64_t)0, codes.size(), [&func](auto start, auto end) { |
|||
|
|||
func(std::span<RawCode> {codes.data() + start, end - start}); |
|||
|
|||
}, 16); |
|||
|
|||
pool.wait(); |
|||
|
|||
} |
|||
|
|||
void helper::short_code_parallel(std::function<void(std::span<ShortCode>)> &&func) { |
|||
static auto codes = []() { |
|||
std::vector<uint32_t> v (klotski::codec::SHORT_CODE_LIMIT); |
|||
std::iota(v.begin(), v.end(), 0); |
|||
return v; |
|||
}(); |
|||
|
|||
BS::thread_pool pool; |
|||
pool.detach_blocks((uint64_t)0, codes.size(), [&func](auto start, auto end) { |
|||
|
|||
auto span = std::span<uint32_t> {codes.data() + start, end - start}; |
|||
func(std::bit_cast<std::span<ShortCode>>(span)); |
|||
|
|||
}, 16); |
|||
|
|||
pool.wait(); |
|||
} |
@ -0,0 +1,51 @@ |
|||
#pragma once |
|||
|
|||
#include <span> |
|||
|
|||
#include "raw_code/raw_code.h" |
|||
#include "short_code/short_code.h" |
|||
#include "common_code/common_code.h" |
|||
|
|||
using klotski::codec::RawCode; |
|||
using klotski::codec::ShortCode; |
|||
using klotski::codec::CommonCode; |
|||
|
|||
using klotski::cases::GroupUnion; |
|||
|
|||
namespace helper { |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
void type_id_parallel(std::function<void(uint32_t type_id)> &&func); |
|||
|
|||
void group_union_parallel(std::function<void(GroupUnion group_union)> &&func); |
|||
|
|||
#define TYPE_ID_PARALLEL(impl) \ |
|||
helper::type_id_parallel([](uint32_t type_id) {impl}) |
|||
|
|||
#define GROUP_UNION_PARALLEL(impl) \ |
|||
helper::group_union_parallel([](GroupUnion group_union) {impl}) |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
/// Spawn all valid RawCodes in parallel.
|
|||
void raw_code_parallel(std::function<void(std::span<RawCode>)> &&func); |
|||
|
|||
/// Spawn all valid ShortCodes in parallel.
|
|||
void short_code_parallel(std::function<void(std::span<ShortCode>)> &&func); |
|||
|
|||
/// Spawn all valid CommonCodes in parallel.
|
|||
void common_code_parallel(std::function<void(std::span<CommonCode>)> &&func); |
|||
|
|||
#define CODE_PARALLEL(Type, type, impl) \ |
|||
helper::type##_code_parallel([](std::span<Type##Code> codes) { \ |
|||
for (auto code : codes) {impl} \ |
|||
}) |
|||
|
|||
#define RAW_CODE_PARALLEL(impl) CODE_PARALLEL(Raw, raw, impl) |
|||
#define SHORT_CODE_PARALLEL(impl) CODE_PARALLEL(Short, short, impl) |
|||
#define COMMON_CODE_PARALLEL(impl) CODE_PARALLEL(Common, common, impl) |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
} // namespace helper
|
Loading…
Reference in new issue