Browse Source

test: perf parallel spawn function

master
Dnomd343 2 months ago
parent
commit
f5487bd88f
  1. 12
      src/core_test/codec/common_code.cc
  2. 16
      src/core_test/codec/helper/codec.h
  3. 14
      src/core_test/codec/raw_code.cc
  4. 12
      src/core_test/codec/short_code.cc

12
src/core_test/codec/common_code.cc

@ -198,8 +198,7 @@ TEST(CommonCode, code_string) {
} }
TEST(CommonCode, DISABLED_global_verify) { TEST(CommonCode, DISABLED_global_verify) {
BS::thread_pool pool; const auto result = parallel_spawn(0x10'0000'0000ULL, [](uint64_t start, uint64_t end) {
auto futures = pool.submit_blocks(0ULL, 0x10'0000'0000ULL, [](auto start, auto end) {
std::vector<uint64_t> codes; std::vector<uint64_t> codes;
for (uint64_t common_code = start; common_code < end; ++common_code) { // brute-force search for (uint64_t common_code = start; common_code < end; ++common_code) { // brute-force search
if (CommonCode::check(common_code)) { if (CommonCode::check(common_code)) {
@ -207,13 +206,6 @@ TEST(CommonCode, DISABLED_global_verify) {
} }
} }
return codes; return codes;
}, 0x1000); // split as 4096 pieces });
std::vector<uint64_t> result;
result.reserve(ALL_CASES_NUM_);
for (auto &future : futures) {
const auto data = future.get();
result.insert(result.end(), data.begin(), data.end()); // combine sections
}
EXPECT_EQ(result, all_common_codes()); EXPECT_EQ(result, all_common_codes());
} }

16
src/core_test/codec/helper/codec.h

@ -36,3 +36,19 @@ void common_code_parallel(std::function<void(std::span<CommonCode>)> &&func);
#define COMMON_CODE_PARALLEL(impl) CODE_PARALLEL(Common, common, impl) #define COMMON_CODE_PARALLEL(impl) CODE_PARALLEL(Common, common, impl)
// ----------------------------------------------------------------------------------------- // // ----------------------------------------------------------------------------------------- //
/// Calculate multiple ranges separately and combine the results.
template<typename T, typename F>
requires std::is_integral_v<T> && std::is_invocable_v<F, T, T>
auto parallel_spawn(T limit, F &&func) -> std::invoke_result_t<F, T, T> {
BS::thread_pool pool;
std::invoke_result_t<F, T, T> result;
for (auto &&future : pool.submit_blocks((T)0, limit, func, 0x1000)) {
const auto data = future.get();
result.insert(result.end(), std::begin(data), std::end(data)); // combine sections
}
pool.wait();
return result;
}
// ----------------------------------------------------------------------------------------- //

14
src/core_test/codec/raw_code.cc

@ -165,22 +165,14 @@ TEST(RawCode, DISABLED_global_verify) {
return raw_code; return raw_code;
}; };
BS::thread_pool pool; const auto result = parallel_spawn(0x10'0000'0000ULL, [](uint64_t start, uint64_t end) {
auto futures = pool.submit_blocks(0ULL, 0x10'0000'0000ULL, [](auto start, auto end) {
std::vector<uint64_t> codes; std::vector<uint64_t> codes;
for (uint64_t common_code = start; common_code < end; ++common_code) { for (uint64_t common_code = start; common_code < end; ++common_code) {
if (RawCode::check(force_convert(common_code))) { if (RawCode::check(force_convert(common_code))) {
codes.emplace_back(common_code); // store valid raw code codes.emplace_back(common_code); // store valid code
} }
} }
return codes; return codes;
}, 0x1000); // split as 4096 pieces });
std::vector<uint64_t> result;
result.reserve(ALL_CASES_NUM_);
for (auto &future : futures) {
const auto data = future.get();
result.insert(result.end(), data.begin(), data.end()); // combine sections
}
EXPECT_EQ(result, all_common_codes()); EXPECT_EQ(result, all_common_codes());
} }

12
src/core_test/codec/short_code.cc

@ -222,8 +222,7 @@ TEST(ShortCode, code_string) {
TEST(ShortCode, DISABLED_global_verify) { TEST(ShortCode, DISABLED_global_verify) {
speed_up_reset(); speed_up_reset();
BS::thread_pool pool; const auto result = parallel_spawn(SHORT_CODE_LIMIT, [](uint32_t start, uint32_t end) {
auto futures = pool.submit_blocks(0U, SHORT_CODE_LIMIT, [](auto start, auto end) {
std::vector<uint64_t> codes; std::vector<uint64_t> codes;
codes.reserve(end - start); codes.reserve(end - start);
for (uint32_t short_code = start; short_code < end; ++short_code) { for (uint32_t short_code = start; short_code < end; ++short_code) {
@ -232,13 +231,6 @@ TEST(ShortCode, DISABLED_global_verify) {
codes.emplace_back(common_code.unwrap()); codes.emplace_back(common_code.unwrap());
} }
return codes; return codes;
}, 0x1000); // split as 4096 pieces });
std::vector<uint64_t> result;
result.reserve(ALL_CASES_NUM_);
for (auto &future : futures) {
const auto data = future.get();
result.insert(result.end(), data.begin(), data.end()); // combine sections
}
EXPECT_EQ(result, all_common_codes()); EXPECT_EQ(result, all_common_codes());
} }

Loading…
Cancel
Save