device_array.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <kfusion/exports.hpp>
5 
6 #include <vector>
7 
8 namespace kfusion
9 {
10  namespace cuda
11  {
13 
19  template<class T>
21  {
22  public:
24  typedef T type;
25 
27  enum { elem_size = sizeof(T) };
28 
30  DeviceArray();
31 
35  DeviceArray(size_t size);
36 
41  DeviceArray(T *ptr, size_t size);
42 
44  DeviceArray(const DeviceArray& other);
45 
47  DeviceArray& operator = (const DeviceArray& other);
48 
52  void create(size_t size);
53 
55  void release();
56 
60  void copyTo(DeviceArray& other) const;
61 
66  void upload(const T *host_ptr, size_t size);
67 
71  void download(T *host_ptr) const;
72 
76  template<class A>
77  void upload(const std::vector<T, A>& data);
78 
82  template<typename A>
83  void download(std::vector<T, A>& data) const;
84 
88  void swap(DeviceArray& other_arg);
89 
91  T* ptr();
92 
94  const T* ptr() const;
95 
96  //using DeviceMemory::ptr;
97 
99  operator T*();
100 
102  operator const T*() const;
103 
105  size_t size() const;
106  };
107 
108 
110 
116  template<class T>
118  {
119  public:
121  typedef T type;
122 
124  enum { elem_size = sizeof(T) };
125 
127  DeviceArray2D();
128 
133  DeviceArray2D(int rows, int cols);
134 
141  DeviceArray2D(int rows, int cols, void *data, size_t stepBytes);
142 
144  DeviceArray2D(const DeviceArray2D& other);
145 
147  DeviceArray2D& operator = (const DeviceArray2D& other);
148 
153  void create(int rows, int cols);
154 
156  void release();
157 
161  void copyTo(DeviceArray2D& other) const;
162 
169  void upload(const void *host_ptr, size_t host_step, int rows, int cols);
170 
175  void download(void *host_ptr, size_t host_step) const;
176 
180  void swap(DeviceArray2D& other_arg);
181 
186  template<class A>
187  void upload(const std::vector<T, A>& data, int cols);
188 
193  template<class A>
194  void download(std::vector<T, A>& data, int& cols) const;
195 
199  T* ptr(int y = 0);
200 
204  const T* ptr(int y = 0) const;
205 
206  //using DeviceMemory2D::ptr;
207 
209  operator T*();
210 
212  operator const T*() const;
213 
215  int cols() const;
216 
218  int rows() const;
219 
221  size_t elem_step() const;
222  };
223  }
224 
225  namespace device
226  {
229  }
230 }
231 
233 
234 template<class T> inline kfusion::cuda::DeviceArray<T>::DeviceArray() {}
235 template<class T> inline kfusion::cuda::DeviceArray<T>::DeviceArray(size_t size) : DeviceMemory(size * elem_size) {}
236 template<class T> inline kfusion::cuda::DeviceArray<T>::DeviceArray(T *ptr, size_t size) : DeviceMemory(ptr, size * elem_size) {}
237 template<class T> inline kfusion::cuda::DeviceArray<T>::DeviceArray(const DeviceArray& other) : DeviceMemory(other) {}
239 { DeviceMemory::operator=(other); return *this; }
240 
241 template<class T> inline void kfusion::cuda::DeviceArray<T>::create(size_t size)
242 { DeviceMemory::create(size * elem_size); }
243 template<class T> inline void kfusion::cuda::DeviceArray<T>::release()
244 { DeviceMemory::release(); }
245 
246 template<class T> inline void kfusion::cuda::DeviceArray<T>::copyTo(DeviceArray& other) const
247 { DeviceMemory::copyTo(other); }
248 template<class T> inline void kfusion::cuda::DeviceArray<T>::upload(const T *host_ptr, size_t size)
249 { DeviceMemory::upload(host_ptr, size * elem_size); }
250 template<class T> inline void kfusion::cuda::DeviceArray<T>::download(T *host_ptr) const
251 { DeviceMemory::download( host_ptr ); }
252 
253 template<class T> void kfusion::cuda::DeviceArray<T>::swap(DeviceArray& other_arg) { DeviceMemory::swap(other_arg); }
254 
255 template<class T> inline kfusion::cuda::DeviceArray<T>::operator T*() { return ptr(); }
256 template<class T> inline kfusion::cuda::DeviceArray<T>::operator const T*() const { return ptr(); }
257 template<class T> inline size_t kfusion::cuda::DeviceArray<T>::size() const { return sizeBytes() / elem_size; }
258 
259 template<class T> inline T* kfusion::cuda::DeviceArray<T>::ptr() { return DeviceMemory::ptr<T>(); }
260 template<class T> inline const T* kfusion::cuda::DeviceArray<T>::ptr() const { return DeviceMemory::ptr<T>(); }
261 
262 template<class T> template<class A> inline void kfusion::cuda::DeviceArray<T>::upload(const std::vector<T, A>& data) { upload(&data[0], data.size()); }
263 template<class T> template<class A> inline void kfusion::cuda::DeviceArray<T>::download(std::vector<T, A>& data) const { data.resize(size()); if (!data.empty()) download(&data[0]); }
264 
266 
268 template<class T> inline kfusion::cuda::DeviceArray2D<T>::DeviceArray2D(int rows, int cols) : DeviceMemory2D(rows, cols * elem_size) {}
269 template<class T> inline kfusion::cuda::DeviceArray2D<T>::DeviceArray2D(int rows, int cols, void *data, size_t stepBytes) : DeviceMemory2D(rows, cols * elem_size, data, stepBytes) {}
270 template<class T> inline kfusion::cuda::DeviceArray2D<T>::DeviceArray2D(const DeviceArray2D& other) : DeviceMemory2D(other) {}
272 { DeviceMemory2D::operator=(other); return *this; }
273 
274 template<class T> inline void kfusion::cuda::DeviceArray2D<T>::create(int rows, int cols)
275 { DeviceMemory2D::create(rows, cols * elem_size); }
276 template<class T> inline void kfusion::cuda::DeviceArray2D<T>::release()
277 { DeviceMemory2D::release(); }
278 
279 template<class T> inline void kfusion::cuda::DeviceArray2D<T>::copyTo(DeviceArray2D& other) const
280 { DeviceMemory2D::copyTo(other); }
281 template<class T> inline void kfusion::cuda::DeviceArray2D<T>::upload(const void *host_ptr, size_t host_step, int rows, int cols)
282 { DeviceMemory2D::upload(host_ptr, host_step, rows, cols * elem_size); }
283 template<class T> inline void kfusion::cuda::DeviceArray2D<T>::download(void *host_ptr, size_t host_step) const
284 { DeviceMemory2D::download( host_ptr, host_step ); }
285 
286 template<class T> template<class A> inline void kfusion::cuda::DeviceArray2D<T>::upload(const std::vector<T, A>& data, int cols)
287 { upload(&data[0], cols * elem_size, data.size()/cols, cols); }
288 
289 template<class T> template<class A> inline void kfusion::cuda::DeviceArray2D<T>::download(std::vector<T, A>& data, int& elem_step) const
290 { elem_step = cols(); data.resize(cols() * rows()); if (!data.empty()) download(&data[0], colsBytes()); }
291 
292 template<class T> void kfusion::cuda::DeviceArray2D<T>::swap(DeviceArray2D& other_arg) { DeviceMemory2D::swap(other_arg); }
293 
294 template<class T> inline T* kfusion::cuda::DeviceArray2D<T>::ptr(int y) { return DeviceMemory2D::ptr<T>(y); }
295 template<class T> inline const T* kfusion::cuda::DeviceArray2D<T>::ptr(int y) const { return DeviceMemory2D::ptr<T>(y); }
296 
297 template<class T> inline kfusion::cuda::DeviceArray2D<T>::operator T*() { return ptr(); }
298 template<class T> inline kfusion::cuda::DeviceArray2D<T>::operator const T*() const { return ptr(); }
299 
300 template<class T> inline int kfusion::cuda::DeviceArray2D<T>::cols() const { return DeviceMemory2D::colsBytes()/elem_size; }
301 template<class T> inline int kfusion::cuda::DeviceArray2D<T>::rows() const { return DeviceMemory2D::rows(); }
302 
303 template<class T> inline size_t kfusion::cuda::DeviceArray2D<T>::elem_step() const { return DeviceMemory2D::step()/elem_size; }
kfusion::cuda::DeviceArray2D::download
void download(void *host_ptr, size_t host_step) const
Downloads data from internal buffer to CPU memory. User is resposible for correct host buffer size.
Definition: device_array.hpp:283
kfusion::cuda::DeviceArray2D::create
void create(int rows, int cols)
Allocates internal buffer in GPU memory. If internal buffer was created before the function recreates...
Definition: device_array.hpp:274
kfusion::cuda::DeviceArray::size
size_t size() const
Returns size in elements.
Definition: device_array.hpp:257
exports.hpp
kfusion::cuda::DeviceArray::download
void download(T *host_ptr) const
Downloads data from internal buffer to CPU memory.
Definition: device_array.hpp:250
kfusion::cuda::DeviceArray2D::DeviceArray2D
DeviceArray2D()
Empty constructor.
Definition: device_array.hpp:267
kfusion::cuda::DeviceArray2D::cols
int cols() const
Returns number of elements in each row.
Definition: device_array.hpp:300
kfusion::cuda::DeviceArray2D::upload
void upload(const void *host_ptr, size_t host_step, int rows, int cols)
Uploads data to internal buffer in GPU memory. It calls create() inside to ensure that intenal buffer...
Definition: device_array.hpp:281
device_memory.hpp
kfusion::cuda::DeviceArray2D::operator=
DeviceArray2D & operator=(const DeviceArray2D &other)
Assigment operator. Just increments reference counter.
Definition: device_array.hpp:271
kfusion::cuda::DeviceMemory
DeviceMemory class
Definition: device_memory.hpp:21
kfusion::cuda::DeviceArray::release
void release()
Decrements reference counter and releases internal buffer if needed.
Definition: device_array.hpp:243
kfusion::cuda::DeviceArray::copyTo
void copyTo(DeviceArray &other) const
Performs data copying. If destination size differs it will be reallocated.
Definition: device_array.hpp:246
kfusion::cuda::DeviceArray::type
T type
Element type.
Definition: device_array.hpp:24
kfusion::cuda::DeviceArray::create
void create(size_t size)
Allocates internal buffer in GPU memory. If internal buffer was created before the function recreates...
Definition: device_array.hpp:241
kfusion::cuda::DeviceArray2D::ptr
T * ptr(int y=0)
Returns pointer to given row in internal buffer.
Definition: device_array.hpp:294
kfusion
Utility.
Definition: capture.hpp:8
kfusion::cuda::DeviceArray2D::elem_step
size_t elem_step() const
Returns step in elements.
Definition: device_array.hpp:303
kfusion::cuda::DeviceArray2D::type
T type
Element type.
Definition: device_array.hpp:121
kfusion::cuda::DeviceArray
DeviceArray class
Definition: device_array.hpp:20
kfusion::cuda::DeviceArray::operator=
DeviceArray & operator=(const DeviceArray &other)
Assigment operator. Just increments reference counter.
Definition: device_array.hpp:238
kfusion::cuda::DeviceMemory2D
DeviceMemory2D class
Definition: device_memory.hpp:109
kfusion::cuda::DeviceArray::ptr
T * ptr()
Returns pointer for internal buffer in GPU memory.
Definition: device_array.hpp:259
kfusion::cuda::DeviceArray2D::rows
int rows() const
Returns number of rows.
Definition: device_array.hpp:301
kfusion::cuda::DeviceArray2D::swap
void swap(DeviceArray2D &other_arg)
Performs swap of data pointed with another device array.
Definition: device_array.hpp:292
kfusion::device::swap
__kf_hdevice__ void swap(T &a, T &b)
Definition: temp_utils.hpp:10
kfusion::cuda::DeviceArray::DeviceArray
DeviceArray()
Empty constructor.
Definition: device_array.hpp:234
kfusion::cuda::DeviceArray::upload
void upload(const T *host_ptr, size_t size)
Uploads data to internal buffer in GPU memory. It calls create() inside to ensure that intenal buffer...
Definition: device_array.hpp:248
kfusion::cuda::DeviceArray2D::copyTo
void copyTo(DeviceArray2D &other) const
Performs data copying. If destination size differs it will be reallocated.
Definition: device_array.hpp:279
kfusion::cuda::DeviceArray2D
DeviceArray2D class
Definition: device_array.hpp:117
scripts.create_png.step
step
Definition: create_png.py:42
KF_EXPORTS
#define KF_EXPORTS
Definition: exports.hpp:6
kfusion::cuda::DeviceArray::swap
void swap(DeviceArray &other_arg)
Performs swap of data pointed with another device array.
Definition: device_array.hpp:253
kfusion::cuda::DeviceArray2D::release
void release()
Decrements reference counter and releases internal buffer if needed.
Definition: device_array.hpp:276


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Wed Mar 2 2022 00:37:23