|
| 1 | +#ifndef DataFormats_Portable_interface_PortableHostObject_h |
| 2 | +#define DataFormats_Portable_interface_PortableHostObject_h |
| 3 | + |
| 4 | +#include <cassert> |
| 5 | +#include <optional> |
| 6 | +#include <type_traits> |
| 7 | + |
| 8 | +#include <alpaka/alpaka.hpp> |
| 9 | + |
| 10 | +#include "HeterogeneousCore/AlpakaInterface/interface/config.h" |
| 11 | +#include "HeterogeneousCore/AlpakaInterface/interface/host.h" |
| 12 | +#include "HeterogeneousCore/AlpakaInterface/interface/memory.h" |
| 13 | + |
| 14 | +// generic object in host memory |
| 15 | +template <typename T> |
| 16 | +class PortableHostObject { |
| 17 | +public: |
| 18 | + using Product = T; |
| 19 | + using Buffer = cms::alpakatools::host_buffer<Product>; |
| 20 | + using ConstBuffer = cms::alpakatools::const_host_buffer<Product>; |
| 21 | + |
| 22 | + PortableHostObject() = default; |
| 23 | + |
| 24 | + PortableHostObject(alpaka_common::DevHost const& host) |
| 25 | + // allocate pageable host memory |
| 26 | + : buffer_{cms::alpakatools::make_host_buffer<Product>()}, product_{buffer_->data()} { |
| 27 | + assert(reinterpret_cast<uintptr_t>(product_) % alignof(Product) == 0); |
| 28 | + } |
| 29 | + |
| 30 | + template <typename TQueue, typename = std::enable_if_t<alpaka::isQueue<TQueue>>> |
| 31 | + PortableHostObject(TQueue const& queue) |
| 32 | + // allocate pinned host memory associated to the given work queue, accessible by the queue's device |
| 33 | + : buffer_{cms::alpakatools::make_host_buffer<Product>(queue)}, product_{buffer_->data()} { |
| 34 | + assert(reinterpret_cast<uintptr_t>(product_) % alignof(Product) == 0); |
| 35 | + } |
| 36 | + |
| 37 | + // non-copyable |
| 38 | + PortableHostObject(PortableHostObject const&) = delete; |
| 39 | + PortableHostObject& operator=(PortableHostObject const&) = delete; |
| 40 | + |
| 41 | + // movable |
| 42 | + PortableHostObject(PortableHostObject&&) = default; |
| 43 | + PortableHostObject& operator=(PortableHostObject&&) = default; |
| 44 | + |
| 45 | + // default destructor |
| 46 | + ~PortableHostObject() = default; |
| 47 | + |
| 48 | + // access the product |
| 49 | + Product& value() { return *product_; } |
| 50 | + Product const& value() const { return *product_; } |
| 51 | + |
| 52 | + Product* data() { return product_; } |
| 53 | + Product const* data() const { return product_; } |
| 54 | + |
| 55 | + Product& operator*() { return *product_; } |
| 56 | + Product const& operator*() const { return *product_; } |
| 57 | + |
| 58 | + Product* operator->() { return product_; } |
| 59 | + Product const* operator->() const { return product_; } |
| 60 | + |
| 61 | + // access the buffer |
| 62 | + Buffer buffer() { return *buffer_; } |
| 63 | + ConstBuffer buffer() const { return *buffer_; } |
| 64 | + ConstBuffer const_buffer() const { return *buffer_; } |
| 65 | + |
| 66 | + // part of the ROOT read streamer |
| 67 | + static void ROOTReadStreamer(PortableHostObject* newObj, Product& product) { |
| 68 | + // destroy the default-constructed object |
| 69 | + newObj->~PortableHostObject(); |
| 70 | + // use the global "host" object returned by cms::alpakatools::host() |
| 71 | + new (newObj) PortableHostObject(cms::alpakatools::host()); |
| 72 | + // copy the data from the on-file object to the new one |
| 73 | + std::memcpy(newObj->product_, &product, sizeof(Product)); |
| 74 | + } |
| 75 | + |
| 76 | +private: |
| 77 | + std::optional<Buffer> buffer_; //! |
| 78 | + Product* product_; |
| 79 | +}; |
| 80 | + |
| 81 | +#endif // DataFormats_Portable_interface_PortableHostObject_h |
0 commit comments