Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-11-11 05:36:29

0001 #ifndef DataFormats_Portable_interface_PortableHostCollection_h
0002 #define DataFormats_Portable_interface_PortableHostCollection_h
0003 
0004 #include <cassert>
0005 #include <optional>
0006 
0007 #include <alpaka/alpaka.hpp>
0008 
0009 #include "HeterogeneousCore/AlpakaInterface/interface/config.h"
0010 #include "HeterogeneousCore/AlpakaInterface/interface/host.h"
0011 #include "HeterogeneousCore/AlpakaInterface/interface/memory.h"
0012 
0013 // generic SoA-based product in host memory
0014 template <typename T>
0015 class PortableHostCollection {
0016 public:
0017   using Layout = T;
0018   using View = typename Layout::View;
0019   using ConstView = typename Layout::ConstView;
0020   using Buffer = cms::alpakatools::host_buffer<std::byte[]>;
0021   using ConstBuffer = cms::alpakatools::const_host_buffer<std::byte[]>;
0022 
0023   PortableHostCollection() = default;
0024 
0025   PortableHostCollection(int32_t elements, alpaka_common::DevHost const& host)
0026       // allocate pageable host memory
0027       : buffer_{cms::alpakatools::make_host_buffer<std::byte[]>(Layout::computeDataSize(elements))},
0028         layout_{buffer_->data(), elements},
0029         view_{layout_} {
0030     // Alpaka set to a default alignment of 128 bytes defining ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT=128
0031     assert(reinterpret_cast<uintptr_t>(buffer_->data()) % Layout::alignment == 0);
0032   }
0033 
0034   template <typename TQueue, typename = std::enable_if_t<alpaka::isQueue<TQueue>>>
0035   PortableHostCollection(int32_t elements, TQueue const& queue)
0036       // allocate pinned host memory associated to the given work queue, accessible by the queue's device
0037       : buffer_{cms::alpakatools::make_host_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   PortableHostCollection(PortableHostCollection const&) = delete;
0046   PortableHostCollection& operator=(PortableHostCollection const&) = delete;
0047 
0048   // movable
0049   PortableHostCollection(PortableHostCollection&&) = default;
0050   PortableHostCollection& operator=(PortableHostCollection&&) = default;
0051 
0052   // default destructor
0053   ~PortableHostCollection() = 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   // part of the ROOT read streamer
0072   static void ROOTReadStreamer(PortableHostCollection* newObj, Layout& layout) {
0073     // destroy the default-constructed collection
0074     newObj->~PortableHostCollection();
0075     // construct in-place a new collection, with the known size, using the global "host" object returned by cms::alpakatools::host()
0076     new (newObj) PortableHostCollection(layout.metadata().size(), cms::alpakatools::host());
0077     // copy the data from the on-file layout to the new collection
0078     newObj->layout_.ROOTReadStreamer(layout);
0079     // free the memory allocated by ROOT
0080     layout.ROOTStreamerCleaner();
0081   }
0082 
0083 private:
0084   std::optional<Buffer> buffer_;  //!
0085   Layout layout_;                 //
0086   View view_;                     //!
0087 };
0088 
0089 #endif  // DataFormats_Portable_interface_PortableHostCollection_h