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___ALGORITHM_PSTL_BACKENDS_CPU_BACKNEDS_FOR_EACH_H 10 #define _LIBCPP___ALGORITHM_PSTL_BACKENDS_CPU_BACKNEDS_FOR_EACH_H 11 12 #include <__algorithm/for_each.h> 13 #include <__algorithm/pstl_backends/cpu_backends/backend.h> 14 #include <__config> 15 #include <__iterator/iterator_traits.h> 16 #include <__type_traits/is_execution_policy.h> 17 #include <__utility/terminate_on_exception.h> 18 19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 20 # pragma GCC system_header 21 #endif 22 23 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17 24 25 _LIBCPP_BEGIN_NAMESPACE_STD 26 27 template <class _Iterator, class _DifferenceType, class _Function> 28 _LIBCPP_HIDE_FROM_ABI _Iterator __simd_walk_1(_Iterator __first, _DifferenceType __n, _Function __f) noexcept { 29 _PSTL_PRAGMA_SIMD 30 for (_DifferenceType __i = 0; __i < __n; ++__i) 31 __f(__first[__i]); 32 33 return __first + __n; 34 } 35 36 template <class _ExecutionPolicy, class _ForwardIterator, class _Functor> 37 _LIBCPP_HIDE_FROM_ABI void 38 __pstl_for_each(__cpu_backend_tag, _ForwardIterator __first, _ForwardIterator __last, _Functor __func) { 39 if constexpr (__is_parallel_execution_policy_v<_ExecutionPolicy> && 40 __has_random_access_iterator_category<_ForwardIterator>::value) { 41 std::__terminate_on_exception([&] { 42 std::__par_backend::__parallel_for( 43 __first, __last, [__func](_ForwardIterator __brick_first, _ForwardIterator __brick_last) { 44 std::__pstl_for_each<__remove_parallel_policy_t<_ExecutionPolicy>>( 45 __cpu_backend_tag{}, __brick_first, __brick_last, __func); 46 }); 47 }); 48 } else if constexpr (__is_unsequenced_execution_policy_v<_ExecutionPolicy> && 49 __has_random_access_iterator_category<_ForwardIterator>::value) { 50 std::__simd_walk_1(__first, __last - __first, __func); 51 } else { 52 std::for_each(__first, __last, __func); 53 } 54 } 55 56 _LIBCPP_END_NAMESPACE_STD 57 58 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17 59 60 #endif // _LIBCPP___ALGORITHM_PSTL_BACKENDS_CPU_BACKNEDS_FOR_EACH_H 61