1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef _LIBCPP___PSTL_BACKENDS_STD_THREAD_H
10#define _LIBCPP___PSTL_BACKENDS_STD_THREAD_H
11
12#include <__config>
13#include <__cstddef/size_t.h>
14#include <__optional/optional.h>
15#include <__pstl/backend_fwd.h>
16#include <__pstl/cpu_algos/any_of.h>
17#include <__pstl/cpu_algos/cpu_traits.h>
18#include <__pstl/cpu_algos/fill.h>
19#include <__pstl/cpu_algos/find_if.h>
20#include <__pstl/cpu_algos/for_each.h>
21#include <__pstl/cpu_algos/merge.h>
22#include <__pstl/cpu_algos/mismatch.h>
23#include <__pstl/cpu_algos/reverse.h>
24#include <__pstl/cpu_algos/search.h>
25#include <__pstl/cpu_algos/search_n.h>
26#include <__pstl/cpu_algos/stable_sort.h>
27#include <__pstl/cpu_algos/transform.h>
28#include <__pstl/cpu_algos/transform_reduce.h>
29#include <__utility/empty.h>
30#include <__utility/move.h>
31
32#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
33# pragma GCC system_header
34#endif
35
36_LIBCPP_PUSH_MACROS
37#include <__undef_macros>
38
39#if _LIBCPP_STD_VER >= 17
40
41_LIBCPP_BEGIN_NAMESPACE_STD
42namespace __pstl {
43
44//
45// This partial backend implementation is for testing purposes only and not meant for production use. This will be
46// replaced by a proper implementation once the PSTL implementation is somewhat stable.
47//
48// This is intended to be used on top of the "default backend".
49//
50
51template <>
52struct __cpu_traits<__std_thread_backend_tag> {
53 template <class _RandomAccessIterator, class _Fp>
54 _LIBCPP_HIDE_FROM_ABI static optional<__empty>
55 __for_each(_RandomAccessIterator __first, _RandomAccessIterator __last, _Fp __f) {
56 __f(__first, __last);
57 return __empty{};
58 }
59
60 template <class _Index, class _UnaryOp, class _Tp, class _BinaryOp, class _Reduce>
61 _LIBCPP_HIDE_FROM_ABI static optional<_Tp>
62 __transform_reduce(_Index __first, _Index __last, _UnaryOp, _Tp __init, _BinaryOp, _Reduce __reduce) {
63 return __reduce(std::move(__first), std::move(__last), std::move(__init));
64 }
65
66 template <class _RandomAccessIterator, class _Compare, class _LeafSort>
67 _LIBCPP_HIDE_FROM_ABI static optional<__empty>
68 __stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp, _LeafSort __leaf_sort) {
69 __leaf_sort(__first, __last, __comp);
70 return __empty{};
71 }
72
73 _LIBCPP_HIDE_FROM_ABI static void __cancel_execution() {}
74
75 template <class _RandomAccessIterator1,
76 class _RandomAccessIterator2,
77 class _RandomAccessIterator3,
78 class _Compare,
79 class _LeafMerge>
80 _LIBCPP_HIDE_FROM_ABI static optional<__empty>
81 __merge(_RandomAccessIterator1 __first1,
82 _RandomAccessIterator1 __last1,
83 _RandomAccessIterator2 __first2,
84 _RandomAccessIterator2 __last2,
85 _RandomAccessIterator3 __outit,
86 _Compare __comp,
87 _LeafMerge __leaf_merge) {
88 __leaf_merge(__first1, __last1, __first2, __last2, __outit, __comp);
89 return __empty{};
90 }
91
92 static constexpr size_t __lane_size = 64;
93};
94
95// Mandatory implementations of the computational basis
96template <class _ExecutionPolicy>
97struct __find_if<__std_thread_backend_tag, _ExecutionPolicy>
98 : __cpu_parallel_find_if<__std_thread_backend_tag, _ExecutionPolicy> {};
99
100template <class _ExecutionPolicy>
101struct __for_each<__std_thread_backend_tag, _ExecutionPolicy>
102 : __cpu_parallel_for_each<__std_thread_backend_tag, _ExecutionPolicy> {};
103
104template <class _ExecutionPolicy>
105struct __merge<__std_thread_backend_tag, _ExecutionPolicy>
106 : __cpu_parallel_merge<__std_thread_backend_tag, _ExecutionPolicy> {};
107
108template <class _ExecutionPolicy>
109struct __mismatch<__std_thread_backend_tag, _ExecutionPolicy>
110 : __cpu_parallel_mismatch<__std_thread_backend_tag, _ExecutionPolicy> {};
111
112template <class _ExecutionPolicy>
113struct __reverse<__std_thread_backend_tag, _ExecutionPolicy>
114 : __cpu_parallel_reverse<__std_thread_backend_tag, _ExecutionPolicy> {};
115
116template <class _ExecutionPolicy>
117struct __search<__std_thread_backend_tag, _ExecutionPolicy>
118 : __cpu_parallel_search<__std_thread_backend_tag, _ExecutionPolicy> {};
119
120template <class _ExecutionPolicy>
121struct __search_n<__std_thread_backend_tag, _ExecutionPolicy>
122 : __cpu_parallel_search_n<__std_thread_backend_tag, _ExecutionPolicy> {};
123
124template <class _ExecutionPolicy>
125struct __stable_sort<__std_thread_backend_tag, _ExecutionPolicy>
126 : __cpu_parallel_stable_sort<__std_thread_backend_tag, _ExecutionPolicy> {};
127
128template <class _ExecutionPolicy>
129struct __transform<__std_thread_backend_tag, _ExecutionPolicy>
130 : __cpu_parallel_transform<__std_thread_backend_tag, _ExecutionPolicy> {};
131
132template <class _ExecutionPolicy>
133struct __transform_binary<__std_thread_backend_tag, _ExecutionPolicy>
134 : __cpu_parallel_transform_binary<__std_thread_backend_tag, _ExecutionPolicy> {};
135
136template <class _ExecutionPolicy>
137struct __transform_reduce<__std_thread_backend_tag, _ExecutionPolicy>
138 : __cpu_parallel_transform_reduce<__std_thread_backend_tag, _ExecutionPolicy> {};
139
140template <class _ExecutionPolicy>
141struct __transform_reduce_binary<__std_thread_backend_tag, _ExecutionPolicy>
142 : __cpu_parallel_transform_reduce_binary<__std_thread_backend_tag, _ExecutionPolicy> {};
143
144// Not mandatory, but better optimized
145template <class _ExecutionPolicy>
146struct __any_of<__std_thread_backend_tag, _ExecutionPolicy>
147 : __cpu_parallel_any_of<__std_thread_backend_tag, _ExecutionPolicy> {};
148
149template <class _ExecutionPolicy>
150struct __fill<__std_thread_backend_tag, _ExecutionPolicy>
151 : __cpu_parallel_fill<__std_thread_backend_tag, _ExecutionPolicy> {};
152
153} // namespace __pstl
154_LIBCPP_END_NAMESPACE_STD
155
156#endif // _LIBCPP_STD_VER >= 17
157
158_LIBCPP_POP_MACROS
159
160#endif // _LIBCPP___PSTL_BACKENDS_STD_THREAD_H
161