Geant4  10.01
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 "G4RunManager.hh"
38 #include "G4Threading.hh"
39 #include "G4RNGHelper.hh"
40 #include <list>
41 #include <map>
42 
44 class G4ScoringManager;
47 
48 //TODO: Split random number storage from this class
49 
50 class G4MTRunManager : public G4RunManager {
51 public:
53  virtual ~G4MTRunManager();
54  //New method
55  void SetNumberOfThreads( G4int n );
56  G4int GetNumberOfThreads() const { return nworkers; }
57  void SetPinAffinity(G4int n=1);
58  G4int GetPinAffinity() const { return pinAffinity; }
59 public:
60 
61  //Inherited methods to re-implement for MT case
62  virtual void Initialize();
63  virtual void InitializeEventLoop(G4int n_event, const char* macroFile=0, G4int n_select=-1);
64 
65  //The following do not do anything for this runmanager
66  virtual void TerminateOneEvent();
67  virtual void ProcessOneEvent(G4int i_event);
69  virtual void ConstructScoringWorlds();
70  virtual void InitializePhysics();
71  virtual void RunTermination();
72 
73  //The following method should be invoked by G4WorkerRunManager for each event.
74  //False is returned if no more event to be processed.
75  // Note: G4Event object must be instantiated by a worker thread. In case no more
76  // event remains to be processed, that worker thread must delete that G4Event
77  // object. If a worker runs with its own random number sequence, the boolean flag
78  // reseedRequired should be set to false. This is *NOT* allowed for the first event.
79  virtual G4bool SetUpAnEvent(G4Event*, long& s1, long& s2, long& s3, G4bool reseedRequired=true);
80  // Same as above method, but the seeds are set only once over "eventModulo" events.
81  // The return value shows the number of events the caller Worker has to process
82  // (between 1 and eventModulo depending on number of events yet to be processed).
83  // G4Event object has the event ID of the first event of this bunch.
84  // If zero is returned no more event needs to be processed, and worker thread
85  // must delete that G4Event.
86  virtual G4int SetUpNEvents(G4Event*, G4SeedsQueue* seedsQueue, G4bool reseedRequired=true);
87 
88  //Method called by Initialize() method
89 protected:
90  //Initialize the seeds list, if derived class does not implement this method
91  //A default generation will be used (nevents*2 random seeds)
92  //Return true if initialization is done.
93  virtual G4bool InitializeSeeds( G4int /*nevts*/) { return false; };
94  //Adds one seed to the list of seeds
95  virtual void PrepareCommandsStack();
96  virtual void StoreRNGStatus(const G4String& filenamePrefix );
97  virtual void CreateAndStartWorkers();
98  //Creates worker threads and signal to start
99 public:
100  std::vector<G4String> GetCommandStack();
101  //This method is invoked just before spawning the threads to
102  //collect from UI managere the list of commands that threads
103  //will execute.
104 private:
105  // Number of worker threads. To be set by SetNumberOfThreads() method.
107  // Force to use this number regardless of SetNumberOfThreads() method.
109  // Pin Affinity parameter
111 
112  //List of workers (i.e. thread)
113  typedef std::list<G4Thread*> G4ThreadsList;
114  G4ThreadsList threads;
115  //List of workers run managers
116  //List of all workers run managers
117  std::vector<G4String> uiCmdsForWorkers;
118  //List of UI commands for workers.
119  CLHEP::HepRandomEngine* masterRNGEngine;
120  //Pointer to the mastet thread random engine
121 protected:
122  virtual void WaitForReadyWorkers();
123  //Master thread barrier:
124  //Call this function to block master thread and
125  //wait workers to be ready to process work.
126  //This function will return only when all
127  //workers are ready to perform event loop.
128  virtual void WaitForEndEventLoopWorkers();
129  //Master thread barrier:
130  //Call this function to block master thread and
131  //wait workers have finished current event loop.
132  //This function will return only when all
133  //workers have finished processing events for this run.
134 protected:
136  virtual void TerminateWorkers();
137  //Empty the workersList
138 
139 public:
140  virtual void ThisWorkerReady();
141  //Worker threads barrier:
142  //This method should be called by each
143  //worker when ready to start thread event-loop
144  //This method will return only when all workers
145  //are ready.
146  //static void ThisWorkerFinishWork();
147  //Worker threads barrier:
148  //This static method should be called by each
149  //worker when finish to process events
150  virtual void ThisWorkerEndEventLoop();
151  //Worker threads barrier:
152  //This method should be called by each
153  //worker when worker event loop is terminated.
154  typedef std::map<G4int,G4VPhysicalVolume*> masterWorlds_t;
156  static masterWorlds_t& GetMasterWorlds() { return masterWorlds; }
157  static void addWorld( G4int counter, G4VPhysicalVolume* w) { masterWorlds.insert( std::make_pair(counter,w) ); }
158  const CLHEP::HepRandomEngine* getMasterRandomEngine() const { return masterRNGEngine; }
159 private:
160  //Handling of master thread scoring worlds, access to it is needed by workers
162  static masterWorlds_t masterWorlds;
163  //Singleton implementing master thread behavior
166 public: // with description
168  // Returns the singleton instance of the run manager common to all threads implementing
169  // the master behavior
172  // Returns the singleton instance of the run manager kernel common to all threads
173 
174  virtual void SetUserInitialization(G4VUserPhysicsList* userPL);
176  virtual void SetUserInitialization(G4UserWorkerInitialization* userInit);
178  virtual void SetUserInitialization(G4VUserActionInitialization* userInit);
179  virtual void SetUserAction(G4UserRunAction* userAction);
180  virtual void SetUserAction(G4VUserPrimaryGeneratorAction* userAction);
181  virtual void SetUserAction(G4UserEventAction* userAction);
182  virtual void SetUserAction(G4UserStackingAction* userAction);
183  virtual void SetUserAction(G4UserTrackingAction* userAction);
184  virtual void SetUserAction(G4UserSteppingAction* userAction);
185 
186 public:
187  // To be invoked solely from G4WorkerRunManager to merge the results
188  void MergeScores(const G4ScoringManager* localScoringManager);
189  void MergeRun(const G4Run* localRun);
190 
191 public:
192  //Handling of more than one run per thread
195  NEXTITERATION , // There is another set of UI commands to be executed
196  ENDWORKER // Terminate thread, work finished
197  };
198 
200  //Worker thread barrier
201  //This method should be used by workers' run manager to wait,
202  //after an event loop for the next action to be performed
203  // (for example execute a new run)
204  //This returns the action to be performed
205 protected:
207  virtual void NewActionRequest( WorkerActionRequest newRequest );
208 
209 protected:
216  double* randDbl;
217 
218  void RefillSeeds();
219 
220 public:
221  inline void SetEventModulo(G4int i=1) { eventModuloDef = i; }
222  inline G4int GetEventModulo() const { return eventModuloDef; }
223 
224 public:
225  virtual void AbortRun(G4bool softAbort=false);
226  virtual void AbortEvent();
227 
228 protected:
230  // - If it is set to 0 (default), seeds that are centrally managed
231  // by G4MTRunManager are set for every event of every worker thread.
232  // This option guarantees event reproducability regardless of number
233  // of threads.
234  // - If it is set to 1, seeds are set only once for the first
235  // event of each run of each worker thread. Event reproducability is
236  // guaranteed only if the same number of worker threads are used.
237  // On the other hand, this option offers better computing performance
238  // in particular for applications with relatively small primary
239  // particle energy and large number of events.
240  // - If it is set to 2, seeds are set only for the first event of
241  // group of N events. This option is reserved for the future use when
242  // Geant4 allows number of threads to be dynatically changed during an
243  // event loop.
244 public:
246  static void SetSeedOncePerCommunication(G4int val) { seedOncePerCommunication = val; }
247 };
248 
249 #endif //G4MTRunManager_h
virtual void InitializeEventLoop(G4int n_event, const char *macroFile=0, G4int n_select=-1)
virtual G4int SetUpNEvents(G4Event *, G4SeedsQueue *seedsQueue, G4bool reseedRequired=true)
std::vector< G4String > GetCommandStack()
virtual void PrepareCommandsStack()
std::map< G4int, G4VPhysicalVolume * > masterWorlds_t
static G4int seedOncePerCommunication
std::vector< G4String > uiCmdsForWorkers
static void addWorld(G4int counter, G4VPhysicalVolume *w)
WorkerActionRequest nextActionRequest
virtual void ThisWorkerEndEventLoop()
void SetNumberOfThreads(G4int n)
virtual G4bool InitializeSeeds(G4int)
void SetEventModulo(G4int i=1)
static masterWorlds_t & GetMasterWorlds()
virtual void ConstructScoringWorlds()
virtual void RunTermination()
int G4int
Definition: G4Types.hh:78
virtual void SetUserAction(G4UserRunAction *userAction)
std::queue< long > G4SeedsQueue
Definition: G4RNGHelper.hh:134
virtual void WaitForEndEventLoopWorkers()
G4MTRunManagerKernel * MTkernel
virtual void TerminateOneEvent()
virtual void TerminateWorkers()
static G4RunManagerKernel * GetMasterRunManagerKernel()
G4ThreadsList threads
virtual void InitializePhysics()
void MergeRun(const G4Run *localRun)
static masterWorlds_t masterWorlds
static G4ScoringManager * masterScM
virtual void ProcessOneEvent(G4int i_event)
bool G4bool
Definition: G4Types.hh:79
virtual void WaitForReadyWorkers()
static G4MTRunManager * GetMasterRunManager()
void SetPinAffinity(G4int n=1)
virtual WorkerActionRequest ThisWorkerWaitForNextAction()
Definition: G4Run.hh:46
virtual void SetUserInitialization(G4VUserPhysicsList *userPL)
G4int GetPinAffinity() const
G4int GetNumberOfThreads() const
virtual void AbortRun(G4bool softAbort=false)
virtual void NewActionRequest(WorkerActionRequest newRequest)
const G4int n
std::list< G4Thread * > G4ThreadsList
virtual void Initialize()
static G4ScoringManager * GetMasterScoringManager()
virtual void ThisWorkerReady()
virtual void AbortEvent()
virtual G4bool SetUpAnEvent(G4Event *, long &s1, long &s2, long &s3, G4bool reseedRequired=true)
const CLHEP::HepRandomEngine * getMasterRandomEngine() const
G4int GetEventModulo() const
virtual ~G4MTRunManager()
static G4int SeedOncePerCommunication()
G4int numberOfEventToBeProcessed
static G4MTRunManagerKernel * GetMTMasterRunManagerKernel()
static void SetSeedOncePerCommunication(G4int val)
void MergeScores(const G4ScoringManager *localScoringManager)
CLHEP::HepRandomEngine * masterRNGEngine
virtual void StoreRNGStatus(const G4String &filenamePrefix)
virtual void CreateAndStartWorkers()
static G4MTRunManager * fMasterRM