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