Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-21 09:29:26

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