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.

359 satır
18KB

  1. // -*- C++ -*-
  2. //===-- numeric_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_NUMERIC_IMPL_H
  10. #define _PSTL_NUMERIC_IMPL_H
  11. #include <iterator>
  12. #include <type_traits>
  13. #include <numeric>
  14. #include "parallel_backend.h"
  15. #include "pstl_config.h"
  16. #include "execution_impl.h"
  17. #include "unseq_backend_simd.h"
  18. #include "algorithm_fwd.h"
  19. namespace __pstl
  20. {
  21. namespace __internal
  22. {
  23. //------------------------------------------------------------------------
  24. // transform_reduce (version with two binary functions, according to draft N4659)
  25. //------------------------------------------------------------------------
  26. template <class _ForwardIterator1, class _ForwardIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2>
  27. _Tp
  28. __brick_transform_reduce(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, _Tp __init,
  29. _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2,
  30. /*is_vector=*/std::false_type) noexcept
  31. {
  32. return std::inner_product(__first1, __last1, __first2, __init, __binary_op1, __binary_op2);
  33. }
  34. template <class _ForwardIterator1, class _ForwardIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2>
  35. _Tp
  36. __brick_transform_reduce(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, _Tp __init,
  37. _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2,
  38. /*is_vector=*/std::true_type) noexcept
  39. {
  40. typedef typename std::iterator_traits<_ForwardIterator1>::difference_type _DifferenceType;
  41. return __unseq_backend::__simd_transform_reduce(
  42. __last1 - __first1, __init, __binary_op1,
  43. [=, &__binary_op2](_DifferenceType __i) { return __binary_op2(__first1[__i], __first2[__i]); });
  44. }
  45. template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _Tp, class _BinaryOperation1,
  46. class _BinaryOperation2, class _IsVector>
  47. _Tp
  48. __pattern_transform_reduce(_ExecutionPolicy&&, _ForwardIterator1 __first1, _ForwardIterator1 __last1,
  49. _ForwardIterator2 __first2, _Tp __init, _BinaryOperation1 __binary_op1,
  50. _BinaryOperation2 __binary_op2, _IsVector __is_vector,
  51. /*is_parallel=*/std::false_type) noexcept
  52. {
  53. return __brick_transform_reduce(__first1, __last1, __first2, __init, __binary_op1, __binary_op2, __is_vector);
  54. }
  55. template <class _ExecutionPolicy, class _RandomAccessIterator1, class _RandomAccessIterator2, class _Tp,
  56. class _BinaryOperation1, class _BinaryOperation2, class _IsVector>
  57. _Tp
  58. __pattern_transform_reduce(_ExecutionPolicy&& __exec, _RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
  59. _RandomAccessIterator2 __first2, _Tp __init, _BinaryOperation1 __binary_op1,
  60. _BinaryOperation2 __binary_op2, _IsVector __is_vector, /*is_parallel=*/std::true_type)
  61. {
  62. return __internal::__except_handler([&]() {
  63. return __par_backend::__parallel_transform_reduce(
  64. std::forward<_ExecutionPolicy>(__exec), __first1, __last1,
  65. [__first1, __first2, __binary_op2](_RandomAccessIterator1 __i) mutable {
  66. return __binary_op2(*__i, *(__first2 + (__i - __first1)));
  67. },
  68. __init,
  69. __binary_op1, // Combine
  70. [__first1, __first2, __binary_op1, __binary_op2,
  71. __is_vector](_RandomAccessIterator1 __i, _RandomAccessIterator1 __j, _Tp __init) -> _Tp {
  72. return __internal::__brick_transform_reduce(__i, __j, __first2 + (__i - __first1), __init, __binary_op1,
  73. __binary_op2, __is_vector);
  74. });
  75. });
  76. }
  77. //------------------------------------------------------------------------
  78. // transform_reduce (version with unary and binary functions)
  79. //------------------------------------------------------------------------
  80. template <class _ForwardIterator, class _Tp, class _BinaryOperation, class _UnaryOperation>
  81. _Tp
  82. __brick_transform_reduce(_ForwardIterator __first, _ForwardIterator __last, _Tp __init, _BinaryOperation __binary_op,
  83. _UnaryOperation __unary_op, /*is_vector=*/std::false_type) noexcept
  84. {
  85. for (; __first != __last; ++__first)
  86. {
  87. __init = __binary_op(__init, __unary_op(*__first));
  88. }
  89. return __init;
  90. }
  91. template <class _ForwardIterator, class _Tp, class _UnaryOperation, class _BinaryOperation>
  92. _Tp
  93. __brick_transform_reduce(_ForwardIterator __first, _ForwardIterator __last, _Tp __init, _BinaryOperation __binary_op,
  94. _UnaryOperation __unary_op, /*is_vector=*/std::true_type) noexcept
  95. {
  96. typedef typename std::iterator_traits<_ForwardIterator>::difference_type _DifferenceType;
  97. return __unseq_backend::__simd_transform_reduce(
  98. __last - __first, __init, __binary_op,
  99. [=, &__unary_op](_DifferenceType __i) { return __unary_op(__first[__i]); });
  100. }
  101. template <class _ExecutionPolicy, class _ForwardIterator, class _Tp, class _BinaryOperation, class _UnaryOperation,
  102. class _IsVector>
  103. _Tp
  104. __pattern_transform_reduce(_ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, _Tp __init,
  105. _BinaryOperation __binary_op, _UnaryOperation __unary_op, _IsVector __is_vector,
  106. /*is_parallel=*/std::false_type) noexcept
  107. {
  108. return __internal::__brick_transform_reduce(__first, __last, __init, __binary_op, __unary_op, __is_vector);
  109. }
  110. template <class _ExecutionPolicy, class _ForwardIterator, class _Tp, class _BinaryOperation, class _UnaryOperation,
  111. class _IsVector>
  112. _Tp
  113. __pattern_transform_reduce(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Tp __init,
  114. _BinaryOperation __binary_op, _UnaryOperation __unary_op, _IsVector __is_vector,
  115. /*is_parallel=*/std::true_type)
  116. {
  117. return __internal::__except_handler([&]() {
  118. return __par_backend::__parallel_transform_reduce(
  119. std::forward<_ExecutionPolicy>(__exec), __first, __last,
  120. [__unary_op](_ForwardIterator __i) mutable { return __unary_op(*__i); }, __init, __binary_op,
  121. [__unary_op, __binary_op, __is_vector](_ForwardIterator __i, _ForwardIterator __j, _Tp __init) {
  122. return __internal::__brick_transform_reduce(__i, __j, __init, __binary_op, __unary_op, __is_vector);
  123. });
  124. });
  125. }
  126. //------------------------------------------------------------------------
  127. // transform_exclusive_scan
  128. //
  129. // walk3 evaluates f(x,y,z) for (x,y,z) drawn from [first1,last1), [first2,...), [first3,...)
  130. //------------------------------------------------------------------------
  131. // Exclusive form
  132. template <class _ForwardIterator, class _OutputIterator, class _UnaryOperation, class _Tp, class _BinaryOperation>
  133. std::pair<_OutputIterator, _Tp>
  134. __brick_transform_scan(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result,
  135. _UnaryOperation __unary_op, _Tp __init, _BinaryOperation __binary_op,
  136. /*Inclusive*/ std::false_type, /*is_vector=*/std::false_type) noexcept
  137. {
  138. for (; __first != __last; ++__first, ++__result)
  139. {
  140. *__result = __init;
  141. _PSTL_PRAGMA_FORCEINLINE
  142. __init = __binary_op(__init, __unary_op(*__first));
  143. }
  144. return std::make_pair(__result, __init);
  145. }
  146. // Inclusive form
  147. template <class _ForwardIterator, class _OutputIterator, class _UnaryOperation, class _Tp, class _BinaryOperation>
  148. std::pair<_OutputIterator, _Tp>
  149. __brick_transform_scan(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result,
  150. _UnaryOperation __unary_op, _Tp __init, _BinaryOperation __binary_op,
  151. /*Inclusive*/ std::true_type, /*is_vector=*/std::false_type) noexcept
  152. {
  153. for (; __first != __last; ++__first, ++__result)
  154. {
  155. _PSTL_PRAGMA_FORCEINLINE
  156. __init = __binary_op(__init, __unary_op(*__first));
  157. *__result = __init;
  158. }
  159. return std::make_pair(__result, __init);
  160. }
  161. // type is arithmetic and binary operation is a user defined operation.
  162. template <typename _Tp, typename _BinaryOperation>
  163. using is_arithmetic_udop = std::integral_constant<bool, std::is_arithmetic<_Tp>::value &&
  164. !std::is_same<_BinaryOperation, std::plus<_Tp>>::value>;
  165. // [restriction] - T shall be DefaultConstructible.
  166. // [violation] - default ctor of T shall set the identity value for binary_op.
  167. template <class _ForwardIterator, class _OutputIterator, class _UnaryOperation, class _Tp, class _BinaryOperation,
  168. class _Inclusive>
  169. typename std::enable_if<!is_arithmetic_udop<_Tp, _BinaryOperation>::value, std::pair<_OutputIterator, _Tp>>::type
  170. __brick_transform_scan(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result,
  171. _UnaryOperation __unary_op, _Tp __init, _BinaryOperation __binary_op, _Inclusive,
  172. /*is_vector=*/std::true_type) noexcept
  173. {
  174. #if (_PSTL_UDS_PRESENT)
  175. return __unseq_backend::__simd_scan(__first, __last - __first, __result, __unary_op, __init, __binary_op,
  176. _Inclusive());
  177. #else
  178. // We need to call serial brick here to call function for inclusive and exclusive scan that depends on _Inclusive() value
  179. return __internal::__brick_transform_scan(__first, __last, __result, __unary_op, __init, __binary_op, _Inclusive(),
  180. /*is_vector=*/std::false_type());
  181. #endif
  182. }
  183. template <class _ForwardIterator, class _OutputIterator, class _UnaryOperation, class _Tp, class _BinaryOperation,
  184. class _Inclusive>
  185. typename std::enable_if<is_arithmetic_udop<_Tp, _BinaryOperation>::value, std::pair<_OutputIterator, _Tp>>::type
  186. __brick_transform_scan(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result,
  187. _UnaryOperation __unary_op, _Tp __init, _BinaryOperation __binary_op, _Inclusive,
  188. /*is_vector=*/std::true_type) noexcept
  189. {
  190. return __internal::__brick_transform_scan(__first, __last, __result, __unary_op, __init, __binary_op, _Inclusive(),
  191. /*is_vector=*/std::false_type());
  192. }
  193. template <class _ExecutionPolicy, class _ForwardIterator, class _OutputIterator, class _UnaryOperation, class _Tp,
  194. class _BinaryOperation, class _Inclusive, class _IsVector>
  195. _OutputIterator
  196. __pattern_transform_scan(_ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last,
  197. _OutputIterator __result, _UnaryOperation __unary_op, _Tp __init, _BinaryOperation __binary_op,
  198. _Inclusive, _IsVector __is_vector, /*is_parallel=*/std::false_type) noexcept
  199. {
  200. return __internal::__brick_transform_scan(__first, __last, __result, __unary_op, __init, __binary_op, _Inclusive(),
  201. __is_vector)
  202. .first;
  203. }
  204. template <class _ExecutionPolicy, class _RandomAccessIterator, class _OutputIterator, class _UnaryOperation, class _Tp,
  205. class _BinaryOperation, class _Inclusive, class _IsVector>
  206. typename std::enable_if<!std::is_floating_point<_Tp>::value, _OutputIterator>::type
  207. __pattern_transform_scan(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last,
  208. _OutputIterator __result, _UnaryOperation __unary_op, _Tp __init, _BinaryOperation __binary_op,
  209. _Inclusive, _IsVector __is_vector, /*is_parallel=*/std::true_type)
  210. {
  211. typedef typename std::iterator_traits<_RandomAccessIterator>::difference_type _DifferenceType;
  212. return __internal::__except_handler([&]() {
  213. __par_backend::__parallel_transform_scan(
  214. std::forward<_ExecutionPolicy>(__exec), __last - __first,
  215. [__first, __unary_op](_DifferenceType __i) mutable { return __unary_op(__first[__i]); }, __init,
  216. __binary_op,
  217. [__first, __unary_op, __binary_op](_DifferenceType __i, _DifferenceType __j, _Tp __init) {
  218. // Execute serial __brick_transform_reduce, due to the explicit SIMD vectorization (reduction) requires a commutative operation for the guarantee of correct scan.
  219. return __internal::__brick_transform_reduce(__first + __i, __first + __j, __init, __binary_op,
  220. __unary_op,
  221. /*__is_vector*/ std::false_type());
  222. },
  223. [__first, __unary_op, __binary_op, __result, __is_vector](_DifferenceType __i, _DifferenceType __j,
  224. _Tp __init) {
  225. return __internal::__brick_transform_scan(__first + __i, __first + __j, __result + __i, __unary_op,
  226. __init, __binary_op, _Inclusive(), __is_vector)
  227. .second;
  228. });
  229. return __result + (__last - __first);
  230. });
  231. }
  232. template <class _ExecutionPolicy, class _RandomAccessIterator, class _OutputIterator, class _UnaryOperation, class _Tp,
  233. class _BinaryOperation, class _Inclusive, class _IsVector>
  234. typename std::enable_if<std::is_floating_point<_Tp>::value, _OutputIterator>::type
  235. __pattern_transform_scan(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last,
  236. _OutputIterator __result, _UnaryOperation __unary_op, _Tp __init, _BinaryOperation __binary_op,
  237. _Inclusive, _IsVector __is_vector, /*is_parallel=*/std::true_type)
  238. {
  239. typedef typename std::iterator_traits<_RandomAccessIterator>::difference_type _DifferenceType;
  240. _DifferenceType __n = __last - __first;
  241. if (__n <= 0)
  242. {
  243. return __result;
  244. }
  245. return __internal::__except_handler([&]() {
  246. __par_backend::__parallel_strict_scan(
  247. std::forward<_ExecutionPolicy>(__exec), __n, __init,
  248. [__first, __unary_op, __binary_op, __result, __is_vector](_DifferenceType __i, _DifferenceType __len) {
  249. return __internal::__brick_transform_scan(__first + __i, __first + (__i + __len), __result + __i,
  250. __unary_op, _Tp{}, __binary_op, _Inclusive(), __is_vector)
  251. .second;
  252. },
  253. __binary_op,
  254. [__result, &__binary_op](_DifferenceType __i, _DifferenceType __len, _Tp __initial) {
  255. return *(std::transform(__result + __i, __result + __i + __len, __result + __i,
  256. [&__initial, &__binary_op](const _Tp& __x) {
  257. _PSTL_PRAGMA_FORCEINLINE
  258. return __binary_op(__initial, __x);
  259. }) -
  260. 1);
  261. },
  262. [](_Tp __res) {});
  263. return __result + (__last - __first);
  264. });
  265. }
  266. //------------------------------------------------------------------------
  267. // adjacent_difference
  268. //------------------------------------------------------------------------
  269. template <class _ForwardIterator, class _OutputIterator, class _BinaryOperation>
  270. _OutputIterator
  271. __brick_adjacent_difference(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __d_first,
  272. _BinaryOperation __op, /*is_vector*/ std::false_type) noexcept
  273. {
  274. return std::adjacent_difference(__first, __last, __d_first, __op);
  275. }
  276. template <class _ForwardIterator1, class _ForwardIterator2, class BinaryOperation>
  277. _ForwardIterator2
  278. __brick_adjacent_difference(_ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __d_first,
  279. BinaryOperation __op, /*is_vector=*/std::true_type) noexcept
  280. {
  281. _PSTL_ASSERT(__first != __last);
  282. typedef typename std::iterator_traits<_ForwardIterator1>::reference _ReferenceType1;
  283. typedef typename std::iterator_traits<_ForwardIterator2>::reference _ReferenceType2;
  284. auto __n = __last - __first;
  285. *__d_first = *__first;
  286. return __unseq_backend::__simd_walk_3(
  287. __first + 1, __n - 1, __first, __d_first + 1,
  288. [&__op](_ReferenceType1 __x, _ReferenceType1 __y, _ReferenceType2 __z) { __z = __op(__x, __y); });
  289. }
  290. template <class _ExecutionPolicy, class _ForwardIterator, class _OutputIterator, class _BinaryOperation,
  291. class _IsVector>
  292. _OutputIterator
  293. __pattern_adjacent_difference(_ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last,
  294. _OutputIterator __d_first, _BinaryOperation __op, _IsVector __is_vector,
  295. /*is_parallel*/ std::false_type) noexcept
  296. {
  297. return __internal::__brick_adjacent_difference(__first, __last, __d_first, __op, __is_vector);
  298. }
  299. template <class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _BinaryOperation,
  300. class _IsVector>
  301. _ForwardIterator2
  302. __pattern_adjacent_difference(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last,
  303. _ForwardIterator2 __d_first, _BinaryOperation __op, _IsVector __is_vector,
  304. /*is_parallel=*/std::true_type)
  305. {
  306. _PSTL_ASSERT(__first != __last);
  307. typedef typename std::iterator_traits<_ForwardIterator1>::reference _ReferenceType1;
  308. typedef typename std::iterator_traits<_ForwardIterator2>::reference _ReferenceType2;
  309. *__d_first = *__first;
  310. __par_backend::__parallel_for(
  311. std::forward<_ExecutionPolicy>(__exec), __first, __last - 1,
  312. [&__op, __is_vector, __d_first, __first](_ForwardIterator1 __b, _ForwardIterator1 __e) {
  313. _ForwardIterator2 __d_b = __d_first + (__b - __first);
  314. __internal::__brick_walk3(
  315. __b, __e, __b + 1, __d_b + 1,
  316. [&__op](_ReferenceType1 __x, _ReferenceType1 __y, _ReferenceType2 __z) { __z = __op(__y, __x); },
  317. __is_vector);
  318. });
  319. return __d_first + (__last - __first);
  320. }
  321. } // namespace __internal
  322. } // namespace __pstl
  323. #endif /* _PSTL_NUMERIC_IMPL_H */