Skip to content

Commit 7bedddd

Browse files
committed
Add std::execution::par supplementation
1 parent 7f841e7 commit 7bedddd

File tree

5 files changed

+99
-1
lines changed

5 files changed

+99
-1
lines changed

.github/workflows/tests.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ jobs:
7373
cmake --build build/ --config Debug
7474
cd build/tests
7575
ctest -C Debug --output-on-failure --verbose
76+
echo "Supplement Test:"
77+
./supplement_test || ./Debug/supplement_test.exe
7678
shell: bash
7779

7880
- name: Benchmark

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ int main() {
3737

3838
### Pool control
3939

40-
Use `poolstl::par_pool` with your own [thread pool](https://github.com/alugowski/task-thread-pool) to have full control over thread count, thread startup, shut down, etc.:
40+
Use `poolstl::par_pool` with your own [thread pool](https://github.com/alugowski/task-thread-pool) to have full control over thread count, thread startup/shutdown, etc.:
4141

4242
```c++
4343
task_thread_pool::task_thread_pool pool;
@@ -47,6 +47,20 @@ std::for_each(poolstl::par_pool(pool), v.cbegin(), v.cbegin(), [](auto) {});
4747
4848
The pool used by `poolstl::par` is managed internally by poolSTL. It is started on first use.
4949
50+
## poolSTL as `std::execution::par` substitute
51+
**USE AT YOUR OWN RISK!**
52+
53+
Two-line fix for missing compiler support. A no-op on compilers with support.
54+
55+
If `POOLSTL_STD_SUPPLEMENT` is defined and native support is not found then poolSTL will alias its `poolstl::par` as `std::execution::par`:
56+
57+
```c++
58+
#define POOLSTL_STD_SUPPLEMENT
59+
#include <poolstl/poolstl.hpp>
60+
```
61+
62+
Now just use `std::execution::par` as normal. See [supplement_test.cpp](tests/supplement_test.cpp).
63+
5064
## Implemented algorithms
5165
Algorithms are added on an as-needed basis. If you need one that is not present feel free to open an issue or submit a PR.
5266

include/poolstl/poolstl.hpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,38 @@
1818
#include "algorithm"
1919
#include "numeric"
2020

21+
/*
22+
* Optionally alias poolstl::par as std::execution::par to enable poolSTL to fill in for missing compiler support.
23+
*
24+
* USE AT YOUR OWN RISK!
25+
*
26+
* To do this define POOLSTL_STD_SUPPLEMENT before including poolstl.hpp.
27+
*
28+
* This aliasing will not happen if native support exists. If this autodetection fails for you:
29+
* - define POOLSTL_ALLOW_SUPPLEMENT=0 to disable
30+
* - define POOLSTL_FORCE_SUPPLEMENT to force enable (use with great care!)
31+
*/
32+
#ifndef POOLSTL_ALLOW_SUPPLEMENT
33+
#define POOLSTL_ALLOW_SUPPLEMENT 1
34+
#endif
35+
36+
#if POOLSTL_ALLOW_SUPPLEMENT && defined(POOLSTL_STD_SUPPLEMENT)
37+
38+
#if __cplusplus >= 201603L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201603L)
39+
#if __has_include(<execution>)
40+
#include <execution>
41+
#endif
42+
#endif
43+
44+
#if !defined(__cpp_lib_parallel_algorithm) || defined(POOLSTL_FORCE_SUPPLEMENT)
45+
namespace std {
46+
namespace execution {
47+
using ::poolstl::execution::parallel_policy;
48+
using ::poolstl::execution::par;
49+
}
50+
}
51+
52+
#endif
53+
#endif
54+
2155
#endif

tests/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,19 @@ catch_discover_tests(poolstl_test)
4545
add_executable(cpp11_test cpp11_test.cpp)
4646
target_link_libraries(cpp11_test PUBLIC poolSTL::poolSTL)
4747
target_compile_features(cpp11_test PUBLIC cxx_std_11)
48+
49+
# Test std::execution supplementation
50+
# This uses only std::execution::par. On compilers with support this should result in a poolSTL no-op,
51+
# and on ones without support it should use fallback to poolstl::par.
52+
add_executable(supplement_test supplement_test.cpp)
53+
target_link_libraries(supplement_test PUBLIC poolSTL::poolSTL)
54+
target_compile_features(supplement_test PUBLIC cxx_std_17)
55+
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
56+
# GCC and Clang require TBB for std::execution::par
57+
find_package(TBB)
58+
if (TBB_FOUND)
59+
target_link_libraries(supplement_test PUBLIC TBB::tbb)
60+
else()
61+
message("No TBB")
62+
endif()
63+
endif()

tests/supplement_test.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (C) 2023 Adam Lugowski. All rights reserved.
2+
// Use of this source code is governed by:
3+
// the BSD 2-clause license, the MIT license, or at your choosing the BSL-1.0 license found in the LICENSE.*.txt files.
4+
// SPDX-License-Identifier: BSD-2-Clause OR MIT OR BSL-1.0
5+
6+
#include <iostream>
7+
#include <numeric>
8+
9+
// The <execution> header defines compiler-provided execution policies, but is not always present.
10+
#if __cplusplus >= 201603L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201603L)
11+
#if __has_include(<execution>)
12+
#include <execution>
13+
#endif
14+
#endif
15+
16+
#define POOLSTL_STD_SUPPLEMENT
17+
#include <poolstl/poolstl.hpp>
18+
19+
int main() {
20+
if (std::is_same_v<std::execution::parallel_policy, poolstl::execution::parallel_policy>) {
21+
std::cout << "Using poolSTL supplement" << std::endl;
22+
} else {
23+
std::cout << "Using native par" << std::endl;
24+
}
25+
26+
std::vector<int> v = {0, 1, 2, 3, 4, 5};
27+
std::for_each(std::execution::par, v.cbegin(), v.cend(), [](int x) {
28+
std::cout << x << " ";
29+
});
30+
std::cout << std::endl;
31+
return 0;
32+
}

0 commit comments

Comments
 (0)