Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-06 08:16:16

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