Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:59

0001 //===----------------------------------------------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef _LIBCPP___PSTL_CPU_ALGOS_FIND_IF_H
0010 #define _LIBCPP___PSTL_CPU_ALGOS_FIND_IF_H
0011 
0012 #include <__algorithm/find_if.h>
0013 #include <__assert>
0014 #include <__atomic/atomic.h>
0015 #include <__config>
0016 #include <__functional/operations.h>
0017 #include <__iterator/concepts.h>
0018 #include <__iterator/iterator_traits.h>
0019 #include <__pstl/backend_fwd.h>
0020 #include <__pstl/cpu_algos/cpu_traits.h>
0021 #include <__type_traits/is_execution_policy.h>
0022 #include <__utility/move.h>
0023 #include <__utility/pair.h>
0024 #include <optional>
0025 
0026 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0027 #  pragma GCC system_header
0028 #endif
0029 
0030 _LIBCPP_PUSH_MACROS
0031 #include <__undef_macros>
0032 
0033 #if _LIBCPP_STD_VER >= 17
0034 
0035 _LIBCPP_BEGIN_NAMESPACE_STD
0036 namespace __pstl {
0037 
0038 template <class _Backend, class _Index, class _Brick, class _Compare>
0039 _LIBCPP_HIDE_FROM_ABI optional<_Index>
0040 __parallel_find(_Index __first, _Index __last, _Brick __f, _Compare __comp, bool __b_first) {
0041   typedef typename std::iterator_traits<_Index>::difference_type _DifferenceType;
0042   const _DifferenceType __n      = __last - __first;
0043   _DifferenceType __initial_dist = __b_first ? __n : -1;
0044   std::atomic<_DifferenceType> __extremum(__initial_dist);
0045   // TODO: find out what is better here: parallel_for or parallel_reduce
0046   auto __res =
0047       __cpu_traits<_Backend>::__for_each(__first, __last, [__comp, __f, __first, &__extremum](_Index __i, _Index __j) {
0048         // See "Reducing Contention Through Priority Updates", PPoPP '13, for discussion of
0049         // why using a shared variable scales fairly well in this situation.
0050         if (__comp(__i - __first, __extremum)) {
0051           _Index __result = __f(__i, __j);
0052           // If not '__last' returned then we found what we want so put this to extremum
0053           if (__result != __j) {
0054             const _DifferenceType __k = __result - __first;
0055             for (_DifferenceType __old = __extremum; __comp(__k, __old); __old = __extremum) {
0056               __extremum.compare_exchange_weak(__old, __k);
0057             }
0058           }
0059         }
0060       });
0061   if (!__res)
0062     return nullopt;
0063   return __extremum.load() != __initial_dist ? __first + __extremum.load() : __last;
0064 }
0065 
0066 template <class _Backend, class _Index, class _DifferenceType, class _Compare>
0067 _LIBCPP_HIDE_FROM_ABI _Index
0068 __simd_first(_Index __first, _DifferenceType __begin, _DifferenceType __end, _Compare __comp) noexcept {
0069   // Experiments show good block sizes like this
0070   const _DifferenceType __block_size                                                = 8;
0071   alignas(__cpu_traits<_Backend>::__lane_size) _DifferenceType __lane[__block_size] = {0};
0072   while (__end - __begin >= __block_size) {
0073     _DifferenceType __found = 0;
0074     _PSTL_PRAGMA_SIMD_REDUCTION(| : __found) for (_DifferenceType __i = __begin; __i < __begin + __block_size; ++__i) {
0075       const _DifferenceType __t = __comp(__first, __i);
0076       __lane[__i - __begin]     = __t;
0077       __found |= __t;
0078     }
0079     if (__found) {
0080       _DifferenceType __i;
0081       // This will vectorize
0082       for (__i = 0; __i < __block_size; ++__i) {
0083         if (__lane[__i]) {
0084           break;
0085         }
0086       }
0087       return __first + __begin + __i;
0088     }
0089     __begin += __block_size;
0090   }
0091 
0092   // Keep remainder scalar
0093   while (__begin != __end) {
0094     if (__comp(__first, __begin)) {
0095       return __first + __begin;
0096     }
0097     ++__begin;
0098   }
0099   return __first + __end;
0100 }
0101 
0102 template <class _Backend, class _RawExecutionPolicy>
0103 struct __cpu_parallel_find_if {
0104   template <class _Policy, class _ForwardIterator, class _Predicate>
0105   _LIBCPP_HIDE_FROM_ABI optional<_ForwardIterator>
0106   operator()(_Policy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) const noexcept {
0107     if constexpr (__is_parallel_execution_policy_v<_RawExecutionPolicy> &&
0108                   __has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
0109       return __pstl::__parallel_find<_Backend>(
0110           __first,
0111           __last,
0112           [&__policy, &__pred](_ForwardIterator __brick_first, _ForwardIterator __brick_last) {
0113             using _FindIfUnseq = __pstl::__find_if<_Backend, __remove_parallel_policy_t<_RawExecutionPolicy>>;
0114             auto __res = _FindIfUnseq()(std::__remove_parallel_policy(__policy), __brick_first, __brick_last, __pred);
0115             _LIBCPP_ASSERT_INTERNAL(__res, "unseq/seq should never try to allocate!");
0116             return *std::move(__res);
0117           },
0118           less<>{},
0119           true);
0120     } else if constexpr (__is_unsequenced_execution_policy_v<_RawExecutionPolicy> &&
0121                          __has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
0122       using __diff_t = __iter_diff_t<_ForwardIterator>;
0123       return __pstl::__simd_first<_Backend>(
0124           __first, __diff_t(0), __last - __first, [&__pred](_ForwardIterator __iter, __diff_t __i) {
0125             return __pred(__iter[__i]);
0126           });
0127     } else {
0128       return std::find_if(__first, __last, __pred);
0129     }
0130   }
0131 };
0132 
0133 } // namespace __pstl
0134 _LIBCPP_END_NAMESPACE_STD
0135 
0136 #endif // _LIBCPP_STD_VER >= 17
0137 
0138 _LIBCPP_POP_MACROS
0139 
0140 #endif // _LIBCPP___PSTL_CPU_ALGOS_FIND_IF_H