Geant4  10.00.p02
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 public:
58 
59  //Inherited methods to re-implement for MT case
60  virtual void Initialize();
61  virtual void InitializeEventLoop(G4int n_event, const char* macroFile=0, G4int n_select=-1);
62 
63  //The following do not do anything for this runmanager
64  virtual void TerminateOneEvent();
65  virtual void ProcessOneEvent(G4int i_event);
66  virtual void TerminateEventLoop();
67  virtual void ConstructScoringWorlds();
68  virtual void InitializePhysics();
69  virtual void RunTermination();
70 
71  //The following method should be invoked by G4WorkerRunManager for each event.
72  //False is returned if no more event to be processed.
73  // Note: G4Event object must be instantiated by a worker thread. In case no more
74  // event remains to be processed, that worker thread must delete that G4Event
75  // object.
76  virtual G4bool SetUpAnEvent(G4Event*, long& s1, long& s2, long& s3);
77  // Same as above method, but the seeds are set only once over "eventModulo" events.
78  // The return value shows the number of events the caller Worker has to process
79  // (between 1 and eventModulo depending on number of events yet to be processed).
80  // G4Event object has the event ID of the first event of this bunch.
81  // If zero is returned no more event needs to be processed, and worker thread
82  // must delete that G4Event.
83  virtual G4int SetUpNEvents(G4Event*, G4SeedsQueue* seedsQueue);
84 
85  //Method called by Initialize() method
86 protected:
87  //Initialize the seeds list, if derived class does not implement this method
88  //A default generation will be used (nevents*2 random seeds)
89  //Return true if initialization is done.
90  virtual G4bool InitializeSeeds( G4int /*nevts*/) { return false; };
91  //Adds one seed to the list of seeds
92  virtual void PrepareCommandsStack();
93  virtual void StoreRNGStatus(const G4String& filenamePrefix );
94  virtual void CreateAndStartWorkers();
95  //Creates worker threads and signal to start
96 public:
97  std::vector<G4String> GetCommandStack();
98  //This method is invoked just before spawning the threads to
99  //collect from UI managere the list of commands that threads
100  //will execute.
101 private:
102  // Number of worker threads. To be set by SetNumberOfThreads() method.
104  // Force to use this number regardless of SetNumberOfThreads() method.
106 
107  //List of workers (i.e. thread)
108  typedef std::list<G4Thread*> G4ThreadsList;
109  G4ThreadsList threads;
110  //List of workers run managers
111  //List of all workers run managers
112  std::vector<G4String> uiCmdsForWorkers;
113  //List of UI commands for workers.
114  CLHEP::HepRandomEngine* masterRNGEngine;
115  //Pointer to the mastet thread random engine
116 protected:
117  virtual void WaitForReadyWorkers();
118  //Master thread barrier:
119  //Call this function to block master thread and
120  //wait workers to be ready to process work.
121  //This function will return only when all
122  //workers are ready to perform event loop.
123  virtual void WaitForEndEventLoopWorkers();
124  //Master thread barrier:
125  //Call this function to block master thread and
126  //wait workers have finished current event loop.
127  //This function will return only when all
128  //workers have finished processing events for this run.
129 protected:
131  virtual void TerminateWorkers();
132  //Empty the workersList
133 
134 public:
135  virtual void ThisWorkerReady();
136  //Worker threads barrier:
137  //This method should be called by each
138  //worker when ready to start thread event-loop
139  //This method will return only when all workers
140  //are ready.
141  //static void ThisWorkerFinishWork();
142  //Worker threads barrier:
143  //This static method should be called by each
144  //worker when finish to process events
145  virtual void ThisWorkerEndEventLoop();
146  //Worker threads barrier:
147  //This method should be called by each
148  //worker when worker event loop is terminated.
149  typedef std::map<G4int,G4VPhysicalVolume*> masterWorlds_t;
151  static masterWorlds_t& GetMasterWorlds() { return masterWorlds; }
152  static void addWorld( G4int counter, G4VPhysicalVolume* w) { masterWorlds.insert( std::make_pair(counter,w) ); }
153  const CLHEP::HepRandomEngine* getMasterRandomEngine() const { return masterRNGEngine; }
154 private:
155  //Handling of master thread scoring worlds, access to it is needed by workers
157  static masterWorlds_t masterWorlds;
158  //Singleton implementing master thread behavior
161 public: // with description
163  // Returns the singleton instance of the run manager common to all threads implementing
164  // the master behavior
167  // Returns the singleton instance of the run manager kernel common to all threads
168 
169  virtual void SetUserInitialization(G4VUserPhysicsList* userPL);
171  virtual void SetUserInitialization(G4UserWorkerInitialization* userInit);
173  virtual void SetUserInitialization(G4VUserActionInitialization* userInit);
174  virtual void SetUserAction(G4UserRunAction* userAction);
175  virtual void SetUserAction(G4VUserPrimaryGeneratorAction* userAction);
176  virtual void SetUserAction(G4UserEventAction* userAction);
177  virtual void SetUserAction(G4UserStackingAction* userAction);
178  virtual void SetUserAction(G4UserTrackingAction* userAction);
179  virtual void SetUserAction(G4UserSteppingAction* userAction);
180 
181 public:
182  // To be invoked solely from G4WorkerRunManager to merge the results
183  void MergeScores(const G4ScoringManager* localScoringManager);
184  void MergeRun(const G4Run* localRun);
185 
186 public:
187  //Handling of more than one run per thread
190  NEXTITERATION , // There is another set of UI commands to be executed
191  ENDWORKER // Terminate thread, work finished
192  };
193 
195  //Worker thread barrier
196  //This method should be used by workers' run manager to wait,
197  //after an event loop for the next action to be performed
198  // (for example execute a new run)
199  //This returns the action to be performed
200 protected:
202  virtual void NewActionRequest( WorkerActionRequest newRequest );
203 
204 protected:
211  double* randDbl;
212 
213  void RefillSeeds();
214 
215 public:
216  inline void SetEventModulo(G4int i=1) { eventModuloDef = i; }
217  inline G4int GetEventModulo() const { return eventModuloDef; }
218 
219 public:
220  virtual void AbortRun(G4bool softAbort=false);
221  virtual void AbortEvent();
222 };
223 
224 #endif //G4MTRunManager_h
virtual void InitializeEventLoop(G4int n_event, const char *macroFile=0, G4int n_select=-1)
std::vector< G4String > GetCommandStack()
virtual void PrepareCommandsStack()
std::map< G4int, G4VPhysicalVolume * > masterWorlds_t
std::vector< G4String > uiCmdsForWorkers
virtual G4bool SetUpAnEvent(G4Event *, long &s1, long &s2, long &s3)
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:133
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()
virtual G4int SetUpNEvents(G4Event *, G4SeedsQueue *seedsQueue)
virtual WorkerActionRequest ThisWorkerWaitForNextAction()
Definition: G4Run.hh:46
virtual void SetUserInitialization(G4VUserPhysicsList *userPL)
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()
const CLHEP::HepRandomEngine * getMasterRandomEngine() const
G4int GetEventModulo() const
virtual ~G4MTRunManager()
G4int numberOfEventToBeProcessed
static G4MTRunManagerKernel * GetMTMasterRunManagerKernel()
void MergeScores(const G4ScoringManager *localScoringManager)
CLHEP::HepRandomEngine * masterRNGEngine
virtual void TerminateEventLoop()
virtual void StoreRNGStatus(const G4String &filenamePrefix)
virtual void CreateAndStartWorkers()
static G4MTRunManager * fMasterRM