CMS 3D CMS Logo

PortableHostObject.h
Go to the documentation of this file.
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 
13 
14 // generic object in host memory
15 template <typename T>
17 public:
18  using Product = T;
21 
22  PortableHostObject() = default;
23 
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;
40 
41  // movable
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()
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_;
79 };
80 
81 #endif // DataFormats_Portable_interface_PortableHostObject_h
ConstBuffer buffer() const
string host
Definition: query.py:115
Product const * operator->() const
std::optional< Buffer > buffer_
cms::alpakatools::const_host_buffer< Product > ConstBuffer
assert(be >=bs)
static void ROOTReadStreamer(PortableHostObject *newObj, Product &product)
Product const & value() const
PortableHostObject()=default
cms::alpakatools::host_buffer< Product > Buffer
alpaka::DevCpu const & host()
Definition: host.h:26
typename detail::buffer_type< DevHost, T >::type host_buffer
Definition: memory.h:56
ConstBuffer const_buffer() const
alpaka::ViewConst< host_buffer< T > > const_host_buffer
Definition: memory.h:59
Product const & operator*() const
PortableHostObject & operator=(PortableHostObject const &)=delete
PortableHostObject(TQueue const &queue)
alpaka::DevCpu DevHost
Definition: config.h:37
long double T
Product const * data() const
~PortableHostObject()=default
PortableHostObject(alpaka_common::DevHost const &host)