File indexing completed on 2025-01-18 10:12:58
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #ifndef __TBB_parallel_for_each_H
0018 #define __TBB_parallel_for_each_H
0019
0020 #include "parallel_do.h"
0021 #include "parallel_for.h"
0022
0023 namespace tbb {
0024
0025
0026 namespace internal {
0027
0028 template <typename Function, typename Iterator>
0029 class parallel_for_each_body_do : internal::no_assign {
0030 const Function &my_func;
0031 public:
0032 parallel_for_each_body_do(const Function &_func) : my_func(_func) {}
0033
0034 void operator()(typename std::iterator_traits<Iterator>::reference value) const {
0035 my_func(value);
0036 }
0037 };
0038
0039
0040 template <typename Function, typename Iterator>
0041 class parallel_for_each_body_for : internal::no_assign {
0042 const Function &my_func;
0043 public:
0044 parallel_for_each_body_for(const Function &_func) : my_func(_func) {}
0045
0046 void operator()(tbb::blocked_range<Iterator> range) const {
0047 #if __INTEL_COMPILER
0048 #pragma ivdep
0049 #endif
0050 for(Iterator it = range.begin(), end = range.end(); it != end; ++it) {
0051 my_func(*it);
0052 }
0053 }
0054 };
0055
0056 template<typename Iterator, typename Function, typename Generic>
0057 struct parallel_for_each_impl {
0058 #if __TBB_TASK_GROUP_CONTEXT
0059 static void doit(Iterator first, Iterator last, const Function& f, task_group_context &context) {
0060 internal::parallel_for_each_body_do<Function, Iterator> body(f);
0061 tbb::parallel_do(first, last, body, context);
0062 }
0063 #endif
0064 static void doit(Iterator first, Iterator last, const Function& f) {
0065 internal::parallel_for_each_body_do<Function, Iterator> body(f);
0066 tbb::parallel_do(first, last, body);
0067 }
0068 };
0069 template<typename Iterator, typename Function>
0070 struct parallel_for_each_impl<Iterator, Function, std::random_access_iterator_tag> {
0071 #if __TBB_TASK_GROUP_CONTEXT
0072 static void doit(Iterator first, Iterator last, const Function& f, task_group_context &context) {
0073 internal::parallel_for_each_body_for<Function, Iterator> body(f);
0074 tbb::parallel_for(tbb::blocked_range<Iterator>(first, last), body, context);
0075 }
0076 #endif
0077 static void doit(Iterator first, Iterator last, const Function& f) {
0078 internal::parallel_for_each_body_for<Function, Iterator> body(f);
0079 tbb::parallel_for(tbb::blocked_range<Iterator>(first, last), body);
0080 }
0081 };
0082 }
0083
0084
0085
0086
0087
0088
0089
0090 #if __TBB_TASK_GROUP_CONTEXT
0091 template<typename Iterator, typename Function>
0092 void parallel_for_each(Iterator first, Iterator last, const Function& f, task_group_context &context) {
0093 internal::parallel_for_each_impl<Iterator, Function, typename std::iterator_traits<Iterator>::iterator_category>::doit(first, last, f, context);
0094 }
0095
0096
0097
0098 template<typename Range, typename Function>
0099 void parallel_for_each(Range& rng, const Function& f, task_group_context& context) {
0100 parallel_for_each(tbb::internal::first(rng), tbb::internal::last(rng), f, context);
0101 }
0102
0103
0104
0105 template<typename Range, typename Function>
0106 void parallel_for_each(const Range& rng, const Function& f, task_group_context& context) {
0107 parallel_for_each(tbb::internal::first(rng), tbb::internal::last(rng), f, context);
0108 }
0109 #endif
0110
0111
0112 template<typename Iterator, typename Function>
0113 void parallel_for_each(Iterator first, Iterator last, const Function& f) {
0114 internal::parallel_for_each_impl<Iterator, Function, typename std::iterator_traits<Iterator>::iterator_category>::doit(first, last, f);
0115 }
0116
0117
0118 template<typename Range, typename Function>
0119 void parallel_for_each(Range& rng, const Function& f) {
0120 parallel_for_each(tbb::internal::first(rng), tbb::internal::last(rng), f);
0121 }
0122
0123
0124 template<typename Range, typename Function>
0125 void parallel_for_each(const Range& rng, const Function& f) {
0126 parallel_for_each(tbb::internal::first(rng), tbb::internal::last(rng), f);
0127 }
0128
0129
0130
0131 }
0132
0133 #endif