MUDA
Loading...
Searching...
No Matches
dense_2d.h
1#pragma once
2#include <muda/viewer/viewer_base.h>
3
4namespace 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
18template <bool IsConst, typename T>
19class Dense2DBase : public ViewerBase<IsConst> // TODO
20{
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. %s(%d)",
86 this->name(),
87 this->kernel_name(),
88 i,
89 total_size(),
90 this->kernel_file(),
91 this->kernel_line());
92 }
93 auto x = i / m_dim.y;
94 auto y = i % m_dim.y;
95 return operator()(x, y);
96 }
97
98 MUDA_GENERIC auto_const_t<T>* data() MUDA_NOEXCEPT { return m_data; }
99
100
101 MUDA_GENERIC const T& operator()(const int2& xy) const MUDA_NOEXCEPT
102 {
103 return remove_const(*this)(xy);
104 }
105
106
107 MUDA_GENERIC const T& operator()(int x, int y) const MUDA_NOEXCEPT
108 {
109 return remove_const(*this)(x, y);
110 }
111
112 MUDA_GENERIC const T& flatten(int i) const
113 {
114 return remove_const(*this).flatten(i);
115 }
116
117 MUDA_GENERIC const T* data() const MUDA_NOEXCEPT { return m_data; }
118
119 MUDA_GENERIC auto total_size() const MUDA_NOEXCEPT
120 {
121 return m_dim.x * m_dim.y;
122 }
123
124 MUDA_GENERIC auto area() const MUDA_NOEXCEPT { return total_size(); }
125
126 MUDA_GENERIC auto dim() const MUDA_NOEXCEPT { return m_dim; }
127
128 MUDA_GENERIC auto pitch_bytes() const MUDA_NOEXCEPT
129 {
130 return m_pitch_bytes;
131 }
132
133 protected:
134 MUDA_INLINE MUDA_GENERIC void check_range(int x, int y) const MUDA_NOEXCEPT
135 {
136 if constexpr(DEBUG_VIEWER)
137 if(!(x >= 0 && x < m_dim.x && y >= 0 && y < m_dim.y))
138 {
139 MUDA_KERNEL_ERROR("Dense2D[%s:%s]: out of range, index=(%d,%d) dim=(%d,%d). %s(%d)",
140 this->name(),
141 this->kernel_name(),
142 x,
143 y,
144 m_dim.x,
145 m_dim.y,
146 this->kernel_file(),
147 this->kernel_line());
148 }
149 }
150
151 MUDA_INLINE MUDA_GENERIC void check() const MUDA_NOEXCEPT
152 {
153 if constexpr(DEBUG_VIEWER)
154 {
155 MUDA_KERNEL_ASSERT(m_data,
156 "Dense2D[%s:%s]: m_data is null. %s(%d)",
157 this->name(),
158 this->kernel_name(),
159 this->kernel_file(),
160 this->kernel_line());
161 }
162 }
163};
164
165template <typename T>
167
168template <typename T>
170
171
172// viewer traits
173template <typename T>
175{
176 using type = CDense2D<T>;
177};
178
179template <typename T>
181{
182 using type = Dense2D<T>;
183};
184
185// make functions
186template <typename T>
187MUDA_INLINE MUDA_GENERIC auto make_cdense_2d(const T* data, const int2& dim) MUDA_NOEXCEPT
188{
189 return CDense2D<T>{data, make_int2(0, 0), dim, static_cast<int>(dim.y * sizeof(T))};
190}
191
192template <typename T>
193MUDA_INLINE MUDA_GENERIC auto make_dense_2d(T* data, const int2& dim) MUDA_NOEXCEPT
194{
195 return Dense2D<T>{data, make_int2(0, 0), dim, static_cast<int>(dim.y * sizeof(T))};
196}
197
198template <typename T>
199MUDA_INLINE MUDA_GENERIC auto make_cdense_2d(const T* data, int dimx, int dimy) MUDA_NOEXCEPT
200{
201 return make_cdense_2d(data, make_int2(dimx, dimy));
202}
203
204template <typename T>
205MUDA_INLINE MUDA_GENERIC auto make_dense_2d(T* data, int dimx, int dimy) MUDA_NOEXCEPT
206{
207 return make_dense_2d(data, make_int2(dimx, dimy));
208}
209
210
211} // namespace muda
Definition dense_2d.h:20
Definition viewer_base.h:18
Definition type_modifier.h:22
Definition type_modifier.h:28