muda
dense_2d.h
1 #pragma once
2 #include <muda/viewer/viewer_base.h>
3 
4 namespace muda
5 {
6 /*****************************************************************************
7  *
8  * Dense2D (2D array)
9  * indexing (x,y)
10  * 1) non-pitched: x * dim_y + y
11  * 2) pitched: x * pitch + y
12  *
13  * Note:
14  * 1) y moves faster than x, which is the same as C/C++ 2d array
15  * 2) as for CUDA Memory2D, x index into height, y index into width.
16  *****************************************************************************/
17 
18 template <bool IsConst, typename T>
19 class Dense2DBase : public ViewerBase<IsConst> // TODO
20 {
21  using Base = ViewerBase<IsConst>;
22 
23  MUDA_VIEWER_COMMON_NAME(Dense2DBase);
24 
25 
26  protected:
27  template <typename U>
28  using auto_const_t = typename Base::template auto_const_t<U>;
29 
30  auto_const_t<T>* m_data;
31  int2 m_offset;
32  int2 m_dim;
33  int m_pitch_bytes;
34 
35  public:
36  using value_type = T;
40 
41 
42  MUDA_GENERIC Dense2DBase() MUDA_NOEXCEPT : m_data(nullptr) {}
43 
44  MUDA_GENERIC Dense2DBase(auto_const_t<T>* p, const int2& offset, const int2& dim, int pitch_bytes) MUDA_NOEXCEPT
45  : m_data(p),
46  m_offset(offset),
47  m_dim(dim),
48  m_pitch_bytes(pitch_bytes)
49  {
50  }
51 
52  MUDA_GENERIC auto as_const() const MUDA_NOEXCEPT
53  {
54  return ConstViewer{m_data, m_offset, m_dim, m_pitch_bytes};
55  }
56 
57  MUDA_GENERIC operator ConstViewer() const MUDA_NOEXCEPT
58  {
59  return as_const();
60  }
61 
62 
63  MUDA_GENERIC auto_const_t<T>& operator()(int x, int y) MUDA_NOEXCEPT
64  {
65  check();
66  check_range(x, y);
67 
68  x += m_offset.x;
69  y += m_offset.y;
70  auto height_begin =
71  reinterpret_cast<auto_const_t<std::byte>*>(m_data) + x * m_pitch_bytes;
72  return *((auto_const_t<T>*)(height_begin) + y);
73  }
74 
75  MUDA_GENERIC auto_const_t<T>& operator()(const int2& xy) MUDA_NOEXCEPT
76  {
77  return operator()(xy.x, xy.y);
78  }
79 
80  MUDA_GENERIC auto_const_t<T>& flatten(int i)
81  {
82  if constexpr(DEBUG_VIEWER)
83  {
84  MUDA_KERNEL_ASSERT(i >= 0 && i < total_size(),
85  "Dense2D[%s:%s]: out of range, index=%d, total_size=%d",
86  this->name(),
87  this->kernel_name(),
88  i,
89  total_size());
90  }
91  auto x = i / m_dim.y;
92  auto y = i % m_dim.y;
93  return operator()(x, y);
94  }
95 
96  MUDA_GENERIC auto_const_t<T>* data() MUDA_NOEXCEPT { return m_data; }
97 
98 
99  MUDA_GENERIC const T& operator()(const int2& xy) const MUDA_NOEXCEPT
100  {
101  return remove_const(*this)(xy);
102  }
103 
104 
105  MUDA_GENERIC const T& operator()(int x, int y) const MUDA_NOEXCEPT
106  {
107  return remove_const(*this)(x, y);
108  }
109 
110  MUDA_GENERIC const T& flatten(int i) const
111  {
112  return remove_const(*this).flatten(i);
113  }
114 
115  MUDA_GENERIC const T* data() const MUDA_NOEXCEPT { return m_data; }
116 
117  MUDA_GENERIC auto total_size() const MUDA_NOEXCEPT
118  {
119  return m_dim.x * m_dim.y;
120  }
121 
122  MUDA_GENERIC auto area() const MUDA_NOEXCEPT { return total_size(); }
123 
124  MUDA_GENERIC auto dim() const MUDA_NOEXCEPT { return m_dim; }
125 
126  MUDA_GENERIC auto pitch_bytes() const MUDA_NOEXCEPT
127  {
128  return m_pitch_bytes;
129  }
130 
131  protected:
132  MUDA_INLINE MUDA_GENERIC void check_range(int x, int y) const MUDA_NOEXCEPT
133  {
134  if constexpr(DEBUG_VIEWER)
135  if(!(x >= 0 && x < m_dim.x && y >= 0 && y < m_dim.y))
136  {
137  MUDA_KERNEL_ERROR("Dense2D[%s:%s]: out of range, index=(%d,%d) dim=(%d,%d)",
138  this->name(),
139  this->kernel_name(),
140  x,
141  y,
142  m_dim.x,
143  m_dim.y);
144  }
145  }
146 
147  MUDA_INLINE MUDA_GENERIC void check() const MUDA_NOEXCEPT
148  {
149  if constexpr(DEBUG_VIEWER)
150  {
151  MUDA_KERNEL_ASSERT(m_data,
152  "Dense2D[%s:%s]: m_data is null",
153  this->name(),
154  this->kernel_name());
155  }
156  }
157 };
158 
159 template <typename T>
161 
162 template <typename T>
164 
165 
166 // viewer traits
167 template <typename T>
169 {
170  using type = CDense2D<T>;
171 };
172 
173 template <typename T>
175 {
176  using type = Dense2D<T>;
177 };
178 
179 // make functions
180 template <typename T>
181 MUDA_INLINE MUDA_GENERIC auto make_cdense_2d(const T* data, const int2& dim) MUDA_NOEXCEPT
182 {
183  return CDense2D<T>{data, make_int2(0, 0), dim, static_cast<int>(dim.y * sizeof(T))};
184 }
185 
186 template <typename T>
187 MUDA_INLINE MUDA_GENERIC auto make_dense_2d(T* data, const int2& dim) MUDA_NOEXCEPT
188 {
189  return Dense2D<T>{data, make_int2(0, 0), dim, static_cast<int>(dim.y * sizeof(T))};
190 }
191 
192 template <typename T>
193 MUDA_INLINE MUDA_GENERIC auto make_cdense_2d(const T* data, int dimx, int dimy) MUDA_NOEXCEPT
194 {
195  return make_cdense_2d(data, make_int2(dimx, dimy));
196 }
197 
198 template <typename T>
199 MUDA_INLINE MUDA_GENERIC auto make_dense_2d(T* data, int dimx, int dimy) MUDA_NOEXCEPT
200 {
201  return make_dense_2d(data, make_int2(dimx, dimy));
202 }
203 
204 
205 } // namespace muda
Definition: type_modifier.h:21
Definition: type_modifier.h:27
Definition: assert.h:13
Definition: dense_2d.h:19
Definition: viewer_base.h:21