File indexing completed on 2026-05-03 08:13:37
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___CXX03___PSTL_CPU_ALGOS_FOR_EACH_H
0010 #define _LIBCPP___CXX03___PSTL_CPU_ALGOS_FOR_EACH_H
0011
0012 #include <__cxx03/__algorithm/for_each.h>
0013 #include <__cxx03/__assert>
0014 #include <__cxx03/__config>
0015 #include <__cxx03/__iterator/concepts.h>
0016 #include <__cxx03/__pstl/backend_fwd.h>
0017 #include <__cxx03/__pstl/cpu_algos/cpu_traits.h>
0018 #include <__cxx03/__type_traits/is_execution_policy.h>
0019 #include <__cxx03/__utility/empty.h>
0020 #include <__cxx03/optional>
0021
0022 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0023 # pragma GCC system_header
0024 #endif
0025
0026 _LIBCPP_BEGIN_NAMESPACE_STD
0027 namespace __pstl {
0028
0029 template <class _Iterator, class _DifferenceType, class _Function>
0030 _LIBCPP_HIDE_FROM_ABI _Iterator __simd_for_each(_Iterator __first, _DifferenceType __n, _Function __f) noexcept {
0031 _PSTL_PRAGMA_SIMD
0032 for (_DifferenceType __i = 0; __i < __n; ++__i)
0033 __f(__first[__i]);
0034
0035 return __first + __n;
0036 }
0037
0038 template <class _Backend, class _RawExecutionPolicy>
0039 struct __cpu_parallel_for_each {
0040 template <class _Policy, class _ForwardIterator, class _Function>
0041 _LIBCPP_HIDE_FROM_ABI optional<__empty>
0042 operator()(_Policy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Function __func) const noexcept {
0043 if constexpr (__is_parallel_execution_policy_v<_RawExecutionPolicy> &&
0044 __has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
0045 return __cpu_traits<_Backend>::__for_each(
0046 __first, __last, [&__policy, __func](_ForwardIterator __brick_first, _ForwardIterator __brick_last) {
0047 using _ForEachUnseq = __pstl::__for_each<_Backend, __remove_parallel_policy_t<_RawExecutionPolicy>>;
0048 [[maybe_unused]] auto __res =
0049 _ForEachUnseq()(std::__remove_parallel_policy(__policy), __brick_first, __brick_last, __func);
0050 _LIBCPP_ASSERT_INTERNAL(__res, "unseq/seq should never try to allocate!");
0051 });
0052 } else if constexpr (__is_unsequenced_execution_policy_v<_RawExecutionPolicy> &&
0053 __has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
0054 __pstl::__simd_for_each(__first, __last - __first, __func);
0055 return __empty{};
0056 } else {
0057 std::for_each(__first, __last, __func);
0058 return __empty{};
0059 }
0060 }
0061 };
0062
0063 }
0064 _LIBCPP_END_NAMESPACE_STD
0065
0066 #endif