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