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 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

  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 // G4SubEvtRunManager
 27 //
 28 // Class description:
 29 //
 30 // This is a class for run control in sub-event parallel mode.
 31 // It extends G4TaskRunManager re-implementing multi-threaded behavior in
 32 // key methods (see documentation for G4MTRunManager).
 33 //
 34 // N.B. G4TaskRunManagerKernel is still used by this G4SubEvtRunManager
 35 // without any modification. -- future work for phase-II developments
 36 //
 37 // Original author: M. Asai (JLAB) - 2024
 38 // --------------------------------------------------------------------
 39 #ifndef G4SubEvtRunManager_hh
 40 #define G4SubEvtRunManager_hh 1
 41 
 42 #include "G4TaskRunManager.hh"
 43 
 44 #include <atomic>
 45 #include <map>
 46 
 47 class G4WorkerSubEvtRunManager;
 48 
 49 class G4SubEvtRunManager : public G4TaskRunManager
 50 {
 51     friend class G4RunManagerFactory;
 52 
 53   public:
 54     static G4SubEvtRunManager* GetMasterRunManager()
 55     {
 56       auto* _rm = G4MTRunManager::GetMasterRunManager();
 57       return dynamic_cast<G4SubEvtRunManager*>(_rm);
 58     }
 59 
 60     G4SubEvtRunManager(G4bool useTBB = G4GetEnv<G4bool>("G4USE_TBB", false));
 61     G4SubEvtRunManager(G4VUserTaskQueue* taskQueue,
 62                      G4bool useTBB = G4GetEnv<G4bool>("G4USE_TBB", false), G4int evtGrainsize = 0);
 63     ~G4SubEvtRunManager() override;
 64 
 65     // Inherited methods to re-implement for MT case
 66     void Initialize() override;
 67     void Initialize(uint64_t nthreads) override { PTL::TaskRunManager::Initialize(nthreads); }
 68     void RunInitialization() override;
 69     void InitializeEventLoop(G4int n_event, const char* macroFile = nullptr,
 70                              G4int n_select = -1) override;
 71     void TerminateOneEvent() override;
 72     void ProcessOneEvent(G4int i_event) override;
 73     void ConstructScoringWorlds() override;
 74     void RunTermination() override;
 75     void CleanUpPreviousEvents() override;
 76 
 77     // The following two methods are not used in sub-event parallel mode.
 78     G4bool SetUpAnEvent(G4Event*, G4long&, G4long&, G4long&, G4bool = true) override
 79     { return false; }
 80     G4int SetUpNEvents(G4Event*, G4SeedsQueue*, G4bool = true) override
 81     { return -1; }
 82 
 83     void RegisterSubEventType(G4int ty, G4int maxEnt) override;
 84 
 85     void RegisterSubEvtWorker(G4WorkerSubEvtRunManager*,G4int);
 86 
 87     // The following method should be invoked by G4WorkerSubEvtRunManager for each
 88     // sub-event. This method returns a sub-event. This sub-event object must not
 89     // be deleted by the worker thread.
 90     // Null pointer is returned if
 91     //  - run is in progress but no sub-event is available yet (notReady=true), or
 92     //  - run is over and no more sub-event is available unless new run starts (notReady=false)
 93     // If a worker runs with its own random number sequence, the Boolean flag 'reseedRequired'
 94     // should be set to false. This is *NOT* allowed for the first event.
 95     // G4Event object should be instantiated by G4WorkerSubEvtRunManager based on the
 96     // input provided in G4SubEvent.
 97     const G4SubEvent* GetSubEvent(G4int ty, G4bool& notReady,
 98               G4long& s1, G4long& s2, G4long& s3, G4bool reseedRequired = true) override;
 99 
100     // Notify the master thread that processing of sub-event "se" is over.
101     // Outcome of processed sub-event must be kept in "evt".
102     // "se" is deleted by the master thread, while "evt" must be deleted by the worker thread
103     // after invoking this method.
104     void SubEventFinished(const G4SubEvent* se,const G4Event* evt) override;
105 
106     // Merge local scores to the master
107     void MergeScores(const G4ScoringManager* localScoringManager) override;
108 
109     // Nothing to do for merging run
110     void MergeRun(const G4Run*) override
111     {}
112 
113     // Returns number of currently active threads.
114     // This number may be different from the number of threads currently
115     // in running state, e.g. the number returned by:
116     // G4Threading::GetNumberOfActiveWorkerThreads() method.
117     std::size_t GetNumberActiveThreads() const override
118     { return threads.size(); }
119 
120     // Worker threads barrier: this method should be called by each
121     // worker when ready to start thread event-loop.
122     // This method will return only when all workers are ready.
123     //   void ThisWorkerReady() override;
124 
125     // Worker threads barrier: this method should be called by each
126     // worker when worker event loop is terminated.
127     //   void ThisWorkerEndEventLoop() override;
128 
129     void SetUserInitialization(G4VUserPhysicsList* userPL) override;
130     void SetUserInitialization(G4VUserDetectorConstruction* userDC) override;
131     void SetUserInitialization(G4UserWorkerInitialization* userInit) override;
132     void SetUserInitialization(G4UserWorkerThreadInitialization* userInit) override;
133     void SetUserInitialization(G4VUserActionInitialization* userInit) override;
134     void SetUserAction(G4UserRunAction* userAction) override;
135     void SetUserAction(G4VUserPrimaryGeneratorAction* userAction) override;
136     void SetUserAction(G4UserEventAction* userAction) override;
137     void SetUserAction(G4UserStackingAction* userAction) override;
138     void SetUserAction(G4UserTrackingAction* userAction) override;
139     void SetUserAction(G4UserSteppingAction* userAction) override;
140 
141     // Called to force workers to request and process the UI commands stack
142     // This will block untill all workers have processed UI commands
143     void RequestWorkersProcessCommandsStack() override;
144 
145     // Called by workers to signal to master it has completed processing of
146     // UI commands
147     void ThisWorkerProcessCommandsStackDone() override;
148 
149     // Worker thread barrier: this method should be used by workers' run
150     // manager to wait, after an event loop for the next action to be
151     // performed (for example execute a new run).
152     void WaitForReadyWorkers() override {}
153     void WaitForEndEventLoopWorkers() override;
154 
155     // This returns the action to be performed.
156     WorkerActionRequest ThisWorkerWaitForNextAction() override
157     {
158       return WorkerActionRequest::UNDEFINED;
159     }
160 
161     void AbortRun(G4bool softAbort = false) override;
162     void AbortEvent() override;
163 
164   protected:
165     void ComputeNumberOfTasks() override
166     {
167       // This method is not used for sub-event parallel mode
168       numberOfTasks = (G4int)threadPool->size();
169       numberOfEventsPerTask = -1;
170       eventModulo = -1;
171     }
172 
173     // Initialize the seeds list, if derived class does not implement this
174     // method, a default generation will be used (nevents*2 random seeds).
175     // Return true if initialization is done.
176     // Adds one seed to the list of seeds.
177     G4bool InitializeSeeds(G4int /*nevts*/) override { return false; };
178 
179     // Adds one seed to the list of seeds
180     void RefillSeeds() override;
181 
182     //void PrepareCommandsStack() override;
183 
184     // Creates worker threads and signal to start
185     void CreateAndStartWorkers() override;
186 
187     void TerminateWorkers() override;
188     void NewActionRequest(WorkerActionRequest) override {}
189     void AddEventTask(G4int) override;
190 
191     void SetUpSeedsForSubEvent(G4long& s1, G4long& s2, G4long& s3);
192 
193     void MergeTrajectories(const G4SubEvent* se,const G4Event* evt) override;
194     void UpdateScoringForSubEvent(const G4SubEvent* se,const G4Event* evt) override;
195 
196     void CleanUpUnnecessaryEvents(G4int keepNEvents) override;
197     void StackPreviousEvent(G4Event* anEvent) override;
198 
199   protected:
200     std::atomic<G4bool> runInProgress = false;
201 
202   private:
203     G4bool trajectoriesToBeMerged = false;
204     std::map<G4int,G4int> fSubEvtTypeMap;
205     std::map<G4WorkerSubEvtRunManager*,G4int> fWorkerMap;
206 
207     G4bool CheckSubEvtTypes();
208 
209   public:
210     void TrajectoriesToBeMerged(G4bool val=true) override
211     { trajectoriesToBeMerged = val; }
212 };
213 
214 #endif  // G4SubEvtRunManager_hh
215