Geant4  10.02.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  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 RunTermination();
71 
72  //The following method should be invoked by G4WorkerRunManager for each event.
73  //False is returned if no more event to be processed.
74  // Note: G4Event object must be instantiated by a worker thread. In case no more
75  // event remains to be processed, that worker thread must delete that G4Event
76  // object. If a worker runs with its own random number sequence, the boolean flag
77  // reseedRequired should be set to false. This is *NOT* allowed for the first event.
78  virtual G4bool SetUpAnEvent(G4Event*, long& s1, long& s2, long& s3, G4bool reseedRequired=true);
79  // Same as above method, but the seeds are set only once over "eventModulo" events.
80  // The return value shows the number of events the caller Worker has to process
81  // (between 1 and eventModulo depending on number of events yet to be processed).
82  // G4Event object has the event ID of the first event of this bunch.
83  // If zero is returned no more event needs to be processed, and worker thread
84  // must delete that G4Event.
85  virtual G4int SetUpNEvents(G4Event*, G4SeedsQueue* seedsQueue, G4bool reseedRequired=true);
86 
87  //Method called by Initialize() method
88 protected:
89  //Initialize the seeds list, if derived class does not implement this method
90  //A default generation will be used (nevents*2 random seeds)
91  //Return true if initialization is done.
92  virtual G4bool InitializeSeeds( G4int /*nevts*/) { return false; };
93  //Adds one seed to the list of seeds
94  virtual void PrepareCommandsStack();
95  virtual void StoreRNGStatus(const G4String& filenamePrefix );
96  virtual void CreateAndStartWorkers();
97  //Creates worker threads and signal to start
98 public:
99  std::vector<G4String> GetCommandStack();
100  //This method is invoked just before spawning the threads to
101  //collect from UI managere the list of commands that threads
102  //will execute.
103 private:
104  // Number of worker threads. To be set by SetNumberOfThreads() method.
106  // Force to use this number regardless of SetNumberOfThreads() method.
108  // Pin Affinity parameter
110 
111  //List of workers (i.e. thread)
112  typedef std::list<G4Thread*> G4ThreadsList;
113  G4ThreadsList threads;
114  //List of workers run managers
115  //List of all workers run managers
116  std::vector<G4String> uiCmdsForWorkers;
117  //List of UI commands for workers.
118  CLHEP::HepRandomEngine* masterRNGEngine;
119  //Pointer to the mastet thread random engine
120 protected:
121  virtual void WaitForReadyWorkers();
122  //Master thread barrier:
123  //Call this function to block master thread and
124  //wait workers to be ready to process work.
125  //This function will return only when all
126  //workers are ready to perform event loop.
127  virtual void WaitForEndEventLoopWorkers();
128  //Master thread barrier:
129  //Call this function to block master thread and
130  //wait workers have finished current event loop.
131  //This function will return only when all
132  //workers have finished processing events for this run.
133 protected:
135  virtual void TerminateWorkers();
136  //Empty the workersList
137 
138 public:
139  virtual void ThisWorkerReady();
140  //Worker threads barrier:
141  //This method should be called by each
142  //worker when ready to start thread event-loop
143  //This method will return only when all workers
144  //are ready.
145  //static void ThisWorkerFinishWork();
146  //Worker threads barrier:
147  //This static method should be called by each
148  //worker when finish to process events
149  virtual void ThisWorkerEndEventLoop();
150  //Worker threads barrier:
151  //This method should be called by each
152  //worker when worker event loop is terminated.
153  typedef std::map<G4int,G4VPhysicalVolume*> masterWorlds_t;
155  static masterWorlds_t& GetMasterWorlds() { return masterWorlds; }
156  static void addWorld( G4int counter, G4VPhysicalVolume* w) { masterWorlds.insert( std::make_pair(counter,w) ); }
157  const CLHEP::HepRandomEngine* getMasterRandomEngine() const { return masterRNGEngine; }
158 private:
159  //Handling of master thread scoring worlds, access to it is needed by workers
161  static masterWorlds_t masterWorlds;
162  //Singleton implementing master thread behavior
165 public: // with description
167  // Returns the singleton instance of the run manager common to all threads implementing
168  // the master behavior
171  // Returns the singleton instance of the run manager kernel common to all threads
172 
173  virtual void SetUserInitialization(G4VUserPhysicsList* userPL);
175  virtual void SetUserInitialization(G4UserWorkerInitialization* userInit);
177  virtual void SetUserInitialization(G4VUserActionInitialization* userInit);
178  virtual void SetUserAction(G4UserRunAction* userAction);
179  virtual void SetUserAction(G4VUserPrimaryGeneratorAction* userAction);
180  virtual void SetUserAction(G4UserEventAction* userAction);
181  virtual void SetUserAction(G4UserStackingAction* userAction);
182  virtual void SetUserAction(G4UserTrackingAction* userAction);
183  virtual void SetUserAction(G4UserSteppingAction* userAction);
184 
185 public:
186  // To be invoked solely from G4WorkerRunManager to merge the results
187  void MergeScores(const G4ScoringManager* localScoringManager);
188  void MergeRun(const G4Run* localRun);
189 
190 public:
191  //Handling of more than one run per thread
194  NEXTITERATION , // There is another set of UI commands to be executed
195  ENDWORKER // Terminate thread, work finished
196  };
197 
199  //Worker thread barrier
200  //This method should be used by workers' run manager to wait,
201  //after an event loop for the next action to be performed
202  // (for example execute a new run)
203  //This returns the action to be performed
204 protected:
206  virtual void NewActionRequest( WorkerActionRequest newRequest );
207 
208 protected:
215  double* randDbl;
216 
217  void RefillSeeds();
218 
219 public:
220  inline void SetEventModulo(G4int i=1) { eventModuloDef = i; }
221  inline G4int GetEventModulo() const { return eventModuloDef; }
222 
223 public:
224  virtual void AbortRun(G4bool softAbort=false);
225  virtual void AbortEvent();
226 
227 protected:
229  // - If it is set to 0 (default), seeds that are centrally managed
230  // by G4MTRunManager are set for every event of every worker thread.
231  // This option guarantees event reproducability regardless of number
232  // of threads.
233  // - If it is set to 1, seeds are set only once for the first
234  // event of each run of each worker thread. Event reproducability is
235  // guaranteed only if the same number of worker threads are used.
236  // On the other hand, this option offers better computing performance
237  // in particular for applications with relatively small primary
238  // particle energy and large number of events.
239  // - If it is set to 2, seeds are set only for the first event of
240  // group of N events. This option is reserved for the future use when
241  // Geant4 allows number of threads to be dynatically changed during an
242  // event loop.
243 public:
245  static void SetSeedOncePerCommunication(G4int val) { seedOncePerCommunication = val; }
246 };
247 
248 #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)
const G4double w[NPOINTSGL]
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
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