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

Separate out variant_policy #25

Merged
merged 1 commit into from
Dec 14, 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
Expand Up @@ -29,7 +29,7 @@ set(HEADER_FILES
include/poolstl/algorithm
include/poolstl/execution
include/poolstl/numeric
include/poolstl/seq_fwd.hpp
include/poolstl/variant_policy.hpp
include/poolstl/iota_iter.hpp
include/poolstl/internal/utils.hpp
include/poolstl/internal/ttp_impl.hpp
Expand Down
47 changes: 0 additions & 47 deletions include/poolstl/execution
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
#include "internal/task_thread_pool.hpp"
#include "internal/utils.hpp"

#if POOLSTL_HAVE_CXX17
#include <variant>
#endif

namespace poolstl {

namespace ttp = task_thread_pool;
Expand Down Expand Up @@ -124,27 +120,6 @@ namespace poolstl {
bool par_ok = true;
};


#if POOLSTL_HAVE_CXX17
/**
* A policy that allows selecting a policy at runtime.
*
* @tparam Variant std::variant<> of policy options.
*/
template <typename Variant>
struct variant_policy {
explicit variant_policy(const Variant& policy): var(policy) {}
Variant var;
};

namespace internal {
using poolstl_policy_variant = std::variant<
poolstl::execution::parallel_policy,
poolstl::execution::sequenced_policy>;
}

#endif

/**
* Choose parallel or sequential at runtime.
*
Expand Down Expand Up @@ -180,9 +155,6 @@ namespace poolstl {
using execution::seq;
using execution::par;
using execution::par_if;
#if POOLSTL_HAVE_CXX17
using execution::variant_policy;
#endif

namespace internal {
/**
Expand Down Expand Up @@ -223,25 +195,6 @@ namespace poolstl {
template <class ExecPolicy>
using is_pure_threads_policy = std::is_same<poolstl::execution::pure_threads_policy,
typename std::remove_cv<typename std::remove_reference<ExecPolicy>::type>::type>;

#if POOLSTL_HAVE_CXX17
/**
* Helper for enable_if_poolstl_variant
*/
template <typename T> struct is_poolstl_variant_policy : std::false_type {};
template <typename V> struct is_poolstl_variant_policy<
::poolstl::execution::variant_policy<V>> :std::true_type {};

/**
* To enable/disable variant_policy (for par_if) overload resolution
*/
template <class ExecPolicy, class Tp>
using enable_if_poolstl_variant =
typename std::enable_if<
is_poolstl_variant_policy<
typename std::remove_cv<typename std::remove_reference<ExecPolicy>::type>::type>::value,
Tp>::type;
#endif
}
}

Expand Down
1 change: 0 additions & 1 deletion include/poolstl/poolstl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "execution"
#include "algorithm"
#include "numeric"
#include "seq_fwd.hpp"

// Note that iota_iter.hpp is self-contained in its own right.
#include "iota_iter.hpp"
Expand Down
48 changes: 46 additions & 2 deletions include/poolstl/seq_fwd.hpp → include/poolstl/variant_policy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,55 @@
// the BSD 2-clause license, the MIT license, or at your choosing the BSL-1.0 license found in the LICENSE.*.txt files.
// SPDX-License-Identifier: BSD-2-Clause OR MIT OR BSL-1.0

#ifndef POOLSTL_SEQ_FWD_HPP
#define POOLSTL_SEQ_FWD_HPP
#ifndef POOLSTL_VARIANT_POLICY_HPP
#define POOLSTL_VARIANT_POLICY_HPP

#include "execution"

#include <variant>

namespace poolstl {
namespace execution {
/**
* A policy that allows selecting a policy at runtime.
*
* @tparam Variant std::variant<> of policy options.
*/
template <typename Variant>
struct variant_policy {
explicit variant_policy(const Variant& policy): var(policy) {}
Variant var;
};

namespace internal {
using poolstl_policy_variant = std::variant<
poolstl::execution::parallel_policy,
poolstl::execution::sequenced_policy>;
}
}

using execution::variant_policy;

namespace internal {
/**
* Helper for enable_if_poolstl_variant
*/
template <typename T> struct is_poolstl_variant_policy : std::false_type {};
template <typename V> struct is_poolstl_variant_policy<
::poolstl::execution::variant_policy<V>> :std::true_type {};

/**
* To enable/disable variant_policy (for par_if) overload resolution
*/
template <class ExecPolicy, class Tp>
using enable_if_poolstl_variant =
typename std::enable_if<
is_poolstl_variant_policy<
typename std::remove_cv<typename std::remove_reference<ExecPolicy>::type>::type>::value,
Tp>::type;
}
}

/*
* Forward poolstl::seq to the native sequential (no policy) method.
*/
Expand Down
17 changes: 12 additions & 5 deletions tests/supplement_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// SPDX-License-Identifier: BSD-2-Clause OR MIT OR BSL-1.0

#include <iostream>
#include <numeric>
#include <string>
#include <variant>

// The <execution> header defines compiler-provided execution policies, but is not always present.
Expand All @@ -21,6 +21,10 @@
#define POOLSTL_STD_SUPPLEMENT
#include <poolstl/poolstl.hpp>

#ifndef POOLSTL_AMALGAM
// Test poolstl::variant_policy.
// Not bundled with the amalgam.
#include <poolstl/variant_policy.hpp>
using std_policy_variant = std::variant<std::execution::parallel_policy, std::execution::sequenced_policy>;

/**
Expand All @@ -33,6 +37,7 @@ poolstl::variant_policy<std_policy_variant> std_par_if(bool call_par) {
return poolstl::variant_policy(std_policy_variant(std::execution::seq));
}
}
#endif

int main() {
if (std::is_same_v<std::execution::parallel_policy, poolstl::execution::parallel_policy>) {
Expand All @@ -45,24 +50,26 @@ int main() {
std::for_each(std::execution::seq, v.cbegin(), v.cend(), [](int x) {
std::cout << x;
});
std::cout << std::endl;
std::cout << " std::execution::seq" << std::endl;

std::for_each(std::execution::par, v.cbegin(), v.cend(), [](int x) {
std::cout << x;
});
std::cout << std::endl;
std::cout << " std::execution::par" << std::endl;

std::for_each(std::execution::par_unseq, v.cbegin(), v.cend(), [](int x) {
std::cout << x;
});
std::cout << std::endl;
std::cout << " std::execution::par_unseq" << std::endl;

#ifndef POOLSTL_AMALGAM
for (bool is_parallel : {true, false}) {
std::for_each(std_par_if(is_parallel), v.cbegin(), v.cend(), [](int x) {
std::cout << x;
});
std::cout << std::endl;
std::cout << " std_par_if(" << std::to_string(is_parallel) << ")" << std::endl;
}
#endif

return 0;
}
4 changes: 4 additions & 0 deletions tools/amalgamate_header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,7 @@
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

#ifndef POOLSTL_AMALGAM
#define POOLSTL_AMALGAM
#endif