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 
18 
19 */
20 
21 #ifndef _TBB_market_H
22 #define _TBB_market_H
23 
24 #include "tbb/tbb_stddef.h"
25 
26 #include "scheduler_common.h"
27 #include "tbb/atomic.h"
28 #include "tbb/spin_rw_mutex.h"
29 #include "../rml/include/rml_tbb.h"
30 
31 #include "intrusive_list.h"
32 
33 #if defined(_MSC_VER) && defined(_Wp64)
34  // Workaround for overzealous compiler warnings in /Wp64 mode
35  #pragma warning (push)
36  #pragma warning (disable: 4244)
37 #endif
38 
39 namespace tbb {
40 
41 class task_group_context;
42 
43 namespace internal {
44 
45 //------------------------------------------------------------------------
46 // Class market
47 //------------------------------------------------------------------------
48 
49 class market : no_copy, rml::tbb_client {
50  friend class generic_scheduler;
51  friend class arena;
53  template<typename SchedulerTraits> friend class custom_scheduler;
55 private:
56  friend void ITT_DoUnsafeOneTimeInitialization ();
57 
60 
62  static market* theMarket;
63 
65 
68 
72 
74  rml::tbb_server* my_server;
75 
77 
79 
81 
83 
86 
88 
91 
94 
95 #if __TBB_ENQUEUE_ENFORCED_CONCURRENCY
96  int my_mandatory_num_requested;
98 #endif
99 
100 #if __TBB_TASK_PRIORITY
101 
106  intptr_t my_global_top_priority;
107 
109 
110  intptr_t my_global_bottom_priority;
111 
113 
115  uintptr_t my_global_reload_epoch;
116 
118  struct priority_level_info {
120  arena_list_type arenas;
121 
123 
124  arena *next_arena;
125 
127  int workers_requested;
128 
130  int workers_available;
131  }; // struct priority_level_info
132 
134  priority_level_info my_priority_levels[num_priority_levels];
135 
136 #else /* !__TBB_TASK_PRIORITY */
137 
140 
142 
144 #endif /* !__TBB_TASK_PRIORITY */
145 
148 
150  unsigned my_ref_count;
151 
154 
157 
160 
162  static const unsigned skip_soft_limit_warning = ~0U;
163 
166 #if __TBB_COUNT_TASK_NODES
167 
169  atomic<intptr_t> my_task_node_count;
170 #endif /* __TBB_COUNT_TASK_NODES */
171 
173  market ( unsigned workers_soft_limit, unsigned workers_hard_limit, size_t stack_size );
174 
176  static market& global_market ( bool is_public, unsigned max_num_workers = 0, size_t stack_size = 0 );
177 
179  void destroy ();
180 
181 #if __TBB_TASK_PRIORITY
182  arena* arena_in_need ( arena* prev_arena );
184 
186 
188  void update_allotment ( intptr_t highest_affected_priority );
189 
191  void update_arena_top_priority ( arena& a, intptr_t newPriority );
192 
194  inline void update_global_top_priority ( intptr_t newPriority );
195 
197  inline void reset_global_priority ();
198 
199  inline void advance_global_reload_epoch () {
200  __TBB_store_with_release( my_global_reload_epoch, my_global_reload_epoch + 1 );
201  }
202 
203  void assert_market_valid () const {
204  __TBB_ASSERT( (my_priority_levels[my_global_top_priority].workers_requested > 0
205  && !my_priority_levels[my_global_top_priority].arenas.empty())
206  || (my_global_top_priority == my_global_bottom_priority &&
207  my_global_top_priority == normalized_normal_priority), NULL );
208  }
209 
210 #else /* !__TBB_TASK_PRIORITY */
211 
213 
216  if ( my_total_demand )
218  }
219 
220  // TODO: consider to rewrite the code with is_arena_in_list function
224  return NULL;
227  }
228  void assert_market_valid () const {}
229 #endif /* !__TBB_TASK_PRIORITY */
230 
232  // Helpers to unify code branches dependent on priority feature presence
233 
234  void insert_arena_into_list ( arena& a );
235 
236  void remove_arena_from_list ( arena& a );
237 
238  arena* arena_in_need ( arena_list_type &arenas, arena *hint );
239 
240  static int update_allotment ( arena_list_type& arenas, int total_demand, int max_workers );
241 
242  bool is_arena_in_list( arena_list_type &arenas, arena *a );
243 
244 
246  // Implementation of rml::tbb_client interface methods
247 
248  version_type version () const __TBB_override { return 0; }
249 
251 
252  size_t min_stack_size () const __TBB_override { return worker_stack_size(); }
253 
254  policy_type policy () const __TBB_override { return throughput; }
255 
257 
258  void cleanup( job& j ) __TBB_override;
259 
261 
262  void process( job& j ) __TBB_override;
263 
264 public:
266 
268  static arena* create_arena ( int num_slots, int num_reserved_slots, size_t stack_size );
269 
271  void try_destroy_arena ( arena*, uintptr_t aba_epoch );
272 
274  void detach_arena ( arena& );
275 
277  bool release ( bool is_public, bool blocking_terminate );
278 
279 #if __TBB_ENQUEUE_ENFORCED_CONCURRENCY
280  bool mandatory_concurrency_enable_impl ( arena *a, bool *enabled = NULL );
282 
284  bool mandatory_concurrency_enable ( arena *a );
285 
287  void mandatory_concurrency_disable ( arena *a );
288 #endif /* __TBB_ENQUEUE_ENFORCED_CONCURRENCY */
289 
291 
292  void adjust_demand ( arena&, int delta );
293 
295  bool must_join_workers () const { return my_join_workers; }
296 
298  size_t worker_stack_size () const { return my_stack_size; }
299 
301  static void set_active_num_workers( unsigned w );
302 
304  static unsigned app_parallelism_limit();
305 
306 #if _WIN32||_WIN64
307  void register_master( ::rml::server::execution_resource_t& rsc_handle ) {
309  __TBB_ASSERT( my_server, "RML server not defined?" );
310  // the server may ignore registration and set master_exec_resource to NULL.
311  my_server->register_master( rsc_handle );
312  }
313 
315  void unregister_master( ::rml::server::execution_resource_t& rsc_handle ) const {
316  my_server->unregister_master( rsc_handle );
317  }
318 #endif /* WIN */
319 
320 #if __TBB_TASK_GROUP_CONTEXT
321 
326  template <typename T>
327  bool propagate_task_group_state ( T task_group_context::*mptr_state, task_group_context& src, T new_state );
328 #endif /* __TBB_TASK_GROUP_CONTEXT */
329 
330 #if __TBB_TASK_PRIORITY
331 
333  bool lower_arena_priority ( arena& a, intptr_t new_priority, uintptr_t old_reload_epoch );
334 
336 
342  bool update_arena_priority ( arena& a, intptr_t new_priority );
343 #endif /* __TBB_TASK_PRIORITY */
344 
345 #if __TBB_COUNT_TASK_NODES
346 
348  void update_task_node_count( intptr_t delta ) { my_task_node_count += delta; }
349 #endif /* __TBB_COUNT_TASK_NODES */
350 
351 #if __TBB_TASK_GROUP_CONTEXT
352  scheduler_list_type my_masters;
354 
356 
358  generic_scheduler* my_workers[1];
359 #endif /* __TBB_TASK_GROUP_CONTEXT */
360 
361  static unsigned max_num_workers() {
362  global_market_mutex_type::scoped_lock lock( theMarketMutex );
364  }
365 }; // class market
366 
367 } // namespace internal
368 } // namespace tbb
369 
370 #if defined(_MSC_VER) && defined(_Wp64)
371  // Workaround for overzealous compiler warnings in /Wp64 mode
372  #pragma warning (pop)
373 #endif // warning 4244 is back
374 
375 #endif /* _TBB_market_H */
Fast, unfair, spinning reader-writer lock with backoff and writer-preference.
Definition: spin_rw_mutex.h:42
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:165
unsigned my_public_ref_count
Count of master threads attached.
Definition: market.h:153
bool my_join_workers
Shutdown mode.
Definition: market.h:159
#define __TBB_override
Definition: tbb_stddef.h:244
uintptr_t my_arenas_aba_epoch
ABA prevention marker to assign to newly created arenas.
Definition: market.h:147
static const unsigned skip_soft_limit_warning
The value indicating that the soft limit warning is unnecessary.
Definition: market.h:162
int my_total_demand
Number of workers that were requested by all arenas.
Definition: market.h:93
arena * my_next_arena
The first arena to be checked when idle worker seeks for an arena to enter.
Definition: market.h:143
version_type version() const __TBB_override
Definition: market.h:248
T __TBB_load_with_acquire(const volatile T &location)
Definition: tbb_machine.h:713
static market * theMarket
Currently active global market.
Definition: market.h:62
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:169
size_t min_stack_size() const __TBB_override
Definition: market.h:252
void adjust_demand(arena &, int delta)
Request that arena's need in workers should be adjusted.
Definition: market.cpp:590
friend void ITT_DoUnsafeOneTimeInitialization()
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:100
Used to form groups of tasks.
Definition: task.h:335
arena * arena_in_need(arena *)
Returns next arena that needs more workers, or NULL.
Definition: market.h:222
Base class for types that should not be copied or assigned.
Definition: tbb_stddef.h:335
int my_num_workers_requested
Number of workers currently requested from RML.
Definition: market.h:85
static arena * create_arena(int num_slots, int num_reserved_slots, size_t stack_size)
Creates an arena object.
Definition: market.cpp:300
Work stealing task scheduler.
Definition: scheduler.h:124
unsigned my_ref_count
Reference count controlling market object lifetime.
Definition: market.h:150
friend class arena
Definition: market.h:51
void detach_arena(arena &)
Removes the arena from the market's list.
Definition: market.cpp:314
arenas_list_mutex_type my_arenas_list_mutex
Definition: market.h:71
bool is_arena_in_list(arena_list_type &arenas, arena *a)
Definition: market.cpp:417
bool must_join_workers() const
Used when RML asks for join mode during workers termination.
Definition: market.h:295
unsigned max_job_count() const __TBB_override
Definition: market.h:250
arena_list_type my_arenas
List of registered arenas.
Definition: market.h:139
static global_market_mutex_type theMarketMutex
Mutex guarding creation/destruction of theMarket, insertions/deletions in my_arenas,...
Definition: market.h:67
intrusive_list< arena > arena_list_type
Definition: market.h:58
market(unsigned workers_soft_limit, unsigned workers_hard_limit, size_t stack_size)
Constructor.
Definition: market.cpp:68
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
scheduler_mutex_type global_market_mutex_type
Definition: market.h:64
static const intptr_t num_priority_levels
The graph class.
void update_allotment()
Recalculates the number of workers assigned to each arena in the list.
Definition: market.h:215
__TBB_SCHEDULER_MUTEX_TYPE scheduler_mutex_type
Mutex type for global locks in the scheduler.
job * create_one_job() __TBB_override
Definition: market.cpp:738
spin_rw_mutex arenas_list_mutex_type
Lightweight mutex guarding accounting operations with arenas list.
Definition: market.h:70
void destroy()
Destroys and deallocates market object created by market::create()
Definition: market.cpp:169
size_t my_stack_size
Stack size of worker threads.
Definition: market.h:156
bool release(bool is_public, bool blocking_terminate)
Decrements market's refcount and destroys it in the end.
Definition: market.cpp:179
size_t worker_stack_size() const
Returns the requested stack size of worker threads.
Definition: market.h:298
atomic< unsigned > my_first_unused_worker_idx
First unused index of worker.
Definition: market.h:90
rml::tbb_server * my_server
Pointer to the RML server object that services this TBB instance.
Definition: market.h:74
static unsigned app_parallelism_limit()
Reports active parallelism level according to user's settings.
Definition: tbb_main.cpp:496
static unsigned max_num_workers()
Definition: market.h:361
void acknowledge_close_connection() __TBB_override
Definition: market.cpp:734
intrusive_list< generic_scheduler > scheduler_list_type
Definition: market.h:59
void remove_arena_from_list(arena &a)
Definition: market.cpp:46
unsigned my_num_workers_hard_limit
Maximal number of workers allowed for use by the underlying resource manager.
Definition: market.h:78
static void set_active_num_workers(unsigned w)
Set number of active workers.
Definition: market.cpp:221
void cleanup(job &j) __TBB_override
Definition: market.cpp:720
void process(job &j) __TBB_override
Definition: market.cpp:698
The scoped locking pattern.
Definition: spin_rw_mutex.h:90
void try_destroy_arena(arena *, uintptr_t aba_epoch)
Removes the arena from the market's list.
Definition: market.cpp:322
A scheduler with a customized evaluation loop.
void __TBB_store_with_release(volatile T &location, V value)
Definition: tbb_machine.h:717
void assert_market_valid() const
Definition: market.h:228
friend class generic_scheduler
Definition: market.h:50
unsigned my_num_workers_soft_limit
Current application-imposed limit on the number of workers (see set_active_num_workers())
Definition: market.h:82
policy_type policy() const __TBB_override
Definition: market.h:254
void insert_arena_into_list(arena &a)
Definition: market.cpp:33

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.