muda
extent.h
1 #pragma once
2 #include <cuda.h>
3 #include <muda/muda_def.h>
4 #include <cinttypes>
5 #undef min
6 #undef max
7 namespace muda
8 {
9 class Extent2D
10 {
11  size_t m_extent[2];
12 
13  public:
14  MUDA_GENERIC Extent2D() MUDA_NOEXCEPT : m_extent{~0ull, ~0ull} {}
15 
16  MUDA_GENERIC Extent2D(size_t height, size_t width) MUDA_NOEXCEPT
17  : m_extent{height, width}
18  {
19  }
20 
21  MUDA_GENERIC size_t height() const MUDA_NOEXCEPT { return m_extent[0]; }
22  MUDA_GENERIC size_t width() const MUDA_NOEXCEPT { return m_extent[1]; }
23 
24  template <typename T>
25  MUDA_GENERIC cudaExtent cuda_extent() const MUDA_NOEXCEPT
26  {
27  return cudaExtent{width() * sizeof(T), height(), 1};
28  }
29 
30  MUDA_GENERIC bool valid() const
31  {
32  return m_extent[0] != ~0ull && m_extent[1] != ~0ull;
33  }
34 
35  static MUDA_GENERIC Extent2D Zero() MUDA_NOEXCEPT { return Extent2D{0, 0}; }
36 };
37 
38 
39 class Extent3D
40 {
41  size_t m_extent[3];
42 
43  public:
44  MUDA_GENERIC Extent3D() MUDA_NOEXCEPT : m_extent{~0ull, ~0ull, ~0ull} {}
45  MUDA_GENERIC Extent3D(size_t depth, size_t height, size_t width) MUDA_NOEXCEPT
46  : m_extent{depth, height, width}
47  {
48  }
49 
50  MUDA_GENERIC size_t depth() const MUDA_NOEXCEPT { return m_extent[0]; }
51  MUDA_GENERIC size_t height() const MUDA_NOEXCEPT { return m_extent[1]; }
52  MUDA_GENERIC size_t width() const MUDA_NOEXCEPT { return m_extent[2]; }
53 
54  template <typename T>
55  MUDA_GENERIC cudaExtent cuda_extent() const MUDA_NOEXCEPT
56  {
57  return cudaExtent{width() * sizeof(T), height(), depth()};
58  }
59 
60  MUDA_GENERIC bool valid() const
61  {
62  return m_extent[0] != ~0ull && m_extent[1] != ~0ull && m_extent[2] != ~0ull;
63  }
64 
65  static MUDA_GENERIC Extent3D Zero() MUDA_NOEXCEPT
66  {
67  return Extent3D{0, 0, 0};
68  }
69 };
70 
71 class Offset2D
72 {
73  size_t m_offset[2];
74 
75  public:
76  MUDA_GENERIC Offset2D() MUDA_NOEXCEPT : m_offset{~0ull, ~0ull} {}
77 
78  static MUDA_GENERIC Offset2D Zero() MUDA_NOEXCEPT { return Offset2D{0, 0}; }
79 
80  MUDA_GENERIC Offset2D(size_t offset_in_height, size_t offset_in_width) MUDA_NOEXCEPT
81  : m_offset{offset_in_height, offset_in_width}
82  {
83  }
84 
85  MUDA_GENERIC size_t offset_in_height() const MUDA_NOEXCEPT
86  {
87  return m_offset[0];
88  }
89 
90  MUDA_GENERIC size_t offset_in_width() const MUDA_NOEXCEPT
91  {
92  return m_offset[1];
93  }
94 
95  template <typename T>
96  MUDA_GENERIC cudaPos cuda_pos() const MUDA_NOEXCEPT
97  {
98  return cudaPos{offset_in_width() * sizeof(T), offset_in_height(), 0};
99  }
100 };
101 
102 class Offset3D
103 {
104  size_t m_offset[3];
105 
106  public:
107  MUDA_GENERIC Offset3D() MUDA_NOEXCEPT : m_offset{~0ull, ~0ull, ~0ull} {}
108 
109  static MUDA_GENERIC Offset3D Zero() MUDA_NOEXCEPT
110  {
111  return Offset3D{0, 0, 0};
112  }
113 
114  MUDA_GENERIC Offset3D(size_t offset_in_depth, size_t offset_in_height, size_t offset_in_width) MUDA_NOEXCEPT
115  : m_offset{offset_in_depth, offset_in_height, offset_in_width}
116  {
117  }
118 
119  MUDA_GENERIC size_t offset_in_depth() const MUDA_NOEXCEPT
120  {
121  return m_offset[0];
122  }
123 
124  MUDA_GENERIC size_t offset_in_height() const MUDA_NOEXCEPT
125  {
126  return m_offset[1];
127  }
128 
129  MUDA_GENERIC size_t offset_in_width() const MUDA_NOEXCEPT
130  {
131  return m_offset[2];
132  }
133 
134  template <typename T>
135  MUDA_GENERIC cudaPos cuda_pos() const MUDA_NOEXCEPT
136  {
137  return cudaPos{offset_in_width() * sizeof(T), offset_in_height(), offset_in_depth()};
138  }
139 };
140 
141 MUDA_INLINE MUDA_GENERIC Extent2D as_extent(const Offset2D& offset) MUDA_NOEXCEPT
142 {
143  return Extent2D{offset.offset_in_height(), offset.offset_in_width()};
144 }
145 
146 MUDA_INLINE MUDA_GENERIC Extent3D as_extent(const Offset3D& offset) MUDA_NOEXCEPT
147 {
148  return Extent3D{offset.offset_in_depth(), offset.offset_in_height(), offset.offset_in_width()};
149 }
150 
151 MUDA_INLINE MUDA_GENERIC Offset2D as_offset(const Extent2D& extent) MUDA_NOEXCEPT
152 {
153  return Offset2D{extent.height(), extent.width()};
154 }
155 
156 MUDA_INLINE MUDA_GENERIC Offset3D as_offset(const Extent3D& extent) MUDA_NOEXCEPT
157 {
158  return Offset3D{extent.depth(), extent.height(), extent.width()};
159 }
160 
161 MUDA_INLINE MUDA_GENERIC Offset2D min(const Offset2D& lhs, const Offset2D& rhs)
162 {
163  return Offset2D{std::min(lhs.offset_in_height(), rhs.offset_in_height()),
164  std::min(lhs.offset_in_width(), rhs.offset_in_width())};
165 }
166 
167 MUDA_INLINE MUDA_GENERIC Offset3D min(const Offset3D& lhs, const Offset3D& rhs)
168 {
169  return Offset3D{std::min(lhs.offset_in_depth(), rhs.offset_in_depth()),
170  std::min(lhs.offset_in_height(), rhs.offset_in_height()),
171  std::min(lhs.offset_in_width(), rhs.offset_in_width())};
172 }
173 
174 MUDA_INLINE MUDA_GENERIC Extent2D max(const Extent2D& lhs, const Extent2D& rhs)
175 {
176  return Extent2D{std::max(lhs.height(), rhs.height()),
177  std::max(lhs.width(), rhs.width())};
178 }
179 
180 MUDA_INLINE MUDA_GENERIC Extent3D max(const Extent3D& lhs, const Extent3D& rhs)
181 {
182  return Extent3D{std::max(lhs.depth(), rhs.depth()),
183  std::max(lhs.height(), rhs.height()),
184  std::max(lhs.width(), rhs.width())};
185 }
186 
187 #define MUDA_DEFINE_COMPARISON_OPERATOR(op) \
188  MUDA_INLINE MUDA_GENERIC bool operator op(const Extent2D& lhs, const Extent2D& rhs) MUDA_NOEXCEPT \
189  { \
190  return (lhs.height() op rhs.height()) && (lhs.width() op rhs.width()); \
191  } \
192  MUDA_INLINE MUDA_GENERIC bool operator op(const Extent3D& lhs, const Extent3D& rhs) MUDA_NOEXCEPT \
193  { \
194  return (lhs.depth() op rhs.depth()) && (lhs.height() op rhs.height()) \
195  && (lhs.width() op rhs.width()); \
196  }
197 
198 MUDA_DEFINE_COMPARISON_OPERATOR(<=);
199 MUDA_DEFINE_COMPARISON_OPERATOR(<);
200 MUDA_DEFINE_COMPARISON_OPERATOR(==);
201 
202 #undef MUDA_DEFINE_COMPARISON_OPERATOR
203 
204 
205 #define MUDA_DEFINE_ARITHMATIC_OPERATOR(op) \
206  MUDA_INLINE MUDA_GENERIC Extent2D operator op(const Extent2D& lhs, \
207  const Extent2D& rhs) MUDA_NOEXCEPT \
208  { \
209  return Extent2D{lhs.height() op rhs.height(), lhs.width() op rhs.width()}; \
210  } \
211  MUDA_INLINE MUDA_GENERIC Extent3D operator op(const Extent3D& lhs, \
212  const Extent3D& rhs) MUDA_NOEXCEPT \
213  { \
214  return Extent3D{lhs.depth() op rhs.depth(), \
215  lhs.height() op rhs.height(), \
216  lhs.width() op rhs.width()}; \
217  } \
218  MUDA_INLINE MUDA_GENERIC Offset2D operator op(const Offset2D& lhs, \
219  const Offset2D& rhs) MUDA_NOEXCEPT \
220  { \
221  return Offset2D{lhs.offset_in_height() op rhs.offset_in_height(), \
222  lhs.offset_in_width() op rhs.offset_in_width()}; \
223  } \
224  MUDA_INLINE MUDA_GENERIC Offset3D operator op(const Offset3D& lhs, \
225  const Offset3D& rhs) MUDA_NOEXCEPT \
226  { \
227  return Offset3D{lhs.offset_in_depth() op rhs.offset_in_depth(), \
228  lhs.offset_in_height() op rhs.offset_in_height(), \
229  lhs.offset_in_width() op rhs.offset_in_width()}; \
230  }
231 
232 MUDA_DEFINE_ARITHMATIC_OPERATOR(+);
233 MUDA_DEFINE_ARITHMATIC_OPERATOR(-);
234 #undef MUDA_DEFINE_ARITHMATIC_OPERATOR
235 
236 
237 } // namespace muda
Definition: extent.h:39
Definition: extent.h:71
Definition: extent.h:9
Definition: assert.h:13
Definition: extent.h:102