You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
4.7KB

  1. // -*- C++ -*-
  2. //===-- execution_impl.h --------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _PSTL_EXECUTION_IMPL_H
  10. #define _PSTL_EXECUTION_IMPL_H
  11. #include <iterator>
  12. #include <type_traits>
  13. #include "execution_defs.h"
  14. namespace __pstl
  15. {
  16. namespace __internal
  17. {
  18. using namespace __pstl::execution;
  19. /* predicate */
  20. template <typename _Tp>
  21. std::false_type __lazy_and(_Tp, std::false_type)
  22. {
  23. return std::false_type{};
  24. };
  25. template <typename _Tp>
  26. inline _Tp
  27. __lazy_and(_Tp __a, std::true_type)
  28. {
  29. return __a;
  30. }
  31. template <typename _Tp>
  32. std::true_type __lazy_or(_Tp, std::true_type)
  33. {
  34. return std::true_type{};
  35. };
  36. template <typename _Tp>
  37. inline _Tp
  38. __lazy_or(_Tp __a, std::false_type)
  39. {
  40. return __a;
  41. }
  42. /* iterator */
  43. template <typename _IteratorType, typename... _OtherIteratorTypes>
  44. struct __is_random_access_iterator
  45. {
  46. static constexpr bool value = __internal::__is_random_access_iterator<_IteratorType>::value &&
  47. __internal::__is_random_access_iterator<_OtherIteratorTypes...>::value;
  48. typedef std::integral_constant<bool, value> type;
  49. };
  50. template <typename _IteratorType>
  51. struct __is_random_access_iterator<_IteratorType>
  52. : std::is_same<typename std::iterator_traits<_IteratorType>::iterator_category, std::random_access_iterator_tag>
  53. {
  54. };
  55. /* policy */
  56. template <typename _Policy>
  57. struct __policy_traits
  58. {
  59. };
  60. template <>
  61. struct __policy_traits<sequenced_policy>
  62. {
  63. typedef std::false_type allow_parallel;
  64. typedef std::false_type allow_unsequenced;
  65. typedef std::false_type allow_vector;
  66. };
  67. template <>
  68. struct __policy_traits<unsequenced_policy>
  69. {
  70. typedef std::false_type allow_parallel;
  71. typedef std::true_type allow_unsequenced;
  72. typedef std::true_type allow_vector;
  73. };
  74. template <>
  75. struct __policy_traits<parallel_policy>
  76. {
  77. typedef std::true_type allow_parallel;
  78. typedef std::false_type allow_unsequenced;
  79. typedef std::false_type allow_vector;
  80. };
  81. template <>
  82. struct __policy_traits<parallel_unsequenced_policy>
  83. {
  84. typedef std::true_type allow_parallel;
  85. typedef std::true_type allow_unsequenced;
  86. typedef std::true_type allow_vector;
  87. };
  88. template <typename _ExecutionPolicy>
  89. using __collector_t =
  90. typename __internal::__policy_traits<typename std::decay<_ExecutionPolicy>::type>::__collector_type;
  91. template <typename _ExecutionPolicy>
  92. using __allow_vector =
  93. typename __internal::__policy_traits<typename std::decay<_ExecutionPolicy>::type>::__allow_vector;
  94. template <typename _ExecutionPolicy>
  95. using __allow_unsequenced =
  96. typename __internal::__policy_traits<typename std::decay<_ExecutionPolicy>::type>::__allow_unsequenced;
  97. template <typename _ExecutionPolicy>
  98. using __allow_parallel =
  99. typename __internal::__policy_traits<typename std::decay<_ExecutionPolicy>::type>::__allow_parallel;
  100. template <typename _ExecutionPolicy, typename... _IteratorTypes>
  101. auto
  102. __is_vectorization_preferred(_ExecutionPolicy&& __exec)
  103. -> decltype(__internal::__lazy_and(__exec.__allow_vector(),
  104. typename __internal::__is_random_access_iterator<_IteratorTypes...>::type()))
  105. {
  106. return __internal::__lazy_and(__exec.__allow_vector(),
  107. typename __internal::__is_random_access_iterator<_IteratorTypes...>::type());
  108. }
  109. template <typename _ExecutionPolicy, typename... _IteratorTypes>
  110. auto
  111. __is_parallelization_preferred(_ExecutionPolicy&& __exec)
  112. -> decltype(__internal::__lazy_and(__exec.__allow_parallel(),
  113. typename __internal::__is_random_access_iterator<_IteratorTypes...>::type()))
  114. {
  115. return __internal::__lazy_and(__exec.__allow_parallel(),
  116. typename __internal::__is_random_access_iterator<_IteratorTypes...>::type());
  117. }
  118. template <typename policy, typename... _IteratorTypes>
  119. struct __prefer_unsequenced_tag
  120. {
  121. static constexpr bool value = __internal::__allow_unsequenced<policy>::value &&
  122. __internal::__is_random_access_iterator<_IteratorTypes...>::value;
  123. typedef std::integral_constant<bool, value> type;
  124. };
  125. template <typename policy, typename... _IteratorTypes>
  126. struct __prefer_parallel_tag
  127. {
  128. static constexpr bool value = __internal::__allow_parallel<policy>::value &&
  129. __internal::__is_random_access_iterator<_IteratorTypes...>::value;
  130. typedef std::integral_constant<bool, value> type;
  131. };
  132. } // namespace __internal
  133. } // namespace __pstl
  134. #endif /* _PSTL_EXECUTION_IMPL_H */