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