task_arena.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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_task_arena_H
  14. #define __TBB_task_arena_H
  15. #define __TBB_task_arena_H_include_area
  16. #include "internal/_warning_suppress_enable_notice.h"
  17. #include "task.h"
  18. #include "tbb_exception.h"
  19. #include "internal/_template_helpers.h"
  20. #if __TBB_NUMA_SUPPORT
  21. #include "info.h"
  22. #endif /*__TBB_NUMA_SUPPORT*/
  23. #if TBB_USE_THREADING_TOOLS
  24. #include "atomic.h" // for as_atomic
  25. #endif
  26. #include "aligned_space.h"
  27. namespace tbb {
  28. namespace this_task_arena {
  29. int max_concurrency();
  30. } // namespace this_task_arena
  31. //! @cond INTERNAL
  32. namespace internal {
  33. //! Internal to library. Should not be used by clients.
  34. /** @ingroup task_scheduling */
  35. class arena;
  36. class task_scheduler_observer_v3;
  37. } // namespace internal
  38. //! @endcond
  39. namespace interface7 {
  40. class task_arena;
  41. //! @cond INTERNAL
  42. namespace internal {
  43. using namespace tbb::internal; //e.g. function_task from task.h
  44. class delegate_base : no_assign {
  45. public:
  46. virtual void operator()() const = 0;
  47. virtual ~delegate_base() {}
  48. };
  49. // If decltype is available, the helper detects the return type of functor of specified type,
  50. // otherwise it defines the void type.
  51. template <typename F>
  52. struct return_type_or_void {
  53. #if __TBB_CPP11_DECLTYPE_PRESENT && !__TBB_CPP11_DECLTYPE_OF_FUNCTION_RETURN_TYPE_BROKEN
  54. typedef decltype(declval<F>()()) type;
  55. #else
  56. typedef void type;
  57. #endif
  58. };
  59. template<typename F, typename R>
  60. class delegated_function : public delegate_base {
  61. F &my_func;
  62. tbb::aligned_space<R> my_return_storage;
  63. // The function should be called only once.
  64. void operator()() const __TBB_override {
  65. new (my_return_storage.begin()) R(my_func());
  66. }
  67. public:
  68. delegated_function(F& f) : my_func(f) {}
  69. // The function can be called only after operator() and only once.
  70. R consume_result() const {
  71. return tbb::internal::move(*(my_return_storage.begin()));
  72. }
  73. ~delegated_function() {
  74. my_return_storage.begin()->~R();
  75. }
  76. };
  77. template<typename F>
  78. class delegated_function<F,void> : public delegate_base {
  79. F &my_func;
  80. void operator()() const __TBB_override {
  81. my_func();
  82. }
  83. public:
  84. delegated_function(F& f) : my_func(f) {}
  85. void consume_result() const {}
  86. friend class task_arena_base;
  87. };
  88. class task_arena_base {
  89. #if __TBB_NUMA_SUPPORT
  90. public:
  91. // TODO: consider version approach to resolve backward compatibility potential issues.
  92. struct constraints {
  93. constraints(numa_node_id id = automatic, int maximal_concurrency = automatic)
  94. : numa_id(id)
  95. , max_concurrency(maximal_concurrency)
  96. {}
  97. numa_node_id numa_id;
  98. int max_concurrency;
  99. };
  100. #endif /*__TBB_NUMA_SUPPORT*/
  101. protected:
  102. //! NULL if not currently initialized.
  103. internal::arena* my_arena;
  104. #if __TBB_TASK_GROUP_CONTEXT
  105. //! default context of the arena
  106. task_group_context *my_context;
  107. #endif
  108. //! Concurrency level for deferred initialization
  109. int my_max_concurrency;
  110. //! Reserved master slots
  111. unsigned my_master_slots;
  112. //! Special settings
  113. intptr_t my_version_and_traits;
  114. bool my_initialized;
  115. #if __TBB_NUMA_SUPPORT
  116. //! The NUMA node index to which the arena will be attached
  117. numa_node_id my_numa_id;
  118. // Do not access my_numa_id without the following runtime check.
  119. // Despite my_numa_id is accesible, it does not exist in task_arena_base on user side
  120. // if TBB_PREVIEW_NUMA_SUPPORT macro is not defined by the user. To be sure that
  121. // my_numa_id exists in task_arena_base layout we check the traits.
  122. // TODO: Consider increasing interface version for task_arena_base instead of this runtime check.
  123. numa_node_id numa_id() {
  124. return (my_version_and_traits & numa_support_flag) == numa_support_flag ? my_numa_id : automatic;
  125. }
  126. #endif
  127. enum {
  128. default_flags = 0
  129. #if __TBB_TASK_GROUP_CONTEXT
  130. | (task_group_context::default_traits & task_group_context::exact_exception) // 0 or 1 << 16
  131. , exact_exception_flag = task_group_context::exact_exception // used to specify flag for context directly
  132. #endif
  133. #if __TBB_NUMA_SUPPORT
  134. , numa_support_flag = 1
  135. #endif
  136. };
  137. task_arena_base(int max_concurrency, unsigned reserved_for_masters)
  138. : my_arena(0)
  139. #if __TBB_TASK_GROUP_CONTEXT
  140. , my_context(0)
  141. #endif
  142. , my_max_concurrency(max_concurrency)
  143. , my_master_slots(reserved_for_masters)
  144. #if __TBB_NUMA_SUPPORT
  145. , my_version_and_traits(default_flags | numa_support_flag)
  146. #else
  147. , my_version_and_traits(default_flags)
  148. #endif
  149. , my_initialized(false)
  150. #if __TBB_NUMA_SUPPORT
  151. , my_numa_id(automatic)
  152. #endif
  153. {}
  154. #if __TBB_NUMA_SUPPORT
  155. task_arena_base(const constraints& constraints_, unsigned reserved_for_masters)
  156. : my_arena(0)
  157. #if __TBB_TASK_GROUP_CONTEXT
  158. , my_context(0)
  159. #endif
  160. , my_max_concurrency(constraints_.max_concurrency)
  161. , my_master_slots(reserved_for_masters)
  162. , my_version_and_traits(default_flags | numa_support_flag)
  163. , my_initialized(false)
  164. , my_numa_id(constraints_.numa_id )
  165. {}
  166. #endif /*__TBB_NUMA_SUPPORT*/
  167. void __TBB_EXPORTED_METHOD internal_initialize();
  168. void __TBB_EXPORTED_METHOD internal_terminate();
  169. void __TBB_EXPORTED_METHOD internal_attach();
  170. void __TBB_EXPORTED_METHOD internal_enqueue( task&, intptr_t ) const;
  171. void __TBB_EXPORTED_METHOD internal_execute( delegate_base& ) const;
  172. void __TBB_EXPORTED_METHOD internal_wait() const;
  173. static int __TBB_EXPORTED_FUNC internal_current_slot();
  174. static int __TBB_EXPORTED_FUNC internal_max_concurrency( const task_arena * );
  175. public:
  176. //! Typedef for number of threads that is automatic.
  177. static const int automatic = -1;
  178. static const int not_initialized = -2;
  179. };
  180. #if __TBB_TASK_ISOLATION
  181. void __TBB_EXPORTED_FUNC isolate_within_arena( delegate_base& d, intptr_t isolation = 0 );
  182. template<typename R, typename F>
  183. R isolate_impl(F& f) {
  184. delegated_function<F, R> d(f);
  185. isolate_within_arena(d);
  186. return d.consume_result();
  187. }
  188. #endif /* __TBB_TASK_ISOLATION */
  189. } // namespace internal
  190. //! @endcond
  191. /** 1-to-1 proxy representation class of scheduler's arena
  192. * Constructors set up settings only, real construction is deferred till the first method invocation
  193. * Destructor only removes one of the references to the inner arena representation.
  194. * Final destruction happens when all the references (and the work) are gone.
  195. */
  196. class task_arena : public internal::task_arena_base {
  197. friend class tbb::internal::task_scheduler_observer_v3;
  198. friend void task::enqueue(task&, task_arena&
  199. #if __TBB_TASK_PRIORITY
  200. , priority_t
  201. #endif
  202. );
  203. friend int tbb::this_task_arena::max_concurrency();
  204. void mark_initialized() {
  205. __TBB_ASSERT( my_arena, "task_arena initialization is incomplete" );
  206. #if __TBB_TASK_GROUP_CONTEXT
  207. __TBB_ASSERT( my_context, "task_arena initialization is incomplete" );
  208. #endif
  209. #if TBB_USE_THREADING_TOOLS
  210. // Actual synchronization happens in internal_initialize & internal_attach.
  211. // The race on setting my_initialized is benign, but should be hidden from Intel(R) Inspector
  212. internal::as_atomic(my_initialized).fetch_and_store<release>(true);
  213. #else
  214. my_initialized = true;
  215. #endif
  216. }
  217. template<typename F>
  218. void enqueue_impl( __TBB_FORWARDING_REF(F) f
  219. #if __TBB_TASK_PRIORITY
  220. , priority_t p = priority_t(0)
  221. #endif
  222. ) {
  223. #if !__TBB_TASK_PRIORITY
  224. intptr_t p = 0;
  225. #endif
  226. initialize();
  227. #if __TBB_TASK_GROUP_CONTEXT
  228. internal_enqueue(*new(task::allocate_root(*my_context)) internal::function_task< typename internal::strip<F>::type >(internal::forward<F>(f)), p);
  229. #else
  230. internal_enqueue(*new(task::allocate_root()) internal::function_task< typename internal::strip<F>::type >(internal::forward<F>(f)), p);
  231. #endif /* __TBB_TASK_GROUP_CONTEXT */
  232. }
  233. template<typename R, typename F>
  234. R execute_impl(F& f) {
  235. initialize();
  236. internal::delegated_function<F, R> d(f);
  237. internal_execute(d);
  238. return d.consume_result();
  239. }
  240. public:
  241. //! Creates task_arena with certain concurrency limits
  242. /** Sets up settings only, real construction is deferred till the first method invocation
  243. * @arg max_concurrency specifies total number of slots in arena where threads work
  244. * @arg reserved_for_masters specifies number of slots to be used by master threads only.
  245. * Value of 1 is default and reflects behavior of implicit arenas.
  246. **/
  247. task_arena(int max_concurrency_ = automatic, unsigned reserved_for_masters = 1)
  248. : task_arena_base(max_concurrency_, reserved_for_masters)
  249. {}
  250. #if __TBB_NUMA_SUPPORT
  251. //! Creates task arena pinned to certain NUMA node
  252. task_arena(const constraints& constraints_, unsigned reserved_for_masters = 1)
  253. : task_arena_base(constraints_, reserved_for_masters)
  254. {}
  255. //! Copies settings from another task_arena
  256. task_arena(const task_arena &s) // copy settings but not the reference or instance
  257. : task_arena_base(constraints(s.my_numa_id, s.my_max_concurrency), s.my_master_slots)
  258. {}
  259. #else
  260. //! Copies settings from another task_arena
  261. task_arena(const task_arena &s) // copy settings but not the reference or instance
  262. : task_arena_base(s.my_max_concurrency, s.my_master_slots)
  263. {}
  264. #endif /*__TBB_NUMA_SUPPORT*/
  265. //! Tag class used to indicate the "attaching" constructor
  266. struct attach {};
  267. //! Creates an instance of task_arena attached to the current arena of the thread
  268. explicit task_arena( attach )
  269. : task_arena_base(automatic, 1) // use default settings if attach fails
  270. {
  271. internal_attach();
  272. if( my_arena ) my_initialized = true;
  273. }
  274. //! Forces allocation of the resources for the task_arena as specified in constructor arguments
  275. inline void initialize() {
  276. if( !my_initialized ) {
  277. internal_initialize();
  278. mark_initialized();
  279. }
  280. }
  281. //! Overrides concurrency level and forces initialization of internal representation
  282. inline void initialize(int max_concurrency_, unsigned reserved_for_masters = 1) {
  283. // TODO: decide if this call must be thread-safe
  284. __TBB_ASSERT(!my_arena, "Impossible to modify settings of an already initialized task_arena");
  285. if( !my_initialized ) {
  286. my_max_concurrency = max_concurrency_;
  287. my_master_slots = reserved_for_masters;
  288. initialize();
  289. }
  290. }
  291. #if __TBB_NUMA_SUPPORT
  292. inline void initialize(constraints constraints_, unsigned reserved_for_masters = 1) {
  293. // TODO: decide if this call must be thread-safe
  294. __TBB_ASSERT(!my_arena, "Impossible to modify settings of an already initialized task_arena");
  295. if( !my_initialized ) {
  296. my_numa_id = constraints_.numa_id;
  297. my_max_concurrency = constraints_.max_concurrency;
  298. my_master_slots = reserved_for_masters;
  299. initialize();
  300. }
  301. }
  302. #endif /*__TBB_NUMA_SUPPORT*/
  303. //! Attaches this instance to the current arena of the thread
  304. inline void initialize(attach) {
  305. // TODO: decide if this call must be thread-safe
  306. __TBB_ASSERT(!my_arena, "Impossible to modify settings of an already initialized task_arena");
  307. if( !my_initialized ) {
  308. internal_attach();
  309. if ( !my_arena ) internal_initialize();
  310. mark_initialized();
  311. }
  312. }
  313. //! Removes the reference to the internal arena representation.
  314. //! Not thread safe wrt concurrent invocations of other methods.
  315. inline void terminate() {
  316. if( my_initialized ) {
  317. internal_terminate();
  318. my_initialized = false;
  319. }
  320. }
  321. //! Removes the reference to the internal arena representation, and destroys the external object.
  322. //! Not thread safe wrt concurrent invocations of other methods.
  323. ~task_arena() {
  324. terminate();
  325. }
  326. //! Returns true if the arena is active (initialized); false otherwise.
  327. //! The name was chosen to match a task_scheduler_init method with the same semantics.
  328. bool is_active() const { return my_initialized; }
  329. //! Enqueues a task into the arena to process a functor, and immediately returns.
  330. //! Does not require the calling thread to join the arena
  331. #if __TBB_CPP11_RVALUE_REF_PRESENT
  332. template<typename F>
  333. void enqueue( F&& f ) {
  334. enqueue_impl(std::forward<F>(f));
  335. }
  336. #else
  337. template<typename F>
  338. void enqueue( const F& f ) {
  339. enqueue_impl(f);
  340. }
  341. #endif
  342. #if __TBB_TASK_PRIORITY
  343. //! Enqueues a task with priority p into the arena to process a functor f, and immediately returns.
  344. //! Does not require the calling thread to join the arena
  345. template<typename F>
  346. #if __TBB_CPP11_RVALUE_REF_PRESENT
  347. __TBB_DEPRECATED void enqueue( F&& f, priority_t p ) {
  348. #if __TBB_PREVIEW_CRITICAL_TASKS
  349. __TBB_ASSERT(p == priority_low || p == priority_normal || p == priority_high
  350. || p == internal::priority_critical, "Invalid priority level value");
  351. #else
  352. __TBB_ASSERT(p == priority_low || p == priority_normal || p == priority_high, "Invalid priority level value");
  353. #endif
  354. enqueue_impl(std::forward<F>(f), p);
  355. }
  356. #else
  357. __TBB_DEPRECATED void enqueue( const F& f, priority_t p ) {
  358. #if __TBB_PREVIEW_CRITICAL_TASKS
  359. __TBB_ASSERT(p == priority_low || p == priority_normal || p == priority_high
  360. || p == internal::priority_critical, "Invalid priority level value");
  361. #else
  362. __TBB_ASSERT(p == priority_low || p == priority_normal || p == priority_high, "Invalid priority level value");
  363. #endif
  364. enqueue_impl(f,p);
  365. }
  366. #endif
  367. #endif// __TBB_TASK_PRIORITY
  368. //! Joins the arena and executes a mutable functor, then returns
  369. //! If not possible to join, wraps the functor into a task, enqueues it and waits for task completion
  370. //! Can decrement the arena demand for workers, causing a worker to leave and free a slot to the calling thread
  371. //! Since C++11, the method returns the value returned by functor (prior to C++11 it returns void).
  372. template<typename F>
  373. typename internal::return_type_or_void<F>::type execute(F& f) {
  374. return execute_impl<typename internal::return_type_or_void<F>::type>(f);
  375. }
  376. //! Joins the arena and executes a constant functor, then returns
  377. //! If not possible to join, wraps the functor into a task, enqueues it and waits for task completion
  378. //! Can decrement the arena demand for workers, causing a worker to leave and free a slot to the calling thread
  379. //! Since C++11, the method returns the value returned by functor (prior to C++11 it returns void).
  380. template<typename F>
  381. typename internal::return_type_or_void<F>::type execute(const F& f) {
  382. return execute_impl<typename internal::return_type_or_void<F>::type>(f);
  383. }
  384. #if __TBB_EXTRA_DEBUG
  385. //! Wait for all work in the arena to be completed
  386. //! Even submitted by other application threads
  387. //! Joins arena if/when possible (in the same way as execute())
  388. void debug_wait_until_empty() {
  389. initialize();
  390. internal_wait();
  391. }
  392. #endif //__TBB_EXTRA_DEBUG
  393. //! Returns the index, aka slot number, of the calling thread in its current arena
  394. //! This method is deprecated and replaced with this_task_arena::current_thread_index()
  395. inline static int current_thread_index() {
  396. return internal_current_slot();
  397. }
  398. //! Returns the maximal number of threads that can work inside the arena
  399. inline int max_concurrency() const {
  400. // Handle special cases inside the library
  401. return (my_max_concurrency>1) ? my_max_concurrency : internal_max_concurrency(this);
  402. }
  403. };
  404. namespace this_task_arena {
  405. #if __TBB_TASK_ISOLATION
  406. //! Executes a mutable functor in isolation within the current task arena.
  407. //! Since C++11, the method returns the value returned by functor (prior to C++11 it returns void).
  408. template<typename F>
  409. typename internal::return_type_or_void<F>::type isolate(F& f) {
  410. return internal::isolate_impl<typename internal::return_type_or_void<F>::type>(f);
  411. }
  412. //! Executes a constant functor in isolation within the current task arena.
  413. //! Since C++11, the method returns the value returned by functor (prior to C++11 it returns void).
  414. template<typename F>
  415. typename internal::return_type_or_void<F>::type isolate(const F& f) {
  416. return internal::isolate_impl<typename internal::return_type_or_void<F>::type>(f);
  417. }
  418. #endif /* __TBB_TASK_ISOLATION */
  419. } // namespace this_task_arena
  420. } // namespace interfaceX
  421. using interface7::task_arena;
  422. namespace this_task_arena {
  423. using namespace interface7::this_task_arena;
  424. //! Returns the index, aka slot number, of the calling thread in its current arena
  425. inline int current_thread_index() {
  426. int idx = tbb::task_arena::current_thread_index();
  427. return idx == -1 ? tbb::task_arena::not_initialized : idx;
  428. }
  429. //! Returns the maximal number of threads that can work inside the arena
  430. inline int max_concurrency() {
  431. return tbb::task_arena::internal_max_concurrency(NULL);
  432. }
  433. } // namespace this_task_arena
  434. //! Enqueue task in task_arena
  435. #if __TBB_TASK_PRIORITY
  436. void task::enqueue( task& t, task_arena& arena, priority_t p ) {
  437. #else
  438. void task::enqueue( task& t, task_arena& arena ) {
  439. intptr_t p = 0;
  440. #endif
  441. arena.initialize();
  442. //! Note: the context of the task may differ from the context instantiated by task_arena
  443. arena.internal_enqueue(t, p);
  444. }
  445. } // namespace tbb
  446. #include "internal/_warning_suppress_disable_notice.h"
  447. #undef __TBB_task_arena_H_include_area
  448. #endif /* __TBB_task_arena_H */