Geant4-11
ThreadData.hh
Go to the documentation of this file.
1//
2// MIT License
3// Copyright (c) 2020 Jonathan R. Madsen
4// Permission is hereby granted, free of charge, to any person obtaining a copy
5// of this software and associated documentation files (the "Software"), to deal
6// in the Software without restriction, including without limitation the rights
7// to use, copy, modify, merge, publish, distribute, sublicense, and
8// copies of the Software, and to permit persons to whom the Software is
9// furnished to do so, subject to the following conditions:
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED
12// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
13// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
15// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
16// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
17// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18//
19// ---------------------------------------------------------------
20// Tasking class header
21// Class Description:
22// ---------------------------------------------------------------
23// Author: Jonathan Madsen
24// ---------------------------------------------------------------
25
26#pragma once
27
28#include <cstddef>
29#include <cstdint>
30#include <deque>
31#include <thread>
32
33#if defined(PTL_USE_TBB)
34# if !defined(TBB_PREVIEW_GLOBAL_CONTROL)
35# define TBB_PREVIEW_GLOBAL_CONTROL 1
36# endif
37# include <tbb/global_control.h>
38# include <tbb/task_arena.h>
39# include <tbb/task_group.h>
40#endif
41
42namespace PTL
43{
44//--------------------------------------------------------------------------------------//
45
46#if defined(PTL_USE_TBB)
47
48using tbb_global_control_t = ::tbb::global_control;
49using tbb_task_group_t = ::tbb::task_group;
50using tbb_task_arena_t = ::tbb::task_arena;
51
52#else
53
54namespace tbb
55{
57{
58public:
59 // dummy constructor
61 // dummy wait
62 inline void wait() {}
63 // run function
64 template <typename FuncT>
65 inline void run(FuncT f)
66 {
67 f();
68 }
69 // run and wait
70 template <typename FuncT>
71 inline void run_and_wait(FuncT f)
72 {
73 f();
74 }
75};
76
78{
79public:
81 {
84 };
85
86 global_control(parameter p, size_t value);
88 static size_t active_value(parameter param);
89};
90
92{
93public:
95 {
97 automatic = -1
98 };
99
100 task_arena(int max_concurrency = automatic, unsigned reserved_for_masters = 1)
101 {
102 (void) max_concurrency;
103 (void) reserved_for_masters;
104 }
105
106 ~task_arena() = default;
107
108 void initialize(int max_concurrency = automatic, unsigned reserved_for_masters = 1);
109
110 template <typename FuncT>
111 auto execute(FuncT&& _func) -> decltype(_func())
112 {
113 return _func();
114 }
115};
116
117} // namespace tbb
118
122
123#endif
124
125//--------------------------------------------------------------------------------------//
126
127class ThreadPool;
128class VUserTaskQueue;
129
130//--------------------------------------------------------------------------------------//
131
133{
134public:
135 template <typename Tp>
136 using TaskStack = std::deque<Tp>;
137
139 ~ThreadData() = default;
140
141 void update();
142
143public:
144 bool is_main = false;
145 bool within_task = false;
146 intmax_t task_depth = 0;
150
151public:
152 // Public functions
153 static ThreadData*& GetInstance();
154};
155
156//--------------------------------------------------------------------------------------//
157
158} // namespace PTL
static ThreadData *& GetInstance()
Definition: ThreadData.cc:35
ThreadData(ThreadPool *tp)
Definition: ThreadData.cc:43
VUserTaskQueue * current_queue
Definition: ThreadData.hh:148
std::deque< Tp > TaskStack
Definition: ThreadData.hh:136
TaskStack< VUserTaskQueue * > queue_stack
Definition: ThreadData.hh:149
~ThreadData()=default
ThreadPool * thread_pool
Definition: ThreadData.hh:147
intmax_t task_depth
Definition: ThreadData.hh:146
global_control(parameter p, size_t value)
static size_t active_value(parameter param)
auto execute(FuncT &&_func) -> decltype(_func())
Definition: ThreadData.hh:111
void initialize(int max_concurrency=automatic, unsigned reserved_for_masters=1)
task_arena(int max_concurrency=automatic, unsigned reserved_for_masters=1)
Definition: ThreadData.hh:100
void run_and_wait(FuncT f)
Definition: ThreadData.hh:71
void run(FuncT f)
Definition: ThreadData.hh:65
Definition: AutoLock.hh:254
tbb::task_group tbb_task_group_t
Definition: ThreadData.hh:120
tbb::global_control tbb_global_control_t
Definition: ThreadData.hh:119
tbb::task_arena tbb_task_arena_t
Definition: ThreadData.hh:121