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