Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
market.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2005-2019 Intel Corporation
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 #ifndef _TBB_market_H
18 #define _TBB_market_H
19 
20 #include "tbb/tbb_stddef.h"
21 
22 #include "scheduler_common.h"
23 #include "tbb/atomic.h"
24 #include "tbb/spin_rw_mutex.h"
25 #include "../rml/include/rml_tbb.h"
26 
27 #include "intrusive_list.h"
28 
29 #if defined(_MSC_VER) && defined(_Wp64)
30  // Workaround for overzealous compiler warnings in /Wp64 mode
31  #pragma warning (push)
32  #pragma warning (disable: 4244)
33 #endif
34 
35 namespace tbb {
36 
37 class task_group_context;
38 
39 namespace internal {
40 
41 //------------------------------------------------------------------------
42 // Class market
43 //------------------------------------------------------------------------
44 
45 class market : no_copy, rml::tbb_client {
46  friend class generic_scheduler;
47  friend class arena;
49  template<typename SchedulerTraits> friend class custom_scheduler;
51 private:
52  friend void ITT_DoUnsafeOneTimeInitialization ();
53 
56 
58  static market* theMarket;
59 
61 
64 
68 
70  rml::tbb_server* my_server;
71 
73 
75 
77 
79 
82 
84 
87 
90 
91 #if __TBB_ENQUEUE_ENFORCED_CONCURRENCY
92  int my_mandatory_num_requested;
94 #endif
95 
96 #if __TBB_TASK_PRIORITY
97 
102  intptr_t my_global_top_priority;
103 
105 
106  intptr_t my_global_bottom_priority;
107 
109 
111  uintptr_t my_global_reload_epoch;
112 
114  struct priority_level_info {
116  arena_list_type arenas;
117 
119 
120  arena *next_arena;
121 
123  int workers_requested;
124 
126  int workers_available;
127  }; // struct priority_level_info
128 
130  priority_level_info my_priority_levels[num_priority_levels];
131 
132 #else /* !__TBB_TASK_PRIORITY */
133 
136 
138 
140 #endif /* !__TBB_TASK_PRIORITY */
141 
144 
146  unsigned my_ref_count;
147 
150 
153 
156 
158  static const unsigned skip_soft_limit_warning = ~0U;
159 
162 #if __TBB_COUNT_TASK_NODES
163 
165  atomic<intptr_t> my_task_node_count;
166 #endif /* __TBB_COUNT_TASK_NODES */
167 
169  market ( unsigned workers_soft_limit, unsigned workers_hard_limit, size_t stack_size );
170 
172  static market& global_market ( bool is_public, unsigned max_num_workers = 0, size_t stack_size = 0 );
173 
175  void destroy ();
176 
177 #if __TBB_TASK_PRIORITY
178  arena* arena_in_need ( arena* prev_arena );
180 
182 
184  void update_allotment ( intptr_t highest_affected_priority );
185 
187  void update_arena_top_priority ( arena& a, intptr_t newPriority );
188 
190  inline void update_global_top_priority ( intptr_t newPriority );
191 
193  inline void reset_global_priority ();
194 
195  inline void advance_global_reload_epoch () {
196  __TBB_store_with_release( my_global_reload_epoch, my_global_reload_epoch + 1 );
197  }
198 
199  void assert_market_valid () const {
200  __TBB_ASSERT( (my_priority_levels[my_global_top_priority].workers_requested > 0
201  && !my_priority_levels[my_global_top_priority].arenas.empty())
202  || (my_global_top_priority == my_global_bottom_priority &&
203  my_global_top_priority == normalized_normal_priority), NULL );
204  }
205 
206 #else /* !__TBB_TASK_PRIORITY */
207 
209 
212  if ( my_total_demand )
214  }
215 
216  // TODO: consider to rewrite the code with is_arena_in_list function
220  return NULL;
223  }
224  void assert_market_valid () const {}
225 #endif /* !__TBB_TASK_PRIORITY */
226 
228  // Helpers to unify code branches dependent on priority feature presence
229 
230  void insert_arena_into_list ( arena& a );
231 
232  void remove_arena_from_list ( arena& a );
233 
234  arena* arena_in_need ( arena_list_type &arenas, arena *hint );
235 
236  static int update_allotment ( arena_list_type& arenas, int total_demand, int max_workers );
237 
238  bool is_arena_in_list( arena_list_type &arenas, arena *a );
239 
240 
242  // Implementation of rml::tbb_client interface methods
243 
244  version_type version () const __TBB_override { return 0; }
245 
247 
248  size_t min_stack_size () const __TBB_override { return worker_stack_size(); }
249 
250  policy_type policy () const __TBB_override { return throughput; }
251 
253 
254  void cleanup( job& j ) __TBB_override;
255 
257 
258  void process( job& j ) __TBB_override;
259 
260 public:
262 
264  static arena* create_arena ( int num_slots, int num_reserved_slots, size_t stack_size );
265 
267  void try_destroy_arena ( arena*, uintptr_t aba_epoch );
268 
270  void detach_arena ( arena& );
271 
273  bool release ( bool is_public, bool blocking_terminate );
274 
275 #if __TBB_ENQUEUE_ENFORCED_CONCURRENCY
276  bool mandatory_concurrency_enable_impl ( arena *a, bool *enabled = NULL );
278 
280  bool mandatory_concurrency_enable ( arena *a );
281 
283  void mandatory_concurrency_disable ( arena *a );
284 #endif /* __TBB_ENQUEUE_ENFORCED_CONCURRENCY */
285 
287 
288  void adjust_demand ( arena&, int delta );
289 
291  bool must_join_workers () const { return my_join_workers; }
292 
294  size_t worker_stack_size () const { return my_stack_size; }
295 
297  static void set_active_num_workers( unsigned w );
298 
300  static unsigned app_parallelism_limit();
301 
302 #if _WIN32||_WIN64
303  void register_master( ::rml::server::execution_resource_t& rsc_handle ) {
305  __TBB_ASSERT( my_server, "RML server not defined?" );
306  // the server may ignore registration and set master_exec_resource to NULL.
307  my_server->register_master( rsc_handle );
308  }
309 
311  void unregister_master( ::rml::server::execution_resource_t& rsc_handle ) const {
312  my_server->unregister_master( rsc_handle );
313  }
314 #endif /* WIN */
315 
316 #if __TBB_TASK_GROUP_CONTEXT
317 
322  template <typename T>
323  bool propagate_task_group_state ( T task_group_context::*mptr_state, task_group_context& src, T new_state );
324 #endif /* __TBB_TASK_GROUP_CONTEXT */
325 
326 #if __TBB_TASK_PRIORITY
327 
329  bool lower_arena_priority ( arena& a, intptr_t new_priority, uintptr_t old_reload_epoch );
330 
332 
338  bool update_arena_priority ( arena& a, intptr_t new_priority );
339 #endif /* __TBB_TASK_PRIORITY */
340 
341 #if __TBB_COUNT_TASK_NODES
342 
344  void update_task_node_count( intptr_t delta ) { my_task_node_count += delta; }
345 #endif /* __TBB_COUNT_TASK_NODES */
346 
347 #if __TBB_TASK_GROUP_CONTEXT
348  scheduler_list_type my_masters;
350 
352 
354  generic_scheduler* my_workers[1];
355 #endif /* __TBB_TASK_GROUP_CONTEXT */
356 
357  static unsigned max_num_workers() {
358  global_market_mutex_type::scoped_lock lock( theMarketMutex );
360  }
361 }; // class market
362 
363 } // namespace internal
364 } // namespace tbb
365 
366 #if defined(_MSC_VER) && defined(_Wp64)
367  // Workaround for overzealous compiler warnings in /Wp64 mode
368  #pragma warning (pop)
369 #endif // warning 4244 is back
370 
371 #endif /* _TBB_market_H */
job * create_one_job() __TBB_override
Definition: market.cpp:734
void assert_market_valid() const
Definition: market.h:224
The scoped locking pattern.
Definition: spin_rw_mutex.h:86
static const intptr_t num_priority_levels
static market & global_market(bool is_public, unsigned max_num_workers=0, size_t stack_size=0)
Factory method creating new market object.
Definition: market.cpp:96
#define __TBB_override
Definition: tbb_stddef.h:240
spin_rw_mutex arenas_list_mutex_type
Lightweight mutex guarding accounting operations with arenas list.
Definition: market.h:66
atomic< unsigned > my_first_unused_worker_idx
First unused index of worker.
Definition: market.h:86
void destroy()
Destroys and deallocates market object created by market::create()
Definition: market.cpp:165
unsigned my_public_ref_count
Count of master threads attached.
Definition: market.h:149
int my_total_demand
Number of workers that were requested by all arenas.
Definition: market.h:89
size_t worker_stack_size() const
Returns the requested stack size of worker threads.
Definition: market.h:294
rml::tbb_server * my_server
Pointer to the RML server object that services this TBB instance.
Definition: market.h:70
size_t min_stack_size() const __TBB_override
Definition: market.h:248
static unsigned app_parallelism_limit()
Reports active parallelism level according to user's settings.
Definition: tbb_main.cpp:492
unsigned my_ref_count
Reference count controlling market object lifetime.
Definition: market.h:146
friend void ITT_DoUnsafeOneTimeInitialization()
void acknowledge_close_connection() __TBB_override
Definition: market.cpp:730
void remove_arena_from_list(arena &a)
Definition: market.cpp:42
The graph class.
static void set_active_num_workers(unsigned w)
Set number of active workers.
Definition: market.cpp:217
unsigned max_job_count() const __TBB_override
Definition: market.h:246
Used to form groups of tasks.
Definition: task.h:332
int my_num_workers_requested
Number of workers currently requested from RML.
Definition: market.h:81
market(unsigned workers_soft_limit, unsigned workers_hard_limit, size_t stack_size)
Constructor.
Definition: market.cpp:64
static arena * create_arena(int num_slots, int num_reserved_slots, size_t stack_size)
Creates an arena object.
Definition: market.cpp:296
Base class for types that should not be copied or assigned.
Definition: tbb_stddef.h:331
void update_allotment()
Recalculates the number of workers assigned to each arena in the list.
Definition: market.h:211
arenas_list_mutex_type my_arenas_list_mutex
Definition: market.h:67
size_t my_stack_size
Stack size of worker threads.
Definition: market.h:152
bool is_arena_in_list(arena_list_type &arenas, arena *a)
Definition: market.cpp:413
static const unsigned skip_soft_limit_warning
The value indicating that the soft limit warning is unnecessary.
Definition: market.h:158
void insert_arena_into_list(arena &a)
Definition: market.cpp:29
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
unsigned my_workers_soft_limit_to_report
Either workers soft limit to be reported via runtime_warning() or skip_soft_limit_warning.
Definition: market.h:161
static market * theMarket
Currently active global market.
Definition: market.h:58
bool my_join_workers
Shutdown mode.
Definition: market.h:155
uintptr_t my_arenas_aba_epoch
ABA prevention marker to assign to newly created arenas.
Definition: market.h:143
void adjust_demand(arena &, int delta)
Request that arena's need in workers should be adjusted.
Definition: market.cpp:586
scheduler_mutex_type global_market_mutex_type
Definition: market.h:60
arena * my_next_arena
The first arena to be checked when idle worker seeks for an arena to enter.
Definition: market.h:139
Work stealing task scheduler.
Definition: scheduler.h:120
version_type version() const __TBB_override
Definition: market.h:244
void __TBB_store_with_release(volatile T &location, V value)
Definition: tbb_machine.h:713
T __TBB_load_with_acquire(const volatile T &location)
Definition: tbb_machine.h:709
bool release(bool is_public, bool blocking_terminate)
Decrements market's refcount and destroys it in the end.
Definition: market.cpp:175
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void * lock
arena * arena_in_need(arena *)
Returns next arena that needs more workers, or NULL.
Definition: market.h:218
A scheduler with a customized evaluation loop.
static unsigned max_num_workers()
Definition: market.h:357
friend class arena
Definition: market.h:47
Fast, unfair, spinning reader-writer lock with backoff and writer-preference.
Definition: spin_rw_mutex.h:38
friend class generic_scheduler
Definition: market.h:46
intrusive_list< generic_scheduler > scheduler_list_type
Definition: market.h:55
unsigned my_num_workers_soft_limit
Current application-imposed limit on the number of workers (see set_active_num_workers())
Definition: market.h:78
bool must_join_workers() const
Used when RML asks for join mode during workers termination.
Definition: market.h:291
arena_list_type my_arenas
List of registered arenas.
Definition: market.h:135
policy_type policy() const __TBB_override
Definition: market.h:250
intrusive_list< arena > arena_list_type
Definition: market.h:54
void detach_arena(arena &)
Removes the arena from the market's list.
Definition: market.cpp:310
unsigned my_num_workers_hard_limit
Maximal number of workers allowed for use by the underlying resource manager.
Definition: market.h:74
__TBB_SCHEDULER_MUTEX_TYPE scheduler_mutex_type
Mutex type for global locks in the scheduler.
void cleanup(job &j) __TBB_override
Definition: market.cpp:716
static global_market_mutex_type theMarketMutex
Mutex guarding creation/destruction of theMarket, insertions/deletions in my_arenas,...
Definition: market.h:63
void process(job &j) __TBB_override
Definition: market.cpp:694
void try_destroy_arena(arena *, uintptr_t aba_epoch)
Removes the arena from the market's list.
Definition: market.cpp:318

Copyright © 2005-2019 Intel Corporation. All Rights Reserved.

Intel, Pentium, Intel Xeon, Itanium, Intel XScale and VTune are registered trademarks or trademarks of Intel Corporation or its subsidiaries in the United States and other countries.

* Other names and brands may be claimed as the property of others.