Geant4 11.1.1
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 "rundefs.hh"
39#include "G4MTBarrier.hh"
40#include "G4MTRunManager.hh"
41#include "G4RNGHelper.hh"
42#include "G4RunManager.hh"
43#include "G4TBBTaskGroup.hh"
44#include "G4TaskGroup.hh"
45#include "G4TaskManager.hh"
46#include "G4ThreadPool.hh"
47#include "G4Threading.hh"
48#include "G4VUserTaskQueue.hh"
49#include "G4EnvironmentUtils.hh"
50#include "G4Profiler.hh"
51
52#include "PTL/TaskRunManager.hh"
53
54#include <list>
55#include <map>
56
63
64//============================================================================//
65
67 : public G4MTRunManager
68 , public PTL::TaskRunManager
69{
70 friend class G4RunManagerFactory;
71
72 public:
73 // the profiler aliases are only used when compiled with GEANT4_USE_TIMEMORY
75
76 public:
77 using InitializeSeedsCallback = std::function<G4bool(G4int, G4int&, G4int&)>;
79
80 public:
81 // Parameters:
82 // taskQueue : provide a custom task queue
83 // useTBB : only relevant if GEANT4_USE_TBB defined
84 // evtGrainsize : the number of events per task
85 G4TaskRunManager(G4bool useTBB = G4GetEnv<G4bool>("G4USE_TBB", false));
87 G4bool useTBB = G4GetEnv<G4bool>("G4USE_TBB", false),
88 G4int evtGrainsize = 0);
89 virtual ~G4TaskRunManager();
90
91 public:
92 void SetGrainsize(G4int n) { eventGrainsize = n; }
93 G4int GetGrainsize() const { return eventGrainsize; }
94 inline G4int GetNumberOfTasks() const { return numberOfTasks; }
96 {
97 return numberOfEventsPerTask;
98 }
99
100 virtual void SetNumberOfThreads(G4int n) override;
101 virtual G4int GetNumberOfThreads() const override
102 {
104 }
105 virtual size_t GetNumberActiveThreads() const override
106 {
108 }
110
111 public:
112 // Inherited methods to re-implement for MT case
113 virtual void Initialize() override;
114 virtual void InitializeEventLoop(G4int n_event,
115 const char* macroFile = nullptr,
116 G4int n_select = -1) override;
117 virtual void InitializeThreadPool() override;
119
120 virtual void Initialize(uint64_t nthreads) override
121 {
123 }
124
125 virtual void TerminateOneEvent() override;
126 virtual void ProcessOneEvent(G4int i_event) override;
127 virtual void ConstructScoringWorlds() override;
128 virtual void RunTermination() override;
129
130 // The following method should be invoked by G4WorkerTaskRunManager for each
131 // event. False is returned if no more event to be processed. Note: G4Event
132 // object must be instantiated by a worker thread. In case no more
133 // event remains to be processed, that worker thread must delete that G4Event
134 // object. If a worker runs with its own random number sequence, the Boolean
135 // flag reseedRequired should be set to false. This is *NOT* allowed for the
136 // first event.
137 virtual G4bool SetUpAnEvent(G4Event*, G4long& s1, G4long& s2, G4long& s3,
138 G4bool reseedRequired = true) override;
139 // Same as above method, but the seeds are set only once over "eventModulo"
140 // events. The return value shows the number of events the caller Worker has
141 // to process (between 1 and eventModulo depending on number of events yet to
142 // be processed). G4Event object has the event ID of the first event of this
143 // bunch. If zero is returned no more event needs to be processed, and worker
144 // thread must delete that G4Event.
145 virtual G4int SetUpNEvents(G4Event*, G4SeedsQueue* seedsQueue,
146 G4bool reseedRequired = true) override;
147
148 // Method called by Initialize() method
149
150 protected:
151 virtual void ComputeNumberOfTasks();
152 // Initialize the seeds list, if derived class does not implement this method
153 // A default generation will be used (nevents*2 random seeds)
154 // Return true if initialization is done.
155 virtual G4bool InitializeSeeds(G4int /*nevts*/) override { return false; }
156 virtual void RefillSeeds() override;
157 // Adds one seed to the list of seeds
158 virtual void StoreRNGStatus(const G4String& filenamePrefix) override;
159 virtual void CreateAndStartWorkers() override;
160 // Creates worker threads and signal to start
161
162 protected:
163 virtual void TerminateWorkers() override;
164
165 public: // with description
167 {
169 return dynamic_cast<G4TaskRunManager*>(_rm);
170 }
171 // Returns the singleton instance of the run manager common to all threads
172 // implementing the master behavior
174 // Returns the singleton instance of the run manager kernel common to all
175 // threads
176
177 public:
178 // To be invoked solely from G4WorkerTaskRunManager to merge the results
179 void MergeScores(const G4ScoringManager* localScoringManager);
180 void MergeRun(const G4Run* localRun);
181
182 public:
183 virtual void RequestWorkersProcessCommandsStack() override;
184 // Called to force workers to request and process the UI commands stack
185 // This will block untill all workers have processed UI commands
186 virtual void ThisWorkerProcessCommandsStackDone() override;
187 // Called by workers to signal to master it has completed processing of
188 // UI commands
189 // virtual WorkerActionRequest ThisWorkerWaitForNextAction();
190 // Worker thread barrier
191 // This method should be used by workers' run manager to wait,
192 // after an event loop for the next action to be performed
193 // (for example execute a new run)
194 // This returns the action to be performed
195 virtual void WaitForReadyWorkers() override {}
196 virtual void WaitForEndEventLoopWorkers() override;
197 virtual void ThisWorkerReady() override {}
198 virtual void ThisWorkerEndEventLoop() override {}
200 {
202 }
203
204 protected:
205 virtual void NewActionRequest(WorkerActionRequest) override {}
206 virtual void AddEventTask(G4int);
207
208 public:
210 {
212 }
213
214 public:
215 virtual void AbortRun(G4bool softAbort = false) override;
216 virtual void AbortEvent() override;
217
218 private:
219 // grainsize
220 bool workersStarted = false;
221 G4int eventGrainsize = 0;
222 G4int numberOfEventsPerTask = -1;
223 G4int numberOfTasks = -1;
224 CLHEP::HepRandomEngine* masterRNGEngine = nullptr;
225 // Pointer to the master thread random engine
226 G4TaskRunManagerKernel* MTkernel = nullptr;
227
228 protected:
229 // Barriers: synch points between master and workers
231
232 // aliases to inherited member values
237
239 return false;
240 };
241};
242
243#endif // G4TaskRunManager_hh
std::queue< G4long > G4SeedsQueue
Definition: G4RNGHelper.hh:136
G4Thread::id G4ThreadId
Definition: G4Threading.hh:286
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
virtual void ConstructScoringWorlds() override
virtual void InitializeThreadPool() override
virtual void CreateAndStartWorkers() override
static G4TaskRunManager * GetMasterRunManager()
virtual G4bool InitializeSeeds(G4int) override
virtual void ThisWorkerProcessCommandsStackDone() override
virtual G4int SetUpNEvents(G4Event *, G4SeedsQueue *seedsQueue, G4bool reseedRequired=true) override
G4TaskManager *& taskManager
G4VUserTaskQueue *& taskQueue
virtual void SetNumberOfThreads(G4int n) override
void SetInitializeSeedsCallback(InitializeSeedsCallback f)
std::function< G4bool(G4int, G4int &, G4int &)> InitializeSeedsCallback
G4int GetNumberOfEventsPerTask() const
virtual void AbortRun(G4bool softAbort=false) override
InitializeSeedsCallback initSeedsCallback
virtual void RefillSeeds() override
virtual void RequestWorkersProcessCommandsStack() override
G4int GetNumberOfTasks() const
virtual void AddEventTask(G4int)
static G4ThreadId GetMasterThreadId()
G4int GetGrainsize() const
virtual void Initialize(uint64_t nthreads) override
virtual void TerminateOneEvent() override
RunTaskGroup * workTaskGroup
virtual void TerminateWorkers() override
virtual void AbortEvent() override
virtual size_t GetNumberActiveThreads() const override
virtual void InitializeEventLoop(G4int n_event, const char *macroFile=nullptr, G4int n_select=-1) override
virtual ~G4TaskRunManager()
virtual WorkerActionRequest ThisWorkerWaitForNextAction() override
void SetGrainsize(G4int n)
virtual void NewActionRequest(WorkerActionRequest) override
virtual void ThisWorkerEndEventLoop() override
virtual void Initialize() override
virtual void StoreRNGStatus(const G4String &filenamePrefix) override
virtual G4int GetNumberOfThreads() const override
virtual void RunTermination() override
void MergeScores(const G4ScoringManager *localScoringManager)
virtual void WaitForReadyWorkers() override
virtual void WaitForEndEventLoopWorkers() override
G4ThreadPool *& threadPool
static G4TaskRunManagerKernel * GetMTMasterRunManagerKernel()
G4bool ThreadPoolIsInitialized() const
virtual void ProcessOneEvent(G4int i_event) override
void MergeRun(const G4Run *localRun)
virtual void ComputeNumberOfTasks()
virtual G4bool SetUpAnEvent(G4Event *, G4long &s1, G4long &s2, G4long &s3, G4bool reseedRequired=true) override
virtual void ThisWorkerReady() override
virtual int GetNumberOfThreads() const
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