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.

82 lines
4.0KB

  1. // -*- C++ -*-
  2. //===-- parallel_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_PARALLEL_IMPL_H
  10. #define _PSTL_PARALLEL_IMPL_H
  11. #include <atomic>
  12. // This header defines the minimum set of parallel routines required to support Parallel STL,
  13. // implemented on top of Intel(R) Threading Building Blocks (Intel(R) TBB) library
  14. namespace __pstl
  15. {
  16. namespace __internal
  17. {
  18. //------------------------------------------------------------------------
  19. // parallel_find
  20. //-----------------------------------------------------------------------
  21. /** Return extremum value returned by brick f[i,j) for subranges [i,j) of [first,last)
  22. Each f[i,j) must return a value in [i,j). */
  23. template <class _ExecutionPolicy, class _Index, class _Brick, class _Compare>
  24. _Index
  25. __parallel_find(_ExecutionPolicy&& __exec, _Index __first, _Index __last, _Brick __f, _Compare __comp, bool __b_first)
  26. {
  27. typedef typename std::iterator_traits<_Index>::difference_type _DifferenceType;
  28. const _DifferenceType __n = __last - __first;
  29. _DifferenceType __initial_dist = __b_first ? __n : -1;
  30. std::atomic<_DifferenceType> __extremum(__initial_dist);
  31. // TODO: find out what is better here: parallel_for or parallel_reduce
  32. __par_backend::__parallel_for(std::forward<_ExecutionPolicy>(__exec), __first, __last,
  33. [__comp, __f, __first, &__extremum](_Index __i, _Index __j) {
  34. // See "Reducing Contention Through Priority Updates", PPoPP '13, for discussion of
  35. // why using a shared variable scales fairly well in this situation.
  36. if (__comp(__i - __first, __extremum))
  37. {
  38. _Index __res = __f(__i, __j);
  39. // If not '__last' returned then we found what we want so put this to extremum
  40. if (__res != __j)
  41. {
  42. const _DifferenceType __k = __res - __first;
  43. for (_DifferenceType __old = __extremum; __comp(__k, __old);
  44. __old = __extremum)
  45. {
  46. __extremum.compare_exchange_weak(__old, __k);
  47. }
  48. }
  49. }
  50. });
  51. return __extremum != __initial_dist ? __first + __extremum : __last;
  52. }
  53. //------------------------------------------------------------------------
  54. // parallel_or
  55. //------------------------------------------------------------------------
  56. //! Return true if brick f[i,j) returns true for some subrange [i,j) of [first,last)
  57. template <class _ExecutionPolicy, class _Index, class _Brick>
  58. bool
  59. __parallel_or(_ExecutionPolicy&& __exec, _Index __first, _Index __last, _Brick __f)
  60. {
  61. std::atomic<bool> __found(false);
  62. __par_backend::__parallel_for(std::forward<_ExecutionPolicy>(__exec), __first, __last,
  63. [__f, &__found](_Index __i, _Index __j) {
  64. if (!__found.load(std::memory_order_relaxed) && __f(__i, __j))
  65. {
  66. __found.store(true, std::memory_order_relaxed);
  67. __par_backend::__cancel_execution();
  68. }
  69. });
  70. return __found;
  71. }
  72. } // namespace __internal
  73. } // namespace __pstl
  74. #endif /* _PSTL_PARALLEL_IMPL_H */