Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:12:58

0001 /*
0002     Copyright (c) 2005-2020 Intel Corporation
0003 
0004     Licensed under the Apache License, Version 2.0 (the "License");
0005     you may not use this file except in compliance with the License.
0006     You may obtain a copy of the License at
0007 
0008         http://www.apache.org/licenses/LICENSE-2.0
0009 
0010     Unless required by applicable law or agreed to in writing, software
0011     distributed under the License is distributed on an "AS IS" BASIS,
0012     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013     See the License for the specific language governing permissions and
0014     limitations under the License.
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 //! @cond INTERNAL
0026 namespace internal {
0027     // The class calls user function in operator()
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     // The class calls user function in operator()
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 } // namespace internal
0083 //! @endcond
0084 
0085 /** \name parallel_for_each
0086     **/
0087 //@{
0088 //! Calls function f for all items from [first, last) interval using user-supplied context
0089 /** @ingroup algorithms */
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 //! Calls function f for all items from rng using user-supplied context
0097 /** @ingroup algorithms */
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 //! Calls function f for all items from const rng user-supplied context
0104 /** @ingroup algorithms */
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 /* __TBB_TASK_GROUP_CONTEXT */
0110 
0111 //! Uses default context
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 //! Uses default context
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 //! Uses default context
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 } // namespace
0132 
0133 #endif /* __TBB_parallel_for_each_H */