ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4MTRunManager.hh
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4MTRunManager.hh
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 "G4MTBarrier.hh"
41 #include <list>
42 #include <map>
43 
45 class G4ScoringManager;
48 
49 //TODO: Split random number storage from this class
50 
51 class G4MTRunManager : public G4RunManager {
52 public:
54  virtual ~G4MTRunManager();
55  //New method
56  void SetNumberOfThreads( G4int n );
57  G4int GetNumberOfThreads() const { return nworkers; }
58  void SetPinAffinity(G4int n=1);
59  G4int GetPinAffinity() const { return pinAffinity; }
60 public:
61 
62  //Inherited methods to re-implement for MT case
63  virtual void Initialize();
64  virtual void InitializeEventLoop(G4int n_event, const char* macroFile=0, G4int n_select=-1);
65 
66  //The following do not do anything for this runmanager
67  virtual void TerminateOneEvent();
68  virtual void ProcessOneEvent(G4int i_event);
70  virtual void ConstructScoringWorlds();
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 rndmSaveThisRun();
98  virtual void rndmSaveThisEvent();
99  virtual void CreateAndStartWorkers();
100  //Creates worker threads and signal to start
101 public:
102  std::vector<G4String> GetCommandStack();
103  //This method is invoked just before spawning the threads to
104  //collect from UI managere the list of commands that threads
105  //will execute.
106  size_t GetNumberActiveThreads() const { return threads.size(); }
107  //Returns number of currently active threads.
108  //This number may be different from the number of threads currently
109  //in running state (e.g. the number returned by:
110  //G4Threading::GetNumberOfActiveWorkerThreads() method).
111 private:
112  // Number of worker threads. To be set by SetNumberOfThreads() method.
114  // Force to use this number regardless of SetNumberOfThreads() method.
116  // Pin Affinity parameter
118 
119  //List of workers (i.e. thread)
120  typedef std::list<G4Thread*> G4ThreadsList;
122  //List of workers run managers
123  //List of all workers run managers
124  std::vector<G4String> uiCmdsForWorkers;
125  //List of UI commands for workers.
127  //Pointer to the mastet thread random engine
128 protected:
129  virtual void WaitForReadyWorkers();
130  //Master thread barrier:
131  //Call this function to block master thread and
132  //wait workers to be ready to process work.
133  //This function will return only when all
134  //workers are ready to perform event loop.
135  virtual void WaitForEndEventLoopWorkers();
136  //Master thread barrier:
137  //Call this function to block master thread and
138  //wait workers have finished current event loop.
139  //This function will return only when all
140  //workers have finished processing events for this run.
141 protected:
143  virtual void TerminateWorkers();
144  //Empty the workersList
145 
146 public:
147  virtual void ThisWorkerReady();
148  //Worker threads barrier:
149  //This method should be called by each
150  //worker when ready to start thread event-loop
151  //This method will return only when all workers
152  //are ready.
153  //static void ThisWorkerFinishWork();
154  //Worker threads barrier:
155  //This static method should be called by each
156  //worker when finish to process events
157  virtual void ThisWorkerEndEventLoop();
158  //Worker threads barrier:
159  //This method should be called by each
160  //worker when worker event loop is terminated.
161  typedef std::map<G4int,G4VPhysicalVolume*> masterWorlds_t;
164  static void addWorld( G4int counter, G4VPhysicalVolume* w) { masterWorlds.insert( std::make_pair(counter,w) ); }
166 private:
167  //Handling of master thread scoring worlds, access to it is needed by workers
170  //Singleton implementing master thread behavior
173 public: // with description
175  // Returns the singleton instance of the run manager common to all threads implementing
176  // the master behavior
179  // Returns the singleton instance of the run manager kernel common to all threads
180 
181  virtual void SetUserInitialization(G4VUserPhysicsList* userPL);
183  virtual void SetUserInitialization(G4UserWorkerInitialization* userInit);
185  virtual void SetUserInitialization(G4VUserActionInitialization* userInit);
186  virtual void SetUserAction(G4UserRunAction* userAction);
187  virtual void SetUserAction(G4VUserPrimaryGeneratorAction* userAction);
188  virtual void SetUserAction(G4UserEventAction* userAction);
189  virtual void SetUserAction(G4UserStackingAction* userAction);
190  virtual void SetUserAction(G4UserTrackingAction* userAction);
191  virtual void SetUserAction(G4UserSteppingAction* userAction);
192 
193 public:
194  // To be invoked solely from G4WorkerRunManager to merge the results
195  void MergeScores(const G4ScoringManager* localScoringManager);
196  void MergeRun(const G4Run* localRun);
197 
198 public:
199  //Handling of more than one run per thread
200  enum class WorkerActionRequest {
201  UNDEFINED ,
202  NEXTITERATION , // There is another set of UI commands to be executed
203  PROCESSUI, // Process UI commands w/o a /run/beamOn
204  ENDWORKER // Terminate thread, work finished
205  };
206  virtual void RequestWorkersProcessCommandsStack();
207  //Called to force workers to request and process the UI commands stack
208  //This will block untill all workers have processed UI commands
209  virtual void ThisWorkerProcessCommandsStackDone();
210  //Called by workers to signal to master it has completed processing of
211  //UI commands
213  //Worker thread barrier
214  //This method should be used by workers' run manager to wait,
215  //after an event loop for the next action to be performed
216  // (for example execute a new run)
217  //This returns the action to be performed
218 protected:
220  virtual void NewActionRequest( WorkerActionRequest newRequest );
221 
222 protected:
229  double* randDbl;
230 
231  void RefillSeeds();
232 
233 public:
234  inline void SetEventModulo(G4int i=1) { eventModuloDef = i; }
235  inline G4int GetEventModulo() const { return eventModuloDef; }
236 
237 public:
238  virtual void AbortRun(G4bool softAbort=false);
239  virtual void AbortEvent();
240 
241 protected:
243  // - If it is set to 0 (default), seeds that are centrally managed
244  // by G4MTRunManager are set for every event of every worker thread.
245  // This option guarantees event reproducability regardless of number
246  // of threads.
247  // - If it is set to 1, seeds are set only once for the first
248  // event of each run of each worker thread. Event reproducability is
249  // guaranteed only if the same number of worker threads are used.
250  // On the other hand, this option offers better computing performance
251  // in particular for applications with relatively small primary
252  // particle energy and large number of events.
253  // - If it is set to 2, seeds are set only for the first event of
254  // group of N events. This option is reserved for the future use when
255  // Geant4 allows number of threads to be dynatically changed during an
256  // event loop.
257 public:
260 protected:
261  //Barriers: synch points between master and workers
266 };
267 
268 #endif //G4MTRunManager_h