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 
54  Product* data() { return buffer_->data(); }
55  Product const* data() const { return buffer_->data(); }
56 
57  Product& operator*() { return *buffer_->data(); }
58  Product const& operator*() const { return *buffer_->data(); }
59 
60  Product* operator->() { return buffer_->data(); }
61  Product const* operator->() const { return buffer_->data(); }
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 private:
69  std::optional<Buffer> buffer_;
70 };
71 
72 #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
Product const * data() const
assert(be >=bs)
std::optional< Buffer > buffer_
ConstBuffer const_buffer() const
cms::alpakatools::const_device_buffer< TDev, Product > ConstBuffer
PortableDeviceObject & operator=(PortableDeviceObject const &)=delete
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