Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:24:40

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 #include "../../tbb/internal/_deprecated_header_message_guard.h"
0018 
0019 #if !defined(__TBB_show_deprecation_message_parallel_for_H) && defined(__TBB_show_deprecated_header_message)
0020 #define  __TBB_show_deprecation_message_parallel_for_H
0021 #pragma message("TBB Warning: serial/tbb/parallel_for.h is deprecated. For details, please see Deprecated Features appendix in the TBB reference manual.")
0022 #endif
0023 
0024 #if defined(__TBB_show_deprecated_header_message)
0025 #undef __TBB_show_deprecated_header_message
0026 #endif
0027 
0028 #ifndef __TBB_SERIAL_parallel_for_H
0029 #define __TBB_SERIAL_parallel_for_H
0030 
0031 #include "tbb_annotate.h"
0032 
0033 #ifndef __TBB_NORMAL_EXECUTION
0034 #include "tbb/blocked_range.h"
0035 #include "tbb/partitioner.h"
0036 #endif
0037 
0038 #if TBB_USE_EXCEPTIONS
0039 #include <stdexcept>
0040 #include <string> // required to construct std exception classes
0041 #else
0042 #include <cstdlib>
0043 #include <iostream>
0044 #endif
0045 
0046 namespace tbb {
0047 namespace serial {
0048 namespace interface9 {
0049 
0050 // parallel_for serial annotated implementation
0051 
0052 template< typename Range, typename Body, typename Partitioner >
0053 class start_for : tbb::internal::no_copy {
0054     Range my_range;
0055     const Body my_body;
0056     typename Partitioner::task_partition_type my_partition;
0057     void execute();
0058 
0059     //! Constructor for root task.
0060     start_for( const Range& range, const Body& body, Partitioner& partitioner ) :
0061         my_range( range ),
0062         my_body( body ),
0063         my_partition( partitioner )
0064     {
0065     }
0066 
0067     //! Splitting constructor used to generate children.
0068     /** this becomes left child.  Newly constructed object is right child. */
0069     start_for( start_for& parent_, typename Partitioner::split_type& split_obj ) :
0070         my_range( parent_.my_range, split_obj ),
0071         my_body( parent_.my_body ),
0072         my_partition( parent_.my_partition, split_obj )
0073     {
0074     }
0075 
0076 public:
0077     static void run(  const Range& range, const Body& body, Partitioner& partitioner ) {
0078         if( !range.empty() ) {
0079             ANNOTATE_SITE_BEGIN( tbb_parallel_for );
0080             {
0081                 start_for a( range, body, partitioner );
0082                 a.execute();
0083             }
0084             ANNOTATE_SITE_END( tbb_parallel_for );
0085         }
0086     }
0087 };
0088 
0089 template< typename Range, typename Body, typename Partitioner >
0090 void start_for< Range, Body, Partitioner >::execute() {
0091     if( !my_range.is_divisible() || !my_partition.is_divisible() ) {
0092         ANNOTATE_TASK_BEGIN( tbb_parallel_for_range );
0093         {
0094             my_body( my_range );
0095         }
0096         ANNOTATE_TASK_END( tbb_parallel_for_range );
0097     } else {
0098         typename Partitioner::split_type split_obj;
0099         start_for b( *this, split_obj );
0100         this->execute(); // Execute the left interval first to keep the serial order.
0101         b.execute();     // Execute the right interval then.
0102     }
0103 }
0104 
0105 //! Parallel iteration over range with default partitioner.
0106 /** @ingroup algorithms **/
0107 template<typename Range, typename Body>
0108 __TBB_DEPRECATED_IN_VERBOSE_MODE void parallel_for( const Range& range, const Body& body ) {
0109     serial::interface9::start_for<Range,Body,const __TBB_DEFAULT_PARTITIONER>::run(range,body,__TBB_DEFAULT_PARTITIONER());
0110 }
0111 
0112 //! Parallel iteration over range with simple partitioner.
0113 /** @ingroup algorithms **/
0114 template<typename Range, typename Body>
0115 __TBB_DEPRECATED_IN_VERBOSE_MODE void parallel_for( const Range& range, const Body& body, const simple_partitioner& partitioner ) {
0116     serial::interface9::start_for<Range,Body,const simple_partitioner>::run(range,body,partitioner);
0117 }
0118 
0119 //! Parallel iteration over range with auto_partitioner.
0120 /** @ingroup algorithms **/
0121 template<typename Range, typename Body>
0122 __TBB_DEPRECATED_IN_VERBOSE_MODE void parallel_for( const Range& range, const Body& body, const auto_partitioner& partitioner ) {
0123     serial::interface9::start_for<Range,Body,const auto_partitioner>::run(range,body,partitioner);
0124 }
0125 
0126 //! Parallel iteration over range with static_partitioner.
0127 /** @ingroup algorithms **/
0128 template<typename Range, typename Body>
0129 __TBB_DEPRECATED_IN_VERBOSE_MODE void parallel_for( const Range& range, const Body& body, const static_partitioner& partitioner ) {
0130     serial::interface9::start_for<Range,Body,const static_partitioner>::run(range,body,partitioner);
0131 }
0132 
0133 //! Parallel iteration over range with affinity_partitioner.
0134 /** @ingroup algorithms **/
0135 template<typename Range, typename Body>
0136 __TBB_DEPRECATED_IN_VERBOSE_MODE void parallel_for( const Range& range, const Body& body, affinity_partitioner& partitioner ) {
0137     serial::interface9::start_for<Range,Body,affinity_partitioner>::run(range,body,partitioner);
0138 }
0139 
0140 //! Implementation of parallel iteration over stepped range of integers with explicit step and partitioner (ignored)
0141 template <typename Index, typename Function, typename Partitioner>
0142 void parallel_for_impl(Index first, Index last, Index step, const Function& f, Partitioner& ) {
0143     if (step <= 0 ) {
0144 #if TBB_USE_EXCEPTIONS
0145         throw std::invalid_argument( "nonpositive_step" );
0146 #else
0147         std::cerr << "nonpositive step in a call to parallel_for" << std::endl;
0148         std::abort();
0149 #endif
0150     } else if (last > first) {
0151         // Above "else" avoids "potential divide by zero" warning on some platforms
0152         ANNOTATE_SITE_BEGIN( tbb_parallel_for );
0153         for( Index i = first; i < last; i = i + step ) {
0154             ANNOTATE_TASK_BEGIN( tbb_parallel_for_iteration );
0155             { f( i ); }
0156             ANNOTATE_TASK_END( tbb_parallel_for_iteration );
0157         }
0158         ANNOTATE_SITE_END( tbb_parallel_for );
0159     }
0160 }
0161 
0162 //! Parallel iteration over a range of integers with explicit step and default partitioner
0163 template <typename Index, typename Function>
0164 __TBB_DEPRECATED_IN_VERBOSE_MODE void parallel_for(Index first, Index last, Index step, const Function& f) {
0165     parallel_for_impl<Index,Function,const auto_partitioner>(first, last, step, f, auto_partitioner());
0166 }
0167 //! Parallel iteration over a range of integers with explicit step and simple partitioner
0168 template <typename Index, typename Function>
0169 __TBB_DEPRECATED_IN_VERBOSE_MODE void parallel_for(Index first, Index last, Index step, const Function& f, const simple_partitioner& p) {
0170     parallel_for_impl<Index,Function,const simple_partitioner>(first, last, step, f, p);
0171 }
0172 //! Parallel iteration over a range of integers with explicit step and auto partitioner
0173 template <typename Index, typename Function>
0174 __TBB_DEPRECATED_IN_VERBOSE_MODE void parallel_for(Index first, Index last, Index step, const Function& f, const auto_partitioner& p) {
0175     parallel_for_impl<Index,Function,const auto_partitioner>(first, last, step, f, p);
0176 }
0177 //! Parallel iteration over a range of integers with explicit step and static partitioner
0178 template <typename Index, typename Function>
0179 __TBB_DEPRECATED_IN_VERBOSE_MODE void parallel_for(Index first, Index last, Index step, const Function& f, const static_partitioner& p) {
0180     parallel_for_impl<Index,Function,const static_partitioner>(first, last, step, f, p);
0181 }
0182 //! Parallel iteration over a range of integers with explicit step and affinity partitioner
0183 template <typename Index, typename Function>
0184 __TBB_DEPRECATED_IN_VERBOSE_MODE void parallel_for(Index first, Index last, Index step, const Function& f, affinity_partitioner& p) {
0185     parallel_for_impl(first, last, step, f, p);
0186 }
0187 
0188 //! Parallel iteration over a range of integers with default step and default partitioner
0189 template <typename Index, typename Function>
0190 __TBB_DEPRECATED_IN_VERBOSE_MODE void parallel_for(Index first, Index last, const Function& f) {
0191     parallel_for_impl<Index,Function,const auto_partitioner>(first, last, static_cast<Index>(1), f, auto_partitioner());
0192 }
0193 //! Parallel iteration over a range of integers with default step and simple partitioner
0194 template <typename Index, typename Function>
0195 __TBB_DEPRECATED_IN_VERBOSE_MODE void parallel_for(Index first, Index last, const Function& f, const simple_partitioner& p) {
0196     parallel_for_impl<Index,Function,const simple_partitioner>(first, last, static_cast<Index>(1), f, p);
0197 }
0198 //! Parallel iteration over a range of integers with default step and auto partitioner
0199 template <typename Index, typename Function>
0200 __TBB_DEPRECATED_IN_VERBOSE_MODE void parallel_for(Index first, Index last, const Function& f, const auto_partitioner& p) {
0201     parallel_for_impl<Index,Function,const auto_partitioner>(first, last, static_cast<Index>(1), f, p);
0202 }
0203 //! Parallel iteration over a range of integers with default step and static partitioner
0204 template <typename Index, typename Function>
0205 __TBB_DEPRECATED_IN_VERBOSE_MODE void parallel_for(Index first, Index last, const Function& f, const static_partitioner& p) {
0206     parallel_for_impl<Index,Function,const static_partitioner>(first, last, static_cast<Index>(1), f, p);
0207 }
0208 //! Parallel iteration over a range of integers with default step and affinity_partitioner
0209 template <typename Index, typename Function>
0210 __TBB_DEPRECATED_IN_VERBOSE_MODE void parallel_for(Index first, Index last, const Function& f, affinity_partitioner& p) {
0211     parallel_for_impl(first, last, static_cast<Index>(1), f, p);
0212 }
0213 
0214 } // namespace interfaceX
0215 
0216 using interface9::parallel_for;
0217 
0218 } // namespace serial
0219 
0220 #ifndef __TBB_NORMAL_EXECUTION
0221 using serial::interface9::parallel_for;
0222 #endif
0223 
0224 } // namespace tbb
0225 
0226 #endif /* __TBB_SERIAL_parallel_for_H */