Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:41:11

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details
0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0004 //---------------------------------------------------------------------------//
0005 //! \file accel/SharedParams.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <memory>
0010 #include <string>
0011 #include <vector>
0012 
0013 #include "corecel/Assert.hh"
0014 #include "geocel/BoundingBox.hh"
0015 
0016 #include "Types.hh"
0017 
0018 class G4ParticleDefinition;
0019 class G4VPhysicalVolume;
0020 
0021 namespace celeritas
0022 {
0023 //---------------------------------------------------------------------------//
0024 namespace detail
0025 {
0026 class OffloadWriter;
0027 }  // namespace detail
0028 
0029 class CoreParams;
0030 class CoreStateInterface;
0031 struct Primary;
0032 struct SetupOptions;
0033 class StepCollector;
0034 class GeantGeoParams;
0035 class OutputRegistry;
0036 class TimeOutput;
0037 class GeantSd;
0038 
0039 //---------------------------------------------------------------------------//
0040 /*!
0041  * Shared (one instance for all threads) Celeritas problem data.
0042  *
0043  * The \c CeleritasDisabled accessor queries the \c CELER_DISABLE environment
0044  * variable as a global option for disabling Celeritas offloading. This is
0045  * implemented by \c SimpleOffload
0046  *
0047  * This should be instantiated on the master thread during problem setup,
0048  * preferably as a shared pointer. The shared pointer should be
0049  * passed to a thread-local \c LocalTransporter instance. At the beginning of
0050  * the run, after Geant4 has initialized physics data, the \c Initialize method
0051  * must be called first on the "master" thread to populate the Celeritas data
0052  * structures (geometry, physics). \c InitializeWorker must subsequently be
0053  * invoked on all worker threads to set up thread-local data (specifically,
0054  * CUDA device initialization).
0055  *
0056  * Some low-level objects, such as the output diagnostics and Geant4 geometry
0057  * wrapper, can be created independently of Celeritas being enabled.
0058  */
0059 class SharedParams
0060 {
0061   public:
0062     //!@{
0063     //! \name Type aliases
0064     using SPParams = std::shared_ptr<CoreParams>;
0065     using SPConstParams = std::shared_ptr<CoreParams const>;
0066     using VecG4ParticleDef = std::vector<G4ParticleDefinition*>;
0067     using Mode = OffloadMode;
0068     //!@}
0069 
0070   public:
0071     //!@{
0072     //! \name Status
0073 
0074     // Whether celeritas is disabled, set to kill, or to be enabled
0075     static Mode GetMode();
0076 
0077     // True if Celeritas is globally disabled using the CELER_DISABLE env
0078     // Remove in 0.7
0079     [[deprecated]]
0080     static bool CeleritasDisabled();
0081 
0082     // Whether to kill tracks that would have been offloaded
0083     // Remove in 0.7
0084     [[deprecated]]
0085     static bool KillOffloadTracks();
0086 
0087     //!@}
0088     //!@{
0089     //! \name Construction
0090 
0091     // Construct in an uninitialized state
0092     SharedParams() = default;
0093 
0094     // Construct Celeritas using Geant4 data on the master thread.
0095     explicit SharedParams(SetupOptions const& options);
0096 
0097     // Initialize shared data on the "master" thread
0098     inline void Initialize(SetupOptions const& options);
0099 
0100     // On worker threads, set up data with thread storage duration
0101     void InitializeWorker(SetupOptions const& options);
0102 
0103     // Write (shared) diagnostic output and clear shared data on master
0104     void Finalize();
0105 
0106     //!@}
0107     //!@{
0108     //! \name Accessors
0109 
0110     // Access constructed Celeritas data
0111     inline SPParams const& Params();
0112 
0113     // Access constructed Celeritas data
0114     inline SPConstParams Params() const;
0115 
0116     // Get a vector of particles supported by Celeritas offloading
0117     inline VecG4ParticleDef const& OffloadParticles() const;
0118 
0119     //! Whether the class has been constructed
0120     explicit operator bool() const { return mode_ != Mode::uninitialized; }
0121 
0122     //!@}
0123     //!@{
0124     //! \name Internal use only
0125 
0126     using SPGeantSd = std::shared_ptr<GeantSd>;
0127     using SPOffloadWriter = std::shared_ptr<detail::OffloadWriter>;
0128     using SPOutputRegistry = std::shared_ptr<OutputRegistry>;
0129     using SPTimeOutput = std::shared_ptr<TimeOutput>;
0130     using SPState = std::shared_ptr<CoreStateInterface>;
0131     using SPConstGeantGeoParams = std::shared_ptr<GeantGeoParams const>;
0132     using BBox = BoundingBox<double>;
0133 
0134     //! Initialization status and integration mode
0135     Mode mode() const { return mode_; }
0136 
0137     // Hit manager, to be used only by LocalTransporter
0138     inline SPGeantSd const& hit_manager() const;
0139 
0140     // Optional offload writer, only for use by LocalTransporter
0141     inline SPOffloadWriter const& offload_writer() const;
0142 
0143     // Output registry
0144     inline SPOutputRegistry const& output_reg() const;
0145 
0146     // Access the timer
0147     inline SPTimeOutput const& timer() const;
0148 
0149     // Let LocalTransporter register the thread's state
0150     void set_state(unsigned int stream_id, SPState&&);
0151 
0152     // Number of streams, lazily obtained from run manager
0153     unsigned int num_streams() const;
0154 
0155     // Geant geometry wrapper, lazily created
0156     SPConstGeantGeoParams const& geant_geo_params() const;
0157 
0158     // Geometry bounding box (CLHEP units)
0159     BBox const& bbox() const { return bbox_; }
0160     //!@}
0161 
0162   private:
0163     //// DATA ////
0164 
0165     // Created during initialization
0166     Mode mode_{Mode::uninitialized};
0167     std::shared_ptr<CoreParams> params_;
0168     std::shared_ptr<GeantSd> geant_sd_;
0169     std::shared_ptr<StepCollector> step_collector_;
0170     VecG4ParticleDef particles_;
0171     std::string output_filename_;
0172     G4VPhysicalVolume const* world_{nullptr};
0173     SPOffloadWriter offload_writer_;
0174     std::vector<std::shared_ptr<CoreStateInterface>> states_;
0175     SPOutputRegistry output_reg_;
0176     SPTimeOutput timer_;
0177     BBox bbox_;
0178 
0179     // Lazily created
0180     SPConstGeantGeoParams geant_geo_;
0181 
0182     //// HELPER FUNCTIONS ////
0183 
0184     void set_num_streams(unsigned int num_streams);
0185     void try_output() const;
0186 };
0187 
0188 //---------------------------------------------------------------------------//
0189 /*!
0190  * Helper for making initialization more obvious from user code.
0191  */
0192 void SharedParams::Initialize(SetupOptions const& options)
0193 {
0194     *this = SharedParams(options);
0195 }
0196 
0197 //---------------------------------------------------------------------------//
0198 /*!
0199  * Access Celeritas data.
0200  *
0201  * This can only be called after \c Initialize.
0202  */
0203 auto SharedParams::Params() -> SPParams const&
0204 {
0205     CELER_EXPECT(mode_ == Mode::enabled);
0206     CELER_ENSURE(params_);
0207     return params_;
0208 }
0209 
0210 //---------------------------------------------------------------------------//
0211 /*!
0212  * Access Celeritas data.
0213  *
0214  * This can only be called after \c Initialize.
0215  */
0216 auto SharedParams::Params() const -> SPConstParams
0217 {
0218     CELER_EXPECT(mode_ == Mode::enabled);
0219     CELER_ENSURE(params_);
0220     return params_;
0221 }
0222 
0223 //---------------------------------------------------------------------------//
0224 /*!
0225  * Get a vector of particles supported by Celeritas offloading.
0226  */
0227 auto SharedParams::OffloadParticles() const -> VecG4ParticleDef const&
0228 {
0229     CELER_EXPECT(*this);
0230     return particles_;
0231 }
0232 
0233 //---------------------------------------------------------------------------//
0234 /*!
0235  * Hit manager, to be used only by LocalTransporter.
0236  *
0237  * If sensitive detector callback is disabled, the hit manager will be null.
0238  */
0239 auto SharedParams::hit_manager() const -> SPGeantSd const&
0240 {
0241     CELER_EXPECT(*this);
0242     return geant_sd_;
0243 }
0244 
0245 //---------------------------------------------------------------------------//
0246 /*!
0247  * Optional offload writer, only for use by LocalTransporter.
0248  */
0249 auto SharedParams::offload_writer() const -> SPOffloadWriter const&
0250 {
0251     CELER_EXPECT(*this);
0252     return offload_writer_;
0253 }
0254 
0255 //---------------------------------------------------------------------------//
0256 /*!
0257  * Output registry for writing data at end of run.
0258  */
0259 auto SharedParams::output_reg() const -> SPOutputRegistry const&
0260 {
0261     CELER_EXPECT(*this);
0262     return output_reg_;
0263 }
0264 
0265 //---------------------------------------------------------------------------//
0266 /*!
0267  * Access the timer.
0268  */
0269 auto SharedParams::timer() const -> SPTimeOutput const&
0270 {
0271     CELER_EXPECT(*this);
0272     CELER_EXPECT(timer_);
0273     return timer_;
0274 }
0275 
0276 //---------------------------------------------------------------------------//
0277 }  // namespace celeritas