Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:03:45

0001 #ifndef CUDADataFormats_Common_interface_PortableDeviceCollection_h
0002 #define CUDADataFormats_Common_interface_PortableDeviceCollection_h
0003 
0004 #include <cassert>
0005 #include <cstdlib>
0006 
0007 #include "HeterogeneousCore/CUDAUtilities/interface/device_unique_ptr.h"
0008 
0009 namespace cms::cuda {
0010 
0011   // generic SoA-based product in device memory
0012   template <typename T>
0013   class PortableDeviceCollection {
0014   public:
0015     using Layout = T;
0016     using View = typename Layout::View;
0017     using ConstView = typename Layout::ConstView;
0018     using Buffer = cms::cuda::device::unique_ptr<std::byte[]>;
0019 
0020     PortableDeviceCollection() = default;
0021 
0022     PortableDeviceCollection(int32_t elements, cudaStream_t stream)
0023         : buffer_{cms::cuda::make_device_unique<std::byte[]>(Layout::computeDataSize(elements), stream)},
0024           layout_{buffer_.get(), elements},
0025           view_{layout_} {
0026       // CUDA device memory uses a default alignment of at least 128 bytes
0027       assert(reinterpret_cast<uintptr_t>(buffer_.get()) % Layout::alignment == 0);
0028     }
0029 
0030     // non-copyable
0031     PortableDeviceCollection(PortableDeviceCollection const&) = delete;
0032     PortableDeviceCollection& operator=(PortableDeviceCollection const&) = delete;
0033 
0034     // movable
0035     PortableDeviceCollection(PortableDeviceCollection&&) = default;
0036     PortableDeviceCollection& operator=(PortableDeviceCollection&&) = default;
0037 
0038     // default destructor
0039     ~PortableDeviceCollection() = default;
0040 
0041     // access the View
0042     View& view() { return view_; }
0043     ConstView const& view() const { return view_; }
0044     ConstView const& const_view() const { return view_; }
0045 
0046     View& operator*() { return view_; }
0047     ConstView const& operator*() const { return view_; }
0048 
0049     View* operator->() { return &view_; }
0050     ConstView const* operator->() const { return &view_; }
0051 
0052     // access the Buffer
0053     Buffer& buffer() { return buffer_; }
0054     Buffer const& buffer() const { return buffer_; }
0055     Buffer const& const_buffer() const { return buffer_; }
0056 
0057     size_t bufferSize() const { return layout_.metadata().byteSize(); }
0058 
0059   private:
0060     Buffer buffer_;  //!
0061     Layout layout_;  //
0062     View view_;      //!
0063   };
0064 
0065 }  // namespace cms::cuda
0066 
0067 #endif  // CUDADataFormats_Common_interface_PortableDeviceCollection_h