Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update cmake version, add new tests, add support for std::future<void> #38

Merged
merged 4 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
cmake_minimum_required(VERSION 3.19 FATAL_ERROR)

# ---- Project ----

Expand Down
2 changes: 1 addition & 1 deletion benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
cmake_minimum_required(VERSION 3.19 FATAL_ERROR)

project(ThreadPoolBenchmarks LANGUAGES CXX)

Expand Down
2 changes: 1 addition & 1 deletion documentation/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
cmake_minimum_required(VERSION 3.19 FATAL_ERROR)

project(ThreadPoolDocs)

Expand Down
2 changes: 1 addition & 1 deletion examples/mandelbrot/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
cmake_minimum_required(VERSION 3.19 FATAL_ERROR)

project(Mandelbrot LANGUAGES CXX)

Expand Down
15 changes: 13 additions & 2 deletions include/thread_pool/thread_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,12 @@ namespace dp {
auto task = [func = std::move(f), ... largs = std::move(args),
promise = std::move(promise)]() mutable {
try {
promise.set_value(func(largs...));
if constexpr (std::is_same_v<ReturnType, void>) {
func(largs...);
promise.set_value();
} else {
promise.set_value(func(largs...));
}
} catch (...) {
promise.set_exception(std::current_exception());
}
Expand All @@ -139,7 +144,13 @@ namespace dp {
auto task = [func = std::move(f), ... largs = std::move(args),
promise = shared_promise]() {
try {
promise->set_value(func(largs...));
if constexpr (std::is_same_v<ReturnType, void>) {
func(largs...);
promise->set_value();
} else {
promise->set_value(func(largs...));
}

} catch (...) {
promise->set_exception(std::current_exception());
}
Expand Down
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
cmake_minimum_required(VERSION 3.19 FATAL_ERROR)

project(ThreadPoolTests LANGUAGES CXX)

Expand Down
68 changes: 68 additions & 0 deletions test/source/thread_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <algorithm>
#include <iostream>
#include <numeric>
#include <random>
#include <string>
#include <thread>
Expand Down Expand Up @@ -47,6 +48,14 @@ TEST_CASE("Pass raw reference to pool") {
CHECK_EQ(x, 2);
}

TEST_CASE("Support enqueue with void return type") {
dp::thread_pool pool;
auto value = 8;
auto future = pool.enqueue([](int& x) { x *= 2; }, std::ref(value));
future.wait();
CHECK_EQ(value, 16);
}

TEST_CASE("Ensure input params are properly passed") {
dp::thread_pool pool(4);
constexpr auto total_tasks = 30;
Expand Down Expand Up @@ -245,3 +254,62 @@ TEST_CASE("Ensure work completes with fewer threads than expected.") {

CHECK_EQ(counter.load(), total_tasks);
}

void recursive_sequential_sum(std::atomic_int32_t& counter, int count, dp::thread_pool<>& pool) {
counter.fetch_add(count);
if (count > 1) {
pool.enqueue_detach(recursive_sequential_sum, std::ref(counter), count - 1, std::ref(pool));
}
}

TEST_CASE("Recursive enqueue calls work correctly") {
std::atomic_int32_t counter = 0;
constexpr auto start = 1000;
{
dp::thread_pool pool(4);
recursive_sequential_sum(counter, start, pool);
}

auto expected_sum = 0;
for (int i = 0; i <= start; i++) {
expected_sum += i;
}
CHECK_EQ(expected_sum, counter.load());
}

void recursive_parallel_sort(int* begin, int* end, int split_level, dp::thread_pool<>& pool) {
if (split_level < 2 || end - begin < 2) {
std::sort(begin, end);
} else {
const auto mid = begin + (end - begin) / 2;
if (split_level == 2) {
const auto future =
pool.enqueue(recursive_parallel_sort, begin, mid, split_level / 2, std::ref(pool));
std::sort(mid, end);
future.wait();
} else {
const auto left =
pool.enqueue(recursive_parallel_sort, begin, mid, split_level / 2, std::ref(pool));
const auto right =
pool.enqueue(recursive_parallel_sort, mid, end, split_level / 2, std::ref(pool));

left.wait();
right.wait();
}
std::inplace_merge(begin, mid, end);
}
}

TEST_CASE("Recursive parallel sort") {
std::vector<int> data(10000);
// std::ranges::iota is a C++23 feature
std::iota(data.begin(), data.end(), 0);
std::ranges::shuffle(data, std::mt19937{std::random_device{}()});

{
dp::thread_pool pool(4);
recursive_parallel_sort(data.data(), data.data() + data.size(), 4, pool);
}

CHECK(std::ranges::is_sorted(data));
}