Point Cloud Library (PCL)  1.9.1
device_memory.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_CONTAINERS_DEVICE_MEMORY_HPP_
38 #define PCL_GPU_CONTAINERS_DEVICE_MEMORY_HPP_
39 
40 #include <pcl/pcl_exports.h>
41 #include <pcl/gpu/containers/kernel_containers.h>
42 
43 namespace pcl
44 {
45  namespace gpu
46  {
47  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
48  /** \brief @b DeviceMemory class
49  *
50  * \note This is a BLOB container class with reference counting for GPU memory.
51  *
52  * \author Anatoly Baksheev
53  */
54 
55  class PCL_EXPORTS DeviceMemory
56  {
57  public:
58  /** \brief Empty constructor. */
59  DeviceMemory();
60 
61  /** \brief Destructor. */
62  ~DeviceMemory();
63 
64  /** \brief Allocates internal buffer in GPU memory
65  * \param sizeBytes_arg amount of memory to allocate
66  * */
67  DeviceMemory(size_t sizeBytes_arg);
68 
69  /** \brief Initializes with user allocated buffer. Reference counting is disabled in this case.
70  * \param ptr_arg pointer to buffer
71  * \param sizeBytes_arg buffer size
72  * */
73  DeviceMemory(void *ptr_arg, size_t sizeBytes_arg);
74 
75  /** \brief Copy constructor. Just increments reference counter. */
76  DeviceMemory(const DeviceMemory& other_arg);
77 
78  /** \brief Assignment operator. Just increments reference counter. */
79  DeviceMemory& operator=(const DeviceMemory& other_arg);
80 
81  /** \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.
82  * \param sizeBytes_arg buffer size
83  * */
84  void create(size_t sizeBytes_arg);
85 
86  /** \brief Decrements reference counter and releases internal buffer if needed. */
87  void release();
88 
89  /** \brief Performs data copying. If destination size differs it will be reallocated.
90  * \param other destination container
91  * */
92  void copyTo(DeviceMemory& other) const;
93 
94  /** \brief Uploads data to internal buffer in GPU memory. It calls create() inside to ensure that intenal buffer size is enough.
95  * \param host_ptr_arg pointer to buffer to upload
96  * \param sizeBytes_arg buffer size
97  * */
98  void upload(const void *host_ptr_arg, size_t sizeBytes_arg);
99 
100  /** \brief Downloads data from internal buffer to CPU memory
101  * \param host_ptr_arg pointer to buffer to download
102  * */
103  void download(void *host_ptr_arg) const;
104 
105  /** \brief Performs swap of data pointed with another device memory.
106  * \param other_arg device memory to swap with
107  * */
108  void swap(DeviceMemory& other_arg);
109 
110  /** \brief Returns pointer for internal buffer in GPU memory. */
111  template<class T> T* ptr();
112 
113  /** \brief Returns constant pointer for internal buffer in GPU memory. */
114  template<class T> const T* ptr() const;
115 
116  /** \brief Conversion to PtrSz for passing to kernel functions. */
117  template <class U> operator PtrSz<U>() const;
118 
119  /** \brief Returns true if unallocated otherwise false. */
120  bool empty() const;
121 
122  size_t sizeBytes() const;
123 
124  private:
125  /** \brief Device pointer. */
126  void *data_;
127 
128  /** \brief Allocated size in bytes. */
129  size_t sizeBytes_;
130 
131  /** \brief Pointer to reference counter in CPU memory. */
132  int* refcount_;
133  };
134 
135  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
136  /** \brief @b DeviceMemory2D class
137  *
138  * \note This is a BLOB container class with reference counting for pitched GPU memory.
139  *
140  * \author Anatoly Baksheev
141  */
142 
143  class PCL_EXPORTS DeviceMemory2D
144  {
145  public:
146  /** \brief Empty constructor. */
147  DeviceMemory2D();
148 
149  /** \brief Destructor. */
150  ~DeviceMemory2D();
151 
152  /** \brief Allocates internal buffer in GPU memory
153  * \param rows_arg number of rows to allocate
154  * \param colsBytes_arg width of the buffer in bytes
155  * */
156  DeviceMemory2D(int rows_arg, int colsBytes_arg);
157 
158 
159  /** \brief Initializes with user allocated buffer. Reference counting is disabled in this case.
160  * \param rows_arg number of rows
161  * \param colsBytes_arg width of the buffer in bytes
162  * \param data_arg pointer to buffer
163  * \param step_arg stride between two consecutive rows in bytes
164  * */
165  DeviceMemory2D(int rows_arg, int colsBytes_arg, void *data_arg, size_t step_arg);
166 
167  /** \brief Copy constructor. Just increments reference counter. */
168  DeviceMemory2D(const DeviceMemory2D& other_arg);
169 
170  /** \brief Assignment operator. Just increments reference counter. */
171  DeviceMemory2D& operator=(const DeviceMemory2D& other_arg);
172 
173  /** \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.
174  * \param rows_arg number of rows to allocate
175  * \param colsBytes_arg width of the buffer in bytes
176  * */
177  void create(int rows_arg, int colsBytes_arg);
178 
179  /** \brief Decrements reference counter and releases internal buffer if needed. */
180  void release();
181 
182  /** \brief Performs data copying. If destination size differs it will be reallocated.
183  * \param other destination container
184  * */
185  void copyTo(DeviceMemory2D& other) const;
186 
187  /** \brief Uploads data to internal buffer in GPU memory. It calls create() inside to ensure that intenal buffer size is enough.
188  * \param host_ptr_arg pointer to host buffer to upload
189  * \param host_step_arg stride between two consecutive rows in bytes for host buffer
190  * \param rows_arg number of rows to upload
191  * \param colsBytes_arg width of host buffer in bytes
192  * */
193  void upload(const void *host_ptr_arg, size_t host_step_arg, int rows_arg, int colsBytes_arg);
194 
195  /** \brief Downloads data from internal buffer to CPU memory. User is responsible for correct host buffer size.
196  * \param host_ptr_arg pointer to host buffer to download
197  * \param host_step_arg stride between two consecutive rows in bytes for host buffer
198  * */
199  void download(void *host_ptr_arg, size_t host_step_arg) const;
200 
201  /** \brief Performs swap of data pointed with another device memory.
202  * \param other_arg device memory to swap with
203  * */
204  void swap(DeviceMemory2D& other_arg);
205 
206  /** \brief Returns pointer to given row in internal buffer.
207  * \param y_arg row index
208  * */
209  template<class T> T* ptr(int y_arg = 0);
210 
211  /** \brief Returns constant pointer to given row in internal buffer.
212  * \param y_arg row index
213  * */
214  template<class T> const T* ptr(int y_arg = 0) const;
215 
216  /** \brief Conversion to PtrStep for passing to kernel functions. */
217  template <class U> operator PtrStep<U>() const;
218 
219  /** \brief Conversion to PtrStepSz for passing to kernel functions. */
220  template <class U> operator PtrStepSz<U>() const;
221 
222  /** \brief Returns true if unallocated otherwise false. */
223  bool empty() const;
224 
225  /** \brief Returns number of bytes in each row. */
226  int colsBytes() const;
227 
228  /** \brief Returns number of rows. */
229  int rows() const;
230 
231  /** \brief Returns stride between two consecutive rows in bytes for internal buffer. Step is stored always and everywhere in bytes!!! */
232  size_t step() const;
233  private:
234  /** \brief Device pointer. */
235  void *data_;
236 
237  /** \brief Stride between two consecutive rows in bytes for internal buffer. Step is stored always and everywhere in bytes!!! */
238  size_t step_;
239 
240  /** \brief Width of the buffer in bytes. */
241  int colsBytes_;
242 
243  /** \brief Number of rows. */
244  int rows_;
245 
246  /** \brief Pointer to reference counter in CPU memory. */
247  int* refcount_;
248  };
249  }
250 
251  namespace device
252  {
255  }
256 }
257 
258 #include <pcl/gpu/containers/impl/device_memory.hpp>
259 
260 #endif /* PCL_GPU_CONTAINERS_DEVICE_MEMORY_HPP_ */
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
DeviceMemory2D class
DeviceMemory class
Definition: device_memory.h:55