Point Cloud Library (PCL)  1.9.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
device_array.h
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_HPP_
38 #define PCL_GPU_CONTAINER_DEVICE_ARRAY_HPP_
39 
40 #include <pcl/pcl_exports.h>
41 #include <pcl/gpu/containers/device_memory.h>
42 
43 #include <vector>
44 
45 namespace pcl
46 {
47  namespace gpu
48  {
49  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
50  /** \brief @b DeviceArray class
51  *
52  * \note Typed container for GPU memory with reference counting.
53  *
54  * \author Anatoly Baksheev
55  */
56  template<class T>
57  class PCL_EXPORTS DeviceArray : public DeviceMemory
58  {
59  public:
60  /** \brief Element type. */
61  typedef T type;
62 
63  /** \brief Element size. */
64  enum { elem_size = sizeof(T) };
65 
66  /** \brief Empty constructor. */
67  DeviceArray();
68 
69  /** \brief Allocates internal buffer in GPU memory
70  * \param size number of elements to allocate
71  * */
72  DeviceArray(size_t size);
73 
74  /** \brief Initializes with user allocated buffer. Reference counting is disabled in this case.
75  * \param ptr pointer to buffer
76  * \param size elements number
77  * */
78  DeviceArray(T *ptr, size_t size);
79 
80  /** \brief Copy constructor. Just increments reference counter. */
81  DeviceArray(const DeviceArray& other);
82 
83  /** \brief Assignment operator. Just increments reference counter. */
84  DeviceArray& operator = (const DeviceArray& other);
85 
86  /** \brief Allocates internal buffer in GPU memory. If internal buffer was created before the function recreates it with new size. If new and old sizes are equal it does nothing.
87  * \param size elements number
88  * */
89  void create(size_t size);
90 
91  /** \brief Decrements reference counter and releases internal buffer if needed. */
92  void release();
93 
94  /** \brief Performs data copying. If destination size differs it will be reallocated.
95  * \param other destination container
96  * */
97  void copyTo(DeviceArray& other) const;
98 
99  /** \brief Uploads data to internal buffer in GPU memory. It calls create() inside to ensure that intenal buffer size is enough.
100  * \param host_ptr pointer to buffer to upload
101  * \param size elements number
102  * */
103  void upload(const T *host_ptr, size_t size);
104 
105  /** \brief Downloads data from internal buffer to CPU memory
106  * \param host_ptr pointer to buffer to download
107  * */
108  void download(T *host_ptr) const;
109 
110  /** \brief Uploads data to internal buffer in GPU memory. It calls create() inside to ensure that intenal buffer size is enough.
111  * \param data host vector to upload from
112  * */
113  template<class A>
114  void upload(const std::vector<T, A>& data);
115 
116  /** \brief Downloads data from internal buffer to CPU memory
117  * \param data host vector to download to
118  * */
119  template<typename A>
120  void download(std::vector<T, A>& data) const;
121 
122  /** \brief Performs swap of data pointed with another device array.
123  * \param other_arg device array to swap with
124  * */
125  void swap(DeviceArray& other_arg);
126 
127  /** \brief Returns pointer for internal buffer in GPU memory. */
128  T* ptr();
129 
130  /** \brief Returns const pointer for internal buffer in GPU memory. */
131  const T* ptr() const;
132 
133  //using DeviceMemory::ptr;
134 
135  /** \brief Returns pointer for internal buffer in GPU memory. */
136  operator T*();
137 
138  /** \brief Returns const pointer for internal buffer in GPU memory. */
139  operator const T*() const;
140 
141  /** \brief Returns size in elements. */
142  size_t size() const;
143  };
144 
145 
146  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
147  /** \brief @b DeviceArray2D class
148  *
149  * \note Typed container for pitched GPU memory with reference counting.
150  *
151  * \author Anatoly Baksheev
152  */
153  template<class T>
154  class PCL_EXPORTS DeviceArray2D : public DeviceMemory2D
155  {
156  public:
157  /** \brief Element type. */
158  typedef T type;
159 
160  /** \brief Element size. */
161  enum { elem_size = sizeof(T) };
162 
163  /** \brief Empty constructor. */
164  DeviceArray2D();
165 
166  /** \brief Allocates internal buffer in GPU memory
167  * \param rows number of rows to allocate
168  * \param cols number of elements in each row
169  * */
170  DeviceArray2D(int rows, int cols);
171 
172  /** \brief Initializes with user allocated buffer. Reference counting is disabled in this case.
173  * \param rows number of rows
174  * \param cols number of elements in each row
175  * \param data pointer to buffer
176  * \param stepBytes stride between two consecutive rows in bytes
177  * */
178  DeviceArray2D(int rows, int cols, void *data, size_t stepBytes);
179 
180  /** \brief Copy constructor. Just increments reference counter. */
181  DeviceArray2D(const DeviceArray2D& other);
182 
183  /** \brief Assignment operator. Just increments reference counter. */
184  DeviceArray2D& operator = (const DeviceArray2D& other);
185 
186  /** \brief Allocates internal buffer in GPU memory. If internal buffer was created before the function recreates it with new size. If new and old sizes are equal it does nothing.
187  * \param rows number of rows to allocate
188  * \param cols number of elements in each row
189  * */
190  void create(int rows, int cols);
191 
192  /** \brief Decrements reference counter and releases internal buffer if needed. */
193  void release();
194 
195  /** \brief Performs data copying. If destination size differs it will be reallocated.
196  * \param other destination container
197  * */
198  void copyTo(DeviceArray2D& other) const;
199 
200  /** \brief Uploads data to internal buffer in GPU memory. It calls create() inside to ensure that intenal buffer size is enough.
201  * \param host_ptr pointer to host buffer to upload
202  * \param host_step stride between two consecutive rows in bytes for host buffer
203  * \param rows number of rows to upload
204  * \param cols number of elements in each row
205  * */
206  void upload(const void *host_ptr, size_t host_step, int rows, int cols);
207 
208  /** \brief Downloads data from internal buffer to CPU memory. User is responsible for correct host buffer size.
209  * \param host_ptr pointer to host buffer to download
210  * \param host_step stride between two consecutive rows in bytes for host buffer
211  * */
212  void download(void *host_ptr, size_t host_step) const;
213 
214  /** \brief Performs swap of data pointed with another device array.
215  * \param other_arg device array to swap with
216  * */
217  void swap(DeviceArray2D& other_arg);
218 
219  /** \brief Uploads data to internal buffer in GPU memory. It calls create() inside to ensure that intenal buffer size is enough.
220  * \param data host vector to upload from
221  * \param cols stride in elements between two consecutive rows for host buffer
222  * */
223  template<class A>
224  void upload(const std::vector<T, A>& data, int cols);
225 
226  /** \brief Downloads data from internal buffer to CPU memory
227  * \param data host vector to download to
228  * \param cols Output stride in elements between two consecutive rows for host vector.
229  * */
230  template<class A>
231  void download(std::vector<T, A>& data, int& cols) const;
232 
233  /** \brief Returns pointer to given row in internal buffer.
234  * \param y row index
235  * */
236  T* ptr(int y = 0);
237 
238  /** \brief Returns const pointer to given row in internal buffer.
239  * \param y row index
240  * */
241  const T* ptr(int y = 0) const;
242 
243  //using DeviceMemory2D::ptr;
244 
245  /** \brief Returns pointer for internal buffer in GPU memory. */
246  operator T*();
247 
248  /** \brief Returns const pointer for internal buffer in GPU memory. */
249  operator const T*() const;
250 
251  /** \brief Returns number of elements in each row. */
252  int cols() const;
253 
254  /** \brief Returns number of rows. */
255  int rows() const;
256 
257  /** \brief Returns step in elements. */
258  size_t elem_step() const;
259  };
260  }
261 
262  namespace device
263  {
264  using pcl::gpu::DeviceArray;
266  }
267 }
268 
269 #include <pcl/gpu/containers/impl/device_array.hpp>
270 
271 #endif /* PCL_GPU_CONTAINER_DEVICE_ARRAY_HPP_ */
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
DeviceArray2D class
Definition: device_array.h:154
T type
Element type.
Definition: device_array.h:158
DeviceMemory2D class
DeviceArray class
Definition: device_array.h:57
T type
Element type.
Definition: device_array.h:61
DeviceMemory class
Definition: device_memory.h:55