partitioner.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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_partitioner_H
  14. #define __TBB_partitioner_H
  15. #define __TBB_partitioner_H_include_area
  16. #include "internal/_warning_suppress_enable_notice.h"
  17. #ifndef __TBB_INITIAL_CHUNKS
  18. // initial task divisions per thread
  19. #define __TBB_INITIAL_CHUNKS 2
  20. #endif
  21. #ifndef __TBB_RANGE_POOL_CAPACITY
  22. // maximum number of elements in range pool
  23. #define __TBB_RANGE_POOL_CAPACITY 8
  24. #endif
  25. #ifndef __TBB_INIT_DEPTH
  26. // initial value for depth of range pool
  27. #define __TBB_INIT_DEPTH 5
  28. #endif
  29. #ifndef __TBB_DEMAND_DEPTH_ADD
  30. // when imbalance is found range splits this value times more
  31. #define __TBB_DEMAND_DEPTH_ADD 1
  32. #endif
  33. #ifndef __TBB_STATIC_THRESHOLD
  34. // necessary number of clocks for the work to be distributed among all tasks
  35. #define __TBB_STATIC_THRESHOLD 40000
  36. #endif
  37. #if __TBB_DEFINE_MIC
  38. #define __TBB_NONUNIFORM_TASK_CREATION 1
  39. #ifdef __TBB_time_stamp
  40. #define __TBB_USE_MACHINE_TIME_STAMPS 1
  41. #define __TBB_task_duration() __TBB_STATIC_THRESHOLD
  42. #endif // __TBB_machine_time_stamp
  43. #endif // __TBB_DEFINE_MIC
  44. #include "task.h"
  45. #include "task_arena.h"
  46. #include "aligned_space.h"
  47. #include "atomic.h"
  48. #include "internal/_template_helpers.h"
  49. #if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
  50. // Workaround for overzealous compiler warnings
  51. #pragma warning (push)
  52. #pragma warning (disable: 4244)
  53. #endif
  54. namespace tbb {
  55. class auto_partitioner;
  56. class simple_partitioner;
  57. class static_partitioner;
  58. class affinity_partitioner;
  59. namespace interface9 {
  60. namespace internal {
  61. class affinity_partition_type;
  62. }
  63. }
  64. namespace internal { //< @cond INTERNAL
  65. size_t __TBB_EXPORTED_FUNC get_initial_auto_partitioner_divisor();
  66. //! Defines entry point for affinity partitioner into TBB run-time library.
  67. class affinity_partitioner_base_v3: no_copy {
  68. friend class tbb::affinity_partitioner;
  69. friend class tbb::interface9::internal::affinity_partition_type;
  70. //! Array that remembers affinities of tree positions to affinity_id.
  71. /** NULL if my_size==0. */
  72. affinity_id* my_array;
  73. //! Number of elements in my_array.
  74. size_t my_size;
  75. //! Zeros the fields.
  76. affinity_partitioner_base_v3() : my_array(NULL), my_size(0) {}
  77. //! Deallocates my_array.
  78. ~affinity_partitioner_base_v3() {resize(0);}
  79. //! Resize my_array.
  80. /** Retains values if resulting size is the same. */
  81. void __TBB_EXPORTED_METHOD resize( unsigned factor );
  82. };
  83. //! Provides backward-compatible methods for partition objects without affinity.
  84. class partition_type_base {
  85. public:
  86. void set_affinity( task & ) {}
  87. void note_affinity( task::affinity_id ) {}
  88. task* continue_after_execute_range() {return NULL;}
  89. bool decide_whether_to_delay() {return false;}
  90. void spawn_or_delay( bool, task& b ) {
  91. task::spawn(b);
  92. }
  93. };
  94. template<typename Range, typename Body, typename Partitioner> class start_scan;
  95. } //< namespace internal @endcond
  96. namespace serial {
  97. namespace interface9 {
  98. template<typename Range, typename Body, typename Partitioner> class start_for;
  99. }
  100. }
  101. namespace interface9 {
  102. //! @cond INTERNAL
  103. namespace internal {
  104. using namespace tbb::internal;
  105. template<typename Range, typename Body, typename Partitioner> class start_for;
  106. template<typename Range, typename Body, typename Partitioner> class start_reduce;
  107. template<typename Range, typename Body, typename Partitioner> class start_deterministic_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() __TBB_override { 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. Partition& self() { return *static_cast<Partition*>(this); } // CRTP helper
  210. template<typename StartType, typename Range>
  211. void work_balance(StartType &start, Range &range) {
  212. start.run_body( range ); // simple partitioner goes always here
  213. }
  214. template<typename StartType, typename Range>
  215. void execute(StartType &start, Range &range) {
  216. // The algorithm in a few words ([]-denotes calls to decision methods of partitioner):
  217. // [If this task is stolen, adjust depth and divisions if necessary, set flag].
  218. // If range is divisible {
  219. // Spread the work while [initial divisions left];
  220. // Create trap task [if necessary];
  221. // }
  222. // If not divisible or [max depth is reached], execute, else do the range pool part
  223. if ( range.is_divisible() ) {
  224. if ( self().is_divisible() ) {
  225. do { // split until is divisible
  226. typename Partition::split_type split_obj = self().template get_split<Range>();
  227. start.offer_work( split_obj );
  228. } while ( range.is_divisible() && self().is_divisible() );
  229. }
  230. }
  231. self().work_balance(start, range);
  232. }
  233. };
  234. //! Provides default splitting strategy for partition objects.
  235. template <typename Partition>
  236. struct adaptive_mode : partition_type_base<Partition> {
  237. typedef Partition my_partition;
  238. size_t my_divisor;
  239. // For affinity_partitioner, my_divisor indicates the number of affinity array indices the task reserves.
  240. // A task which has only one index must produce the right split without reserved index in order to avoid
  241. // it to be overwritten in note_affinity() of the created (right) task.
  242. // I.e. a task created deeper than the affinity array can remember must not save its affinity (LIFO order)
  243. static const unsigned factor = 1;
  244. adaptive_mode() : my_divisor(tbb::internal::get_initial_auto_partitioner_divisor() / 4 * my_partition::factor) {}
  245. adaptive_mode(adaptive_mode &src, split) : my_divisor(do_split(src, split())) {}
  246. /*! Override do_split methods in order to specify splitting strategy */
  247. size_t do_split(adaptive_mode &src, split) {
  248. return src.my_divisor /= 2u;
  249. }
  250. };
  251. //! A helper class to create a proportional_split object for a given type of Range.
  252. /** If the Range has static boolean constant 'is_splittable_in_proportion' set to 'true',
  253. the created object splits a provided value in an implemenation-defined proportion;
  254. otherwise it represents equal-size split. */
  255. // TODO: check if this helper can be a nested class of proportional_mode.
  256. template <typename Range, typename = void>
  257. struct proportion_helper {
  258. static proportional_split get_split(size_t) { return proportional_split(1,1); }
  259. };
  260. template <typename Range>
  261. struct proportion_helper<Range, typename enable_if<Range::is_splittable_in_proportion, void>::type> {
  262. static proportional_split get_split(size_t n) {
  263. #if __TBB_NONUNIFORM_TASK_CREATION
  264. size_t right = (n + 2) / 3;
  265. #else
  266. size_t right = n / 2;
  267. #endif
  268. size_t left = n - right;
  269. return proportional_split(left, right);
  270. }
  271. };
  272. //! Provides proportional splitting strategy for partition objects
  273. template <typename Partition>
  274. struct proportional_mode : adaptive_mode<Partition> {
  275. typedef Partition my_partition;
  276. using partition_type_base<Partition>::self; // CRTP helper to get access to derived classes
  277. proportional_mode() : adaptive_mode<Partition>() {}
  278. proportional_mode(proportional_mode &src, split) : adaptive_mode<Partition>(src, split()) {}
  279. proportional_mode(proportional_mode &src, const proportional_split& split_obj) { self().my_divisor = do_split(src, split_obj); }
  280. size_t do_split(proportional_mode &src, const proportional_split& split_obj) {
  281. #if __TBB_ENABLE_RANGE_FEEDBACK
  282. size_t portion = size_t(float(src.my_divisor) * float(split_obj.right())
  283. / float(split_obj.left() + split_obj.right()) + 0.5f);
  284. #else
  285. size_t portion = split_obj.right() * my_partition::factor;
  286. #endif
  287. portion = (portion + my_partition::factor/2) & (0ul - my_partition::factor);
  288. #if __TBB_ENABLE_RANGE_FEEDBACK
  289. /** Corner case handling */
  290. if (!portion)
  291. portion = my_partition::factor;
  292. else if (portion == src.my_divisor)
  293. portion = src.my_divisor - my_partition::factor;
  294. #endif
  295. src.my_divisor -= portion;
  296. return portion;
  297. }
  298. bool is_divisible() { // part of old should_execute_range()
  299. return self().my_divisor > my_partition::factor;
  300. }
  301. template <typename Range>
  302. proportional_split get_split() {
  303. // Create a proportion for the number of threads expected to handle "this" subrange
  304. return proportion_helper<Range>::get_split( self().my_divisor / my_partition::factor );
  305. }
  306. };
  307. static size_t get_initial_partition_head() {
  308. int current_index = tbb::this_task_arena::current_thread_index();
  309. if (current_index == tbb::task_arena::not_initialized)
  310. current_index = 0;
  311. return size_t(current_index);
  312. }
  313. //! Provides default linear indexing of partitioner's sequence
  314. template <typename Partition>
  315. struct linear_affinity_mode : proportional_mode<Partition> {
  316. size_t my_head;
  317. size_t my_max_affinity;
  318. using proportional_mode<Partition>::self;
  319. linear_affinity_mode() : proportional_mode<Partition>(), my_head(get_initial_partition_head()),
  320. my_max_affinity(self().my_divisor) {}
  321. linear_affinity_mode(linear_affinity_mode &src, split) : proportional_mode<Partition>(src, split())
  322. , my_head((src.my_head + src.my_divisor) % src.my_max_affinity), my_max_affinity(src.my_max_affinity) {}
  323. linear_affinity_mode(linear_affinity_mode &src, const proportional_split& split_obj) : proportional_mode<Partition>(src, split_obj)
  324. , my_head((src.my_head + src.my_divisor) % src.my_max_affinity), my_max_affinity(src.my_max_affinity) {}
  325. void set_affinity( task &t ) {
  326. if( self().my_divisor )
  327. t.set_affinity( affinity_id(my_head) + 1 );
  328. }
  329. };
  330. /*! Determine work-balance phase implementing splitting & stealing actions */
  331. template<class Mode>
  332. struct dynamic_grainsize_mode : Mode {
  333. using Mode::self;
  334. #ifdef __TBB_USE_MACHINE_TIME_STAMPS
  335. tbb::internal::machine_tsc_t my_dst_tsc;
  336. #endif
  337. enum {
  338. begin = 0,
  339. run,
  340. pass
  341. } my_delay;
  342. depth_t my_max_depth;
  343. static const unsigned range_pool_size = __TBB_RANGE_POOL_CAPACITY;
  344. dynamic_grainsize_mode(): Mode()
  345. #ifdef __TBB_USE_MACHINE_TIME_STAMPS
  346. , my_dst_tsc(0)
  347. #endif
  348. , my_delay(begin)
  349. , my_max_depth(__TBB_INIT_DEPTH) {}
  350. dynamic_grainsize_mode(dynamic_grainsize_mode& p, split)
  351. : Mode(p, split())
  352. #ifdef __TBB_USE_MACHINE_TIME_STAMPS
  353. , my_dst_tsc(0)
  354. #endif
  355. , my_delay(pass)
  356. , my_max_depth(p.my_max_depth) {}
  357. dynamic_grainsize_mode(dynamic_grainsize_mode& p, const proportional_split& split_obj)
  358. : Mode(p, split_obj)
  359. #ifdef __TBB_USE_MACHINE_TIME_STAMPS
  360. , my_dst_tsc(0)
  361. #endif
  362. , my_delay(begin)
  363. , my_max_depth(p.my_max_depth) {}
  364. bool check_being_stolen(task &t) { // part of old should_execute_range()
  365. if( !(self().my_divisor / Mode::my_partition::factor) ) { // if not from the top P tasks of binary tree
  366. self().my_divisor = 1; // TODO: replace by on-stack flag (partition_state's member)?
  367. if( t.is_stolen_task() && t.parent()->ref_count() >= 2 ) { // runs concurrently with the left task
  368. #if __TBB_USE_OPTIONAL_RTTI
  369. // RTTI is available, check whether the cast is valid
  370. __TBB_ASSERT(dynamic_cast<flag_task*>(t.parent()), 0);
  371. // correctness of the cast relies on avoiding the root task for which:
  372. // - initial value of my_divisor != 0 (protected by separate assertion)
  373. // - is_stolen_task() always returns false for the root task.
  374. #endif
  375. flag_task::mark_task_stolen(t);
  376. if( !my_max_depth ) my_max_depth++;
  377. my_max_depth += __TBB_DEMAND_DEPTH_ADD;
  378. return true;
  379. }
  380. }
  381. return false;
  382. }
  383. depth_t max_depth() { return my_max_depth; }
  384. void align_depth(depth_t base) {
  385. __TBB_ASSERT(base <= my_max_depth, 0);
  386. my_max_depth -= base;
  387. }
  388. template<typename StartType, typename Range>
  389. void work_balance(StartType &start, Range &range) {
  390. if( !range.is_divisible() || !self().max_depth() ) {
  391. start.run_body( range ); // simple partitioner goes always here
  392. }
  393. else { // do range pool
  394. internal::range_vector<Range, range_pool_size> range_pool(range);
  395. do {
  396. range_pool.split_to_fill(self().max_depth()); // fill range pool
  397. if( self().check_for_demand( start ) ) {
  398. if( range_pool.size() > 1 ) {
  399. start.offer_work( range_pool.front(), range_pool.front_depth() );
  400. range_pool.pop_front();
  401. continue;
  402. }
  403. if( range_pool.is_divisible(self().max_depth()) ) // was not enough depth to fork a task
  404. continue; // note: next split_to_fill() should split range at least once
  405. }
  406. start.run_body( range_pool.back() );
  407. range_pool.pop_back();
  408. } while( !range_pool.empty() && !start.is_cancelled() );
  409. }
  410. }
  411. bool check_for_demand( task &t ) {
  412. if( pass == my_delay ) {
  413. if( self().my_divisor > 1 ) // produce affinitized tasks while they have slot in array
  414. return true; // do not do my_max_depth++ here, but be sure range_pool is splittable once more
  415. else if( self().my_divisor && my_max_depth ) { // make balancing task
  416. self().my_divisor = 0; // once for each task; depth will be decreased in align_depth()
  417. return true;
  418. }
  419. else if( flag_task::is_peer_stolen(t) ) {
  420. my_max_depth += __TBB_DEMAND_DEPTH_ADD;
  421. return true;
  422. }
  423. } else if( begin == my_delay ) {
  424. #ifndef __TBB_USE_MACHINE_TIME_STAMPS
  425. my_delay = pass;
  426. #else
  427. my_dst_tsc = __TBB_time_stamp() + __TBB_task_duration();
  428. my_delay = run;
  429. } else if( run == my_delay ) {
  430. if( __TBB_time_stamp() < my_dst_tsc ) {
  431. __TBB_ASSERT(my_max_depth > 0, NULL);
  432. my_max_depth--; // increase granularity since tasks seem having too small work
  433. return false;
  434. }
  435. my_delay = pass;
  436. return true;
  437. #endif // __TBB_USE_MACHINE_TIME_STAMPS
  438. }
  439. return false;
  440. }
  441. };
  442. class auto_partition_type: public dynamic_grainsize_mode<adaptive_mode<auto_partition_type> > {
  443. public:
  444. auto_partition_type( const auto_partitioner& )
  445. : dynamic_grainsize_mode<adaptive_mode<auto_partition_type> >() {
  446. my_divisor *= __TBB_INITIAL_CHUNKS;
  447. }
  448. auto_partition_type( auto_partition_type& src, split)
  449. : dynamic_grainsize_mode<adaptive_mode<auto_partition_type> >(src, split()) {}
  450. bool is_divisible() { // part of old should_execute_range()
  451. if( my_divisor > 1 ) return true;
  452. if( my_divisor && my_max_depth ) { // can split the task. TODO: on-stack flag instead
  453. // keep same fragmentation while splitting for the local task pool
  454. my_max_depth--;
  455. my_divisor = 0; // decrease max_depth once per task
  456. return true;
  457. } else return false;
  458. }
  459. bool check_for_demand(task &t) {
  460. if( flag_task::is_peer_stolen(t) ) {
  461. my_max_depth += __TBB_DEMAND_DEPTH_ADD;
  462. return true;
  463. } else return false;
  464. }
  465. };
  466. class simple_partition_type: public partition_type_base<simple_partition_type> {
  467. public:
  468. simple_partition_type( const simple_partitioner& ) {}
  469. simple_partition_type( const simple_partition_type&, split ) {}
  470. //! simplified algorithm
  471. template<typename StartType, typename Range>
  472. void execute(StartType &start, Range &range) {
  473. split_type split_obj = split(); // start.offer_work accepts split_type as reference
  474. while( range.is_divisible() )
  475. start.offer_work( split_obj );
  476. start.run_body( range );
  477. }
  478. };
  479. class static_partition_type : public linear_affinity_mode<static_partition_type> {
  480. public:
  481. typedef proportional_split split_type;
  482. static_partition_type( const static_partitioner& )
  483. : linear_affinity_mode<static_partition_type>() {}
  484. static_partition_type( static_partition_type& p, split )
  485. : linear_affinity_mode<static_partition_type>(p, split()) {}
  486. static_partition_type( static_partition_type& p, const proportional_split& split_obj )
  487. : linear_affinity_mode<static_partition_type>(p, split_obj) {}
  488. };
  489. class affinity_partition_type : public dynamic_grainsize_mode<linear_affinity_mode<affinity_partition_type> > {
  490. static const unsigned factor_power = 4; // TODO: get a unified formula based on number of computing units
  491. tbb::internal::affinity_id* my_array;
  492. public:
  493. static const unsigned factor = 1 << factor_power; // number of slots in affinity array per task
  494. typedef proportional_split split_type;
  495. affinity_partition_type( tbb::internal::affinity_partitioner_base_v3& ap )
  496. : dynamic_grainsize_mode<linear_affinity_mode<affinity_partition_type> >() {
  497. __TBB_ASSERT( (factor&(factor-1))==0, "factor must be power of two" );
  498. ap.resize(factor);
  499. my_array = ap.my_array;
  500. my_max_depth = factor_power + 1;
  501. __TBB_ASSERT( my_max_depth < __TBB_RANGE_POOL_CAPACITY, 0 );
  502. }
  503. affinity_partition_type(affinity_partition_type& p, split)
  504. : dynamic_grainsize_mode<linear_affinity_mode<affinity_partition_type> >(p, split())
  505. , my_array(p.my_array) {}
  506. affinity_partition_type(affinity_partition_type& p, const proportional_split& split_obj)
  507. : dynamic_grainsize_mode<linear_affinity_mode<affinity_partition_type> >(p, split_obj)
  508. , my_array(p.my_array) {}
  509. void set_affinity( task &t ) {
  510. if( my_divisor ) {
  511. if( !my_array[my_head] )
  512. // TODO: consider new ideas with my_array for both affinity and static partitioner's, then code reuse
  513. t.set_affinity( affinity_id(my_head / factor + 1) );
  514. else
  515. t.set_affinity( my_array[my_head] );
  516. }
  517. }
  518. void note_affinity( task::affinity_id id ) {
  519. if( my_divisor )
  520. my_array[my_head] = id;
  521. }
  522. };
  523. //! Backward-compatible partition for auto and affinity partition objects.
  524. class old_auto_partition_type: public tbb::internal::partition_type_base {
  525. size_t num_chunks;
  526. static const size_t VICTIM_CHUNKS = 4;
  527. public:
  528. bool should_execute_range(const task &t) {
  529. if( num_chunks<VICTIM_CHUNKS && t.is_stolen_task() )
  530. num_chunks = VICTIM_CHUNKS;
  531. return num_chunks==1;
  532. }
  533. old_auto_partition_type( const auto_partitioner& )
  534. : num_chunks(internal::get_initial_auto_partitioner_divisor()*__TBB_INITIAL_CHUNKS/4) {}
  535. old_auto_partition_type( const affinity_partitioner& )
  536. : num_chunks(internal::get_initial_auto_partitioner_divisor()*__TBB_INITIAL_CHUNKS/4) {}
  537. old_auto_partition_type( old_auto_partition_type& pt, split ) {
  538. num_chunks = pt.num_chunks = (pt.num_chunks+1u) / 2u;
  539. }
  540. };
  541. } // namespace interfaceX::internal
  542. //! @endcond
  543. } // namespace interfaceX
  544. //! A simple partitioner
  545. /** Divides the range until the range is not divisible.
  546. @ingroup algorithms */
  547. class simple_partitioner {
  548. public:
  549. simple_partitioner() {}
  550. private:
  551. template<typename Range, typename Body, typename Partitioner> friend class serial::interface9::start_for;
  552. template<typename Range, typename Body, typename Partitioner> friend class interface9::internal::start_for;
  553. template<typename Range, typename Body, typename Partitioner> friend class interface9::internal::start_reduce;
  554. template<typename Range, typename Body, typename Partitioner> friend class interface9::internal::start_deterministic_reduce;
  555. template<typename Range, typename Body, typename Partitioner> friend class internal::start_scan;
  556. // backward compatibility
  557. class partition_type: public internal::partition_type_base {
  558. public:
  559. bool should_execute_range(const task& ) {return false;}
  560. partition_type( const simple_partitioner& ) {}
  561. partition_type( const partition_type&, split ) {}
  562. };
  563. // new implementation just extends existing interface
  564. typedef interface9::internal::simple_partition_type task_partition_type;
  565. // TODO: consider to make split_type public
  566. typedef interface9::internal::simple_partition_type::split_type split_type;
  567. };
  568. //! An auto partitioner
  569. /** The range is initial divided into several large chunks.
  570. Chunks are further subdivided into smaller pieces if demand detected and they are divisible.
  571. @ingroup algorithms */
  572. class auto_partitioner {
  573. public:
  574. auto_partitioner() {}
  575. private:
  576. template<typename Range, typename Body, typename Partitioner> friend class serial::interface9::start_for;
  577. template<typename Range, typename Body, typename Partitioner> friend class interface9::internal::start_for;
  578. template<typename Range, typename Body, typename Partitioner> friend class interface9::internal::start_reduce;
  579. template<typename Range, typename Body, typename Partitioner> friend class internal::start_scan;
  580. // backward compatibility
  581. typedef interface9::internal::old_auto_partition_type partition_type;
  582. // new implementation just extends existing interface
  583. typedef interface9::internal::auto_partition_type task_partition_type;
  584. // TODO: consider to make split_type public
  585. typedef interface9::internal::auto_partition_type::split_type split_type;
  586. };
  587. //! A static partitioner
  588. class static_partitioner {
  589. public:
  590. static_partitioner() {}
  591. private:
  592. template<typename Range, typename Body, typename Partitioner> friend class serial::interface9::start_for;
  593. template<typename Range, typename Body, typename Partitioner> friend class interface9::internal::start_for;
  594. template<typename Range, typename Body, typename Partitioner> friend class interface9::internal::start_reduce;
  595. template<typename Range, typename Body, typename Partitioner> friend class interface9::internal::start_deterministic_reduce;
  596. template<typename Range, typename Body, typename Partitioner> friend class internal::start_scan;
  597. // backward compatibility
  598. typedef interface9::internal::old_auto_partition_type partition_type;
  599. // new implementation just extends existing interface
  600. typedef interface9::internal::static_partition_type task_partition_type;
  601. // TODO: consider to make split_type public
  602. typedef interface9::internal::static_partition_type::split_type split_type;
  603. };
  604. //! An affinity partitioner
  605. class affinity_partitioner: internal::affinity_partitioner_base_v3 {
  606. public:
  607. affinity_partitioner() {}
  608. private:
  609. template<typename Range, typename Body, typename Partitioner> friend class serial::interface9::start_for;
  610. template<typename Range, typename Body, typename Partitioner> friend class interface9::internal::start_for;
  611. template<typename Range, typename Body, typename Partitioner> friend class interface9::internal::start_reduce;
  612. template<typename Range, typename Body, typename Partitioner> friend class internal::start_scan;
  613. // backward compatibility - for parallel_scan only
  614. typedef interface9::internal::old_auto_partition_type partition_type;
  615. // new implementation just extends existing interface
  616. typedef interface9::internal::affinity_partition_type task_partition_type;
  617. // TODO: consider to make split_type public
  618. typedef interface9::internal::affinity_partition_type::split_type split_type;
  619. };
  620. } // namespace tbb
  621. #if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
  622. #pragma warning (pop)
  623. #endif // warning 4244 is back
  624. #undef __TBB_INITIAL_CHUNKS
  625. #undef __TBB_RANGE_POOL_CAPACITY
  626. #undef __TBB_INIT_DEPTH
  627. #include "internal/_warning_suppress_disable_notice.h"
  628. #undef __TBB_partitioner_H_include_area
  629. #endif /* __TBB_partitioner_H */