Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 09:09:05

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/CoreState.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <vector>
0010 
0011 #include "corecel/Types.hh"
0012 #include "corecel/data/AuxInterface.hh"
0013 #include "corecel/data/AuxStateData.hh"
0014 #include "corecel/data/AuxStateVec.hh"
0015 #include "corecel/data/Collection.hh"
0016 #include "corecel/data/CollectionStateStore.hh"
0017 #include "corecel/data/DeviceVector.hh"
0018 #include "corecel/data/Ref.hh"
0019 #include "corecel/sys/ThreadId.hh"
0020 #include "celeritas/track/CoreStateCounters.hh"
0021 
0022 #include "CoreTrackData.hh"
0023 
0024 #include "detail/CoreStateThreadOffsets.hh"
0025 
0026 namespace celeritas
0027 {
0028 class CoreParams;
0029 //---------------------------------------------------------------------------//
0030 /*!
0031  * Abstract base class for CoreState.
0032  */
0033 class CoreStateInterface
0034 {
0035   public:
0036     //!@{
0037     //! \name Type aliases
0038     using size_type = TrackSlotId::size_type;
0039     //!@}
0040 
0041   public:
0042     // Support polymorphic deletion
0043     virtual ~CoreStateInterface();
0044 
0045     //! Thread/stream ID
0046     virtual StreamId stream_id() const = 0;
0047 
0048     //! Number of track slots
0049     virtual size_type size() const = 0;
0050 
0051     //! Access track initialization counters
0052     virtual CoreStateCounters const& counters() const = 0;
0053 
0054     //! Access auxiliary state data
0055     virtual AuxStateVec const& aux() const = 0;
0056 
0057     //! Access mutable auxiliary state data
0058     virtual AuxStateVec& aux() = 0;
0059 
0060   protected:
0061     CoreStateInterface() = default;
0062     CELER_DEFAULT_COPY_MOVE(CoreStateInterface);
0063 };
0064 
0065 //---------------------------------------------------------------------------//
0066 /*!
0067  * Store all state data for a single thread.
0068  *
0069  * When the state lives on the device, we maintain a separate copy of the
0070  * device "ref" in device memory: otherwise we'd have to copy the entire state
0071  * in launch arguments and access it through constant memory.
0072  */
0073 template<MemSpace M>
0074 class CoreState final : public CoreStateInterface
0075 {
0076   public:
0077     //!@{
0078     //! \name Type aliases
0079     template<template<Ownership, MemSpace> class S>
0080     using StateRef = S<Ownership::reference, M>;
0081 
0082     using Ref = StateRef<CoreStateData>;
0083     using Ptr = ObserverPtr<Ref, M>;
0084     //!@}
0085 
0086     //! Memory space
0087     static constexpr MemSpace memspace = M;
0088 
0089   public:
0090     // Construct from CoreParams
0091     CoreState(CoreParams const& params, StreamId stream_id);
0092 
0093     // Construct with manual slot count
0094     CoreState(CoreParams const& params,
0095               StreamId stream_id,
0096               size_type num_track_slots);
0097 
0098     // Default destructor
0099     ~CoreState() final;
0100 
0101     // Prevent move/copy
0102     CELER_DELETE_COPY_MOVE(CoreState);
0103 
0104     //! Thread/stream ID
0105     StreamId stream_id() const final { return this->ref().stream_id; }
0106 
0107     //! Number of track slots
0108     size_type size() const final { return states_.size(); }
0109 
0110     // Set a warmup flag
0111     void warming_up(bool);
0112 
0113     //! Whether the state is being transported with no active particles
0114     bool warming_up() const { return warming_up_; }
0115 
0116     //// CORE DATA ////
0117 
0118     //! Get a reference to the mutable state data
0119     Ref& ref() { return states_.ref(); }
0120 
0121     //! Get a reference to the mutable state data
0122     Ref const& ref() const { return states_.ref(); }
0123 
0124     //! Get a native-memspace pointer to the mutable state data
0125     Ptr ptr() { return ptr_; }
0126 
0127     //! Reset the state data
0128     void reset();
0129 
0130     //// COUNTERS ////
0131 
0132     //! Track initialization counters
0133     CoreStateCounters& counters() { return counters_; }
0134 
0135     //! Track initialization counters
0136     CoreStateCounters const& counters() const final { return counters_; }
0137 
0138     //// USER DATA ////
0139 
0140     //! Access auxiliary state data
0141     AuxStateVec const& aux() const final { return aux_state_; }
0142 
0143     //! Access auxiliary state data (mutable)
0144     AuxStateVec& aux() final { return aux_state_; }
0145 
0146     // Convenience function to access auxiliary "collection group" data
0147     template<template<Ownership, MemSpace> class S>
0148     inline StateRef<S>& aux_data(AuxId auxid);
0149 
0150     //// TRACK SORTING ////
0151 
0152     //! Return whether tracks can be sorted by action
0153     bool has_action_range() const { return !offsets_.empty(); }
0154 
0155     // Get a range of sorted track slots about to undergo a given action
0156     Range<ThreadId> get_action_range(ActionId action_id) const;
0157 
0158     // Access the range of actions to apply for all track IDs
0159     inline auto& action_thread_offsets();
0160 
0161     // Access the range of actions to apply for all track IDs
0162     inline auto const& action_thread_offsets() const;
0163 
0164     // Access action offsets for computation (native memory space)
0165     inline auto& native_action_thread_offsets();
0166 
0167   private:
0168     // State data
0169     CollectionStateStore<CoreStateData, M> states_;
0170 
0171     // Copy of state ref in device memory, if M == MemSpace::device
0172     DeviceVector<Ref> device_ref_vec_;
0173 
0174     // Native pointer to ref data
0175     Ptr ptr_;
0176 
0177     // Counters for track initialization and activity
0178     CoreStateCounters counters_;
0179 
0180     // User-added data associated with params
0181     AuxStateVec aux_state_;
0182 
0183     // Indices of first thread assigned to a given action
0184     detail::CoreStateThreadOffsets<M> offsets_;
0185 
0186     // Whether no primaries should be generated
0187     bool warming_up_{false};
0188 };
0189 
0190 //---------------------------------------------------------------------------//
0191 /*!
0192  * Convenience function to access auxiliary "collection group" data.
0193  */
0194 template<MemSpace M>
0195 template<template<Ownership, MemSpace> class S>
0196 auto CoreState<M>::aux_data(AuxId auxid) -> StateRef<S>&
0197 {
0198     CELER_EXPECT(auxid < aux_state_.size());
0199 
0200     // TODO: use "checked static cast" for better runtime performance
0201     auto* state = dynamic_cast<AuxStateData<S, M>*>(&aux_state_.at(auxid));
0202     CELER_ASSERT(state);
0203 
0204     CELER_ENSURE(*state);
0205     return state->ref();
0206 }
0207 
0208 //---------------------------------------------------------------------------//
0209 /*!
0210  * Access the range of actions to apply for all track IDs.
0211  */
0212 template<MemSpace M>
0213 auto& CoreState<M>::action_thread_offsets()
0214 {
0215     return offsets_.host_action_thread_offsets();
0216 }
0217 
0218 //---------------------------------------------------------------------------//
0219 /*!
0220  * Access the range of actions to apply for all track IDs.
0221  */
0222 template<MemSpace M>
0223 auto const& CoreState<M>::action_thread_offsets() const
0224 {
0225     return offsets_.host_action_thread_offsets();
0226 }
0227 
0228 //---------------------------------------------------------------------------//
0229 /*!
0230  * Access action offsets for computation (native memory space).
0231  */
0232 template<MemSpace M>
0233 auto& CoreState<M>::native_action_thread_offsets()
0234 {
0235     return offsets_.native_action_thread_offsets();
0236 }
0237 
0238 //---------------------------------------------------------------------------//
0239 // EXPLICIT INSTANTIATION
0240 //---------------------------------------------------------------------------//
0241 
0242 extern template class CoreState<MemSpace::host>;
0243 extern template class CoreState<MemSpace::device>;
0244 
0245 //---------------------------------------------------------------------------//
0246 }  // namespace celeritas