Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-16 08:52:21

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 celeritas/global/CoreParams.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <memory>
0010 
0011 #include "corecel/Assert.hh"
0012 #include "corecel/data/DeviceVector.hh"
0013 #include "corecel/data/ObserverPtr.hh"
0014 #include "corecel/data/ParamsDataInterface.hh"
0015 #include "corecel/random/params/RngParamsFwd.hh"
0016 #include "celeritas/geo/GeoFwd.hh"
0017 
0018 #include "ActionInterface.hh"
0019 #include "CoreTrackData.hh"
0020 
0021 namespace celeritas
0022 {
0023 //---------------------------------------------------------------------------//
0024 class ActionRegistry;
0025 class CutoffParams;
0026 class GeoMaterialParams;
0027 class MaterialParams;
0028 class MpiCommunicator;
0029 class OutputRegistry;
0030 class ParticleParams;
0031 class PhysicsParams;
0032 class SimParams;
0033 class TrackInitParams;
0034 class AuxParamsRegistry;
0035 class WentzelOKVIParams;
0036 
0037 //---------------------------------------------------------------------------//
0038 /*!
0039  * Global parameters required to run a problem.
0040  *
0041  * \todo Applications specify \c tracks_per_stream to build the states, but
0042  * unit tests currently omit this option.
0043  */
0044 class CoreParams final : public ParamsDataInterface<CoreParamsData>
0045 {
0046   public:
0047     //!@{
0048     //! \name Type aliases
0049     using SPConstGeo = std::shared_ptr<GeoParams const>;
0050     using SPConstMaterial = std::shared_ptr<MaterialParams const>;
0051     using SPConstGeoMaterial = std::shared_ptr<GeoMaterialParams const>;
0052     using SPConstParticle = std::shared_ptr<ParticleParams const>;
0053     using SPConstCutoff = std::shared_ptr<CutoffParams const>;
0054     using SPConstPhysics = std::shared_ptr<PhysicsParams const>;
0055     using SPConstRng = std::shared_ptr<RngParams const>;
0056     using SPConstSim = std::shared_ptr<SimParams const>;
0057     using SPConstTrackInit = std::shared_ptr<TrackInitParams const>;
0058     using SPConstWentzelOKVI = std::shared_ptr<WentzelOKVIParams const>;
0059     using SPConstMpiCommunicator = std::shared_ptr<MpiCommunicator const>;
0060     using SPActionRegistry = std::shared_ptr<ActionRegistry>;
0061     using SPOutputRegistry = std::shared_ptr<OutputRegistry>;
0062     using SPUserRegistry = std::shared_ptr<AuxParamsRegistry>;
0063 
0064     template<MemSpace M>
0065     using ConstRef = CoreParamsData<Ownership::const_reference, M>;
0066     template<MemSpace M>
0067     using ConstPtr = ObserverPtr<ConstRef<M> const, M>;
0068     //!@}
0069 
0070     struct Input
0071     {
0072         SPConstGeo geometry;
0073         SPConstMaterial material;
0074         SPConstGeoMaterial geomaterial;
0075         SPConstParticle particle;
0076         SPConstCutoff cutoff;
0077         SPConstPhysics physics;
0078         SPConstRng rng;
0079         SPConstSim sim;
0080         SPConstTrackInit init;
0081         SPConstWentzelOKVI wentzel;  //!< Optional (TODO: aux data?)
0082 
0083         SPActionRegistry action_reg;
0084         SPOutputRegistry output_reg;
0085         SPUserRegistry aux_reg;  //!< Optional, empty default
0086         SPConstMpiCommunicator mpi_comm;  //!< Optional, world_comm default
0087 
0088         //! Maximum number of simultaneous threads/tasks per process
0089         StreamId::size_type max_streams{1};
0090 
0091         //! Number of track slots per stream
0092         StreamId::size_type tracks_per_stream{0};
0093 
0094         //! True if all params are assigned and valid
0095         explicit operator bool() const
0096         {
0097             return geometry && material && geomaterial && particle && cutoff
0098                    && physics && rng && sim && init && action_reg && output_reg
0099                    && max_streams;
0100         }
0101     };
0102 
0103   public:
0104     // Construct with all problem data, creating some actions too
0105     explicit CoreParams(Input inp);
0106 
0107     //!@{
0108     //! Access shared problem parameter data.
0109     SPConstGeo const& geometry() const { return input_.geometry; }
0110     SPConstMaterial const& material() const { return input_.material; }
0111     SPConstGeoMaterial const& geomaterial() const
0112     {
0113         return input_.geomaterial;
0114     }
0115     SPConstParticle const& particle() const { return input_.particle; }
0116     SPConstCutoff const& cutoff() const { return input_.cutoff; }
0117     SPConstPhysics const& physics() const { return input_.physics; }
0118     SPConstRng const& rng() const { return input_.rng; }
0119     SPConstSim const& sim() const { return input_.sim; }
0120     SPConstTrackInit const& init() const { return input_.init; }
0121     SPConstWentzelOKVI const& wentzel() const { return input_.wentzel; }
0122     SPActionRegistry const& action_reg() const { return input_.action_reg; }
0123     SPOutputRegistry const& output_reg() const { return input_.output_reg; }
0124     SPUserRegistry const& aux_reg() const { return input_.aux_reg; }
0125     SPConstMpiCommunicator const& mpi_comm() const { return input_.mpi_comm; }
0126     //!@}
0127 
0128     //! Access data on the host
0129     HostRef const& host_ref() const final { return host_ref_; }
0130 
0131     //! Access data on the device
0132     DeviceRef const& device_ref() const final { return device_ref_; }
0133 
0134     // Access host pointers to core data
0135     using ParamsDataInterface<CoreParamsData>::ref;
0136 
0137     // Access a native pointer to properties in the native memory space
0138     template<MemSpace M>
0139     inline ConstPtr<M> ptr() const;
0140 
0141     //! Maximum number of streams
0142     size_type max_streams() const { return input_.max_streams; }
0143 
0144     //! Number of track slots per stream
0145     size_type tracks_per_stream() const { return input_.tracks_per_stream; }
0146 
0147   private:
0148     Input input_;
0149     HostRef host_ref_;
0150     DeviceRef device_ref_;
0151 
0152     // Copy of DeviceRef in device memory
0153     DeviceVector<DeviceRef> device_ref_vec_;
0154 };
0155 
0156 //---------------------------------------------------------------------------//
0157 /*!
0158  * Access a native pointer to a NativeCRef.
0159  *
0160  * This way, CUDA kernels only need to copy a pointer in the kernel arguments,
0161  * rather than the entire (rather large) DeviceRef object.
0162  */
0163 template<MemSpace M>
0164 auto CoreParams::ptr() const -> ConstPtr<M>
0165 {
0166     if constexpr (M == MemSpace::host)
0167     {
0168         return make_observer(&host_ref_);
0169     }
0170     else
0171     {
0172         CELER_ENSURE(!device_ref_vec_.empty());
0173         return make_observer(device_ref_vec_);
0174     }
0175 #if CELER_CUDACC_BUGGY_IF_CONSTEXPR
0176     CELER_ASSERT_UNREACHABLE();
0177 #endif
0178 }
0179 
0180 //---------------------------------------------------------------------------//
0181 }  // namespace celeritas