Geant4 11.2.2
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
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#ifndef G4GMAKE
29#include "PTL/Config.hh"
30#endif
31
32#include <cstdint>
33#include <deque>
34
35#if defined(PTL_USE_TBB)
36# if !defined(TBB_PREVIEW_GLOBAL_CONTROL)
37# define TBB_PREVIEW_GLOBAL_CONTROL 1
38# endif
39# include <tbb/global_control.h>
40# include <tbb/task_arena.h>
41# include <tbb/task_group.h>
42#else
43# include <cstddef>
44#endif
45
46namespace PTL
47{
48//--------------------------------------------------------------------------------------//
49
50#if defined(PTL_USE_TBB)
51
52using tbb_global_control_t = ::tbb::global_control;
53using tbb_task_group_t = ::tbb::task_group;
54using tbb_task_arena_t = ::tbb::task_arena;
55
56#else
57
58namespace tbb
59{
61{
62public:
63 // dummy constructor
65 // dummy wait
66 inline void wait() {}
67 // run function
68 template <typename FuncT>
69 inline void run(FuncT f)
70 {
71 f();
72 }
73 // run and wait
74 template <typename FuncT>
75 inline void run_and_wait(FuncT f)
76 {
77 f();
78 }
79};
80
82{
83public:
89
90 global_control(parameter p, size_t value);
92 static size_t active_value(parameter param);
93};
94
96{
97public:
99 {
101 automatic = -1
102 };
103
104 task_arena(int max_concurrency = automatic, unsigned reserved_for_masters = 1)
105 {
106 (void) max_concurrency;
107 (void) reserved_for_masters;
108 }
109
110 ~task_arena() = default;
111
112 void initialize(int max_concurrency = automatic, unsigned reserved_for_masters = 1);
113
114 template <typename FuncT>
115 auto execute(FuncT&& _func) -> decltype(_func())
116 {
117 return _func();
118 }
119};
120
121} // namespace tbb
122
126
127#endif
128
129//--------------------------------------------------------------------------------------//
130
131class ThreadPool;
132class VUserTaskQueue;
133
134//--------------------------------------------------------------------------------------//
135
137{
138public:
139 template <typename Tp>
140 using TaskStack = std::deque<Tp>;
141
143 ~ThreadData() = default;
144
145 void update();
146
147public:
148 bool is_main = false;
149 bool within_task = false;
150 intmax_t task_depth = 0;
154
155public:
156 // Public functions
157 static ThreadData*& GetInstance();
158};
159
160//--------------------------------------------------------------------------------------//
161
162} // namespace PTL
static ThreadData *& GetInstance()
Definition ThreadData.cc:32
ThreadData(ThreadPool *tp)
Definition ThreadData.cc:40
VUserTaskQueue * current_queue
std::deque< Tp > TaskStack
TaskStack< VUserTaskQueue * > queue_stack
~ThreadData()=default
ThreadPool * thread_pool
intmax_t task_depth
global_control(parameter p, size_t value)
static size_t active_value(parameter param)
auto execute(FuncT &&_func) -> decltype(_func())
void initialize(int max_concurrency=automatic, unsigned reserved_for_masters=1)
task_arena(int max_concurrency=automatic, unsigned reserved_for_masters=1)
void run_and_wait(FuncT f)
Definition ThreadData.hh:75
void run(FuncT f)
Definition ThreadData.hh:69
tbb::task_arena tbb_task_arena_t
tbb::global_control tbb_global_control_t
tbb::task_group tbb_task_group_t