Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/Geant4/PTL/ThreadData.hh was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 //
0002 // MIT License
0003 // Copyright (c) 2020 Jonathan R. Madsen
0004 // Permission is hereby granted, free of charge, to any person obtaining a copy
0005 // of this software and associated documentation files (the "Software"), to deal
0006 // in the Software without restriction, including without limitation the rights
0007 // to use, copy, modify, merge, publish, distribute, sublicense, and
0008 // copies of the Software, and to permit persons to whom the Software is
0009 // furnished to do so, subject to the following conditions:
0010 // The above copyright notice and this permission notice shall be included in
0011 // all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED
0012 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
0013 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
0014 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
0015 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
0016 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
0017 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0018 //
0019 //  ---------------------------------------------------------------
0020 //  Tasking class header
0021 //  Class Description:
0022 //  ---------------------------------------------------------------
0023 //  Author: Jonathan Madsen
0024 //  ---------------------------------------------------------------
0025 
0026 #pragma once
0027 
0028 #ifndef G4GMAKE
0029 #include "PTL/Config.hh"
0030 #endif
0031 
0032 #include <cstdint>
0033 #include <deque>
0034 
0035 #if defined(PTL_USE_TBB)
0036 #    if !defined(TBB_PREVIEW_GLOBAL_CONTROL)
0037 #        define TBB_PREVIEW_GLOBAL_CONTROL 1
0038 #    endif
0039 #    include <tbb/global_control.h>
0040 #    include <tbb/task_arena.h>
0041 #    include <tbb/task_group.h>
0042 #else
0043 #    include <cstddef>
0044 #endif
0045 
0046 namespace PTL
0047 {
0048 //--------------------------------------------------------------------------------------//
0049 
0050 #if defined(PTL_USE_TBB)
0051 
0052 using tbb_global_control_t = ::tbb::global_control;
0053 using tbb_task_group_t     = ::tbb::task_group;
0054 using tbb_task_arena_t     = ::tbb::task_arena;
0055 
0056 #else
0057 
0058 namespace tbb
0059 {
0060 class task_group
0061 {
0062 public:
0063     // dummy constructor
0064     task_group() {}
0065     // dummy wait
0066     inline void wait() {}
0067     // run function
0068     template <typename FuncT>
0069     inline void run(FuncT f)
0070     {
0071         f();
0072     }
0073     // run and wait
0074     template <typename FuncT>
0075     inline void run_and_wait(FuncT f)
0076     {
0077         f();
0078     }
0079 };
0080 
0081 class global_control
0082 {
0083 public:
0084     enum parameter
0085     {
0086         max_allowed_parallelism,
0087         thread_stack_size
0088     };
0089 
0090     global_control(parameter p, size_t value);
0091     ~global_control();
0092     static size_t active_value(parameter param);
0093 };
0094 
0095 class task_arena
0096 {
0097 public:
0098     enum parameter
0099     {
0100         not_initialized = -2,
0101         automatic       = -1
0102     };
0103 
0104     task_arena(int max_concurrency = automatic, unsigned reserved_for_masters = 1)
0105     {
0106         (void) max_concurrency;
0107         (void) reserved_for_masters;
0108     }
0109 
0110     ~task_arena() = default;
0111 
0112     void initialize(int max_concurrency = automatic, unsigned reserved_for_masters = 1);
0113 
0114     template <typename FuncT>
0115     auto execute(FuncT&& _func) -> decltype(_func())
0116     {
0117         return _func();
0118     }
0119 };
0120 
0121 }  // namespace tbb
0122 
0123 using tbb_global_control_t = tbb::global_control;
0124 using tbb_task_group_t     = tbb::task_group;
0125 using tbb_task_arena_t     = tbb::task_arena;
0126 
0127 #endif
0128 
0129 //--------------------------------------------------------------------------------------//
0130 
0131 class ThreadPool;
0132 class VUserTaskQueue;
0133 
0134 //--------------------------------------------------------------------------------------//
0135 
0136 class ThreadData
0137 {
0138 public:
0139     template <typename Tp>
0140     using TaskStack = std::deque<Tp>;
0141 
0142     ThreadData(ThreadPool* tp);
0143     ~ThreadData() = default;
0144 
0145     void update();
0146 
0147 public:
0148     bool                       is_main       = false;
0149     bool                       within_task   = false;
0150     intmax_t                   task_depth    = 0;
0151     ThreadPool*                thread_pool   = nullptr;
0152     VUserTaskQueue*            current_queue = nullptr;
0153     TaskStack<VUserTaskQueue*> queue_stack   = {};
0154 
0155 public:
0156     // Public functions
0157     static ThreadData*& GetInstance();
0158 };
0159 
0160 //--------------------------------------------------------------------------------------//
0161 
0162 }  // namespace PTL