Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 09:14:38

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 // G4GeometryManager
0027 //
0028 // Class description:
0029 //
0030 // A class responsible for high level geometrical functions, and for
0031 // high level objects in the geometry subdomain.
0032 // The class is a `singleton', with access via the static method
0033 // G4GeometryManager::GetInstance().
0034 //
0035 // Member data:
0036 //
0037 //   - fgInstance
0038 //     Ptr to the unique instance of class (per Thread)
0039 
0040 // 26.07.95, P.Kent - Initial version, including optimisation build
0041 // 12.06.24, J.Apostolakis - Added parallel optimisation in workers
0042 // --------------------------------------------------------------------
0043 #ifndef G4GEOMETRYMANAGER_HH
0044 #define G4GEOMETRYMANAGER_HH 1
0045 
0046 #include <vector>
0047 
0048 #include "G4Types.hh"
0049 #include "G4SmartVoxelStat.hh"
0050 #include "G4ios.hh"
0051 
0052 class G4VPhysicalVolume;
0053 class G4Timer;
0054 
0055 class G4GeometryManager
0056 {
0057   public:
0058   
0059     G4bool CloseGeometry(G4bool pOptimise = true, G4bool verbose = false,
0060                          G4VPhysicalVolume* vol = nullptr);
0061       // Close (`lock') the geometry: perform sanity and `completion' checks
0062       // and optionally [default=yes] build optimisation information.
0063       // Applies to just a specific subtree if a physical volume is specified.
0064 
0065     void OpenGeometry(G4VPhysicalVolume* vol = nullptr);
0066       // Open (`unlock') the geometry and remove optimisation information if
0067       // present. Applies to just a specific subtree if a physical volume is
0068       // specified.
0069 
0070     inline G4bool IsGeometryClosed() { return fIsClosed; }
0071       // Return true/false according to state of optimised geometry.
0072 
0073     void SetWorldMaximumExtent(G4double worldExtent);
0074       // Set the maximum extent of the world volume. The operation is
0075       // allowed only if NO solids have been created already.
0076 
0077     static G4GeometryManager* GetInstance();
0078       // Return ptr to singleton instance of the class, creating it if
0079       // not existing.
0080 
0081     static G4GeometryManager* GetInstanceIfExist();
0082       // Return ptr to singleton instance.
0083 
0084     void OptimiseInParallel(G4bool val = true);
0085       // Request optimisation using threads (if MT is enabled & used ).
0086   
0087     void UndertakeOptimisation();
0088       // Method that contributes to (Voxel) optimisation until all work is done.
0089       // Must be called by Worker thread initialisation - not a user callable
0090       // method.
0091 
0092     void RequestParallelOptimisation(G4bool val = true,
0093                                      G4bool verbose = true);
0094       // Detailed method for user to request parallel Optimisation
0095       // (if verbosity is required). Calling this is enough to ask for it.
0096       // It will be used if Geant4 is built with MT/tasks.
0097   
0098     G4bool IsParallelOptimisationConfigured();
0099       // Check whether parallel optimisation was requested.
0100     G4bool IsParallelOptimisationFinished();
0101       // Report whether parallel optimisation is done.
0102   
0103     ~G4GeometryManager();
0104       // Destructor; called by G4RunManagerKernel.
0105 
0106   private:
0107 
0108     G4GeometryManager() = default;
0109       // Private constructor. Set the geometry to be open.
0110 
0111     G4bool BuildOptimisations(G4bool allOpt, G4bool verbose = false);  
0112        // Optimise all or just multi-volumes (parameterisations, .. ).
0113     void BuildOptimisations(G4bool allOpt, G4VPhysicalVolume* vol);
0114        // Optimise one volume or subtree only.
0115     void DeleteOptimisations();
0116     void DeleteOptimisations(G4VPhysicalVolume* vol);
0117   
0118     void ReportVoxelStats( std::vector<G4SmartVoxelStat>& stats,
0119                            G4double totalCpuTime,
0120                            std::ostream &os = G4cout );
0121     void ReportVoxelInfo(G4LogicalVolume * logVolume, std::ostream& os);
0122    
0123     void PrepareParallelOptimisation(G4bool allOpts, G4bool verbose = true);
0124     void BuildOptimisationsSequential(G4bool allOpts, G4bool verbose = true);
0125 
0126     // Methods for parallel initialization
0127     void CreateListOfVolumesToOptimise(G4bool allOpts, G4bool verbose);
0128       // Build vector of relevant volumes.
0129     G4LogicalVolume* ObtainVolumeToOptimise();
0130 
0131     void ConfigureParallelOptimisation(G4bool verbose);
0132       // Prepare for parallel optimisation.
0133 
0134     G4int ReportWorkerIsDoneOptimising(unsigned int numVolumesOptimised);
0135       // Thread-safe method for worker to report it's finished its work.
0136       // It counts the number of workers that finished, and returns count.
0137       // It counts the number of volumes optimised; if all workers have
0138       // reported, it results in a 'Finished' state.
0139   
0140     void InformOptimisationIsFinished(G4bool verbose);
0141       // Returns true if all workers are finished (or all work is done).
0142   
0143     void ResetListOfVolumesToOptimise();
0144       // Resets (empties) the list of candidate volumes for optimisation.
0145       // Must be called when Optimisation is finished.
0146   
0147     G4int CheckOptimisation();
0148       // Check volumes marked to optimised are done, and report number
0149       // that are missing voxel header.
0150   
0151     void WaitForVoxelisationFinish(G4bool verbose = false);
0152       // Wait until the voxelisation is all done.
0153 
0154   private:
0155 
0156     static G4ThreadLocal G4GeometryManager* fgInstance;
0157     G4bool fIsClosed = false;
0158 
0159     static std::vector<G4LogicalVolume*> fVolumesToOptimise;
0160       // The list of volumes which threads need to optimise.
0161     static std::vector<G4LogicalVolume*>::const_iterator fLogVolumeIterator;
0162       // Iterator used by UndertakeOptimisation().
0163 
0164     static std::vector<G4SmartVoxelStat> fGlobVoxelStats;
0165       // Statistics container shared by all workers
0166   
0167     // Flags for parallel initialization
0168     // ---------------------------------
0169     static G4bool fVerboseParallel;
0170     static G4bool fParallelVoxelOptimisationRequested;
0171       // Flag to register it was requested.
0172     static G4bool fOptimiseInParallelConfigured;
0173       // Not just requested, but adopted (i.e. also in MT/tasking mode).
0174     static G4bool fParallelVoxelOptimisationUnderway; // It has started
0175     static G4bool fParallelVoxelOptimisationFinished; // It is done
0176     static G4bool fUsingExistingWorkers; // Can and will use existing MT/tasks.
0177 
0178     // Statistics for parallel Optimisation - used in 'verbose' mode
0179     // ------------------------------------
0180     static G4double fSumVoxelTime;
0181     static G4int fNumberThreadsReporting;
0182     static unsigned int fTotalNumberVolumesOptimised;
0183       // Counters.
0184   
0185     // For Wall Clock time in parallel mode ...
0186     //
0187     static G4Timer* fWallClockTimer;   // Owned by master thread
0188     static G4bool fWallClockStarted;
0189 };
0190 
0191 #endif