Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-15 10:10:59

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/optical/CoreParams.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include "corecel/Assert.hh"
0010 #include "corecel/data/DeviceVector.hh"
0011 #include "corecel/data/ObserverPtr.hh"
0012 #include "corecel/data/ParamsDataInterface.hh"
0013 #include "corecel/random/params/RngParamsFwd.hh"
0014 #include "celeritas/geo/GeoFwd.hh"
0015 
0016 #include "CoreTrackData.hh"
0017 
0018 namespace celeritas
0019 {
0020 //---------------------------------------------------------------------------//
0021 class ActionRegistry;
0022 
0023 namespace optical
0024 {
0025 //---------------------------------------------------------------------------//
0026 class MaterialParams;
0027 class TrackInitParams;
0028 class PhysicsParams;
0029 
0030 //---------------------------------------------------------------------------//
0031 /*!
0032  * Shared parameters for the optical photon loop.
0033  */
0034 class CoreParams final : public ParamsDataInterface<CoreParamsData>
0035 {
0036   public:
0037     //!@{
0038     //! \name Type aliases
0039     using SPConstGeo = std::shared_ptr<GeoParams const>;
0040     using SPConstMaterial = std::shared_ptr<MaterialParams const>;
0041     using SPConstPhysics = std::shared_ptr<PhysicsParams const>;
0042     using SPConstRng = std::shared_ptr<RngParams const>;
0043     using SPConstTrackInit = std::shared_ptr<TrackInitParams const>;
0044     using SPActionRegistry = std::shared_ptr<ActionRegistry>;
0045 
0046     template<MemSpace M>
0047     using ConstRef = CoreParamsData<Ownership::const_reference, M>;
0048     template<MemSpace M>
0049     using ConstPtr = ObserverPtr<ConstRef<M> const, M>;
0050     //!@}
0051 
0052     struct Input
0053     {
0054         SPConstGeo geometry;
0055         SPConstMaterial material;
0056         SPConstPhysics physics;
0057         SPConstRng rng;
0058         SPConstTrackInit init;
0059 
0060         SPActionRegistry action_reg;
0061 
0062         //! Maximum number of simultaneous threads/tasks per process
0063         StreamId::size_type max_streams{1};
0064 
0065         //! True if all params are assigned and valid
0066         explicit operator bool() const
0067         {
0068             return geometry && material && rng && init && action_reg
0069                    && max_streams;
0070         }
0071     };
0072 
0073   public:
0074     // Construct with all problem data, creating some actions too
0075     CoreParams(Input&& inp);
0076 
0077     //!@{
0078     //! \name Data interface
0079 
0080     //! Access data on the host
0081     HostRef const& host_ref() const final { return host_ref_; }
0082     //! Access data on the device
0083     DeviceRef const& device_ref() const final { return device_ref_; }
0084     //!@}
0085 
0086     //!@{
0087     //! Access shared problem parameter data.
0088     SPConstGeo const& geometry() const { return input_.geometry; }
0089     SPConstMaterial const& material() const { return input_.material; }
0090     SPConstPhysics const& physics() const { return input_.physics; }
0091     SPConstRng const& rng() const { return input_.rng; }
0092     SPConstTrackInit const& init() const { return input_.init; }
0093     SPActionRegistry const& action_reg() const { return input_.action_reg; }
0094     //!@}
0095 
0096     // Access host pointers to core data
0097     using ParamsDataInterface<CoreParamsData>::ref;
0098 
0099     // Access a native pointer to properties in the native memory space
0100     template<MemSpace M>
0101     inline ConstPtr<M> ptr() const;
0102 
0103     //! Maximum number of streams
0104     size_type max_streams() const { return input_.max_streams; }
0105 
0106   private:
0107     Input input_;
0108     HostRef host_ref_;
0109     DeviceRef device_ref_;
0110 
0111     // Copy of DeviceRef in device memory
0112     DeviceVector<DeviceRef> device_ref_vec_;
0113 };
0114 
0115 //---------------------------------------------------------------------------//
0116 /*!
0117  * Access a native pointer to a NativeCRef.
0118  *
0119  * This way, CUDA kernels only need to copy a pointer in the kernel arguments,
0120  * rather than the entire (rather large) DeviceRef object.
0121  */
0122 template<MemSpace M>
0123 auto CoreParams::ptr() const -> ConstPtr<M>
0124 {
0125     if constexpr (M == MemSpace::host)
0126     {
0127         return make_observer(&host_ref_);
0128     }
0129     else
0130     {
0131         CELER_ENSURE(!device_ref_vec_.empty());
0132         return make_observer(device_ref_vec_);
0133     }
0134 #if CELER_CUDACC_BUGGY_IF_CONSTEXPR
0135     CELER_ASSERT_UNREACHABLE();
0136 #endif
0137 }
0138 
0139 //---------------------------------------------------------------------------//
0140 }  // namespace optical
0141 }  // namespace celeritas