Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-10-26 09:34:34

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/ParamsDataStore.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <utility>
0010 
0011 #include "corecel/Assert.hh"
0012 #include "corecel/Types.hh"
0013 #include "corecel/sys/Device.hh"
0014 
0015 #include "ParamsDataInterface.hh"
0016 
0017 namespace celeritas
0018 {
0019 //---------------------------------------------------------------------------//
0020 /*!
0021  * Store and reference persistent collection groups on host and device.
0022  * \tparam P Params data collection group
0023  *
0024  * This should generally be an implementation detail of Params classes, which
0025  * are constructed on host and must have the same data both on host and device.
0026  * The template \c P must be a `FooData` class that:
0027  * - Is templated on ownership and memory space
0028  * - Has a templated assignment operator to copy from one space to another
0029  * - Has a boolean operator returning whether it's in a valid state.
0030  *
0031  * On assignment, it will copy the data to the device if the GPU is enabled.
0032  *
0033  * \par Example:
0034  * \code
0035  * class FooParams
0036  * {
0037  *   public:
0038  *     using CollectionDeviceRef = FooData<Ownership::const_reference,
0039  *                                         MemSpace::device>;
0040  *
0041  *     const CollectionDeviceRef& device_ref() const
0042  *     {
0043  *         return data_.device_ref();
0044  *     }
0045  *   private:
0046  *     ParamsDataStore<FooData> data_;
0047  * };
0048  * \endcode
0049  */
0050 template<template<Ownership, MemSpace> class P>
0051 class ParamsDataStore final : public ParamsDataInterface<P>
0052 {
0053   public:
0054     //!@{
0055     //! \name Type aliases
0056     using HostValue = celeritas::HostVal<P>;
0057     using HostRef = celeritas::HostCRef<P>;
0058     using DeviceRef = celeritas::DeviceCRef<P>;
0059     //!@}
0060 
0061   public:
0062     //! Default constructor leaves the class in an "unassigned" state
0063     ParamsDataStore() = default;
0064 
0065     // Construct from host data
0066     explicit inline ParamsDataStore(HostValue&& host);
0067 
0068     //! Whether the data is assigned
0069     explicit operator bool() const { return static_cast<bool>(host_); }
0070 
0071     //! Access data on host
0072     CELER_FORCEINLINE HostRef const& host_ref() const final
0073     {
0074         return host_ref_;
0075     }
0076 
0077     //! Access data on device, if the device is enabled
0078     CELER_FORCEINLINE DeviceRef const& device_ref() const final
0079     {
0080         return device_ref_;
0081     }
0082 
0083     using ParamsDataInterface<P>::ref;
0084 
0085   private:
0086     HostValue host_;
0087     HostRef host_ref_;
0088     P<Ownership::value, MemSpace::device> device_;
0089     DeviceRef device_ref_;
0090 };
0091 
0092 //---------------------------------------------------------------------------//
0093 // INLINE DEFINITIONS
0094 //---------------------------------------------------------------------------//
0095 /*!
0096  * Construct by capturing host data.
0097  */
0098 template<template<Ownership, MemSpace> class P>
0099 ParamsDataStore<P>::ParamsDataStore(HostValue&& host) : host_(std::move(host))
0100 {
0101     if (CELER_UNLIKELY(!host_))
0102     {
0103         CELER_DEBUG_FAIL("incomplete host data or bad copy", precondition);
0104     }
0105 
0106     host_ref_ = host_;
0107 
0108     if (celeritas::device())
0109     {
0110         if constexpr (!CELER_USE_DEVICE)
0111         {
0112             // Mark unreachable for optimization and coverage
0113             CELER_ASSERT_UNREACHABLE();
0114         }
0115 
0116         // Copy data to device and save reference
0117         device_ = host_;
0118         device_ref_ = device_;
0119     }
0120 }
0121 
0122 //---------------------------------------------------------------------------//
0123 }  // namespace celeritas