muda
device_buffer_2d.h
1 #pragma once
2 #include <cuda.h>
3 #include <cuda_runtime.h>
4 #include <cuda_runtime_api.h>
5 #include <vector>
6 #include <muda/viewer/dense.h>
7 #include <muda/buffer/buffer_2d_view.h>
8 
9 namespace muda
10 {
11 template <typename T>
12 class DeviceBuffer2D
13 {
14  private:
15  friend class BufferLaunch;
16  friend class NDReshaper;
17  T* m_data = nullptr;
18  size_t m_pitch_bytes = 0;
19  Extent2D m_extent = Extent2D::Zero();
20  Extent2D m_capacity = Extent2D::Zero();
21 
22  public:
23  using value_type = T;
24 
25  DeviceBuffer2D(const Extent2D& n);
26  DeviceBuffer2D();
27 
28  DeviceBuffer2D(const DeviceBuffer2D<T>& other);
29  DeviceBuffer2D(DeviceBuffer2D&& other) MUDA_NOEXCEPT;
30  DeviceBuffer2D& operator=(const DeviceBuffer2D<T>& other);
31  DeviceBuffer2D& operator=(DeviceBuffer2D<T>&& other);
32 
33  DeviceBuffer2D(CBuffer2DView<T> other);
34  DeviceBuffer2D& operator=(CBuffer2DView<T> other);
35 
36  void copy_to(std::vector<T>& host) const;
37  void copy_from(const std::vector<T>& host);
38 
39  void resize(Extent2D new_extent);
40  void resize(Extent2D new_extent, const T& value);
41  void reserve(Extent2D new_capacity);
42  void clear();
43  void shrink_to_fit();
44  void fill(const T& v);
45 
46  Dense2D<T> viewer() MUDA_NOEXCEPT { return view().viewer(); }
47  CDense2D<T> cviewer() const MUDA_NOEXCEPT { return view().viewer(); }
48 
49  Buffer2DView<T> view(Offset2D offset, Extent2D extent = {}) MUDA_NOEXCEPT
50  {
51  return view().subview(offset, extent);
52  }
53  Buffer2DView<T> view() MUDA_NOEXCEPT
54  {
55  return Buffer2DView<T>{m_data, m_pitch_bytes, Offset2D::Zero(), m_extent};
56  }
57  operator Buffer2DView<T>() MUDA_NOEXCEPT { return view(); }
58 
59  CBuffer2DView<T> view(Offset2D offset, Extent2D extent = {}) const MUDA_NOEXCEPT
60  {
61  return view().subview(offset, extent);
62  }
63 
64  CBuffer2DView<T> view() const MUDA_NOEXCEPT
65  {
66  return CBuffer2DView<T>{m_data, m_pitch_bytes, Offset2D::Zero(), m_extent};
67  }
68  operator CBuffer2DView<T>() const MUDA_NOEXCEPT { return view(); }
69 
70  ~DeviceBuffer2D();
71 
72  auto extent() const MUDA_NOEXCEPT { return m_extent; }
73  auto capacity() const MUDA_NOEXCEPT { return m_capacity; }
74  auto pitch_bytes() const MUDA_NOEXCEPT { return m_pitch_bytes; }
75  auto total_size() const MUDA_NOEXCEPT
76  {
77  return m_extent.width() * m_extent.height();
78  }
79  T* data() MUDA_NOEXCEPT { return m_data; }
80  const T* data() const MUDA_NOEXCEPT { return m_data; }
81 };
82 } // namespace muda
83 
84 #include "details/device_buffer_2d.inl"
Definition: assert.h:13