Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-02 08:29:21

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 // G4SubEvtRunManager
0027 //
0028 // Class description:
0029 //
0030 // This is a class for run control in sub-event parallel mode.
0031 // It extends G4TaskRunManager re-implementing multi-threaded behavior in
0032 // key methods (see documentation for G4MTRunManager).
0033 //
0034 // N.B. G4TaskRunManagerKernel is still used by this G4SubEvtRunManager
0035 // without any modification. -- future work for phase-II developments
0036 //
0037 // Original author: M. Asai (JLAB) - 2024
0038 // --------------------------------------------------------------------
0039 #ifndef G4SubEvtRunManager_hh
0040 #define G4SubEvtRunManager_hh 1
0041 
0042 #include "G4TaskRunManager.hh"
0043 
0044 #include <atomic>
0045 #include <map>
0046 
0047 class G4WorkerSubEvtRunManager;
0048 
0049 class G4SubEvtRunManager : public G4TaskRunManager
0050 {
0051     friend class G4RunManagerFactory;
0052 
0053   public:
0054     static G4SubEvtRunManager* GetMasterRunManager()
0055     {
0056       auto* _rm = G4MTRunManager::GetMasterRunManager();
0057       return dynamic_cast<G4SubEvtRunManager*>(_rm);
0058     }
0059 
0060     G4SubEvtRunManager(G4bool useTBB = G4GetEnv<G4bool>("G4USE_TBB", false));
0061     G4SubEvtRunManager(G4VUserTaskQueue* taskQueue,
0062                      G4bool useTBB = G4GetEnv<G4bool>("G4USE_TBB", false), G4int evtGrainsize = 0);
0063     ~G4SubEvtRunManager() override;
0064 
0065     // Inherited methods to re-implement for MT case
0066     void Initialize() override;
0067     void Initialize(uint64_t nthreads) override { PTL::TaskRunManager::Initialize(nthreads); }
0068     void RunInitialization() override;
0069     void InitializeEventLoop(G4int n_event, const char* macroFile = nullptr,
0070                              G4int n_select = -1) override;
0071     void TerminateOneEvent() override;
0072     void ProcessOneEvent(G4int i_event) override;
0073     void ConstructScoringWorlds() override;
0074     void RunTermination() override;
0075     void CleanUpPreviousEvents() override;
0076 
0077     // The following two methods are not used in sub-event parallel mode.
0078     G4bool SetUpAnEvent(G4Event*, G4long&, G4long&, G4long&, G4bool = true) override
0079     { return false; }
0080     G4int SetUpNEvents(G4Event*, G4SeedsQueue*, G4bool = true) override
0081     { return -1; }
0082 
0083     void RegisterSubEventType(G4int ty, G4int maxEnt) override;
0084 
0085     void RegisterSubEvtWorker(G4WorkerSubEvtRunManager*,G4int);
0086 
0087     // The following method should be invoked by G4WorkerSubEvtRunManager for each
0088     // sub-event. This method returns a sub-event. This sub-event object must not
0089     // be deleted by the worker thread.
0090     // Null pointer is returned if
0091     //  - run is in progress but no sub-event is available yet (notReady=true), or
0092     //  - run is over and no more sub-event is available unless new run starts (notReady=false)
0093     // If a worker runs with its own random number sequence, the Boolean flag 'reseedRequired'
0094     // should be set to false. This is *NOT* allowed for the first event.
0095     // G4Event object should be instantiated by G4WorkerSubEvtRunManager based on the
0096     // input provided in G4SubEvent.
0097     const G4SubEvent* GetSubEvent(G4int ty, G4bool& notReady,
0098               G4long& s1, G4long& s2, G4long& s3, G4bool reseedRequired = true) override;
0099 
0100     // Notify the master thread that processing of sub-event "se" is over.
0101     // Outcome of processed sub-event must be kept in "evt".
0102     // "se" is deleted by the master thread, while "evt" must be deleted by the worker thread
0103     // after invoking this method.
0104     void SubEventFinished(const G4SubEvent* se,const G4Event* evt) override;
0105 
0106     // Merge local scores to the master
0107     void MergeScores(const G4ScoringManager* localScoringManager) override;
0108 
0109     // Nothing to do for merging run
0110     void MergeRun(const G4Run*) override
0111     {}
0112 
0113     // Returns number of currently active threads.
0114     // This number may be different from the number of threads currently
0115     // in running state, e.g. the number returned by:
0116     // G4Threading::GetNumberOfActiveWorkerThreads() method.
0117     std::size_t GetNumberActiveThreads() const override
0118     { return threads.size(); }
0119 
0120     // Worker threads barrier: this method should be called by each
0121     // worker when ready to start thread event-loop.
0122     // This method will return only when all workers are ready.
0123     //   void ThisWorkerReady() override;
0124 
0125     // Worker threads barrier: this method should be called by each
0126     // worker when worker event loop is terminated.
0127     //   void ThisWorkerEndEventLoop() override;
0128 
0129     void SetUserInitialization(G4VUserPhysicsList* userPL) override;
0130     void SetUserInitialization(G4VUserDetectorConstruction* userDC) override;
0131     void SetUserInitialization(G4UserWorkerInitialization* userInit) override;
0132     void SetUserInitialization(G4UserWorkerThreadInitialization* userInit) override;
0133     void SetUserInitialization(G4VUserActionInitialization* userInit) override;
0134     void SetUserAction(G4UserRunAction* userAction) override;
0135     void SetUserAction(G4VUserPrimaryGeneratorAction* userAction) override;
0136     void SetUserAction(G4UserEventAction* userAction) override;
0137     void SetUserAction(G4UserStackingAction* userAction) override;
0138     void SetUserAction(G4UserTrackingAction* userAction) override;
0139     void SetUserAction(G4UserSteppingAction* userAction) override;
0140 
0141     // Called to force workers to request and process the UI commands stack
0142     // This will block untill all workers have processed UI commands
0143     void RequestWorkersProcessCommandsStack() override;
0144 
0145     // Called by workers to signal to master it has completed processing of
0146     // UI commands
0147     void ThisWorkerProcessCommandsStackDone() override;
0148 
0149     // Worker thread barrier: this method should be used by workers' run
0150     // manager to wait, after an event loop for the next action to be
0151     // performed (for example execute a new run).
0152     void WaitForReadyWorkers() override {}
0153     void WaitForEndEventLoopWorkers() override;
0154 
0155     // This returns the action to be performed.
0156     WorkerActionRequest ThisWorkerWaitForNextAction() override
0157     {
0158       return WorkerActionRequest::UNDEFINED;
0159     }
0160 
0161     void AbortRun(G4bool softAbort = false) override;
0162     void AbortEvent() override;
0163 
0164   protected:
0165     void ComputeNumberOfTasks() override
0166     {
0167       // This method is not used for sub-event parallel mode
0168       numberOfTasks = (G4int)threadPool->size();
0169       numberOfEventsPerTask = -1;
0170       eventModulo = -1;
0171     }
0172 
0173     // Initialize the seeds list, if derived class does not implement this
0174     // method, a default generation will be used (nevents*2 random seeds).
0175     // Return true if initialization is done.
0176     // Adds one seed to the list of seeds.
0177     G4bool InitializeSeeds(G4int /*nevts*/) override { return false; };
0178 
0179     // Adds one seed to the list of seeds
0180     void RefillSeeds() override;
0181 
0182     //void PrepareCommandsStack() override;
0183 
0184     // Creates worker threads and signal to start
0185     void CreateAndStartWorkers() override;
0186 
0187     void TerminateWorkers() override;
0188     void NewActionRequest(WorkerActionRequest) override {}
0189     void AddEventTask(G4int) override;
0190 
0191     void SetUpSeedsForSubEvent(G4long& s1, G4long& s2, G4long& s3);
0192 
0193     void MergeTrajectories(const G4SubEvent* se,const G4Event* evt) override;
0194     void UpdateScoringForSubEvent(const G4SubEvent* se,const G4Event* evt) override;
0195 
0196     void CleanUpUnnecessaryEvents(G4int keepNEvents) override;
0197     void StackPreviousEvent(G4Event* anEvent) override;
0198 
0199   protected:
0200     std::atomic<G4bool> runInProgress = false;
0201 
0202   private:
0203     G4bool trajectoriesToBeMerged = false;
0204     std::map<G4int,G4int> fSubEvtTypeMap;
0205     std::map<G4WorkerSubEvtRunManager*,G4int> fWorkerMap;
0206 
0207     G4bool CheckSubEvtTypes();
0208 
0209   public:
0210     void TrajectoriesToBeMerged(G4bool val=true) override
0211     { trajectoriesToBeMerged = val; }
0212 };
0213 
0214 #endif  // G4SubEvtRunManager_hh