Point Cloud Library (PCL)  1.14.0-dev
device_array.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage, Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  * Author: Anatoly Baskeheev, Itseez Ltd, (myname.mysurname@mycompany.com)
35  */
36 
37 #ifndef PCL_GPU_CONTAINER_DEVICE_ARRAY_IMPL_HPP_
38 #define PCL_GPU_CONTAINER_DEVICE_ARRAY_IMPL_HPP_
39 
40 namespace pcl {
41 
42 namespace gpu {
43 
44 //////////////////// Inline implementations of DeviceArray //////////////////
45 
46 template <class T>
48 {}
49 
50 template <class T>
51 inline DeviceArray<T>::DeviceArray(std::size_t size) : DeviceMemory(size * elem_size)
52 {}
53 
54 template <class T>
55 inline DeviceArray<T>::DeviceArray(T* ptr, std::size_t size)
56 : DeviceMemory(ptr, size * elem_size)
57 {}
58 
59 template <class T>
61 {}
62 
63 template <class T>
64 inline DeviceArray<T>&
66 {
68  return *this;
69 }
70 
71 template <class T>
72 inline void
73 DeviceArray<T>::create(std::size_t size)
74 {
75  DeviceMemory::create(size * elem_size);
76 }
77 
78 template <class T>
79 inline void
81 {
83 }
84 
85 template <class T>
86 inline void
88 {
89  DeviceMemory::copyTo(other);
90 }
91 
92 template <class T>
93 inline void
94 DeviceArray<T>::upload(const T* host_ptr, std::size_t size)
95 {
96  DeviceMemory::upload(host_ptr, size * elem_size);
97 }
98 
99 template <class T>
100 inline bool
101 DeviceArray<T>::upload(const T* host_ptr,
102  std::size_t device_begin_offset,
103  std::size_t num_elements)
104 {
105  std::size_t begin_byte_offset = device_begin_offset * sizeof(T);
106  std::size_t num_bytes = num_elements * sizeof(T);
107  return DeviceMemory::upload(host_ptr, begin_byte_offset, num_bytes);
108 }
109 
110 template <class T>
111 inline void
112 DeviceArray<T>::download(T* host_ptr) const
113 {
114  DeviceMemory::download(host_ptr);
115 }
116 
117 template <class T>
118 inline bool
120  std::size_t device_begin_offset,
121  std::size_t num_elements) const
122 {
123  std::size_t begin_byte_offset = device_begin_offset * sizeof(T);
124  std::size_t num_bytes = num_elements * sizeof(T);
125  return DeviceMemory::download(host_ptr, begin_byte_offset, num_bytes);
126 }
127 
128 template <class T>
129 void
131 {
132  DeviceMemory::swap(other_arg);
133 }
134 
135 template <class T>
137 {
138  return ptr();
139 }
140 
141 template <class T>
142 inline DeviceArray<T>::operator const T*() const
143 {
144  return ptr();
145 }
146 
147 template <class T>
148 inline std::size_t
150 {
151  return sizeBytes() / elem_size;
152 }
153 
154 template <class T>
155 inline T*
157 {
158  return DeviceMemory::ptr<T>();
159 }
160 
161 template <class T>
162 inline const T*
164 {
165  return DeviceMemory::ptr<T>();
166 }
167 
168 template <class T>
169 template <class A>
170 inline void
171 DeviceArray<T>::upload(const std::vector<T, A>& data)
172 {
173  upload(&data[0], data.size());
174 }
175 
176 template <class T>
177 template <class A>
178 inline void
179 DeviceArray<T>::download(std::vector<T, A>& data) const
180 {
181  data.resize(size());
182  if (!data.empty())
183  download(&data[0]);
184 }
185 
186 /////////////////// Inline implementations of DeviceArray2D //////////////////
187 
188 template <class T>
190 {}
191 
192 template <class T>
193 inline DeviceArray2D<T>::DeviceArray2D(int rows, int cols)
194 : DeviceMemory2D(rows, cols * elem_size)
195 {}
196 
197 template <class T>
199  int cols,
200  void* data,
201  std::size_t stepBytes)
202 : DeviceMemory2D(rows, cols * elem_size, data, stepBytes)
203 {}
204 
205 template <class T>
207 : DeviceMemory2D(other)
208 {}
209 
210 template <class T>
211 inline DeviceArray2D<T>&
213 {
215  return *this;
216 }
217 
218 template <class T>
219 inline void
220 DeviceArray2D<T>::create(int rows, int cols)
221 {
222  DeviceMemory2D::create(rows, cols * elem_size);
223 }
224 
225 template <class T>
226 inline void
228 {
230 }
231 
232 template <class T>
233 inline void
235 {
236  DeviceMemory2D::copyTo(other);
237 }
238 
239 template <class T>
240 inline void
241 DeviceArray2D<T>::upload(const void* host_ptr,
242  std::size_t host_step,
243  int rows,
244  int cols)
245 {
246  DeviceMemory2D::upload(host_ptr, host_step, rows, cols * elem_size);
247 }
248 
249 template <class T>
250 inline void
251 DeviceArray2D<T>::download(void* host_ptr, std::size_t host_step) const
252 {
253  DeviceMemory2D::download(host_ptr, host_step);
254 }
255 
256 template <class T>
257 template <class A>
258 inline void
259 DeviceArray2D<T>::upload(const std::vector<T, A>& data, int cols)
260 {
261  upload(&data[0], cols * elem_size, data.size() / cols, cols);
262 }
263 
264 template <class T>
265 template <class A>
266 inline void
267 DeviceArray2D<T>::download(std::vector<T, A>& data, int& elem_step) const
268 {
269  elem_step = cols();
270  data.resize(cols() * rows());
271  if (!data.empty())
272  download(&data[0], colsBytes());
273 }
274 
275 template <class T>
276 void
278 {
280 }
281 
282 template <class T>
283 inline T*
285 {
286  return DeviceMemory2D::ptr<T>(y);
287 }
288 
289 template <class T>
290 inline const T*
292 {
293  return DeviceMemory2D::ptr<T>(y);
294 }
295 
296 template <class T>
298 {
299  return ptr();
300 }
301 
302 template <class T>
303 inline DeviceArray2D<T>::operator const T*() const
304 {
305  return ptr();
306 }
307 
308 template <class T>
309 inline int
311 {
312  return DeviceMemory2D::colsBytes() / elem_size;
313 }
314 
315 template <class T>
316 inline int
318 {
319  return DeviceMemory2D::rows();
320 }
321 
322 template <class T>
323 inline std::size_t
325 {
326  return DeviceMemory2D::step() / elem_size;
327 }
328 
329 } // namespace gpu
330 } // namespace pcl
331 
332 #endif /* PCL_GPU_CONTAINER_DEVICE_ARRAY_IMPL_HPP_ */
DeviceArray2D class
Definition: device_array.h:188
std::size_t elem_step() const
Returns step in elements.
int rows() const
Returns number of rows.
DeviceArray2D & operator=(const DeviceArray2D &other)
Assignment operator.
void swap(DeviceArray2D &other_arg)
Performs swap of data pointed with another device array.
void release()
Decrements reference counter and releases internal buffer if needed.
void upload(const void *host_ptr, std::size_t host_step, int rows, int cols)
Uploads data to internal buffer in GPU memory.
int cols() const
Returns number of elements in each row.
void create(int rows, int cols)
Allocates internal buffer in GPU memory.
T * ptr(int y=0)
Returns pointer to given row in internal buffer.
void download(void *host_ptr, std::size_t host_step) const
Downloads data from internal buffer to CPU memory.
DeviceArray2D()
Empty constructor.
void copyTo(DeviceArray2D &other) const
Performs data copying.
DeviceArray class
Definition: device_array.h:54
DeviceArray()
Empty constructor.
void upload(const T *host_ptr, std::size_t size)
Uploads data to internal buffer in GPU memory.
std::size_t size() const
Returns size in elements.
void copyTo(DeviceArray &other) const
Performs data copying.
void download(T *host_ptr) const
Downloads data from internal buffer to CPU memory.
DeviceArray & operator=(const DeviceArray &other)
Assignment operator.
void swap(DeviceArray &other_arg)
Performs swap of data pointed with another device array.
void release()
Decrements reference counter and releases internal buffer if needed.
void create(std::size_t size)
Allocates internal buffer in GPU memory.
T * ptr()
Returns pointer for internal buffer in GPU memory.
DeviceMemory2D class
void swap(DeviceMemory2D &other_arg)
Performs swap of data pointed with another device memory.
std::size_t step() const
Returns stride between two consecutive rows in bytes for internal buffer.
void download(void *host_ptr_arg, std::size_t host_step_arg) const
Downloads data from internal buffer to CPU memory.
void create(int rows_arg, int colsBytes_arg)
Allocates internal buffer in GPU memory.
int colsBytes() const
Returns number of bytes in each row.
int rows() const
Returns number of rows.
void upload(const void *host_ptr_arg, std::size_t host_step_arg, int rows_arg, int colsBytes_arg)
Uploads data to internal buffer in GPU memory.
DeviceMemory2D & operator=(const DeviceMemory2D &other_arg)
Assignment operator.
void release()
Decrements reference counter and releases internal buffer if needed.
void copyTo(DeviceMemory2D &other) const
Performs data copying.
DeviceMemory class
Definition: device_memory.h:54
void swap(DeviceMemory &other_arg)
Performs swap of data pointed with another device memory.
DeviceMemory & operator=(const DeviceMemory &other_arg)
Assignment operator.
void copyTo(DeviceMemory &other) const
Performs data copying.
void release()
Decrements reference counter and releases internal buffer if needed.
void download(void *host_ptr_arg) const
Downloads data from internal buffer to CPU memory.
void upload(const void *host_ptr_arg, std::size_t sizeBytes_arg)
Uploads data to internal buffer in GPU memory.
void create(std::size_t sizeBytes_arg)
Allocates internal buffer in GPU memory.