Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4MTRunManager.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//
27// class description:
28// This is a class for run control in GEANT4 for multi-threaded runs
29// It extends G4RunManager re-implementing multi-threaded behavior in
30// key methods. See documentation for G4RunManager
31// Users initializes an instance of this class instead of G4RunManager
32// to start a multi-threaded simulation.
33
34#ifndef G4MTRunManager_h
35#define G4MTRunManager_h 1
36
37#include "G4MTBarrier.hh"
38#include "G4RNGHelper.hh"
39#include "G4RunManager.hh"
40#include "G4Threading.hh"
41#include "G4Profiler.hh"
42#include <list>
43#include <map>
44
50
51// TODO: Split random number storage from this class
52
54{
55 friend class G4RunManagerFactory;
56
57 public:
58 // the profiler aliases are only used when compiled with GEANT4_USE_TIMEMORY
60
61 public:
63 virtual ~G4MTRunManager();
64 // New method
65 virtual void SetNumberOfThreads(G4int n);
66 virtual G4int GetNumberOfThreads() const { return nworkers; }
67 void SetPinAffinity(G4int n = 1);
68 G4int GetPinAffinity() const { return pinAffinity; }
69
70 public:
71 // Inherited methods to re-implement for MT case
72 virtual void Initialize();
73 virtual void InitializeEventLoop(G4int n_event, const char* macroFile = 0,
74 G4int n_select = -1);
75 virtual void InitializeThreadPool() {}
76
77 // The following do not do anything for this runmanager
78 virtual void TerminateOneEvent();
79 virtual void ProcessOneEvent(G4int i_event);
80 ////virtual void TerminateEventLoop();
81 virtual void ConstructScoringWorlds();
82 virtual void RunTermination();
83
84 // The following method should be invoked by G4WorkerRunManager for each
85 // event. False is returned if no more event to be processed. Note: G4Event
86 // object must be instantiated by a worker thread. In case no more
87 // event remains to be processed, that worker thread must delete that G4Event
88 // object. If a worker runs with its own random number sequence, the boolean
89 // flag reseedRequired should be set to false. This is *NOT* allowed for the
90 // first event.
91 virtual G4bool SetUpAnEvent(G4Event*, long& s1, long& s2, long& s3,
92 G4bool reseedRequired = true);
93 // Same as above method, but the seeds are set only once over "eventModulo"
94 // events. The return value shows the number of events the caller Worker has
95 // to process (between 1 and eventModulo depending on number of events yet to
96 // be processed). G4Event object has the event ID of the first event of this
97 // bunch. If zero is returned no more event needs to be processed, and worker
98 // thread must delete that G4Event.
99 virtual G4int SetUpNEvents(G4Event*, G4SeedsQueue* seedsQueue,
100 G4bool reseedRequired = true);
101
102 // Method called by Initialize() method
103 protected:
104 // Initialize the seeds list, if derived class does not implement this method
105 // A default generation will be used (nevents*2 random seeds)
106 // Return true if initialization is done.
107 virtual G4bool InitializeSeeds(G4int /*nevts*/) { return false; };
108 // Adds one seed to the list of seeds
109 virtual void PrepareCommandsStack();
110 virtual void StoreRNGStatus(const G4String& filenamePrefix);
111 virtual void rndmSaveThisRun();
112 virtual void rndmSaveThisEvent();
113 virtual void CreateAndStartWorkers();
114 // Creates worker threads and signal to start
115 public:
116 std::vector<G4String> GetCommandStack();
117 // This method is invoked just before spawning the threads to
118 // collect from UI managere the list of commands that threads
119 // will execute.
120 virtual size_t GetNumberActiveThreads() const { return threads.size(); }
121 // Returns number of currently active threads.
122 // This number may be different from the number of threads currently
123 // in running state (e.g. the number returned by:
124 // G4Threading::GetNumberOfActiveWorkerThreads() method).
126
127 protected:
128 // Number of worker threads. To be set by SetNumberOfThreads() method.
130 // Force to use this number regardless of SetNumberOfThreads() method.
132
133 private:
134 // Pin Affinity parameter
135 G4int pinAffinity;
136 // List of workers (i.e. thread)
137 typedef std::list<G4Thread*> G4ThreadsList;
138 G4ThreadsList threads;
139 // List of workers run managers
140 // List of all workers run managers
141 std::vector<G4String> uiCmdsForWorkers;
142 // List of UI commands for workers.
143 CLHEP::HepRandomEngine* masterRNGEngine;
144 // Pointer to the mastet thread random engine
145 protected:
146 virtual void WaitForReadyWorkers();
147 // Master thread barrier:
148 // Call this function to block master thread and
149 // wait workers to be ready to process work.
150 // This function will return only when all
151 // workers are ready to perform event loop.
152 virtual void WaitForEndEventLoopWorkers();
153 // Master thread barrier:
154 // Call this function to block master thread and
155 // wait workers have finished current event loop.
156 // This function will return only when all
157 // workers have finished processing events for this run.
158 protected:
160 virtual void TerminateWorkers();
161 // Empty the workersList
162
163 public:
164 virtual void ThisWorkerReady();
165 // Worker threads barrier:
166 // This method should be called by each
167 // worker when ready to start thread event-loop
168 // This method will return only when all workers
169 // are ready.
170 // static void ThisWorkerFinishWork();
171 // Worker threads barrier:
172 // This static method should be called by each
173 // worker when finish to process events
174 virtual void ThisWorkerEndEventLoop();
175 // Worker threads barrier:
176 // This method should be called by each
177 // worker when worker event loop is terminated.
178 typedef std::map<G4int, G4VPhysicalVolume*> masterWorlds_t;
181 static void addWorld(G4int counter, G4VPhysicalVolume* w);
182
184 {
185 return masterRNGEngine;
186 }
187
188 protected:
189 // Handling of master thread scoring worlds, access to it is needed by workers
192 // Singleton implementing master thread behavior
194
195 private:
196 G4MTRunManagerKernel* MTkernel;
197
198 public: // with description
200 // Returns the singleton instance of the run manager common to all threads
201 // implementing the master behavior
204 // Returns the singleton instance of the run manager kernel common to all
205 // threads
206
207 virtual void SetUserInitialization(G4VUserPhysicsList* userPL);
210 virtual void SetUserInitialization(
213 virtual void SetUserAction(G4UserRunAction* userAction);
214 virtual void SetUserAction(G4VUserPrimaryGeneratorAction* userAction);
215 virtual void SetUserAction(G4UserEventAction* userAction);
216 virtual void SetUserAction(G4UserStackingAction* userAction);
217 virtual void SetUserAction(G4UserTrackingAction* userAction);
218 virtual void SetUserAction(G4UserSteppingAction* userAction);
219
220 public:
221 // To be invoked solely from G4WorkerRunManager to merge the results
222 void MergeScores(const G4ScoringManager* localScoringManager);
223 void MergeRun(const G4Run* localRun);
224
225 public:
226 // Handling of more than one run per thread
228 {
229 UNDEFINED,
230 NEXTITERATION, // There is another set of UI commands to be executed
231 PROCESSUI, // Process UI commands w/o a /run/beamOn
232 ENDWORKER // Terminate thread, work finished
233 };
235 // Called to force workers to request and process the UI commands stack
236 // This will block untill all workers have processed UI commands
238 // Called by workers to signal to master it has completed processing of
239 // UI commands
241 // Worker thread barrier
242 // This method should be used by workers' run manager to wait,
243 // after an event loop for the next action to be performed
244 // (for example execute a new run)
245 // This returns the action to be performed
246 protected:
248 virtual void NewActionRequest(WorkerActionRequest newRequest);
249
250 protected:
257 double* randDbl;
258
259 virtual void RefillSeeds();
260
261 public:
262 inline void SetEventModulo(G4int i = 1) { eventModuloDef = i; }
263 inline G4int GetEventModulo() const { return eventModuloDef; }
264
265 public:
266 virtual void AbortRun(G4bool softAbort = false);
267 virtual void AbortEvent();
268
269 protected:
272 // - If it is set to 0 (default), seeds that are centrally managed
273 // by G4MTRunManager are set for every event of every worker thread.
274 // This option guarantees event reproducability regardless of number
275 // of threads.
276 // - If it is set to 1, seeds are set only once for the first
277 // event of each run of each worker thread. Event reproducability is
278 // guaranteed only if the same number of worker threads are used.
279 // On the other hand, this option offers better computing performance
280 // in particular for applications with relatively small primary
281 // particle energy and large number of events.
282 // - If it is set to 2, seeds are set only for the first event of
283 // group of N events. This option is reserved for the future use when
284 // Geant4 allows number of threads to be dynatically changed during an
285 // event loop.
286 public:
288 static void SetSeedOncePerCommunication(G4int val);
290
291 protected:
292 // Barriers: synch points between master and workers
297};
298
299#endif // G4MTRunManager_h
std::queue< G4long > G4SeedsQueue
Definition: G4RNGHelper.hh:133
G4Thread::id G4ThreadId
Definition: G4Threading.hh:286
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
static G4int SeedOncePerCommunication()
virtual void SetUserAction(G4UserRunAction *userAction)
virtual void InitializeThreadPool()
virtual WorkerActionRequest ThisWorkerWaitForNextAction()
std::map< G4int, G4VPhysicalVolume * > masterWorlds_t
static void SetSeedOncePerCommunication(G4int val)
virtual void AbortEvent()
G4int numberOfEventToBeProcessed
virtual void ProcessOneEvent(G4int i_event)
virtual void TerminateOneEvent()
G4int GetPinAffinity() const
virtual void rndmSaveThisEvent()
virtual void SetUserInitialization(G4VUserPhysicsList *userPL)
virtual G4bool InitializeSeeds(G4int)
virtual void WaitForReadyWorkers()
G4MTBarrier beginOfEventLoopBarrier
void SetEventModulo(G4int i=1)
virtual void CreateAndStartWorkers()
virtual void SetNumberOfThreads(G4int n)
virtual ~G4MTRunManager()
virtual void ThisWorkerProcessCommandsStackDone()
virtual G4int SetUpNEvents(G4Event *, G4SeedsQueue *seedsQueue, G4bool reseedRequired=true)
virtual G4bool SetUpAnEvent(G4Event *, long &s1, long &s2, long &s3, G4bool reseedRequired=true)
const CLHEP::HepRandomEngine * getMasterRandomEngine() const
static void addWorld(G4int counter, G4VPhysicalVolume *w)
virtual void InitializeEventLoop(G4int n_event, const char *macroFile=0, G4int n_select=-1)
virtual void PrepareCommandsStack()
static G4ScoringManager * GetMasterScoringManager()
static G4MTRUN_DLL G4ScoringManager * masterScM
virtual void Initialize()
static G4MTRUN_DLL G4MTRunManager * fMasterRM
virtual void ThisWorkerReady()
static G4MTRunManagerKernel * GetMTMasterRunManagerKernel()
static G4int seedOncePerCommunication
virtual void RunTermination()
static G4MTRUN_DLL masterWorlds_t masterWorlds
virtual G4int GetNumberOfThreads() const
virtual size_t GetNumberActiveThreads() const
virtual void ConstructScoringWorlds()
G4int GetEventModulo() const
static G4MTRunManager * GetMasterRunManager()
virtual void StoreRNGStatus(const G4String &filenamePrefix)
virtual void WaitForEndEventLoopWorkers()
WorkerActionRequest nextActionRequest
virtual void AbortRun(G4bool softAbort=false)
virtual void RefillSeeds()
virtual void rndmSaveThisRun()
static G4ThreadId masterThreadId
G4MTBarrier processUIBarrier
static G4ThreadId GetMasterThreadId()
void MergeRun(const G4Run *localRun)
G4MTBarrier endOfEventLoopBarrier
G4MTBarrier nextActionRequestBarrier
virtual void TerminateWorkers()
static masterWorlds_t & GetMasterWorlds()
static G4ThreadId GetMasterTheadId()
virtual void RequestWorkersProcessCommandsStack()
static G4RunManagerKernel * GetMasterRunManagerKernel()
void MergeScores(const G4ScoringManager *localScoringManager)
virtual void NewActionRequest(WorkerActionRequest newRequest)
void SetPinAffinity(G4int n=1)
std::vector< G4String > GetCommandStack()
virtual void ThisWorkerEndEventLoop()
Definition: G4Run.hh:49
#define G4MTRUN_DLL
Definition: rundefs.hh:57