execution_impl.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 =
  47. __internal::__is_random_access_iterator<_IteratorType>::value && __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. #if __PSTL_USE_PAR_POLICIES
  75. template <>
  76. struct __policy_traits<parallel_policy>
  77. {
  78. typedef std::true_type allow_parallel;
  79. typedef std::false_type allow_unsequenced;
  80. typedef std::false_type allow_vector;
  81. };
  82. template <>
  83. struct __policy_traits<parallel_unsequenced_policy>
  84. {
  85. typedef std::true_type allow_parallel;
  86. typedef std::true_type allow_unsequenced;
  87. typedef std::true_type allow_vector;
  88. };
  89. #endif
  90. template <typename _ExecutionPolicy>
  91. using __collector_t = typename __internal::__policy_traits<typename std::decay<_ExecutionPolicy>::type>::__collector_type;
  92. template <typename _ExecutionPolicy>
  93. using __allow_vector = typename __internal::__policy_traits<typename std::decay<_ExecutionPolicy>::type>::__allow_vector;
  94. template <typename _ExecutionPolicy>
  95. using __allow_unsequenced = typename __internal::__policy_traits<typename std::decay<_ExecutionPolicy>::type>::__allow_unsequenced;
  96. template <typename _ExecutionPolicy>
  97. using __allow_parallel = typename __internal::__policy_traits<typename std::decay<_ExecutionPolicy>::type>::__allow_parallel;
  98. template <typename _ExecutionPolicy, typename... _IteratorTypes>
  99. auto
  100. __is_vectorization_preferred(_ExecutionPolicy&& __exec)
  101. -> decltype(__internal::__lazy_and(__exec.__allow_vector(), typename __internal::__is_random_access_iterator<_IteratorTypes...>::type()))
  102. {
  103. return __internal::__lazy_and(__exec.__allow_vector(), typename __internal::__is_random_access_iterator<_IteratorTypes...>::type());
  104. }
  105. template <typename _ExecutionPolicy, typename... _IteratorTypes>
  106. auto
  107. __is_parallelization_preferred(_ExecutionPolicy&& __exec)
  108. -> decltype(__internal::__lazy_and(__exec.__allow_parallel(), typename __internal::__is_random_access_iterator<_IteratorTypes...>::type()))
  109. {
  110. return __internal::__lazy_and(__exec.__allow_parallel(), typename __internal::__is_random_access_iterator<_IteratorTypes...>::type());
  111. }
  112. template <typename policy, typename... _IteratorTypes>
  113. struct __prefer_unsequenced_tag
  114. {
  115. static constexpr bool value =
  116. __internal::__allow_unsequenced<policy>::value && __internal::__is_random_access_iterator<_IteratorTypes...>::value;
  117. typedef std::integral_constant<bool, value> type;
  118. };
  119. template <typename policy, typename... _IteratorTypes>
  120. struct __prefer_parallel_tag
  121. {
  122. static constexpr bool value =
  123. __internal::__allow_parallel<policy>::value && __internal::__is_random_access_iterator<_IteratorTypes...>::value;
  124. typedef std::integral_constant<bool, value> type;
  125. };
  126. } // namespace __internal
  127. } // namespace __pstl
  128. #endif /* __PSTL_execution_impl_H */