Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-14 08:50:55

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  * a templated params data class \c P, a stream ID, and a state size.
0029  */
0030 template<template<Ownership, MemSpace> class S, MemSpace M>
0031 class AuxStateData final : public AuxStateInterface
0032 {
0033   public:
0034     //!@{
0035     //! \name Type aliases
0036     using Ref = S<Ownership::reference, M>;
0037     //!@}
0038 
0039   public:
0040     // Construct by resizing and passing host params
0041     template<template<Ownership, MemSpace> class P>
0042     inline AuxStateData(HostCRef<P> const& p,
0043                         StreamId stream_id,
0044                         size_type size);
0045 
0046     //! Whether any data is being stored
0047     explicit operator bool() const { return static_cast<bool>(store_); }
0048 
0049     //! Number of elements in the state
0050     size_type size() const { return store_.size(); }
0051 
0052     //! Get a reference to the mutable state data
0053     Ref& ref() { return store_.ref(); }
0054 
0055     //! Get a reference to immutable state data
0056     Ref const& ref() const { return store_.ref(); }
0057 
0058   private:
0059     CollectionStateStore<S, M> store_;
0060 };
0061 
0062 //---------------------------------------------------------------------------//
0063 /*!
0064  * Create an auxiliary state given a runtime memory space.
0065  *
0066  * Example:
0067  * \code
0068     return make_aux_state<ParticleTallyStateData>(
0069         *this, memspace, stream, size);
0070  * \endcode
0071  */
0072 template<template<Ownership, MemSpace> class S, template<Ownership, MemSpace> class P>
0073 std::unique_ptr<AuxStateInterface>
0074 make_aux_state(ParamsDataInterface<P> const& params,
0075                MemSpace m,
0076                StreamId stream_id,
0077                size_type size)
0078 {
0079     if (m == MemSpace::host)
0080     {
0081         using ASD = AuxStateData<S, MemSpace::host>;
0082         return std::make_unique<ASD>(params.host_ref(), stream_id, size);
0083     }
0084     else if (m == MemSpace::device)
0085     {
0086         using ASD = AuxStateData<S, MemSpace::device>;
0087         return std::make_unique<ASD>(params.host_ref(), stream_id, size);
0088     }
0089     CELER_ASSERT_UNREACHABLE();
0090 }
0091 
0092 //---------------------------------------------------------------------------//
0093 // INLINE DEFINITIONS
0094 //---------------------------------------------------------------------------//
0095 /*!
0096  * Construct by resizing and passing host params.
0097  */
0098 template<template<Ownership, MemSpace> class S, MemSpace M>
0099 template<template<Ownership, MemSpace> class P>
0100 AuxStateData<S, M>::AuxStateData(HostCRef<P> const& p,
0101                                  StreamId stream_id,
0102                                  size_type size)
0103     : store_{p, stream_id, size}
0104 {
0105 }
0106 
0107 //---------------------------------------------------------------------------//
0108 }  // namespace celeritas