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