partitioner.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /*
  2. Copyright 2005-2015 Intel Corporation. All Rights Reserved.
  3. This file is part of Threading Building Blocks. Threading Building Blocks is free software;
  4. you can redistribute it and/or modify it under the terms of the GNU General Public License
  5. version 2 as published by the Free Software Foundation. Threading Building Blocks is
  6. distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the GNU General Public License for more details. You should have received a copy of
  9. the GNU General Public License along with Threading Building Blocks; if not, write to the
  10. Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  11. As a special exception, you may use this file as part of a free software library without
  12. restriction. Specifically, if other files instantiate templates or use macros or inline
  13. functions from this file, or you compile this file and link it with other files to produce
  14. an executable, this file does not by itself cause the resulting executable to be covered
  15. by the GNU General Public License. This exception does not however invalidate any other
  16. reasons why the executable file might be covered by the GNU General Public License.
  17. */
  18. #ifndef __TBB_partitioner_H
  19. #define __TBB_partitioner_H
  20. #ifndef __TBB_INITIAL_CHUNKS
  21. // initial task divisions per thread
  22. #define __TBB_INITIAL_CHUNKS 2
  23. #endif
  24. #ifndef __TBB_RANGE_POOL_CAPACITY
  25. // maximum number of elements in range pool
  26. #define __TBB_RANGE_POOL_CAPACITY 8
  27. #endif
  28. #ifndef __TBB_INIT_DEPTH
  29. // initial value for depth of range pool
  30. #define __TBB_INIT_DEPTH 5
  31. #endif
  32. #ifndef __TBB_DEMAND_DEPTH_ADD
  33. // when imbalance is found range splits this value times more
  34. #define __TBB_DEMAND_DEPTH_ADD 2
  35. #endif
  36. #ifndef __TBB_STATIC_THRESHOLD
  37. // necessary number of clocks for the work to be distributed among all tasks
  38. #define __TBB_STATIC_THRESHOLD 40000
  39. #endif
  40. #if __TBB_DEFINE_MIC
  41. #define __TBB_NONUNIFORM_TASK_CREATION 1
  42. #ifdef __TBB_machine_time_stamp
  43. #define __TBB_USE_MACHINE_TIME_STAMPS 1
  44. #define __TBB_task_duration() __TBB_STATIC_THRESHOLD
  45. #endif // __TBB_machine_time_stamp
  46. #endif // __TBB_DEFINE_MIC
  47. #include "task.h"
  48. #include "aligned_space.h"
  49. #include "atomic.h"
  50. #include "internal/_template_helpers.h"
  51. #if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
  52. // Workaround for overzealous compiler warnings
  53. #pragma warning (push)
  54. #pragma warning (disable: 4244)
  55. #endif
  56. namespace tbb {
  57. class auto_partitioner;
  58. class simple_partitioner;
  59. class affinity_partitioner;
  60. namespace interface7 {
  61. namespace internal {
  62. class affinity_partition_type;
  63. }
  64. }
  65. namespace internal { //< @cond INTERNAL
  66. size_t __TBB_EXPORTED_FUNC get_initial_auto_partitioner_divisor();
  67. //! Defines entry point for affinity partitioner into tbb run-time library.
  68. class affinity_partitioner_base_v3: no_copy {
  69. friend class tbb::affinity_partitioner;
  70. friend class tbb::interface7::internal::affinity_partition_type;
  71. //! Array that remembers affinities of tree positions to affinity_id.
  72. /** NULL if my_size==0. */
  73. affinity_id* my_array;
  74. //! Number of elements in my_array.
  75. size_t my_size;
  76. //! Zeros the fields.
  77. affinity_partitioner_base_v3() : my_array(NULL), my_size(0) {}
  78. //! Deallocates my_array.
  79. ~affinity_partitioner_base_v3() {resize(0);}
  80. //! Resize my_array.
  81. /** Retains values if resulting size is the same. */
  82. void __TBB_EXPORTED_METHOD resize( unsigned factor );
  83. };
  84. //! Provides backward-compatible methods for partition objects without affinity.
  85. class partition_type_base {
  86. public:
  87. void set_affinity( task & ) {}
  88. void note_affinity( task::affinity_id ) {}
  89. task* continue_after_execute_range() {return NULL;}
  90. bool decide_whether_to_delay() {return false;}
  91. void spawn_or_delay( bool, task& b ) {
  92. task::spawn(b);
  93. }
  94. };
  95. template<typename Range, typename Body, typename Partitioner> class start_scan;
  96. } //< namespace internal @endcond
  97. namespace serial {
  98. namespace interface7 {
  99. template<typename Range, typename Body, typename Partitioner> class start_for;
  100. }
  101. }
  102. namespace interface7 {
  103. //! @cond INTERNAL
  104. namespace internal {
  105. using namespace tbb::internal;
  106. template<typename Range, typename Body, typename Partitioner> class start_for;
  107. template<typename Range, typename Body, typename Partitioner> class start_reduce;
  108. //! Join task node that contains shared flag for stealing feedback
  109. class flag_task: public task {
  110. public:
  111. tbb::atomic<bool> my_child_stolen;
  112. flag_task() { my_child_stolen = false; }
  113. task* execute() { return NULL; }
  114. static void mark_task_stolen(task &t) {
  115. tbb::atomic<bool> &flag = static_cast<flag_task*>(t.parent())->my_child_stolen;
  116. #if TBB_USE_THREADING_TOOLS
  117. // Threading tools respect lock prefix but report false-positive data-race via plain store
  118. flag.fetch_and_store<release>(true);
  119. #else
  120. flag = true;
  121. #endif //TBB_USE_THREADING_TOOLS
  122. }
  123. static bool is_peer_stolen(task &t) {
  124. return static_cast<flag_task*>(t.parent())->my_child_stolen;
  125. }
  126. };
  127. //! Depth is a relative depth of recursive division inside a range pool. Relative depth allows
  128. //! infinite absolute depth of the recursion for heavily unbalanced workloads with range represented
  129. //! by a number that cannot fit into machine word.
  130. typedef unsigned char depth_t;
  131. //! Range pool stores ranges of type T in a circular buffer with MaxCapacity
  132. template <typename T, depth_t MaxCapacity>
  133. class range_vector {
  134. depth_t my_head;
  135. depth_t my_tail;
  136. depth_t my_size;
  137. depth_t my_depth[MaxCapacity]; // relative depths of stored ranges
  138. tbb::aligned_space<T, MaxCapacity> my_pool;
  139. public:
  140. //! initialize via first range in pool
  141. range_vector(const T& elem) : my_head(0), my_tail(0), my_size(1) {
  142. my_depth[0] = 0;
  143. new( static_cast<void *>(my_pool.begin()) ) T(elem);//TODO: std::move?
  144. }
  145. ~range_vector() {
  146. while( !empty() ) pop_back();
  147. }
  148. bool empty() const { return my_size == 0; }
  149. depth_t size() const { return my_size; }
  150. //! Populates range pool via ranges up to max depth or while divisible
  151. //! max_depth starts from 0, e.g. value 2 makes 3 ranges in the pool up to two 1/4 pieces
  152. void split_to_fill(depth_t max_depth) {
  153. while( my_size < MaxCapacity && is_divisible(max_depth) ) {
  154. depth_t prev = my_head;
  155. my_head = (my_head + 1) % MaxCapacity;
  156. new(my_pool.begin()+my_head) T(my_pool.begin()[prev]); // copy TODO: std::move?
  157. my_pool.begin()[prev].~T(); // instead of assignment
  158. new(my_pool.begin()+prev) T(my_pool.begin()[my_head], split()); // do 'inverse' split
  159. my_depth[my_head] = ++my_depth[prev];
  160. my_size++;
  161. }
  162. }
  163. void pop_back() {
  164. __TBB_ASSERT(my_size > 0, "range_vector::pop_back() with empty size");
  165. my_pool.begin()[my_head].~T();
  166. my_size--;
  167. my_head = (my_head + MaxCapacity - 1) % MaxCapacity;
  168. }
  169. void pop_front() {
  170. __TBB_ASSERT(my_size > 0, "range_vector::pop_front() with empty size");
  171. my_pool.begin()[my_tail].~T();
  172. my_size--;
  173. my_tail = (my_tail + 1) % MaxCapacity;
  174. }
  175. T& back() {
  176. __TBB_ASSERT(my_size > 0, "range_vector::back() with empty size");
  177. return my_pool.begin()[my_head];
  178. }
  179. T& front() {
  180. __TBB_ASSERT(my_size > 0, "range_vector::front() with empty size");
  181. return my_pool.begin()[my_tail];
  182. }
  183. //! similarly to front(), returns depth of the first range in the pool
  184. depth_t front_depth() {
  185. __TBB_ASSERT(my_size > 0, "range_vector::front_depth() with empty size");
  186. return my_depth[my_tail];
  187. }
  188. depth_t back_depth() {
  189. __TBB_ASSERT(my_size > 0, "range_vector::back_depth() with empty size");
  190. return my_depth[my_head];
  191. }
  192. bool is_divisible(depth_t max_depth) {
  193. return back_depth() < max_depth && back().is_divisible();
  194. }
  195. };
  196. //! Provides default methods for partition objects and common algorithm blocks.
  197. template <typename Partition>
  198. struct partition_type_base {
  199. typedef split split_type;
  200. // decision makers
  201. void set_affinity( task & ) {}
  202. void note_affinity( task::affinity_id ) {}
  203. bool check_being_stolen(task &) { return false; } // part of old should_execute_range()
  204. bool check_for_demand(task &) { return false; }
  205. bool is_divisible() { return true; } // part of old should_execute_range()
  206. depth_t max_depth() { return 0; }
  207. void align_depth(depth_t) { }
  208. template <typename Range> split_type get_split() { return split(); }
  209. // common function blocks
  210. Partition& self() { return *static_cast<Partition*>(this); } // CRTP helper
  211. template<typename StartType, typename Range>
  212. void execute(StartType &start, Range &range) {
  213. // The algorithm in a few words ([]-denotes calls to decision methods of partitioner):
  214. // [If this task is stolen, adjust depth and divisions if necessary, set flag].
  215. // If range is divisible {
  216. // Spread the work while [initial divisions left];
  217. // Create trap task [if necessary];
  218. // }
  219. // If not divisible or [max depth is reached], execute, else do the range pool part
  220. if ( range.is_divisible() ) {
  221. if ( self().is_divisible() ) {
  222. do { // split until is divisible
  223. typename Partition::split_type split_obj = self().template get_split<Range>();
  224. start.offer_work( split_obj );
  225. } while ( range.is_divisible() && self().is_divisible() );
  226. }
  227. }
  228. if( !range.is_divisible() || !self().max_depth() )
  229. start.run_body( range ); // simple partitioner goes always here
  230. else { // do range pool
  231. internal::range_vector<Range, Partition::range_pool_size> range_pool(range);
  232. do {
  233. range_pool.split_to_fill(self().max_depth()); // fill range pool
  234. if( self().check_for_demand( start ) ) {
  235. if( range_pool.size() > 1 ) {
  236. start.offer_work( range_pool.front(), range_pool.front_depth() );
  237. range_pool.pop_front();
  238. continue;
  239. }
  240. if( range_pool.is_divisible(self().max_depth()) ) // was not enough depth to fork a task
  241. continue; // note: next split_to_fill() should split range at least once
  242. }
  243. start.run_body( range_pool.back() );
  244. range_pool.pop_back();
  245. } while( !range_pool.empty() && !start.is_cancelled() );
  246. }
  247. }
  248. };
  249. //! Provides default methods for auto (adaptive) partition objects.
  250. template <typename Partition>
  251. struct adaptive_partition_type_base : partition_type_base<Partition> {
  252. size_t my_divisor;
  253. depth_t my_max_depth;
  254. static const unsigned factor = 1;
  255. adaptive_partition_type_base() : my_max_depth(__TBB_INIT_DEPTH) {
  256. my_divisor = tbb::internal::get_initial_auto_partitioner_divisor() / 4 * Partition::factor;
  257. __TBB_ASSERT(my_divisor, "initial value of get_initial_auto_partitioner_divisor() is not valid");
  258. }
  259. adaptive_partition_type_base(adaptive_partition_type_base &src, split) {
  260. my_max_depth = src.my_max_depth;
  261. #if TBB_USE_ASSERT
  262. size_t old_divisor = src.my_divisor;
  263. #endif
  264. #if __TBB_INITIAL_TASK_IMBALANCE
  265. if( src.my_divisor <= 1 ) my_divisor = 0;
  266. else my_divisor = src.my_divisor = (src.my_divisor + 1u) / 2u;
  267. #else
  268. my_divisor = src.my_divisor / 2u;
  269. src.my_divisor = src.my_divisor - my_divisor; // TODO: check the effect separately
  270. if (my_divisor) src.my_max_depth += static_cast<depth_t>(__TBB_Log2(src.my_divisor / my_divisor));
  271. #endif
  272. // For affinity_partitioner, my_divisor indicates the number of affinity array indices the task reserves.
  273. // A task which has only one index must produce the right split without reserved index in order to avoid
  274. // it to be overwritten in note_affinity() of the created (right) task.
  275. // I.e. a task created deeper than the affinity array can remember must not save its affinity (LIFO order)
  276. __TBB_ASSERT( (old_divisor <= 1 && my_divisor == 0) ||
  277. (old_divisor > 1 && my_divisor != 0), NULL);
  278. }
  279. adaptive_partition_type_base(adaptive_partition_type_base &src, const proportional_split& split_obj) {
  280. my_max_depth = src.my_max_depth;
  281. #if __TBB_ENABLE_RANGE_FEEDBACK
  282. my_divisor = size_t(float(src.my_divisor) * float(split_obj.right())
  283. / float(split_obj.left() + split_obj.right()));
  284. #else
  285. my_divisor = split_obj.right() * Partition::factor;
  286. #endif
  287. src.my_divisor -= my_divisor;
  288. }
  289. bool check_being_stolen( task &t) { // part of old should_execute_range()
  290. if( !my_divisor ) { // if not from the top P tasks of binary tree
  291. my_divisor = 1; // TODO: replace by on-stack flag (partition_state's member)?
  292. if( t.is_stolen_task() && t.parent()->ref_count() >= 2 ) { // runs concurrently with the left task
  293. #if TBB_USE_EXCEPTIONS
  294. // RTTI is available, check whether the cast is valid
  295. __TBB_ASSERT(dynamic_cast<flag_task*>(t.parent()), 0);
  296. // correctness of the cast relies on avoiding the root task for which:
  297. // - initial value of my_divisor != 0 (protected by separate assertion)
  298. // - is_stolen_task() always returns false for the root task.
  299. #endif
  300. flag_task::mark_task_stolen(t);
  301. if( !my_max_depth ) my_max_depth++;
  302. my_max_depth += __TBB_DEMAND_DEPTH_ADD;
  303. return true;
  304. }
  305. }
  306. return false;
  307. }
  308. void align_depth(depth_t base) {
  309. __TBB_ASSERT(base <= my_max_depth, 0);
  310. my_max_depth -= base;
  311. }
  312. depth_t max_depth() { return my_max_depth; }
  313. };
  314. //! Class determines whether template parameter has static boolean constant
  315. //! 'is_splittable_in_proportion' initialized with value of 'true' or not.
  316. /** If template parameter has such field that has been initialized with non-zero
  317. * value then class field will be set to 'true', otherwise - 'false'
  318. */
  319. template <typename Range>
  320. class is_splittable_in_proportion {
  321. private:
  322. typedef char yes[1];
  323. typedef char no [2];
  324. template <typename range_type> static yes& decide(typename enable_if<range_type::is_splittable_in_proportion>::type *);
  325. template <typename range_type> static no& decide(...);
  326. public:
  327. // equals to 'true' if and only if static const variable 'is_splittable_in_proportion' of template parameter
  328. // initialized with the value of 'true'
  329. static const bool value = (sizeof(decide<Range>(0)) == sizeof(yes));
  330. };
  331. //! Provides default methods for affinity (adaptive) partition objects.
  332. class affinity_partition_type : public adaptive_partition_type_base<affinity_partition_type> {
  333. static const unsigned factor_power = 4;
  334. enum {
  335. start = 0,
  336. run,
  337. pass
  338. } my_delay;
  339. #ifdef __TBB_USE_MACHINE_TIME_STAMPS
  340. machine_tsc_t my_dst_tsc;
  341. #endif
  342. size_t my_begin;
  343. tbb::internal::affinity_id* my_array;
  344. public:
  345. static const unsigned factor = 1 << factor_power; // number of slots in affinity array per task
  346. typedef proportional_split split_type;
  347. affinity_partition_type( tbb::internal::affinity_partitioner_base_v3& ap )
  348. : adaptive_partition_type_base<affinity_partition_type>(), my_delay(start)
  349. #ifdef __TBB_USE_MACHINE_TIME_STAMPS
  350. , my_dst_tsc(0)
  351. #endif
  352. {
  353. __TBB_ASSERT( (factor&(factor-1))==0, "factor must be power of two" );
  354. ap.resize(factor);
  355. my_array = ap.my_array;
  356. my_begin = 0;
  357. my_max_depth = factor_power + 1; // the first factor_power ranges will be spawned, and >=1 ranges should be left
  358. __TBB_ASSERT( my_max_depth < __TBB_RANGE_POOL_CAPACITY, 0 );
  359. }
  360. affinity_partition_type(affinity_partition_type& p, split)
  361. : adaptive_partition_type_base<affinity_partition_type>(p, split()),
  362. my_delay(pass),
  363. #ifdef __TBB_USE_MACHINE_TIME_STAMPS
  364. my_dst_tsc(0),
  365. #endif
  366. my_array(p.my_array) {
  367. // the sum of the divisors represents original value of p.my_divisor before split
  368. __TBB_ASSERT(my_divisor + p.my_divisor <= factor, NULL);
  369. my_begin = p.my_begin + p.my_divisor;
  370. }
  371. affinity_partition_type(affinity_partition_type& p, const proportional_split& split_obj)
  372. : adaptive_partition_type_base<affinity_partition_type>(p, split_obj),
  373. my_delay(start),
  374. #ifdef __TBB_USE_MACHINE_TIME_STAMPS
  375. my_dst_tsc(0),
  376. #endif
  377. my_array(p.my_array) {
  378. size_t total_divisor = my_divisor + p.my_divisor;
  379. __TBB_ASSERT(total_divisor % factor == 0, NULL);
  380. my_divisor = (my_divisor + factor/2) & (0u - factor);
  381. #if __TBB_ENABLE_RANGE_FEEDBACK
  382. if (!my_divisor)
  383. my_divisor = factor;
  384. else if (my_divisor == total_divisor)
  385. my_divisor = total_divisor - factor;
  386. #endif
  387. p.my_divisor = total_divisor - my_divisor;
  388. __TBB_ASSERT(my_divisor && p.my_divisor, NULL);
  389. my_begin = p.my_begin + p.my_divisor;
  390. }
  391. void set_affinity( task &t ) {
  392. if( my_divisor ) {
  393. if( !my_array[my_begin] )
  394. // TODO: consider code reuse for static_paritioner
  395. t.set_affinity( affinity_id(my_begin / factor + 1) );
  396. else
  397. t.set_affinity( my_array[my_begin] );
  398. }
  399. }
  400. void note_affinity( task::affinity_id id ) {
  401. if( my_divisor )
  402. my_array[my_begin] = id;
  403. }
  404. bool check_for_demand( task &t ) {
  405. if( pass == my_delay ) {
  406. if( my_divisor > 1 ) // produce affinitized tasks while they have slot in array
  407. return true; // do not do my_max_depth++ here, but be sure range_pool is splittable once more
  408. else if( my_divisor && my_max_depth ) { // make balancing task
  409. my_divisor = 0; // once for each task; depth will be decreased in align_depth()
  410. return true;
  411. }
  412. else if( flag_task::is_peer_stolen(t) ) {
  413. my_max_depth += __TBB_DEMAND_DEPTH_ADD;
  414. return true;
  415. }
  416. } else if( start == my_delay ) {
  417. #ifndef __TBB_USE_MACHINE_TIME_STAMPS
  418. my_delay = pass;
  419. #else
  420. my_dst_tsc = __TBB_machine_time_stamp() + __TBB_task_duration();
  421. my_delay = run;
  422. } else if( run == my_delay ) {
  423. if( __TBB_machine_time_stamp() < my_dst_tsc ) {
  424. __TBB_ASSERT(my_max_depth > 0, NULL);
  425. return false;
  426. }
  427. my_delay = pass;
  428. return true;
  429. #endif // __TBB_USE_MACHINE_TIME_STAMPS
  430. }
  431. return false;
  432. }
  433. bool is_divisible() { // part of old should_execute_range()
  434. return my_divisor > factor;
  435. }
  436. #if _MSC_VER && !defined(__INTEL_COMPILER)
  437. // Suppress "conditional expression is constant" warning.
  438. #pragma warning( push )
  439. #pragma warning( disable: 4127 )
  440. #endif
  441. template <typename Range>
  442. split_type get_split() {
  443. if (is_splittable_in_proportion<Range>::value) {
  444. size_t size = my_divisor / factor;
  445. #if __TBB_NONUNIFORM_TASK_CREATION
  446. size_t right = (size + 2) / 3;
  447. #else
  448. size_t right = size / 2;
  449. #endif
  450. size_t left = size - right;
  451. return split_type(left, right);
  452. } else {
  453. return split_type(1, 1);
  454. }
  455. }
  456. #if _MSC_VER && !defined(__INTEL_COMPILER)
  457. #pragma warning( pop )
  458. #endif // warning 4127 is back
  459. static const unsigned range_pool_size = __TBB_RANGE_POOL_CAPACITY;
  460. };
  461. class auto_partition_type: public adaptive_partition_type_base<auto_partition_type> {
  462. public:
  463. auto_partition_type( const auto_partitioner& ) {
  464. my_divisor *= __TBB_INITIAL_CHUNKS;
  465. }
  466. auto_partition_type( auto_partition_type& src, split)
  467. : adaptive_partition_type_base<auto_partition_type>(src, split()) {}
  468. bool is_divisible() { // part of old should_execute_range()
  469. if( my_divisor > 1 ) return true;
  470. if( my_divisor && my_max_depth ) { // can split the task. TODO: on-stack flag instead
  471. // keep same fragmentation while splitting for the local task pool
  472. my_max_depth--;
  473. my_divisor = 0; // decrease max_depth once per task
  474. return true;
  475. } else return false;
  476. }
  477. bool check_for_demand(task &t) {
  478. if( flag_task::is_peer_stolen(t) ) {
  479. my_max_depth += __TBB_DEMAND_DEPTH_ADD;
  480. return true;
  481. } else return false;
  482. }
  483. static const unsigned range_pool_size = __TBB_RANGE_POOL_CAPACITY;
  484. };
  485. class simple_partition_type: public partition_type_base<simple_partition_type> {
  486. public:
  487. simple_partition_type( const simple_partitioner& ) {}
  488. simple_partition_type( const simple_partition_type&, split ) {}
  489. //! simplified algorithm
  490. template<typename StartType, typename Range>
  491. void execute(StartType &start, Range &range) {
  492. split_type split_obj = split(); // start.offer_work accepts split_type as reference
  493. while( range.is_divisible() )
  494. start.offer_work( split_obj );
  495. start.run_body( range );
  496. }
  497. //static const unsigned range_pool_size = 1; - not necessary because execute() is overridden
  498. };
  499. //! Backward-compatible partition for auto and affinity partition objects.
  500. class old_auto_partition_type: public tbb::internal::partition_type_base {
  501. size_t num_chunks;
  502. static const size_t VICTIM_CHUNKS = 4;
  503. public:
  504. bool should_execute_range(const task &t) {
  505. if( num_chunks<VICTIM_CHUNKS && t.is_stolen_task() )
  506. num_chunks = VICTIM_CHUNKS;
  507. return num_chunks==1;
  508. }
  509. old_auto_partition_type( const auto_partitioner& )
  510. : num_chunks(internal::get_initial_auto_partitioner_divisor()*__TBB_INITIAL_CHUNKS/4) {}
  511. old_auto_partition_type( const affinity_partitioner& )
  512. : num_chunks(internal::get_initial_auto_partitioner_divisor()*__TBB_INITIAL_CHUNKS/4) {}
  513. old_auto_partition_type( old_auto_partition_type& pt, split ) {
  514. num_chunks = pt.num_chunks = (pt.num_chunks+1u) / 2u;
  515. }
  516. };
  517. } // namespace interfaceX::internal
  518. //! @endcond
  519. } // namespace interfaceX
  520. //! A simple partitioner
  521. /** Divides the range until the range is not divisible.
  522. @ingroup algorithms */
  523. class simple_partitioner {
  524. public:
  525. simple_partitioner() {}
  526. private:
  527. template<typename Range, typename Body, typename Partitioner> friend class serial::interface7::start_for;
  528. template<typename Range, typename Body, typename Partitioner> friend class interface7::internal::start_for;
  529. template<typename Range, typename Body, typename Partitioner> friend class interface7::internal::start_reduce;
  530. template<typename Range, typename Body, typename Partitioner> friend class internal::start_scan;
  531. // backward compatibility
  532. class partition_type: public internal::partition_type_base {
  533. public:
  534. bool should_execute_range(const task& ) {return false;}
  535. partition_type( const simple_partitioner& ) {}
  536. partition_type( const partition_type&, split ) {}
  537. };
  538. // new implementation just extends existing interface
  539. typedef interface7::internal::simple_partition_type task_partition_type;
  540. // TODO: consider to make split_type public
  541. typedef interface7::internal::simple_partition_type::split_type split_type;
  542. };
  543. //! An auto partitioner
  544. /** The range is initial divided into several large chunks.
  545. Chunks are further subdivided into smaller pieces if demand detected and they are divisible.
  546. @ingroup algorithms */
  547. class auto_partitioner {
  548. public:
  549. auto_partitioner() {}
  550. private:
  551. template<typename Range, typename Body, typename Partitioner> friend class serial::interface7::start_for;
  552. template<typename Range, typename Body, typename Partitioner> friend class interface7::internal::start_for;
  553. template<typename Range, typename Body, typename Partitioner> friend class interface7::internal::start_reduce;
  554. template<typename Range, typename Body, typename Partitioner> friend class internal::start_scan;
  555. // backward compatibility
  556. typedef interface7::internal::old_auto_partition_type partition_type;
  557. // new implementation just extends existing interface
  558. typedef interface7::internal::auto_partition_type task_partition_type;
  559. // TODO: consider to make split_type public
  560. typedef interface7::internal::auto_partition_type::split_type split_type;
  561. };
  562. //! An affinity partitioner
  563. class affinity_partitioner: internal::affinity_partitioner_base_v3 {
  564. public:
  565. affinity_partitioner() {}
  566. private:
  567. template<typename Range, typename Body, typename Partitioner> friend class serial::interface7::start_for;
  568. template<typename Range, typename Body, typename Partitioner> friend class interface7::internal::start_for;
  569. template<typename Range, typename Body, typename Partitioner> friend class interface7::internal::start_reduce;
  570. template<typename Range, typename Body, typename Partitioner> friend class internal::start_scan;
  571. // backward compatibility - for parallel_scan only
  572. typedef interface7::internal::old_auto_partition_type partition_type;
  573. // new implementation just extends existing interface
  574. typedef interface7::internal::affinity_partition_type task_partition_type;
  575. // TODO: consider to make split_type public
  576. typedef interface7::internal::affinity_partition_type::split_type split_type;
  577. };
  578. } // namespace tbb
  579. #if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
  580. #pragma warning (pop)
  581. #endif // warning 4244 is back
  582. #undef __TBB_INITIAL_CHUNKS
  583. #undef __TBB_RANGE_POOL_CAPACITY
  584. #undef __TBB_INIT_DEPTH
  585. #endif /* __TBB_partitioner_H */