muda
buffer_2d_view.h
1 #pragma once
2 #include <cuda.h>
3 #include <cuda_runtime.h>
4 #include <cuda_runtime_api.h>
5 #include <cinttypes>
6 #include <muda/type_traits/type_modifier.h>
7 #include <muda/viewer/dense/dense_2d.h>
8 #include <muda/tools/extent.h>
9 #include <muda/buffer/buffer_info_accessor.h>
10 #include <muda/view/view_base.h>
11 namespace muda
12 {
13 template <bool IsConst, typename T>
14 class Buffer2DViewBase : public ViewBase<IsConst>
15 {
16  using Base = ViewBase<IsConst>;
17  public:
18  static_assert(!std::is_const_v<T>, "Ty must be non-const");
23 
24  using CViewer = CDense2D<T>;
25  using Viewer = Dense2D<T>;
26  using ThisViewer = std::conditional_t<IsConst, CViewer, Viewer>;
27 
28  private:
29  template <typename U>
30  using auto_const_t = typename Base::template auto_const_t<T>;
31  friend class BufferLaunch;
32  friend class Buffer2DViewBase<!IsConst, T>;
33  friend class details::buffer::BufferInfoAccessor<ThisView>;
34 
35  protected:
36  auto_const_t<T>* m_data = nullptr;
37  size_t m_pitch_bytes = ~0;
38  size_t m_origin_width = 0;
39  size_t m_origin_height = 0;
40  Offset2D m_offset;
41  Extent2D m_extent;
42 
43  public:
44  MUDA_GENERIC Buffer2DViewBase() MUDA_NOEXCEPT {}
45 
46  MUDA_GENERIC Buffer2DViewBase(auto_const_t<T>* data,
47  size_t pitch_bytes,
48  size_t origin_width,
49  size_t origin_height,
50  const Offset2D& offset,
51  const Extent2D& extent) MUDA_NOEXCEPT
52  : m_data(data),
53  m_pitch_bytes(pitch_bytes),
54  m_origin_width(origin_width),
55  m_origin_height(origin_height),
56  m_offset(offset),
57  m_extent(extent)
58  {
59  }
60 
61  MUDA_GENERIC Buffer2DViewBase(auto_const_t<T>* data,
62  size_t pitch_bytes,
63  const Offset2D& offset,
64  const Extent2D& extent) MUDA_NOEXCEPT
65  : Buffer2DViewBase(data, pitch_bytes, extent.width(), extent.height(), offset, extent)
66  {
67  }
68 
69  // implicit conversion
70 
71  ConstView as_const() const MUDA_NOEXCEPT
72  {
73  return ConstView{m_data, m_pitch_bytes, m_origin_width, m_origin_height, m_offset, m_extent};
74  }
75 
76  operator ConstView() const MUDA_NOEXCEPT { return as_const(); }
77 
78  // non-const accessor
79 
80  MUDA_GENERIC auto_const_t<T>* data(size_t x, size_t y) MUDA_NOEXCEPT;
81  MUDA_GENERIC auto_const_t<T>* data(size_t flatten_i) MUDA_NOEXCEPT;
82  MUDA_GENERIC auto_const_t<T>* origin_data() MUDA_NOEXCEPT { return m_data; }
83  MUDA_GENERIC ThisView subview(Offset2D offset, Extent2D extent = {}) MUDA_NOEXCEPT;
84  MUDA_GENERIC ThisViewer viewer() MUDA_NOEXCEPT;
85 
86  // const accessor
87 
88  MUDA_GENERIC auto extent() const MUDA_NOEXCEPT { return m_extent; }
89  MUDA_GENERIC size_t pitch_bytes() const MUDA_NOEXCEPT
90  {
91  return m_pitch_bytes;
92  }
93  MUDA_GENERIC auto data(size_t x, size_t y) const MUDA_NOEXCEPT
94  {
95  return remove_const(*this).data(x, y);
96  }
97  MUDA_GENERIC auto data(size_t flatten_i) const MUDA_NOEXCEPT
98  {
99  return remove_const(*this).data(flatten_i);
100  }
101  MUDA_GENERIC auto origin_data() const MUDA_NOEXCEPT { return m_data; }
102  MUDA_GENERIC auto offset() const MUDA_NOEXCEPT { return m_offset; }
103  MUDA_GENERIC auto total_size() const MUDA_NOEXCEPT
104  {
105  return m_extent.width() * m_extent.height();
106  }
107 
108  MUDA_GENERIC ConstView subview(Offset2D offset, Extent2D extent = {}) const MUDA_NOEXCEPT;
109  MUDA_GENERIC CViewer cviewer() const MUDA_NOEXCEPT;
110 
111  MUDA_GENERIC cudaPitchedPtr cuda_pitched_ptr() const MUDA_NOEXCEPT;
112 };
113 
114 template <typename T>
115 class CBuffer2DView : public Buffer2DViewBase<true, T>
116 {
118 
119  public:
120  using Base::Base;
121 
122  MUDA_GENERIC CBuffer2DView(const Base& base) MUDA_NOEXCEPT : Base(base) {}
123 
124  MUDA_GENERIC CBuffer2DView<T> subview(Offset2D offset, Extent2D extent = {}) const MUDA_NOEXCEPT
125  {
126  return CBuffer2DView<T>{Base::subview(offset, extent)};
127  }
128 
129  MUDA_HOST void copy_to(T* host) const;
130 
131  MUDA_GENERIC auto as_const() const MUDA_NOEXCEPT { return *this; }
132 };
133 
134 template <typename T>
135 class Buffer2DView : public Buffer2DViewBase<false, T>
136 {
138 
139  public:
140  using Base::Base;
141 
142  MUDA_GENERIC Buffer2DView(const Base& base)
143  : Base(base)
144  {
145  }
146 
147  MUDA_GENERIC Buffer2DView(const CBuffer2DView<T>&) = delete;
148 
149  MUDA_GENERIC CBuffer2DView<T> as_const() const MUDA_NOEXCEPT
150  {
151  return CBuffer2DView<T>{Base::as_const()};
152  }
153 
154  MUDA_GENERIC operator CBuffer2DView<T>() const MUDA_NOEXCEPT
155  {
156  return as_const();
157  }
158 
159  MUDA_GENERIC Buffer2DView<T> subview(Offset2D offset, Extent2D extent = {}) MUDA_NOEXCEPT
160  {
161  return Buffer2DView<T>{Base::subview(offset, extent)};
162  }
163 
164  MUDA_GENERIC CBuffer2DView<T> subview(Offset2D offset, Extent2D extent = {}) const MUDA_NOEXCEPT
165  {
166  return CBuffer2DView<T>{Base::subview(offset, extent)};
167  }
168 
169  MUDA_HOST void fill(const T& v);
170  MUDA_HOST void copy_from(CBuffer2DView<T> other);
171  MUDA_HOST void copy_from(const T* host);
172  MUDA_HOST void copy_to(T* host) const
173  {
174  return CBuffer2DView<T>{*this}.copy_to(host);
175  }
176 };
177 
178 template <typename T>
180 {
181  using type = CBuffer2DView<T>;
182 };
183 
184 template <typename T>
186 {
187  using type = Buffer2DView<T>;
188 };
189 } // namespace muda
190 
191 #include "details/buffer_2d_view.inl"
Definition: view_base.h:7
Definition: type_modifier.h:21
Definition: type_modifier.h:27
Definition: buffer_info_accessor.h:6
Definition: extent.h:71
Definition: buffer_2d_view.h:14
Definition: extent.h:9
Definition: assert.h:13
Definition: dense_2d.h:19
Definition: buffer_2d_view.h:115
Definition: buffer_launch.h:36
Definition: buffer_2d_view.h:135