Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #ifndef DataFormats_Portable_interface_PortableHostCollection_h
0002 #define DataFormats_Portable_interface_PortableHostCollection_h
0003 
0004 #include <cassert>
0005 #include <optional>
0006 
0007 #include "HeterogeneousCore/AlpakaInterface/interface/config.h"
0008 #include "HeterogeneousCore/AlpakaInterface/interface/host.h"
0009 #include "HeterogeneousCore/AlpakaInterface/interface/memory.h"
0010 #include "HeterogeneousCore/AlpakaInterface/interface/traits.h"
0011 
0012 // generic SoA-based product in host memory
0013 template <typename T>
0014 class PortableHostCollection {
0015 public:
0016   using Layout = T;
0017   using View = typename Layout::View;
0018   using ConstView = typename Layout::ConstView;
0019   using Buffer = cms::alpakatools::host_buffer<std::byte[]>;
0020   using ConstBuffer = cms::alpakatools::const_host_buffer<std::byte[]>;
0021 
0022   PortableHostCollection() = default;
0023 
0024   PortableHostCollection(int32_t elements, alpaka_common::DevHost const& host)
0025       // allocate pageable host memory
0026       : buffer_{cms::alpakatools::make_host_buffer<std::byte[]>(Layout::computeDataSize(elements))},
0027         layout_{buffer_->data(), elements},
0028         view_{layout_} {
0029     // Alpaka set to a default alignment of 128 bytes defining ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT=128
0030     assert(reinterpret_cast<uintptr_t>(buffer_->data()) % Layout::alignment == 0);
0031   }
0032 
0033   template <typename TQueue, typename = std::enable_if_t<cms::alpakatools::is_queue_v<TQueue>>>
0034   PortableHostCollection(int32_t elements, TQueue const& queue)
0035       // allocate pinned host memory associated to the given work queue, accessible by the queue's device
0036       : buffer_{cms::alpakatools::make_host_buffer<std::byte[]>(queue, Layout::computeDataSize(elements))},
0037         layout_{buffer_->data(), elements},
0038         view_{layout_} {
0039     // Alpaka set to a default alignment of 128 bytes defining ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT=128
0040     assert(reinterpret_cast<uintptr_t>(buffer_->data()) % Layout::alignment == 0);
0041   }
0042 
0043   // non-copyable
0044   PortableHostCollection(PortableHostCollection const&) = delete;
0045   PortableHostCollection& operator=(PortableHostCollection const&) = delete;
0046 
0047   // movable
0048   PortableHostCollection(PortableHostCollection&&) = default;
0049   PortableHostCollection& operator=(PortableHostCollection&&) = default;
0050 
0051   // default destructor
0052   ~PortableHostCollection() = default;
0053 
0054   // access the View
0055   View& view() { return view_; }
0056   ConstView const& view() const { return view_; }
0057   ConstView const& const_view() const { return view_; }
0058 
0059   View& operator*() { return view_; }
0060   ConstView const& operator*() const { return view_; }
0061 
0062   View* operator->() { return &view_; }
0063   ConstView const* operator->() const { return &view_; }
0064 
0065   // access the Buffer
0066   Buffer buffer() { return *buffer_; }
0067   ConstBuffer buffer() const { return *buffer_; }
0068   ConstBuffer const_buffer() const { return *buffer_; }
0069 
0070   // part of the ROOT read streamer
0071   static void ROOTReadStreamer(PortableHostCollection* newObj, Layout const& layout) {
0072     newObj->~PortableHostCollection();
0073     // use the global "host" object returned by cms::alpakatools::host()
0074     new (newObj) PortableHostCollection(layout.metadata().size(), cms::alpakatools::host());
0075     newObj->layout_.ROOTReadStreamer(layout);
0076   }
0077 
0078 private:
0079   std::optional<Buffer> buffer_;  //!
0080   Layout layout_;                 //
0081   View view_;                     //!
0082 };
0083 
0084 #endif  // DataFormats_Portable_interface_PortableHostCollection_h