parallel_for_each.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. Copyright (c) 2005-2020 Intel Corporation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #ifndef __TBB_parallel_for_each_H
  14. #define __TBB_parallel_for_each_H
  15. #include "parallel_do.h"
  16. #include "parallel_for.h"
  17. namespace tbb {
  18. //! @cond INTERNAL
  19. namespace internal {
  20. // The class calls user function in operator()
  21. template <typename Function, typename Iterator>
  22. class parallel_for_each_body_do : internal::no_assign {
  23. const Function &my_func;
  24. public:
  25. parallel_for_each_body_do(const Function &_func) : my_func(_func) {}
  26. void operator()(typename std::iterator_traits<Iterator>::reference value) const {
  27. my_func(value);
  28. }
  29. };
  30. // The class calls user function in operator()
  31. template <typename Function, typename Iterator>
  32. class parallel_for_each_body_for : internal::no_assign {
  33. const Function &my_func;
  34. public:
  35. parallel_for_each_body_for(const Function &_func) : my_func(_func) {}
  36. void operator()(tbb::blocked_range<Iterator> range) const {
  37. #if __INTEL_COMPILER
  38. #pragma ivdep
  39. #endif
  40. for(Iterator it = range.begin(), end = range.end(); it != end; ++it) {
  41. my_func(*it);
  42. }
  43. }
  44. };
  45. template<typename Iterator, typename Function, typename Generic>
  46. struct parallel_for_each_impl {
  47. #if __TBB_TASK_GROUP_CONTEXT
  48. static void doit(Iterator first, Iterator last, const Function& f, task_group_context &context) {
  49. internal::parallel_for_each_body_do<Function, Iterator> body(f);
  50. tbb::parallel_do(first, last, body, context);
  51. }
  52. #endif
  53. static void doit(Iterator first, Iterator last, const Function& f) {
  54. internal::parallel_for_each_body_do<Function, Iterator> body(f);
  55. tbb::parallel_do(first, last, body);
  56. }
  57. };
  58. template<typename Iterator, typename Function>
  59. struct parallel_for_each_impl<Iterator, Function, std::random_access_iterator_tag> {
  60. #if __TBB_TASK_GROUP_CONTEXT
  61. static void doit(Iterator first, Iterator last, const Function& f, task_group_context &context) {
  62. internal::parallel_for_each_body_for<Function, Iterator> body(f);
  63. tbb::parallel_for(tbb::blocked_range<Iterator>(first, last), body, context);
  64. }
  65. #endif
  66. static void doit(Iterator first, Iterator last, const Function& f) {
  67. internal::parallel_for_each_body_for<Function, Iterator> body(f);
  68. tbb::parallel_for(tbb::blocked_range<Iterator>(first, last), body);
  69. }
  70. };
  71. } // namespace internal
  72. //! @endcond
  73. /** \name parallel_for_each
  74. **/
  75. //@{
  76. //! Calls function f for all items from [first, last) interval using user-supplied context
  77. /** @ingroup algorithms */
  78. #if __TBB_TASK_GROUP_CONTEXT
  79. template<typename Iterator, typename Function>
  80. void parallel_for_each(Iterator first, Iterator last, const Function& f, task_group_context &context) {
  81. internal::parallel_for_each_impl<Iterator, Function, typename std::iterator_traits<Iterator>::iterator_category>::doit(first, last, f, context);
  82. }
  83. //! Calls function f for all items from rng using user-supplied context
  84. /** @ingroup algorithms */
  85. template<typename Range, typename Function>
  86. void parallel_for_each(Range& rng, const Function& f, task_group_context& context) {
  87. parallel_for_each(tbb::internal::first(rng), tbb::internal::last(rng), f, context);
  88. }
  89. //! Calls function f for all items from const rng user-supplied context
  90. /** @ingroup algorithms */
  91. template<typename Range, typename Function>
  92. void parallel_for_each(const Range& rng, const Function& f, task_group_context& context) {
  93. parallel_for_each(tbb::internal::first(rng), tbb::internal::last(rng), f, context);
  94. }
  95. #endif /* __TBB_TASK_GROUP_CONTEXT */
  96. //! Uses default context
  97. template<typename Iterator, typename Function>
  98. void parallel_for_each(Iterator first, Iterator last, const Function& f) {
  99. internal::parallel_for_each_impl<Iterator, Function, typename std::iterator_traits<Iterator>::iterator_category>::doit(first, last, f);
  100. }
  101. //! Uses default context
  102. template<typename Range, typename Function>
  103. void parallel_for_each(Range& rng, const Function& f) {
  104. parallel_for_each(tbb::internal::first(rng), tbb::internal::last(rng), f);
  105. }
  106. //! Uses default context
  107. template<typename Range, typename Function>
  108. void parallel_for_each(const Range& rng, const Function& f) {
  109. parallel_for_each(tbb::internal::first(rng), tbb::internal::last(rng), f);
  110. }
  111. //@}
  112. } // namespace
  113. #endif /* __TBB_parallel_for_each_H */