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  Product const& const_value() const { return *product_; }
52 
53  Product* data() { return product_; }
54  Product const* data() const { return product_; }
55  Product const* const_data() const { return product_; }
56 
57  Product& operator*() { return *product_; }
58  Product const& operator*() const { return *product_; }
59 
60  Product* operator->() { return product_; }
61  Product const* operator->() const { return product_; }
62 
63  // access the buffer
64  Buffer buffer() { return *buffer_; }
65  ConstBuffer buffer() const { return *buffer_; }
66  ConstBuffer const_buffer() const { return *buffer_; }
67 
68  // erases the data in the Buffer by writing zeros (bytes containing '\0') to it
69  void zeroInitialise() {
70  std::memset(std::data(*buffer_), 0x00, alpaka::getExtentProduct(*buffer_) * sizeof(std::byte));
71  }
72 
73  template <typename TQueue, typename = std::enable_if_t<alpaka::isQueue<TQueue>>>
74  void zeroInitialise(TQueue&& queue) {
75  alpaka::memset(std::forward<TQueue>(queue), *buffer_, 0x00);
76  }
77 
78  // part of the ROOT read streamer
79  static void ROOTReadStreamer(PortableHostObject* newObj, Product& product) {
80  // destroy the default-constructed object
81  newObj->~PortableHostObject();
82  // use the global "host" object returned by cms::alpakatools::host()
84  // copy the data from the on-file object to the new one
85  std::memcpy(newObj->product_, &product, sizeof(Product));
86  }
87 
88 private:
89  std::optional<Buffer> buffer_;
91 };
92 
93 #endif // DataFormats_Portable_interface_PortableHostObject_h
Product const * const_data() const
ConstBuffer buffer() const
string host
Definition: query.py:115
Product const * operator->() const
std::optional< Buffer > buffer_
Product const & const_value() const
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:14
typename detail::buffer_type< DevHost, T >::type host_buffer
Definition: memory.h:57
ConstBuffer const_buffer() const
alpaka::ViewConst< host_buffer< T > > const_host_buffer
Definition: memory.h:60
Product const & operator*() const
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:80
PortableHostObject & operator=(PortableHostObject const &)=delete
void zeroInitialise(TQueue &&queue)
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)