Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-09-11 22:29:18

0001 #ifndef DataFormats_Portable_interface_PortableDeviceCollection_h
0002 #define DataFormats_Portable_interface_PortableDeviceCollection_h
0003 
0004 #include <cassert>
0005 #include <optional>
0006 #include <type_traits>
0007 
0008 #include "HeterogeneousCore/AlpakaInterface/interface/config.h"
0009 #include "HeterogeneousCore/AlpakaInterface/interface/memory.h"
0010 #include "HeterogeneousCore/AlpakaInterface/interface/traits.h"
0011 
0012 // generic SoA-based product in device memory
0013 template <typename T, typename TDev, typename = std::enable_if_t<cms::alpakatools::is_device_v<TDev>>>
0014 class PortableDeviceCollection {
0015   static_assert(not std::is_same_v<TDev, alpaka_common::DevHost>,
0016                 "Use PortableHostCollection<T> instead of PortableDeviceCollection<T, DevHost>");
0017 
0018 public:
0019   using Layout = T;
0020   using View = typename Layout::View;
0021   using ConstView = typename Layout::ConstView;
0022   using Buffer = cms::alpakatools::device_buffer<TDev, std::byte[]>;
0023   using ConstBuffer = cms::alpakatools::const_device_buffer<TDev, std::byte[]>;
0024 
0025   PortableDeviceCollection() = default;
0026 
0027   PortableDeviceCollection(int32_t elements, TDev const& device)
0028       : buffer_{cms::alpakatools::make_device_buffer<std::byte[]>(device, Layout::computeDataSize(elements))},
0029         layout_{buffer_->data(), elements},
0030         view_{layout_} {
0031     // Alpaka set to a default alignment of 128 bytes defining ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT=128
0032     assert(reinterpret_cast<uintptr_t>(buffer_->data()) % Layout::alignment == 0);
0033   }
0034 
0035   template <typename TQueue, typename = std::enable_if_t<cms::alpakatools::is_queue_v<TQueue>>>
0036   PortableDeviceCollection(int32_t elements, TQueue const& queue)
0037       : buffer_{cms::alpakatools::make_device_buffer<std::byte[]>(queue, Layout::computeDataSize(elements))},
0038         layout_{buffer_->data(), elements},
0039         view_{layout_} {
0040     // Alpaka set to a default alignment of 128 bytes defining ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT=128
0041     assert(reinterpret_cast<uintptr_t>(buffer_->data()) % Layout::alignment == 0);
0042   }
0043 
0044   // non-copyable
0045   PortableDeviceCollection(PortableDeviceCollection const&) = delete;
0046   PortableDeviceCollection& operator=(PortableDeviceCollection const&) = delete;
0047 
0048   // movable
0049   PortableDeviceCollection(PortableDeviceCollection&&) = default;
0050   PortableDeviceCollection& operator=(PortableDeviceCollection&&) = default;
0051 
0052   // default destructor
0053   ~PortableDeviceCollection() = default;
0054 
0055   // access the View
0056   View& view() { return view_; }
0057   ConstView const& view() const { return view_; }
0058   ConstView const& const_view() const { return view_; }
0059 
0060   View& operator*() { return view_; }
0061   ConstView const& operator*() const { return view_; }
0062 
0063   View* operator->() { return &view_; }
0064   ConstView const* operator->() const { return &view_; }
0065 
0066   // access the Buffer
0067   Buffer buffer() { return *buffer_; }
0068   ConstBuffer buffer() const { return *buffer_; }
0069   ConstBuffer const_buffer() const { return *buffer_; }
0070 
0071 private:
0072   std::optional<Buffer> buffer_;  //!
0073   Layout layout_;                 //
0074   View view_;                     //!
0075 };
0076 
0077 #endif  // DataFormats_Portable_interface_PortableDeviceCollection_h