From 761eb0848169e5752404b483acca122db93ff15f Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Thu, 13 Apr 2023 15:39:05 +0800 Subject: [PATCH] build: update tiny thread pool --- test/CMakeLists.txt | 46 +++++++++++++++++++++--------------- test/global/common_code.cc | 2 +- test/global/global_utils.cc | 21 ---------------- test/global/global_utils.h | 44 +++++++--------------------------- test/global/raw_code.cc | 2 +- test/global/short_code.cc | 2 +- third_party/tiny_thread_pool | 2 +- 7 files changed, 40 insertions(+), 79 deletions(-) delete mode 100644 test/global/global_utils.cc diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 1c03aa4..922a972 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,25 +1,23 @@ cmake_minimum_required(VERSION 3.0) -################################################################################ +####################################################################################### enable_testing() set(TEST_DEPS gtest gtest_main klotski) -################################################################################ +####################################################################################### unset(CMAKE_ARCHIVE_OUTPUT_DIRECTORY) include_directories(../third_party/md5) add_library(md5 STATIC ../third_party/md5/md5.cpp) -################################################################################ +####################################################################################### unset(CMAKE_ARCHIVE_OUTPUT_DIRECTORY) include_directories(../third_party/tiny_thread_pool) -add_library(tiny_pool STATIC ../third_party/tiny_thread_pool/tiny_pool.c) +add_library(tiny_pool STATIC ../third_party/tiny_thread_pool/tiny_pool/tiny_pool.c) -# TODO: tiny_pool as cpp module - -################################################################################ +####################################################################################### include_directories(../src/klotski_core) include_directories(../src/klotski_core/utils) @@ -32,7 +30,7 @@ include_directories(../src/klotski_core/common_code) include_directories(../src/klotski_core/core) include_directories(../src/klotski_core/group) -################################################################################ +####################################################################################### set(TEST_BASIC_SRC basic/utils.cc @@ -42,7 +40,7 @@ add_executable(test_basic ${TEST_BASIC_SRC}) target_link_libraries(test_basic PUBLIC ${TEST_DEPS} md5) add_test(NAME basic COMMAND test_basic) -################################################################################ +####################################################################################### set(TEST_CODEC_SRC codec/short_code.cc @@ -53,14 +51,14 @@ add_executable(test_codec ${TEST_CODEC_SRC}) target_link_libraries(test_codec ${TEST_DEPS}) add_test(NAME codec COMMAND test_codec) -################################################################################ +####################################################################################### set(TEST_CORE_SRC core/core.cc) add_executable(test_core ${TEST_CORE_SRC}) target_link_libraries(test_core ${TEST_DEPS} absl::flat_hash_map) add_test(NAME core COMMAND test_core) -################################################################################ +####################################################################################### set(TEST_GROUP_SRC group/block_num.cc @@ -70,7 +68,7 @@ add_executable(test_group ${TEST_GROUP_SRC}) target_link_libraries(test_group ${TEST_DEPS} md5 absl::flat_hash_map) add_test(NAME group COMMAND test_group) -################################################################################ +####################################################################################### set(TEST_FFI_SRC ffi/metadata.cc @@ -81,15 +79,25 @@ add_executable(test_ffi ${TEST_FFI_SRC}) target_link_libraries(test_ffi ${TEST_DEPS}) add_test(NAME ffi COMMAND test_ffi) -################################################################################ +####################################################################################### + +set(TEST_CODEC_GLOBAL_SRC + global/short_code.cc + global/common_code.cc + global/raw_code.cc +) + +add_executable(test_codec_global ${TEST_CODEC_GLOBAL_SRC}) +target_link_libraries(test_codec_global ${TEST_DEPS} tiny_pool) +add_test(NAME codec_global COMMAND test_codec_global) # TODO: update cmake configure -add_library(test_global_utils STATIC global/global_utils.cc) -set(TEST_GLOBAL_DEPS ${TEST_DEPS} test_global_utils tiny_pool) +#add_library(test_global_utils STATIC global/global_utils.cc) +#set(TEST_GLOBAL_DEPS ${TEST_DEPS} test_global_utils tiny_pool) -add_executable(test_codec_global global/short_code.cc global/common_code.cc global/raw_code.cc) -target_link_libraries(test_codec_global ${TEST_GLOBAL_DEPS}) -add_test(NAME codec_global COMMAND test_codec_global) +#add_executable(test_codec_global global/short_code.cc global/common_code.cc global/raw_code.cc) +#target_link_libraries(test_codec_global ${TEST_GLOBAL_DEPS}) +#add_test(NAME codec_global COMMAND test_codec_global) -################################################################################ +####################################################################################### diff --git a/test/global/common_code.cc b/test/global/common_code.cc index c095144..78d09ec 100644 --- a/test/global/common_code.cc +++ b/test/global/common_code.cc @@ -18,7 +18,7 @@ std::vector common_code_search(uint64_t start, uint64_t end) { TEST(GLOBAL, common_code) { /// create common code check tasks - auto pool = TinyPool(thread_num()); + auto pool = TinyPool(); std::vector>> futures; for (const auto &range : range_split(0, 0x10'0000'0000, 0x10'0000)) { futures.emplace_back( diff --git a/test/global/global_utils.cc b/test/global/global_utils.cc deleted file mode 100644 index d2d5803..0000000 --- a/test/global/global_utils.cc +++ /dev/null @@ -1,21 +0,0 @@ -#include -#include "global_utils.h" - -const static uint32_t DEFAULT_THREAD_NUM = 1; - -range_split_t range_split(uint64_t start, uint64_t end, uint64_t size) { - uint64_t rear = end - (end - start) % size; // (rear - start) % size == 0 - range_split_t ranges; - for (uint64_t i = start; i < rear; i += size) { - ranges.emplace_back(i, i + size); - } - if (rear != end) { - ranges.emplace_back(rear, end); - } - return ranges; -} - -uint32_t thread_num() { - auto num = std::thread::hardware_concurrency(); // CPU core number - return (num == 0) ? DEFAULT_THREAD_NUM : num; // fetch failed -> use default number -} diff --git a/test/global/global_utils.h b/test/global/global_utils.h index 602af02..e549fd8 100644 --- a/test/global/global_utils.h +++ b/test/global/global_utils.h @@ -1,44 +1,18 @@ #pragma once -#include #include -#include #include "tiny_pool.h" -uint32_t thread_num(); - typedef std::vector> range_split_t; -range_split_t range_split(uint64_t start, uint64_t end, uint64_t size); -class TinyPool { // OOP for tiny_thread_pool - pool_t *pool; - static void wrap_c_func(void *func) { // wrap std::function as c-style function ptr - auto func_ptr = static_cast*>(func); - (*func_ptr)(); - delete func_ptr; // free lambda function +inline range_split_t range_split(uint64_t start, uint64_t end, uint64_t size) { + uint64_t rear = end - (end - start) % size; // (rear - start) % size == 0 + range_split_t ranges; + for (uint64_t i = start; i < rear; i += size) { + ranges.emplace_back(i, i + size); } - -public: - void boot() { tiny_pool_boot(pool); } - void join() { tiny_pool_join(pool); } - void kill() { tiny_pool_kill(pool); } - void detach() { tiny_pool_detach(pool); } - explicit TinyPool(uint32_t size) { pool = tiny_pool_create(size); } - // TODO: thread pool destroy - - template - auto submit(Func &&func, Args &&...args) -> std::future { // submit new task - std::function wrap_func = std::bind( - std::forward(func), std::forward(args)... // wrap as a function without params - ); - auto func_ptr = std::make_shared< - std::packaged_task // function task as shared ptr - >(wrap_func); - tiny_pool_submit(pool, TinyPool::wrap_c_func, (void*)( // submit with thread pool interface - new std::function ( - [func_ptr]() { (*func_ptr)(); } // create lambda for running task - ) - )); - return func_ptr->get_future(); // return future object + if (rear != end) { + ranges.emplace_back(rear, end); } -}; + return ranges; +} diff --git a/test/global/raw_code.cc b/test/global/raw_code.cc index e165aa0..bf23aa1 100644 --- a/test/global/raw_code.cc +++ b/test/global/raw_code.cc @@ -47,7 +47,7 @@ std::vector raw_code_search(uint64_t start, uint64_t end) { TEST(GLOBAL, raw_code) { /// create raw code check tasks - auto pool = TinyPool(thread_num()); + auto pool = TinyPool(); std::vector>> futures; for (const auto &range : range_split(0, 0x10'0000'0000, 0x10'0000)) { futures.emplace_back( diff --git a/test/global/short_code.cc b/test/global/short_code.cc index eb026fc..127ebef 100644 --- a/test/global/short_code.cc +++ b/test/global/short_code.cc @@ -19,7 +19,7 @@ std::vector short_code_check(uint32_t start, uint32_t end) { void short_code_verify() { /// create short code check tasks - auto pool = TinyPool(thread_num()); + auto pool = TinyPool(); std::vector>> futures; for (const auto &range : range_split(0, klotski::ALL_CASES_SIZE_SUM, 10000)) { futures.emplace_back( diff --git a/third_party/tiny_thread_pool b/third_party/tiny_thread_pool index 755faf4..40068b9 160000 --- a/third_party/tiny_thread_pool +++ b/third_party/tiny_thread_pool @@ -1 +1 @@ -Subproject commit 755faf4116ecd046b1a9278e292ea91ba8ef8e7c +Subproject commit 40068b99d6406ec0d0418773f0080c2ee247f165