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_CPU_ALGOS_FIND_IF_H
10#define _LIBCPP___PSTL_CPU_ALGOS_FIND_IF_H
11
12#include <__algorithm/find_if.h>
13#include <__assert>
14#include <__atomic/atomic.h>
15#include <__config>
16#include <__functional/operations.h>
17#include <__iterator/concepts.h>
18#include <__iterator/iterator_traits.h>
19#include <__optional/nullopt_t.h>
20#include <__optional/optional.h>
21#include <__pstl/backend_fwd.h>
22#include <__pstl/cpu_algos/cpu_traits.h>
23#include <__type_traits/is_execution_policy.h>
24#include <__utility/move.h>
25#include <__utility/pair.h>
26
27#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28# pragma GCC system_header
29#endif
30
31_LIBCPP_PUSH_MACROS
32#include <__undef_macros>
33
34#if _LIBCPP_STD_VER >= 17
35
36_LIBCPP_BEGIN_NAMESPACE_STD
37namespace __pstl {
38
39template <class _Backend, class _Index, class _Brick, class _Compare>
40_LIBCPP_HIDE_FROM_ABI optional<_Index>
41__parallel_find(_Index __first, _Index __last, _Brick __f, _Compare __comp, bool __b_first) {
42 typedef typename std::iterator_traits<_Index>::difference_type _DifferenceType;
43 const _DifferenceType __n = __last - __first;
44 _DifferenceType __initial_dist = __b_first ? __n : -1;
45 std::atomic<_DifferenceType> __extremum(__initial_dist);
46 // TODO: find out what is better here: parallel_for or parallel_reduce
47 auto __res =
48 __cpu_traits<_Backend>::__for_each(__first, __last, [__comp, __f, __first, &__extremum](_Index __i, _Index __j) {
49 // See "Reducing Contention Through Priority Updates", PPoPP '13, for discussion of
50 // why using a shared variable scales fairly well in this situation.
51 if (__comp(__i - __first, __extremum)) {
52 _Index __result = __f(__i, __j);
53 // If not '__last' returned then we found what we want so put this to extremum
54 if (__result != __j) {
55 const _DifferenceType __k = __result - __first;
56 for (_DifferenceType __old = __extremum; __comp(__k, __old); __old = __extremum) {
57 __extremum.compare_exchange_weak(__old, __k);
58 }
59 }
60 }
61 });
62 if (!__res)
63 return nullopt;
64 return __extremum.load() != __initial_dist ? __first + __extremum.load() : __last;
65}
66
67template <class _Backend, class _Index, class _DifferenceType, class _Compare>
68_LIBCPP_HIDE_FROM_ABI _Index
69__simd_first(_Index __first, _DifferenceType __begin, _DifferenceType __end, _Compare __comp) noexcept {
70 // Experiments show good block sizes like this
71 const _DifferenceType __block_size = 8;
72 alignas(__cpu_traits<_Backend>::__lane_size) _DifferenceType __lane[__block_size] = {0};
73 while (__end - __begin >= __block_size) {
74 _DifferenceType __found = 0;
75 _PSTL_PRAGMA_SIMD_REDUCTION(| : __found) for (_DifferenceType __i = __begin; __i < __begin + __block_size; ++__i) {
76 const _DifferenceType __t = __comp(__first, __i);
77 __lane[__i - __begin] = __t;
78 __found |= __t;
79 }
80 if (__found) {
81 _DifferenceType __i;
82 // This will vectorize
83 for (__i = 0; __i < __block_size; ++__i) {
84 if (__lane[__i]) {
85 break;
86 }
87 }
88 return __first + __begin + __i;
89 }
90 __begin += __block_size;
91 }
92
93 // Keep remainder scalar
94 while (__begin != __end) {
95 if (__comp(__first, __begin)) {
96 return __first + __begin;
97 }
98 ++__begin;
99 }
100 return __first + __end;
101}
102
103template <class _Backend, class _RawExecutionPolicy>
104struct __cpu_parallel_find_if {
105 template <class _Policy, class _ForwardIterator, class _Predicate>
106 _LIBCPP_HIDE_FROM_ABI optional<_ForwardIterator>
107 operator()(_Policy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) const noexcept {
108 if constexpr (__is_parallel_execution_policy_v<_RawExecutionPolicy> &&
109 __has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
110 return __pstl::__parallel_find<_Backend>(
111 __first,
112 __last,
113 [&__policy, &__pred](_ForwardIterator __brick_first, _ForwardIterator __brick_last) {
114 using _FindIfUnseq = __pstl::__find_if<_Backend, __remove_parallel_policy_t<_RawExecutionPolicy>>;
115 auto __res = _FindIfUnseq()(std::__remove_parallel_policy(__policy), __brick_first, __brick_last, __pred);
116 _LIBCPP_ASSERT_INTERNAL(__res, "unseq/seq should never try to allocate!");
117 return *std::move(__res);
118 },
119 less<>{},
120 true);
121 } else if constexpr (__is_unsequenced_execution_policy_v<_RawExecutionPolicy> &&
122 __has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
123 using __diff_t = __iterator_difference_type<_ForwardIterator>;
124 return __pstl::__simd_first<_Backend>(
125 __first, __diff_t(0), __last - __first, [&__pred](_ForwardIterator __iter, __diff_t __i) {
126 return __pred(__iter[__i]);
127 });
128 } else {
129 return std::find_if(__first, __last, __pred);
130 }
131 }
132};
133
134} // namespace __pstl
135_LIBCPP_END_NAMESPACE_STD
136
137#endif // _LIBCPP_STD_VER >= 17
138
139_LIBCPP_POP_MACROS
140
141#endif // _LIBCPP___PSTL_CPU_ALGOS_FIND_IF_H
142