parallel_backend_serial.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // -*- C++ -*-
  2. //===-- parallel_backend_serial.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_PARALLEL_BACKEND_SERIAL_H
  10. #define _PSTL_PARALLEL_BACKEND_SERIAL_H
  11. #include <algorithm>
  12. #include <cstddef>
  13. #include <memory>
  14. #include <numeric>
  15. #include <utility>
  16. namespace __pstl
  17. {
  18. namespace __serial_backend
  19. {
  20. template <typename _Tp>
  21. class __buffer
  22. {
  23. std::allocator<_Tp> __allocator_;
  24. _Tp* __ptr_;
  25. const std::size_t __buf_size_;
  26. __buffer(const __buffer&) = delete;
  27. void
  28. operator=(const __buffer&) = delete;
  29. public:
  30. __buffer(std::size_t __n) : __allocator_(), __ptr_(__allocator_.allocate(__n)), __buf_size_(__n) {}
  31. operator bool() const { return __ptr_ != nullptr; }
  32. _Tp*
  33. get() const
  34. {
  35. return __ptr_;
  36. }
  37. ~__buffer() { __allocator_.deallocate(__ptr_, __buf_size_); }
  38. };
  39. inline void
  40. __cancel_execution()
  41. {
  42. }
  43. template <class _ExecutionPolicy, class _Index, class _Fp>
  44. void
  45. __parallel_for(_ExecutionPolicy&&, _Index __first, _Index __last, _Fp __f)
  46. {
  47. __f(__first, __last);
  48. }
  49. template <class _ExecutionPolicy, class _Value, class _Index, typename _RealBody, typename _Reduction>
  50. _Value
  51. __parallel_reduce(_ExecutionPolicy&&, _Index __first, _Index __last, const _Value& __identity,
  52. const _RealBody& __real_body, const _Reduction&)
  53. {
  54. if (__first == __last)
  55. {
  56. return __identity;
  57. }
  58. else
  59. {
  60. return __real_body(__first, __last, __identity);
  61. }
  62. }
  63. template <class _ExecutionPolicy, class _Index, class _UnaryOp, class _Tp, class _BinaryOp, class _Reduce>
  64. _Tp
  65. __parallel_transform_reduce(_ExecutionPolicy&&, _Index __first, _Index __last, _UnaryOp, _Tp __init, _BinaryOp,
  66. _Reduce __reduce)
  67. {
  68. return __reduce(__first, __last, __init);
  69. }
  70. template <class _ExecutionPolicy, typename _Index, typename _Tp, typename _Rp, typename _Cp, typename _Sp, typename _Ap>
  71. void
  72. __parallel_strict_scan(_ExecutionPolicy&&, _Index __n, _Tp __initial, _Rp __reduce, _Cp __combine, _Sp __scan,
  73. _Ap __apex)
  74. {
  75. _Tp __sum = __initial;
  76. if (__n)
  77. __sum = __combine(__sum, __reduce(_Index(0), __n));
  78. __apex(__sum);
  79. if (__n)
  80. __scan(_Index(0), __n, __initial);
  81. }
  82. template <class _ExecutionPolicy, class _Index, class _UnaryOp, class _Tp, class _BinaryOp, class _Reduce, class _Scan>
  83. _Tp
  84. __parallel_transform_scan(_ExecutionPolicy&&, _Index __n, _UnaryOp, _Tp __init, _BinaryOp, _Reduce, _Scan __scan)
  85. {
  86. return __scan(_Index(0), __n, __init);
  87. }
  88. template <class _ExecutionPolicy, typename _RandomAccessIterator, typename _Compare, typename _LeafSort>
  89. void
  90. __parallel_stable_sort(_ExecutionPolicy&&, _RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
  91. _LeafSort __leaf_sort, std::size_t = 0)
  92. {
  93. __leaf_sort(__first, __last, __comp);
  94. }
  95. template <class _ExecutionPolicy, typename _RandomAccessIterator1, typename _RandomAccessIterator2,
  96. typename _RandomAccessIterator3, typename _Compare, typename _LeafMerge>
  97. void
  98. __parallel_merge(_ExecutionPolicy&&, _RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
  99. _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _RandomAccessIterator3 __outit,
  100. _Compare __comp, _LeafMerge __leaf_merge)
  101. {
  102. __leaf_merge(__first1, __last1, __first2, __last2, __outit, __comp);
  103. }
  104. template <class _ExecutionPolicy, typename _F1, typename _F2>
  105. void
  106. __parallel_invoke(_ExecutionPolicy&&, _F1&& __f1, _F2&& __f2)
  107. {
  108. std::forward<_F1>(__f1)();
  109. std::forward<_F2>(__f2)();
  110. }
  111. } // namespace __serial_backend
  112. } // namespace __pstl
  113. #endif /* _PSTL_PARALLEL_BACKEND_SERIAL_H */