Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //----------------------------------*-C++-*----------------------------------//
0002 // Copyright 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/optical/CoreParams.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include "corecel/Assert.hh"
0011 #include "corecel/data/DeviceVector.hh"
0012 #include "corecel/data/ObserverPtr.hh"
0013 #include "corecel/data/ParamsDataInterface.hh"
0014 #include "celeritas/geo/GeoFwd.hh"
0015 #include "celeritas/random/RngParamsFwd.hh"
0016 
0017 #include "CoreTrackData.hh"
0018 
0019 namespace celeritas
0020 {
0021 //---------------------------------------------------------------------------//
0022 class ActionRegistry;
0023 
0024 namespace optical
0025 {
0026 //---------------------------------------------------------------------------//
0027 class MaterialParams;
0028 class TrackInitParams;
0029 // TODO: class PhysicsParams;
0030 
0031 //---------------------------------------------------------------------------//
0032 /*!
0033  * Shared parameters for the optical photon loop.
0034  */
0035 class CoreParams final : public ParamsDataInterface<CoreParamsData>
0036 {
0037   public:
0038     //!@{
0039     //! \name Type aliases
0040     using SPConstGeo = std::shared_ptr<GeoParams const>;
0041     using SPConstMaterial = std::shared_ptr<MaterialParams 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         // TODO: 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     //! Access data on the host
0080     HostRef const& host_ref() const final { return host_ref_; }
0081     //! Access data on the device
0082     DeviceRef const& device_ref() const final { return device_ref_; }
0083     //!@}
0084 
0085     //!@{
0086     //! Access shared problem parameter data.
0087     SPConstGeo const& geometry() const { return input_.geometry; }
0088     SPConstMaterial const& material() const { return input_.material; }
0089     SPConstRng const& rng() const { return input_.rng; }
0090     SPConstTrackInit const& init() const { return input_.init; }
0091     SPActionRegistry const& action_reg() const { return input_.action_reg; }
0092     //!@}
0093 
0094     // Access host pointers to core data
0095     using ParamsDataInterface<CoreParamsData>::ref;
0096 
0097     // Access a native pointer to properties in the native memory space
0098     template<MemSpace M>
0099     inline ConstPtr<M> ptr() const;
0100 
0101     //! Maximum number of streams
0102     size_type max_streams() const { return input_.max_streams; }
0103 
0104   private:
0105     Input input_;
0106     HostRef host_ref_;
0107     DeviceRef device_ref_;
0108 
0109     // Copy of DeviceRef in device memory
0110     DeviceVector<DeviceRef> device_ref_vec_;
0111 };
0112 
0113 //---------------------------------------------------------------------------//
0114 /*!
0115  * Access a native pointer to a NativeCRef.
0116  *
0117  * This way, CUDA kernels only need to copy a pointer in the kernel arguments,
0118  * rather than the entire (rather large) DeviceRef object.
0119  */
0120 template<MemSpace M>
0121 auto CoreParams::ptr() const -> ConstPtr<M>
0122 {
0123     if constexpr (M == MemSpace::host)
0124     {
0125         return make_observer(&host_ref_);
0126     }
0127 #ifndef __NVCC__
0128     // CUDA 11.4 complains about 'else if constexpr' ("missing return
0129     // statement") and GCC 11.2 complains about leaving off the 'else'
0130     // ("inconsistent deduction for auto return type")
0131     else
0132 #endif
0133     {
0134         CELER_ENSURE(!device_ref_vec_.empty());
0135         return make_observer(device_ref_vec_);
0136     }
0137 }
0138 
0139 //---------------------------------------------------------------------------//
0140 }  // namespace optical
0141 }  // namespace celeritas