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