Point Cloud Library (PCL) 1.13.0
Loading...
Searching...
No Matches
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
40namespace pcl {
41
42namespace gpu {
43
44//////////////////// Inline implementations of DeviceArray //////////////////
45
46template <class T>
49
50template <class T>
51inline DeviceArray<T>::DeviceArray(std::size_t size) : DeviceMemory(size * elem_size)
52{}
53
54template <class T>
55inline DeviceArray<T>::DeviceArray(T* ptr, std::size_t size)
56: DeviceMemory(ptr, size * elem_size)
57{}
58
59template <class T>
61{}
62
63template <class T>
64inline DeviceArray<T>&
66{
68 return *this;
69}
70
71template <class T>
72inline void
73DeviceArray<T>::create(std::size_t size)
74{
75 DeviceMemory::create(size * elem_size);
76}
77
78template <class T>
79inline void
81{
83}
84
85template <class T>
86inline void
91
92template <class T>
93inline void
94DeviceArray<T>::upload(const T* host_ptr, std::size_t size)
95{
96 DeviceMemory::upload(host_ptr, size * elem_size);
97}
98
99template <class T>
100inline bool
101DeviceArray<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);
109
110template <class T>
111inline void
112DeviceArray<T>::download(T* host_ptr) const
113{
114 DeviceMemory::download(host_ptr);
115}
116
117template <class T>
118inline 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
128template <class T>
129void
131{
132 DeviceMemory::swap(other_arg);
133}
135template <class T>
137{
138 return ptr();
139}
140
141template <class T>
142inline DeviceArray<T>::operator const T*() const
143{
144 return ptr();
145}
146
147template <class T>
148inline std::size_t
150{
151 return sizeBytes() / elem_size;
152}
153
154template <class T>
155inline T*
158 return DeviceMemory::ptr<T>();
159}
160
161template <class T>
162inline const T*
164{
165 return DeviceMemory::ptr<T>();
166}
167
168template <class T>
169template <class A>
170inline void
171DeviceArray<T>::upload(const std::vector<T, A>& data)
172{
173 upload(&data[0], data.size());
174}
175
176template <class T>
177template <class A>
178inline void
179DeviceArray<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
188template <class T>
191
192template <class T>
193inline DeviceArray2D<T>::DeviceArray2D(int rows, int cols)
194: DeviceMemory2D(rows, cols * elem_size)
195{}
196
197template <class T>
199 int cols,
200 void* data,
201 std::size_t stepBytes)
202: DeviceMemory2D(rows, cols * elem_size, data, stepBytes)
204
205template <class T>
207: DeviceMemory2D(other)
208{}
209
210template <class T>
211inline DeviceArray2D<T>&
213{
215 return *this;
216}
217
218template <class T>
219inline void
220DeviceArray2D<T>::create(int rows, int cols)
221{
222 DeviceMemory2D::create(rows, cols * elem_size);
223}
224
225template <class T>
226inline void
230}
231
232template <class T>
233inline void
239template <class T>
240inline void
241DeviceArray2D<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}
249template <class T>
250inline void
251DeviceArray2D<T>::download(void* host_ptr, std::size_t host_step) const
252{
253 DeviceMemory2D::download(host_ptr, host_step);
254}
255
256template <class T>
257template <class A>
258inline void
259DeviceArray2D<T>::upload(const std::vector<T, A>& data, int cols)
260{
261 upload(&data[0], cols * elem_size, data.size() / cols, cols);
263
264template <class T>
265template <class A>
266inline void
267DeviceArray2D<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
275template <class T>
276void
278{
280}
281
282template <class T>
283inline T*
286 return DeviceMemory2D::ptr<T>(y);
287}
288
289template <class T>
290inline const T*
292{
293 return DeviceMemory2D::ptr<T>(y);
294}
295
296template <class T>
298{
299 return ptr();
300}
301
302template <class T>
303inline DeviceArray2D<T>::operator const T*() const
304{
305 return ptr();
306}
308template <class T>
309inline int
312 return DeviceMemory2D::colsBytes() / elem_size;
313}
314
315template <class T>
316inline int
318{
319 return DeviceMemory2D::rows();
320}
321
322template <class T>
323inline 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
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
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
DeviceMemory & operator=(const DeviceMemory &other_arg)
Assignment operator.
void swap(DeviceMemory &other_arg)
Performs swap of data pointed with another device memory.
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.