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___CXX03___PSTL_CPU_ALGOS_FOR_EACH_H 10 #define _LIBCPP___CXX03___PSTL_CPU_ALGOS_FOR_EACH_H 11 12 #include <__cxx03/__algorithm/for_each.h> 13 #include <__cxx03/__assert> 14 #include <__cxx03/__config> 15 #include <__cxx03/__iterator/concepts.h> 16 #include <__cxx03/__pstl/backend_fwd.h> 17 #include <__cxx03/__pstl/cpu_algos/cpu_traits.h> 18 #include <__cxx03/__type_traits/is_execution_policy.h> 19 #include <__cxx03/__utility/empty.h> 20 #include <__cxx03/optional> 21 22 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 23 # pragma GCC system_header 24 #endif 25 26 _LIBCPP_BEGIN_NAMESPACE_STD 27 namespace __pstl { 28 29 template <class _Iterator, class _DifferenceType, class _Function> 30 _LIBCPP_HIDE_FROM_ABI _Iterator __simd_for_each(_Iterator __first, _DifferenceType __n, _Function __f) noexcept { 31 _PSTL_PRAGMA_SIMD 32 for (_DifferenceType __i = 0; __i < __n; ++__i) 33 __f(__first[__i]); 34 35 return __first + __n; 36 } 37 38 template <class _Backend, class _RawExecutionPolicy> 39 struct __cpu_parallel_for_each { 40 template <class _Policy, class _ForwardIterator, class _Function> 41 _LIBCPP_HIDE_FROM_ABI optional<__empty> 42 operator()(_Policy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Function __func) const noexcept { 43 if constexpr (__is_parallel_execution_policy_v<_RawExecutionPolicy> && 44 __has_random_access_iterator_category_or_concept<_ForwardIterator>::value) { 45 return __cpu_traits<_Backend>::__for_each( 46 __first, __last, [&__policy, __func](_ForwardIterator __brick_first, _ForwardIterator __brick_last) { 47 using _ForEachUnseq = __pstl::__for_each<_Backend, __remove_parallel_policy_t<_RawExecutionPolicy>>; 48 [[maybe_unused]] auto __res = 49 _ForEachUnseq()(std::__remove_parallel_policy(__policy), __brick_first, __brick_last, __func); 50 _LIBCPP_ASSERT_INTERNAL(__res, "unseq/seq should never try to allocate!"); 51 }); 52 } else if constexpr (__is_unsequenced_execution_policy_v<_RawExecutionPolicy> && 53 __has_random_access_iterator_category_or_concept<_ForwardIterator>::value) { 54 __pstl::__simd_for_each(__first, __last - __first, __func); 55 return __empty{}; 56 } else { 57 std::for_each(__first, __last, __func); 58 return __empty{}; 59 } 60 } 61 }; 62 63 } // namespace __pstl 64 _LIBCPP_END_NAMESPACE_STD 65 66 #endif // _LIBCPP___CXX03___PSTL_CPU_ALGOS_FOR_EACH_H 67