Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-09 09:05:19

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 corecel/data/AuxStateData.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <memory>
0010 
0011 #include "corecel/Types.hh"
0012 
0013 #include "AuxInterface.hh"
0014 #include "CollectionStateStore.hh"
0015 #include "ParamsDataInterface.hh"
0016 
0017 namespace celeritas
0018 {
0019 //---------------------------------------------------------------------------//
0020 /*!
0021  * Helper class for retrieving templated state data on a single stream.
0022  *
0023  * This class is most easily used with \c make_aux_state to create a
0024  * "collection group"-style state (see \ref collections) associated with a
0025  * \c AuxParamsInterface subclass.
0026  *
0027  * The state class \c S must have a \c resize method that's constructable with
0028  * an optional templated params data class \c P, a stream ID, and a state size.
0029  *
0030  * The \c make_aux_state helper functions can be used to construct this class:
0031  * \code
0032     return make_aux_state<FooStateData>(*this, memspace, stream, size);
0033  * \endcode
0034  * or
0035  * \code
0036     return make_aux_state<BarStateData>(memspace, stream, size);
0037  * \endcode
0038  */
0039 template<template<Ownership, MemSpace> class S, MemSpace M>
0040 class AuxStateData final : public AuxStateInterface
0041 {
0042   public:
0043     //!@{
0044     //! \name Type aliases
0045     using Ref = S<Ownership::reference, M>;
0046     //!@}
0047 
0048   public:
0049     // Construct by resizing and passing host params
0050     template<template<Ownership, MemSpace> class P>
0051     inline AuxStateData(HostCRef<P> const& p,
0052                         StreamId stream_id,
0053                         size_type size);
0054 
0055     // Construct by resizing without params
0056     inline AuxStateData(StreamId stream_id, size_type size);
0057 
0058     //! Whether any data is being stored
0059     explicit operator bool() const { return static_cast<bool>(store_); }
0060 
0061     //! Number of elements in the state
0062     size_type size() const { return store_.size(); }
0063 
0064     //! Get a reference to the mutable state data
0065     Ref& ref() { return store_.ref(); }
0066 
0067     //! Get a reference to immutable state data
0068     Ref const& ref() const { return store_.ref(); }
0069 
0070   private:
0071     CollectionStateStore<S, M> store_;
0072 };
0073 
0074 //! \cond (CELERITAS_DOC_DEV)
0075 //---------------------------------------------------------------------------//
0076 /*!
0077  * Create an auxiliary state given a runtime memory space and host params.
0078  */
0079 template<template<Ownership, MemSpace> class S,
0080          template<Ownership, MemSpace> class P>
0081 std::unique_ptr<AuxStateInterface>
0082 make_aux_state(ParamsDataInterface<P> const& params,
0083                MemSpace m,
0084                StreamId stream_id,
0085                size_type size)
0086 {
0087     if (m == MemSpace::host)
0088     {
0089         using ASD = AuxStateData<S, MemSpace::host>;
0090         return std::make_unique<ASD>(params.host_ref(), stream_id, size);
0091     }
0092     else if (m == MemSpace::device)
0093     {
0094         using ASD = AuxStateData<S, MemSpace::device>;
0095         return std::make_unique<ASD>(params.host_ref(), stream_id, size);
0096     }
0097     CELER_ASSERT_UNREACHABLE();
0098 }
0099 
0100 //---------------------------------------------------------------------------//
0101 /*!
0102  * Create an auxiliary state given a runtime memory space.
0103  */
0104 template<template<Ownership, MemSpace> class S>
0105 std::unique_ptr<AuxStateInterface>
0106 make_aux_state(MemSpace m, StreamId stream_id, size_type size)
0107 {
0108     if (m == MemSpace::host)
0109     {
0110         using ASD = AuxStateData<S, MemSpace::host>;
0111         return std::make_unique<ASD>(stream_id, size);
0112     }
0113     else if (m == MemSpace::device)
0114     {
0115         using ASD = AuxStateData<S, MemSpace::device>;
0116         return std::make_unique<ASD>(stream_id, size);
0117     }
0118     CELER_ASSERT_UNREACHABLE();
0119 }
0120 //! \endcond
0121 
0122 //---------------------------------------------------------------------------//
0123 // INLINE DEFINITIONS
0124 //---------------------------------------------------------------------------//
0125 /*!
0126  * Construct by resizing and passing host params.
0127  */
0128 template<template<Ownership, MemSpace> class S, MemSpace M>
0129 template<template<Ownership, MemSpace> class P>
0130 AuxStateData<S, M>::AuxStateData(HostCRef<P> const& p,
0131                                  StreamId stream_id,
0132                                  size_type size)
0133     : store_{p, stream_id, size}
0134 {
0135 }
0136 
0137 //---------------------------------------------------------------------------//
0138 /*!
0139  * Construct by resizing.
0140  */
0141 template<template<Ownership, MemSpace> class S, MemSpace M>
0142 AuxStateData<S, M>::AuxStateData(StreamId stream_id, size_type size)
0143     : store_{stream_id, size}
0144 {
0145 }
0146 
0147 //---------------------------------------------------------------------------//
0148 }  // namespace celeritas