Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-02 10:13:15

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