Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:03:41

0001 //----------------------------------*-C++-*----------------------------------//
0002 // Copyright 2021-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 corecel/data/CollectionMirror.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include <utility>
0011 
0012 #include "corecel/Assert.hh"
0013 #include "corecel/Types.hh"
0014 #include "corecel/sys/Device.hh"
0015 
0016 #include "ParamsDataInterface.hh"
0017 
0018 namespace celeritas
0019 {
0020 //---------------------------------------------------------------------------//
0021 /*!
0022  * Helper class for copying setup-time Collection groups to host and device.
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 `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  * 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  *     CollectionMirror<FooData> data_;
0047  * };
0048  * \endcode
0049  */
0050 template<template<Ownership, MemSpace> class P>
0051 class CollectionMirror : 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 in an "unassigned" state
0063     CollectionMirror() = default;
0064 
0065     // Construct from host data
0066     explicit inline CollectionMirror(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     HostRef const& host_ref() const final { return host_ref_; }
0073 
0074     //! Access data on device
0075     DeviceRef const& device_ref() const final { return device_ref_; }
0076 
0077     using ParamsDataInterface<P>::ref;
0078 
0079   private:
0080     HostValue host_;
0081     HostRef host_ref_;
0082     P<Ownership::value, MemSpace::device> device_;
0083     DeviceRef device_ref_;
0084 };
0085 
0086 //---------------------------------------------------------------------------//
0087 // INLINE DEFINITIONS
0088 //---------------------------------------------------------------------------//
0089 /*!
0090  * Construct with defaults.
0091  */
0092 template<template<Ownership, MemSpace> class P>
0093 CollectionMirror<P>::CollectionMirror(HostValue&& host)
0094     : host_(std::move(host))
0095 {
0096     CELER_EXPECT(host_);
0097     host_ref_ = host_;
0098     if (celeritas::device())
0099     {
0100         // Copy data to device and save reference
0101         device_ = host_;
0102         device_ref_ = device_;
0103     }
0104 }
0105 
0106 //---------------------------------------------------------------------------//
0107 }  // namespace celeritas