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/CoreState.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include "corecel/cont/Span.hh"
0010 #include "corecel/data/AuxInterface.hh"
0011 #include "corecel/data/CollectionStateStore.hh"
0012 #include "corecel/data/ObserverPtr.hh"
0013 #include "celeritas/Types.hh"
0014 #include "celeritas/track/CoreStateCounters.hh"
0015 
0016 #include "CoreTrackData.hh"
0017 #include "TrackInitializer.hh"
0018 
0019 namespace celeritas
0020 {
0021 namespace optical
0022 {
0023 class CoreParams;
0024 
0025 //---------------------------------------------------------------------------//
0026 /*!
0027  * Interface class for optical state data.
0028  *
0029  * This inherits from the "aux state" interface to allow stream-local storage
0030  * with the optical offload data.
0031  */
0032 class CoreStateInterface : public AuxStateInterface
0033 {
0034   public:
0035     //!@{
0036     //! \name Type aliases
0037     using size_type = TrackSlotId::size_type;
0038     //!@}
0039 
0040   public:
0041     // Support polymorphic deletion
0042     ~CoreStateInterface() override;
0043 
0044     //! Thread/stream ID
0045     virtual StreamId stream_id() const = 0;
0046 
0047     //! Access track initialization counters
0048     virtual CoreStateCounters const& counters() const = 0;
0049 
0050     //! Number of track slots
0051     virtual size_type size() const = 0;
0052 
0053     // Inject optical primaries
0054     virtual void insert_primaries(Span<TrackInitializer const> host_primaries)
0055         = 0;
0056 
0057   protected:
0058     CoreStateInterface() = default;
0059     CELER_DEFAULT_COPY_MOVE(CoreStateInterface);
0060 };
0061 
0062 //---------------------------------------------------------------------------//
0063 /*!
0064  * Store all state data for a single thread.
0065  *
0066  * When the state lives on the device, we maintain a separate copy of the
0067  * device "ref" in device memory: otherwise we'd have to copy the entire state
0068  * in launch arguments and access it through constant memory.
0069  *
0070  * \todo Encapsulate all the action management accessors in a helper class.
0071  */
0072 template<MemSpace M>
0073 class CoreState final : public CoreStateInterface
0074 {
0075   public:
0076     //!@{
0077     //! \name Type aliases
0078     template<template<Ownership, MemSpace> class S>
0079     using StateRef = S<Ownership::reference, M>;
0080 
0081     using Ref = StateRef<CoreStateData>;
0082     using Ptr = ObserverPtr<Ref, M>;
0083     //!@}
0084 
0085   public:
0086     // Construct from CoreParams
0087     CoreState(CoreParams const& params,
0088               StreamId stream_id,
0089               size_type num_track_slots);
0090 
0091     //! Thread/stream ID
0092     StreamId stream_id() const final { return this->ref().stream_id; }
0093 
0094     //! Number of track slots
0095     size_type size() const final { return states_.size(); }
0096 
0097     // Whether the state is being transported with no active particles
0098     bool warming_up() const;
0099 
0100     //// CORE DATA ////
0101 
0102     //! Get a reference to the mutable state data
0103     Ref& ref() { return states_.ref(); }
0104 
0105     //! Get a reference to the mutable state data
0106     Ref const& ref() const { return states_.ref(); }
0107 
0108     //! Get a native-memspace pointer to the mutable state data
0109     Ptr ptr() { return ptr_; }
0110 
0111     //// COUNTERS ////
0112 
0113     //! Track initialization counters
0114     CoreStateCounters& counters() { return counters_; }
0115 
0116     //! Track initialization counters
0117     CoreStateCounters const& counters() const final { return counters_; }
0118 
0119     // Inject primaries to be turned into TrackInitializers
0120     void insert_primaries(Span<TrackInitializer const> host_primaries) final;
0121 
0122   private:
0123     // State data
0124     CollectionStateStore<CoreStateData, M> states_;
0125 
0126     // Copy of state ref in device memory, if M == MemSpace::device
0127     DeviceVector<Ref> device_ref_vec_;
0128 
0129     // Native pointer to ref or
0130     Ptr ptr_;
0131 
0132     // Counters for track initialization and activity
0133     CoreStateCounters counters_;
0134 };
0135 
0136 //---------------------------------------------------------------------------//
0137 }  // namespace optical
0138 }  // namespace celeritas