Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-22 10:31:23

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