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; }
void create(size_t size)
Allocates internal buffer in GPU memory. If internal buffer was created before the function recreates...
size_t size() const
Returns size in elements.
void copyTo(DeviceArray &other) const
Performs data copying. If destination size differs it will be reallocated.
T * ptr(int y=0)
Returns pointer to given row in internal buffer.
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...
__kf_hdevice__ void swap(T &a, T &b)
Definition: temp_utils.hpp:10
int rows() const
Returns number of rows.
DeviceArray2D & operator=(const DeviceArray2D &other)
Assigment operator. Just increments reference counter.
DeviceArray2D()
Empty constructor.
DeviceArray & operator=(const DeviceArray &other)
Assigment operator. Just increments reference counter.
T * ptr()
Returns pointer for internal buffer in GPU memory.
size_t elem_step() const
Returns step in elements.
DeviceArray2D class
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...
#define KF_EXPORTS
Definition: exports.hpp:6
DeviceArray class
DeviceArray()
Empty constructor.
void download(T *host_ptr) const
Downloads data from internal buffer to CPU memory.
void release()
Decrements reference counter and releases internal buffer if needed.
void swap(DeviceArray2D &other_arg)
Performs swap of data pointed with another device array.
void swap(DeviceArray &other_arg)
Performs swap of data pointed with another device array.
Utility.
Definition: capture.hpp:8
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...
void create(int rows, int cols)
Allocates internal buffer in GPU memory. If internal buffer was created before the function recreates...
void release()
Decrements reference counter and releases internal buffer if needed.
int cols() const
Returns number of elements in each row.
void copyTo(DeviceArray2D &other) const
Performs data copying. If destination size differs it will be reallocated.


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 Mon Feb 28 2022 22:46:06