Geant4 11.2.2
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4TaskRunManager.hh
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26// Author: Jonathan Madsen (May 28st 2020)
27//
28// class description:
29// This is a class for run control in GEANT4 for multi-threaded runs
30// It extends G4RunManager re-implementing multi-threaded behavior in
31// key methods. See documentation for G4RunManager
32// Users initializes an instance of this class instead of G4RunManager
33// to start a multi-threaded simulation.
34
35#ifndef G4TaskRunManager_hh
36#define G4TaskRunManager_hh 1
37
38#include "G4EnvironmentUtils.hh"
39#include "G4MTBarrier.hh"
40#include "G4MTRunManager.hh"
41#include "G4Profiler.hh"
42#include "G4RNGHelper.hh"
43#include "G4RunManager.hh"
44#include "G4TBBTaskGroup.hh"
45#include "G4TaskGroup.hh"
46#include "G4TaskManager.hh"
47#include "G4ThreadPool.hh"
48#include "G4Threading.hh"
49#include "G4VUserTaskQueue.hh"
50
51#include "PTL/TaskRunManager.hh"
52#include "rundefs.hh"
53
54#include <list>
55#include <map>
56
63
64//============================================================================//
65
67{
68 friend class G4RunManagerFactory;
69
70 public:
71 // the profiler aliases are only used when compiled with GEANT4_USE_TIMEMORY
73 using InitializeSeedsCallback = std::function<G4bool(G4int, G4int&, G4int&)>;
75
76 public:
77 // Returns the singleton instance of the run manager common to all threads
78 // implementing the master behavior
80 {
82 return dynamic_cast<G4TaskRunManager*>(_rm);
83 }
84
85 // Returns the singleton instance of the run manager kernel common to all
86 // threads
88
89 // Parameters:
90 // taskQueue : provide a custom task queue
91 // useTBB : only relevant if GEANT4_USE_TBB defined
92 // evtGrainsize : the number of events per task
93 G4TaskRunManager(G4bool useTBB = G4GetEnv<G4bool>("G4USE_TBB", false));
95 G4bool useTBB = G4GetEnv<G4bool>("G4USE_TBB", false), G4int evtGrainsize = 0);
96 ~G4TaskRunManager() override;
97
98 void SetGrainsize(G4int n) { eventGrainsize = n; }
99 G4int GetGrainsize() const { return eventGrainsize; }
100 inline G4int GetNumberOfTasks() const { return numberOfTasks; }
101 inline G4int GetNumberOfEventsPerTask() const { return numberOfEventsPerTask; }
102
103 void SetNumberOfThreads(G4int n) override;
104 G4int GetNumberOfThreads() const override;
105 size_t GetNumberActiveThreads() const override
106 {
108 }
110
111 // Inherited methods to re-implement for MT case
112 void Initialize() override;
113 void InitializeEventLoop(G4int n_event, const char* macroFile = nullptr,
114 G4int n_select = -1) override;
115 void InitializeThreadPool() override;
117
118 void Initialize(uint64_t nthreads) override { PTL::TaskRunManager::Initialize(nthreads); }
119
120 void TerminateOneEvent() override;
121 void ProcessOneEvent(G4int i_event) override;
122 void ConstructScoringWorlds() override;
123 void RunTermination() override;
124
125 // The following method should be invoked by G4WorkerTaskRunManager for each
126 // event. False is returned if no more event to be processed. Note: G4Event
127 // object must be instantiated by a worker thread. In case no more
128 // event remains to be processed, that worker thread must delete that G4Event
129 // object. If a worker runs with its own random number sequence, the Boolean
130 // flag reseedRequired should be set to false. This is *NOT* allowed for the
131 // first event.
133 G4bool reseedRequired = true) override;
134
135 // Same as above method, but the seeds are set only once over "eventModulo"
136 // events. The return value shows the number of events the caller Worker has
137 // to process (between 1 and eventModulo depending on number of events yet to
138 // be processed). G4Event object has the event ID of the first event of this
139 // bunch. If zero is returned no more event needs to be processed, and worker
140 // thread must delete that G4Event.
141 G4int SetUpNEvents(G4Event*, G4SeedsQueue* seedsQueue, G4bool reseedRequired = true) override;
142
143 // To be invoked solely from G4WorkerTaskRunManager to merge the results
144 void MergeScores(const G4ScoringManager* localScoringManager);
145 void MergeRun(const G4Run* localRun);
146
147 // Called to force workers to request and process the UI commands stack
148 // This will block untill all workers have processed UI commands
150
151 // Called by workers to signal to master it has completed processing of
152 // UI commands
153 // virtual WorkerActionRequest ThisWorkerWaitForNextAction();
154 // Worker thread barrier
155 // This method should be used by workers' run manager to wait,
156 // after an event loop for the next action to be performed
157 // (for example execute a new run)
158 // This returns the action to be performed
160
161 void WaitForReadyWorkers() override {}
162 void WaitForEndEventLoopWorkers() override;
163 void ThisWorkerReady() override {}
164 void ThisWorkerEndEventLoop() override {}
165
170
172
173 void AbortRun(G4bool softAbort = false) override;
174 void AbortEvent() override;
175
176 protected:
177 virtual void ComputeNumberOfTasks();
178
179 // Initialize the seeds list, if derived class does not implement this method
180 // A default generation will be used (nevents*2 random seeds)
181 // Return true if initialization is done.
182 G4bool InitializeSeeds(G4int /*nevts*/) override { return false; }
183
184 // Adds one seed to the list of seeds
185 void RefillSeeds() override;
186 void StoreRNGStatus(const G4String& filenamePrefix) override;
187
188 // Creates worker threads and signal to start
189 void CreateAndStartWorkers() override;
190
191 void TerminateWorkers() override;
193 virtual void AddEventTask(G4int);
194
195 protected:
196 // Barriers: synch points between master and workers
198
199 // aliases to inherited member values
204
206 return false;
207 };
208
209 private:
210 // grainsize
211 G4bool workersStarted = false;
212 G4int eventGrainsize = 0;
213 G4int numberOfEventsPerTask = -1;
214 G4int numberOfTasks = -1;
215 CLHEP::HepRandomEngine* masterRNGEngine = nullptr;
216 // Pointer to the master thread random engine
217 G4TaskRunManagerKernel* MTkernel = nullptr;
218};
219
220#endif // G4TaskRunManager_hh
_Tp G4GetEnv(const std::string &env_id, _Tp _default=_Tp())
std::queue< G4long > G4SeedsQueue
G4Thread::id G4ThreadId
long G4long
Definition G4Types.hh:87
bool G4bool
Definition G4Types.hh:86
int G4int
Definition G4Types.hh:85
static G4MTRunManager * GetMasterRunManager()
Definition G4Run.hh:49
G4int GetNumberOfThreads() const override
void ConstructScoringWorlds() override
void InitializeThreadPool() override
void ThisWorkerEndEventLoop() override
void CreateAndStartWorkers() override
G4bool InitializeSeeds(G4int) override
static G4TaskRunManager * GetMasterRunManager()
void ThisWorkerProcessCommandsStackDone() override
G4int SetUpNEvents(G4Event *, G4SeedsQueue *seedsQueue, G4bool reseedRequired=true) override
WorkerActionRequest ThisWorkerWaitForNextAction() override
G4TaskManager *& taskManager
G4VUserTaskQueue *& taskQueue
void SetNumberOfThreads(G4int n) override
void SetInitializeSeedsCallback(InitializeSeedsCallback f)
std::function< G4bool(G4int, G4int &, G4int &)> InitializeSeedsCallback
G4int GetNumberOfEventsPerTask() const
void AbortRun(G4bool softAbort=false) override
InitializeSeedsCallback initSeedsCallback
void RefillSeeds() override
void RequestWorkersProcessCommandsStack() override
G4int GetNumberOfTasks() const
virtual void AddEventTask(G4int)
static G4ThreadId GetMasterThreadId()
G4int GetGrainsize() const
void TerminateOneEvent() override
RunTaskGroup * workTaskGroup
void TerminateWorkers() override
void AbortEvent() override
~G4TaskRunManager() override
size_t GetNumberActiveThreads() const override
void InitializeEventLoop(G4int n_event, const char *macroFile=nullptr, G4int n_select=-1) override
void WaitForReadyWorkers() override
void SetGrainsize(G4int n)
void Initialize() override
void StoreRNGStatus(const G4String &filenamePrefix) override
void ThisWorkerReady() override
void RunTermination() override
void MergeScores(const G4ScoringManager *localScoringManager)
void WaitForEndEventLoopWorkers() override
G4ThreadPool *& threadPool
static G4TaskRunManagerKernel * GetMTMasterRunManagerKernel()
G4bool ThreadPoolIsInitialized() const
void ProcessOneEvent(G4int i_event) override
void MergeRun(const G4Run *localRun)
void Initialize(uint64_t nthreads) override
void NewActionRequest(WorkerActionRequest) override
virtual void ComputeNumberOfTasks()
G4TaskRunManager(G4bool useTBB=G4GetEnv< G4bool >("G4USE_TBB", false))
G4bool SetUpAnEvent(G4Event *, G4long &s1, G4long &s2, G4long &s3, G4bool reseedRequired=true) override
TaskManager * m_task_manager
virtual void Initialize(uint64_t n=std::thread::hardware_concurrency())
ThreadPool * m_thread_pool
VUserTaskQueue * m_task_queue
virtual size_t GetNumberActiveThreads() const