-
Notifications
You must be signed in to change notification settings - Fork 2
/
supplement_test.cpp
74 lines (63 loc) · 2.58 KB
/
supplement_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (C) 2023 Adam Lugowski. All rights reserved.
// Use of this source code is governed by:
// 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
#include <iostream>
#include <string>
#include <variant>
// The <execution> header defines compiler-provided execution policies, but is not always present.
#if __cplusplus >= 201603L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201603L)
#if __has_include(<execution>)
#ifndef POOLSTL_STD_SUPPLEMENT_NO_INCLUDE
// On GCC and Clang simply including <execution> requires linking with TBB.
// MinGW does not require TBB, but performance may be sequential. To keep parallel performance fallback to poolSTL.
#include <execution>
#endif
#endif
#endif
#define POOLSTL_STD_SUPPLEMENT
#include <poolstl/poolstl.hpp>
#if __has_include(<poolstl/variant_policy.hpp>)
// Test poolstl::variant_policy (independent header, not bundled with the amalgam).
#include <poolstl/variant_policy.hpp>
using std_policy_variant = std::variant<std::execution::parallel_policy, std::execution::sequenced_policy>;
/**
* A version of poolstl::par_if that works on the std::execution policies.
*/
poolstl::variant_policy<std_policy_variant> std_par_if(bool call_par) {
if (call_par) {
return poolstl::variant_policy(std_policy_variant(std::execution::par));
} else {
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>) {
std::cout << "Using poolSTL supplement" << std::endl;
} else {
std::cout << "Using native par" << std::endl;
}
std::vector<int> v = {0, 1, 2, 3, 4, 5};
std::for_each(std::execution::seq, v.cbegin(), v.cend(), [](int x) {
std::cout << x;
});
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::execution::par" << std::endl;
std::for_each(std::execution::par_unseq, v.cbegin(), v.cend(), [](int x) {
std::cout << x;
});
std::cout << " std::execution::par_unseq" << std::endl;
#if __has_include(<poolstl/variant_policy.hpp>)
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_par_if(" << std::to_string(is_parallel) << ")" << std::endl;
}
#endif
return 0;
}