File indexing completed on 2025-01-18 09:59:08
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035 #ifndef G4TaskRunManager_hh
0036 #define G4TaskRunManager_hh 1
0037
0038 #include "G4EnvironmentUtils.hh"
0039 #include "G4MTBarrier.hh"
0040 #include "G4MTRunManager.hh"
0041 #include "G4Profiler.hh"
0042 #include "G4RNGHelper.hh"
0043 #include "G4RunManager.hh"
0044 #include "G4TBBTaskGroup.hh"
0045 #include "G4TaskGroup.hh"
0046 #include "G4TaskManager.hh"
0047 #include "G4ThreadPool.hh"
0048 #include "G4Threading.hh"
0049 #include "G4VUserTaskQueue.hh"
0050
0051 #include "PTL/TaskRunManager.hh"
0052 #include "rundefs.hh"
0053
0054 #include <list>
0055 #include <map>
0056
0057 class G4TaskRunManagerKernel;
0058 class G4ScoringManager;
0059 class G4UserTaskInitialization;
0060 class G4UserTaskThreadInitialization;
0061 class G4WorkerTaskRunManager;
0062 class G4RunManagerFactory;
0063
0064
0065
0066 class G4TaskRunManager : public G4MTRunManager, public PTL::TaskRunManager
0067 {
0068 friend class G4RunManagerFactory;
0069
0070 public:
0071
0072 using ProfilerConfig = G4ProfilerConfig<G4ProfileType::Run>;
0073 using InitializeSeedsCallback = std::function<G4bool(G4int, G4int&, G4int&)>;
0074 using RunTaskGroup = G4TaskGroup<void>;
0075
0076 public:
0077
0078
0079 static G4TaskRunManager* GetMasterRunManager()
0080 {
0081 auto* _rm = G4MTRunManager::GetMasterRunManager();
0082 return dynamic_cast<G4TaskRunManager*>(_rm);
0083 }
0084
0085
0086
0087 static G4TaskRunManagerKernel* GetMTMasterRunManagerKernel();
0088
0089
0090
0091
0092
0093 G4TaskRunManager(G4bool useTBB = G4GetEnv<G4bool>("G4USE_TBB", false));
0094 G4TaskRunManager(G4VUserTaskQueue* taskQueue,
0095 G4bool useTBB = G4GetEnv<G4bool>("G4USE_TBB", false), G4int evtGrainsize = 0);
0096 ~G4TaskRunManager() override;
0097
0098 void SetGrainsize(G4int n) { eventGrainsize = n; }
0099 G4int GetGrainsize() const { return eventGrainsize; }
0100 inline G4int GetNumberOfTasks() const { return numberOfTasks; }
0101 inline G4int GetNumberOfEventsPerTask() const { return numberOfEventsPerTask; }
0102
0103 void SetNumberOfThreads(G4int n) override;
0104 G4int GetNumberOfThreads() const override;
0105 size_t GetNumberActiveThreads() const override
0106 {
0107 return PTL::TaskRunManager::GetNumberActiveThreads();
0108 }
0109 static G4ThreadId GetMasterThreadId();
0110
0111
0112 void Initialize() override;
0113 void InitializeEventLoop(G4int n_event, const char* macroFile = nullptr,
0114 G4int n_select = -1) override;
0115 void InitializeThreadPool() override;
0116 G4bool ThreadPoolIsInitialized() const { return poolInitialized; }
0117
0118 void Initialize(uint64_t nthreads) override { PTL::TaskRunManager::Initialize(nthreads); }
0119
0120 void TerminateOneEvent() override;
0121 void ProcessOneEvent(G4int i_event) override;
0122 void ConstructScoringWorlds() override;
0123 void RunTermination() override;
0124
0125
0126
0127
0128
0129
0130
0131
0132 G4bool SetUpAnEvent(G4Event*, G4long& s1, G4long& s2, G4long& s3,
0133 G4bool reseedRequired = true) override;
0134
0135
0136
0137
0138
0139
0140
0141 G4int SetUpNEvents(G4Event*, G4SeedsQueue* seedsQueue, G4bool reseedRequired = true) override;
0142
0143
0144 void MergeScores(const G4ScoringManager* localScoringManager);
0145 void MergeRun(const G4Run* localRun);
0146
0147
0148
0149 void RequestWorkersProcessCommandsStack() override;
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159 void ThisWorkerProcessCommandsStackDone() override;
0160
0161 void WaitForReadyWorkers() override {}
0162 void WaitForEndEventLoopWorkers() override;
0163 void ThisWorkerReady() override {}
0164 void ThisWorkerEndEventLoop() override {}
0165
0166 WorkerActionRequest ThisWorkerWaitForNextAction() override
0167 {
0168 return WorkerActionRequest::UNDEFINED;
0169 }
0170
0171 inline void SetInitializeSeedsCallback(InitializeSeedsCallback f) { initSeedsCallback = f; }
0172
0173 void AbortRun(G4bool softAbort = false) override;
0174 void AbortEvent() override;
0175
0176 protected:
0177 virtual void ComputeNumberOfTasks();
0178
0179
0180
0181
0182 G4bool InitializeSeeds(G4int ) override { return false; }
0183
0184
0185 void RefillSeeds() override;
0186 void StoreRNGStatus(const G4String& filenamePrefix) override;
0187
0188
0189 void CreateAndStartWorkers() override;
0190
0191 void TerminateWorkers() override;
0192 void NewActionRequest(WorkerActionRequest) override {}
0193 virtual void AddEventTask(G4int);
0194
0195 protected:
0196
0197 RunTaskGroup* workTaskGroup = nullptr;
0198
0199
0200 G4bool& poolInitialized = PTL::TaskRunManager::m_is_initialized;
0201 G4ThreadPool*& threadPool = PTL::TaskRunManager::m_thread_pool;
0202 G4VUserTaskQueue*& taskQueue = PTL::TaskRunManager::m_task_queue;
0203 G4TaskManager*& taskManager = PTL::TaskRunManager::m_task_manager;
0204
0205 InitializeSeedsCallback initSeedsCallback = [](G4int, G4int&, G4int&) {
0206 return false;
0207 };
0208
0209 private:
0210
0211 G4bool workersStarted = false;
0212 G4int eventGrainsize = 0;
0213 G4int numberOfEventsPerTask = -1;
0214 G4int numberOfTasks = -1;
0215 CLHEP::HepRandomEngine* masterRNGEngine = nullptr;
0216
0217 G4TaskRunManagerKernel* MTkernel = nullptr;
0218 };
0219
0220 #endif