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