Browse Source

build: update tiny thread pool

master
Dnomd343 2 years ago
parent
commit
761eb08481
  1. 46
      test/CMakeLists.txt
  2. 2
      test/global/common_code.cc
  3. 21
      test/global/global_utils.cc
  4. 44
      test/global/global_utils.h
  5. 2
      test/global/raw_code.cc
  6. 2
      test/global/short_code.cc
  7. 2
      third_party/tiny_thread_pool

46
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)
################################################################################
#######################################################################################

2
test/global/common_code.cc

@ -18,7 +18,7 @@ std::vector<uint64_t> 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<std::future<std::vector<uint64_t>>> futures;
for (const auto &range : range_split(0, 0x10'0000'0000, 0x10'0000)) {
futures.emplace_back(

21
test/global/global_utils.cc

@ -1,21 +0,0 @@
#include <thread>
#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
}

44
test/global/global_utils.h

@ -1,44 +1,18 @@
#pragma once
#include <future>
#include <vector>
#include <functional>
#include "tiny_pool.h"
uint32_t thread_num();
typedef std::vector<std::pair<uint64_t, uint64_t>> 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<std::function<void()>*>(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 <typename Func, typename ...Args>
auto submit(Func &&func, Args &&...args) -> std::future<decltype(func(args...))> { // submit new task
std::function<decltype(func(args...))()> wrap_func = std::bind(
std::forward<Func>(func), std::forward<Args>(args)... // wrap as a function without params
);
auto func_ptr = std::make_shared<
std::packaged_task<decltype(func(args...))()> // function task as shared ptr
>(wrap_func);
tiny_pool_submit(pool, TinyPool::wrap_c_func, (void*)( // submit with thread pool interface
new std::function<void()> (
[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;
}

2
test/global/raw_code.cc

@ -47,7 +47,7 @@ std::vector<uint64_t> 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<std::future<std::vector<uint64_t>>> futures;
for (const auto &range : range_split(0, 0x10'0000'0000, 0x10'0000)) {
futures.emplace_back(

2
test/global/short_code.cc

@ -19,7 +19,7 @@ std::vector<uint64_t> 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<std::future<std::vector<uint64_t>>> futures;
for (const auto &range : range_split(0, klotski::ALL_CASES_SIZE_SUM, 10000)) {
futures.emplace_back(

2
third_party/tiny_thread_pool

@ -1 +1 @@
Subproject commit 755faf4116ecd046b1a9278e292ea91ba8ef8e7c
Subproject commit 40068b99d6406ec0d0418773f0080c2ee247f165
Loading…
Cancel
Save