CMS 3D CMS Logo

PortableDeviceObject.h
Go to the documentation of this file.
1 #ifndef DataFormats_Portable_interface_PortableDeviceObject_h
2 #define DataFormats_Portable_interface_PortableDeviceObject_h
3 
4 #include <cassert>
5 #include <optional>
6 #include <type_traits>
7 
8 #include <alpaka/alpaka.hpp>
9 
12 
13 // generic object in device memory
14 template <typename T, typename TDev, typename = std::enable_if_t<alpaka::isDevice<TDev>>>
16  static_assert(not std::is_same_v<TDev, alpaka_common::DevHost>,
17  "Use PortableHostObject<T> instead of PortableDeviceObject<T, DevHost>");
18 
19 public:
20  using Product = T;
23 
24  PortableDeviceObject() = default;
25 
26  PortableDeviceObject(TDev const& device)
27  // allocate global device memory
28  : buffer_{cms::alpakatools::make_device_buffer<Product>(device)} {
29  assert(reinterpret_cast<uintptr_t>(buffer_->data()) % alignof(Product) == 0);
30  }
31 
32  template <typename TQueue, typename = std::enable_if_t<alpaka::isQueue<TQueue>>>
33  PortableDeviceObject(TQueue const& queue)
34  // allocate global device memory with queue-ordered semantic
35  : buffer_{cms::alpakatools::make_device_buffer<Product>(queue)} {
36  assert(reinterpret_cast<uintptr_t>(buffer_->data()) % alignof(Product) == 0);
37  }
38 
39  // non-copyable
42 
43  // movable
46 
47  // default destructor
48  ~PortableDeviceObject() = default;
49 
50  // access the product
51  Product& value() { return *buffer_->data(); }
52  Product const& value() const { return *buffer_->data(); }
53  Product const& const_value() const { return *buffer_->data(); }
54 
55  Product* data() { return buffer_->data(); }
56  Product const* data() const { return buffer_->data(); }
57  Product const* const_data() const { return buffer_->data(); }
58 
59  Product& operator*() { return *buffer_->data(); }
60  Product const& operator*() const { return *buffer_->data(); }
61 
62  Product* operator->() { return buffer_->data(); }
63  Product const* operator->() const { return buffer_->data(); }
64 
65  // access the buffer
66  Buffer buffer() { return *buffer_; }
67  ConstBuffer buffer() const { return *buffer_; }
68  ConstBuffer const_buffer() const { return *buffer_; }
69 
70  // erases the data in the Buffer by writing zeros (bytes containing '\0') to it
71  template <typename TQueue, typename = std::enable_if_t<alpaka::isQueue<TQueue>>>
72  void zeroInitialise(TQueue&& queue) {
73  alpaka::memset(std::forward<TQueue>(queue), *buffer_, 0x00);
74  }
75 
76 private:
77  std::optional<Buffer> buffer_;
78 };
79 
80 #endif // DataFormats_Portable_interface_PortableDeviceObject_h
Product const & operator*() const
PortableDeviceObject(TQueue const &queue)
~PortableDeviceObject()=default
Product const & value() const
ConstBuffer buffer() const
PortableDeviceObject()=default
PortableDeviceObject(TDev const &device)
cms::alpakatools::device_buffer< TDev, Product > Buffer
void zeroInitialise(TQueue &&queue)
Product const * data() const
assert(be >=bs)
std::optional< Buffer > buffer_
ConstBuffer const_buffer() const
cms::alpakatools::const_device_buffer< TDev, Product > ConstBuffer
Product const * const_data() const
PortableDeviceObject & operator=(PortableDeviceObject const &)=delete
Product const & const_value() const
long double T
typename detail::buffer_type< TDev, T >::type device_buffer
Definition: memory.h:177
alpaka::ViewConst< device_buffer< TDev, T > > const_device_buffer
Definition: memory.h:180
Product const * operator->() const