Geant4 Cross Reference |
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 "" 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 46 namespace PTL 47 { 48 //--------------------------------------------------------------------------------------// 49 50 #if defined(PTL_USE_TBB) 51 52 using tbb_global_control_t = ::tbb::global_control; 53 using tbb_task_group_t = ::tbb::task_group; 54 using tbb_task_arena_t = ::tbb::task_arena; 55 56 #else 57 58 namespace tbb 59 { 60 class task_group 61 { 62 public: 63 // dummy constructor 64 task_group() {} 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 81 class global_control 82 { 83 public: 84 enum parameter 85 { 86 max_allowed_parallelism, 87 thread_stack_size 88 }; 89 90 global_control(parameter p, size_t value); 91 ~global_control(); 92 static size_t active_value(parameter param); 93 }; 94 95 class task_arena 96 { 97 public: 98 enum parameter 99 { 100 not_initialized = -2, 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 123 using tbb_global_control_t = tbb::global_control; 124 using tbb_task_group_t = tbb::task_group; 125 using tbb_task_arena_t = tbb::task_arena; 126 127 #endif 128 129 //--------------------------------------------------------------------------------------// 130 131 class ThreadPool; 132 class VUserTaskQueue; 133 134 //--------------------------------------------------------------------------------------// 135 136 class ThreadData 137 { 138 public: 139 template <typename Tp> 140 using TaskStack = std::deque<Tp>; 141 142 ThreadData(ThreadPool* tp); 143 ~ThreadData() = default; 144 145 void update(); 146 147 public: 148 bool is_main = false; 149 bool within_task = false; 150 intmax_t task_depth = 0; 151 ThreadPool* thread_pool = nullptr; 152 VUserTaskQueue* current_queue = nullptr; 153 TaskStack<VUserTaskQueue*> queue_stack = {}; 154 155 public: 156 // Public functions 157 static ThreadData*& GetInstance(); 158 }; 159 160 //--------------------------------------------------------------------------------------// 161 162 } // namespace PTL 163