Loading...
Searching...
No Matches
device_array.h
/*
* Software License Agreement (BSD License)
*
* Copyright (c) 2011, Willow Garage, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of Willow Garage, Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Author: Anatoly Baskeheev, Itseez Ltd, (myname.mysurname@mycompany.com)
*/
#pragma once
#include <pcl/gpu/containers/device_memory.h>
#include <pcl/pcl_exports.h>
#include <vector>
namespace pcl {
namespace gpu {
//////////////////////////////////////////////////////////////////////////////
/** \brief @b DeviceArray class
*
* \note Typed container for GPU memory with reference counting.
*
* \author Anatoly Baksheev
*/
template <class T>
class PCL_EXPORTS DeviceArray : public DeviceMemory {
public:
/** \brief Element type. */
using type = T;
/** \brief Element size. */
enum { elem_size = sizeof(T) };
/** \brief Empty constructor. */
DeviceArray();
/** \brief Allocates internal buffer in GPU memory
* \param size number of elements to allocate
* */
DeviceArray(std::size_t size);
/** \brief Initializes with user allocated buffer. Reference counting is disabled in
* this case.
* \param ptr pointer to buffer
* \param size elements number
* */
DeviceArray(T* ptr, std::size_t size);
/** \brief Copy constructor. Just increments reference counter. */
DeviceArray(const DeviceArray& other);
/** \brief Assignment operator. Just increments reference counter. */
DeviceArray&
operator=(const DeviceArray& other);
/** \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.
* \param size elements number
* */
void
create(std::size_t size);
/** \brief Decrements reference counter and releases internal buffer if needed. */
void
release();
/** \brief Performs data copying. If destination size differs it will be reallocated.
* \param other destination container
* */
void
copyTo(DeviceArray& other) const;
/** \brief Uploads data to internal buffer in GPU memory. It calls create() inside to
* ensure that internal buffer size is enough.
* \param host_ptr pointer to buffer to upload
* \param size elements number
* */
void
upload(const T* host_ptr, std::size_t size);
/** \brief Uploads data from CPU memory to internal buffer.
* \return true if upload successful
* \note In contrast to the other upload function, this function
* never allocates memory.
* \param host_ptr pointer to buffer to upload
* \param device_begin_offset begin upload
* \param num_elements number of elements from device_bein_offset
* */
bool
upload(const T* host_ptr, std::size_t device_begin_offset, std::size_t num_elements);
/** \brief Downloads data from internal buffer to CPU memory
* \param host_ptr pointer to buffer to download
* */
void
download(T* host_ptr) const;
/** \brief Downloads data from internal buffer to CPU memory.
* \return true if download successful
* \param host_ptr pointer to buffer to download
* \param device_begin_offset begin download location
* \param num_elements number of elements from device_begin_offset
* */
bool
download(T* host_ptr,
std::size_t device_begin_offset,
std::size_t num_elements) const;
/** \brief Uploads data to internal buffer in GPU memory. It calls create() inside to
* ensure that internal buffer size is enough.
* \param data host vector to upload from
* */
template <class A>
void
upload(const std::vector<T, A>& data);
/** \brief Downloads data from internal buffer to CPU memory
* \param data host vector to download to
* */
template <typename A>
void
download(std::vector<T, A>& data) const;
/** \brief Performs swap of data pointed with another device array.
* \param other_arg device array to swap with
* */
void
swap(DeviceArray& other_arg);
/** \brief Returns pointer for internal buffer in GPU memory. */
T*
ptr();
/** \brief Returns const pointer for internal buffer in GPU memory. */
const T*
ptr() const;
// using DeviceMemory::ptr;
/** \brief Returns pointer for internal buffer in GPU memory. */
operator T*();
/** \brief Returns const pointer for internal buffer in GPU memory. */
operator const T*() const;
/** \brief Returns size in elements. */
std::size_t
size() const;
};
///////////////////////////////////////////////////////////////////////////////
/** \brief @b DeviceArray2D class
*
* \note Typed container for pitched GPU memory with reference counting.
*
* \author Anatoly Baksheev
*/
template <class T>
class PCL_EXPORTS DeviceArray2D : public DeviceMemory2D {
public:
/** \brief Element type. */
using type = T;
/** \brief Element size. */
enum { elem_size = sizeof(T) };
/** \brief Empty constructor. */
DeviceArray2D();
/** \brief Allocates internal buffer in GPU memory
* \param rows number of rows to allocate
* \param cols number of elements in each row
* */
DeviceArray2D(int rows, int cols);
/** \brief Initializes with user allocated buffer. Reference counting is disabled in
* this case.
* \param rows number of rows
* \param cols number of elements in each row
* \param data pointer to buffer
* \param stepBytes stride between two consecutive rows in bytes
* */
DeviceArray2D(int rows, int cols, void* data, std::size_t stepBytes);
/** \brief Copy constructor. Just increments reference counter. */
DeviceArray2D(const DeviceArray2D& other);
/** \brief Assignment operator. Just increments reference counter. */
DeviceArray2D&
operator=(const DeviceArray2D& other);
/** \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.
* \param rows number of rows to allocate
* \param cols number of elements in each row
* */
void
create(int rows, int cols);
/** \brief Decrements reference counter and releases internal buffer if needed. */
void
release();
/** \brief Performs data copying. If destination size differs it will be reallocated.
* \param other destination container
* */
void
copyTo(DeviceArray2D& other) const;
/** \brief Uploads data to internal buffer in GPU memory. It calls create() inside to
* ensure that internal buffer size is enough.
* \param host_ptr pointer to host buffer to upload
* \param host_step stride between two consecutive rows in bytes for host buffer
* \param rows number of rows to upload
* \param cols number of elements in each row
* */
void
upload(const void* host_ptr, std::size_t host_step, int rows, int cols);
/** \brief Downloads data from internal buffer to CPU memory. User is responsible for
* correct host buffer size.
* \param host_ptr pointer to host buffer to download
* \param host_step stride between two consecutive rows in bytes for host buffer
* */
void
download(void* host_ptr, std::size_t host_step) const;
/** \brief Performs swap of data pointed with another device array.
* \param other_arg device array to swap with
* */
void
swap(DeviceArray2D& other_arg);
/** \brief Uploads data to internal buffer in GPU memory. It calls create() inside to
* ensure that internal buffer size is enough.
* \param data host vector to upload from
* \param cols stride in elements between two consecutive rows for host buffer
* */
template <class A>
void
upload(const std::vector<T, A>& data, int cols);
/** \brief Downloads data from internal buffer to CPU memory
* \param data host vector to download to
* \param cols Output stride in elements between two consecutive rows for host vector.
* */
template <class A>
void
download(std::vector<T, A>& data, int& cols) const;
/** \brief Returns pointer to given row in internal buffer.
* \param y row index
* */
T*
ptr(int y = 0);
/** \brief Returns const pointer to given row in internal buffer.
* \param y row index
* */
const T*
ptr(int y = 0) const;
// using DeviceMemory2D::ptr;
/** \brief Returns pointer for internal buffer in GPU memory. */
operator T*();
/** \brief Returns const pointer for internal buffer in GPU memory. */
operator const T*() const;
/** \brief Returns number of elements in each row. */
int
cols() const;
/** \brief Returns number of rows. */
int
rows() const;
/** \brief Returns step in elements. */
std::size_t
elem_step() const;
};
} // namespace gpu
namespace device {
using pcl::gpu::DeviceArray;
using pcl::gpu::DeviceArray2D;
} // namespace device
} // namespace pcl
#include <pcl/gpu/containers/impl/device_array.hpp>
pcl::device::DeviceArray2D::DeviceArray2D
DeviceArray2D()
Empty constructor.
Definition device_array.hpp:189
pcl::device::DeviceArray::DeviceArray
DeviceArray()
Empty constructor.
Definition device_array.hpp:47
pcl::gpu::DeviceArray2D
DeviceArray2D class
Definition device_array.h:188
pcl::gpu::DeviceArray2D::upload
void upload(const std::vector< T, A > &data, int cols)
Uploads data to internal buffer in GPU memory.
Definition device_array.hpp:259
pcl::gpu::DeviceArray2D::DeviceArray2D
DeviceArray2D(const DeviceArray2D &other)
Copy constructor.
Definition device_array.hpp:206
pcl::gpu::DeviceArray2D::elem_step
std::size_t elem_step() const
Returns step in elements.
Definition device_array.hpp:324
pcl::gpu::DeviceArray2D< PixelRGB >::rows
int rows() const
pcl::gpu::DeviceArray2D::operator=
DeviceArray2D & operator=(const DeviceArray2D &other)
Assignment operator.
Definition device_array.hpp:212
pcl::gpu::DeviceArray2D::swap
void swap(DeviceArray2D &other_arg)
Performs swap of data pointed with another device array.
Definition device_array.hpp:277
pcl::gpu::DeviceArray2D::ptr
const T * ptr(int y=0) const
Returns const pointer to given row in internal buffer.
Definition device_array.hpp:291
pcl::gpu::DeviceArray2D::type
T type
Element type.
Definition device_array.h:191
pcl::gpu::DeviceArray2D::release
void release()
Decrements reference counter and releases internal buffer if needed.
Definition device_array.hpp:227
pcl::gpu::DeviceArray2D::upload
void upload(const void *host_ptr, std::size_t host_step, int rows, int cols)
Uploads data to internal buffer in GPU memory.
Definition device_array.hpp:241
pcl::gpu::DeviceArray2D< PixelRGB >::cols
int cols() const
pcl::gpu::DeviceArray2D::create
void create(int rows, int cols)
Allocates internal buffer in GPU memory.
Definition device_array.hpp:220
pcl::gpu::DeviceArray2D< PixelRGB >::elem_size
@ elem_size
Definition device_array.h:194
pcl::gpu::DeviceArray2D::ptr
T * ptr(int y=0)
Returns pointer to given row in internal buffer.
Definition device_array.hpp:284
pcl::gpu::DeviceArray2D::download
void download(void *host_ptr, std::size_t host_step) const
Downloads data from internal buffer to CPU memory.
Definition device_array.hpp:251
pcl::gpu::DeviceArray2D::DeviceArray2D
DeviceArray2D(int rows, int cols)
Allocates internal buffer in GPU memory.
Definition device_array.hpp:193
pcl::gpu::DeviceArray2D::DeviceArray2D
DeviceArray2D(int rows, int cols, void *data, std::size_t stepBytes)
Initializes with user allocated buffer.
Definition device_array.hpp:198
pcl::gpu::DeviceArray2D::DeviceArray2D
DeviceArray2D()
Empty constructor.
Definition device_array.hpp:189
pcl::gpu::DeviceArray2D::download
void download(std::vector< T, A > &data, int &cols) const
Downloads data from internal buffer to CPU memory.
Definition device_array.hpp:267
pcl::gpu::DeviceArray2D::copyTo
void copyTo(DeviceArray2D &other) const
Performs data copying.
Definition device_array.hpp:234
pcl::gpu::DeviceArray
DeviceArray class
Definition device_array.h:54
pcl::gpu::DeviceArray::download
bool download(T *host_ptr, std::size_t device_begin_offset, std::size_t num_elements) const
Downloads data from internal buffer to CPU memory.
Definition device_array.hpp:119
pcl::gpu::DeviceArray::DeviceArray
DeviceArray()
Empty constructor.
Definition device_array.hpp:47
pcl::gpu::DeviceArray::DeviceArray
DeviceArray(std::size_t size)
Allocates internal buffer in GPU memory.
Definition device_array.hpp:51
pcl::gpu::DeviceArray::upload
void upload(const T *host_ptr, std::size_t size)
Uploads data to internal buffer in GPU memory.
Definition device_array.hpp:94
pcl::gpu::DeviceArray::upload
bool upload(const T *host_ptr, std::size_t device_begin_offset, std::size_t num_elements)
Uploads data from CPU memory to internal buffer.
Definition device_array.hpp:101
pcl::gpu::DeviceArray::download
void download(std::vector< T, A > &data) const
Downloads data from internal buffer to CPU memory.
Definition device_array.hpp:179
pcl::gpu::DeviceArray< PointType >::size
std::size_t size() const
pcl::gpu::DeviceArray::copyTo
void copyTo(DeviceArray &other) const
Performs data copying.
Definition device_array.hpp:87
pcl::gpu::DeviceArray::download
void download(T *host_ptr) const
Downloads data from internal buffer to CPU memory.
Definition device_array.hpp:112
pcl::gpu::DeviceArray::operator=
DeviceArray & operator=(const DeviceArray &other)
Assignment operator.
Definition device_array.hpp:65
pcl::gpu::DeviceArray::swap
void swap(DeviceArray &other_arg)
Performs swap of data pointed with another device array.
Definition device_array.hpp:130
pcl::gpu::DeviceArray::release
void release()
Decrements reference counter and releases internal buffer if needed.
Definition device_array.hpp:80
pcl::gpu::DeviceArray::create
void create(std::size_t size)
Allocates internal buffer in GPU memory.
Definition device_array.hpp:73
pcl::gpu::DeviceArray::upload
void upload(const std::vector< T, A > &data)
Uploads data to internal buffer in GPU memory.
Definition device_array.hpp:171
pcl::gpu::DeviceArray::DeviceArray
DeviceArray(T *ptr, std::size_t size)
Initializes with user allocated buffer.
Definition device_array.hpp:55
pcl::gpu::DeviceArray< PointType >::elem_size
@ elem_size
Definition device_array.h:60
pcl::gpu::DeviceArray::ptr
const T * ptr() const
Returns const pointer for internal buffer in GPU memory.
Definition device_array.hpp:163
pcl::gpu::DeviceArray::type
T type
Element type.
Definition device_array.h:57
pcl::gpu::DeviceArray< PointType >::ptr
PointType * ptr()
pcl::gpu::DeviceArray::DeviceArray
DeviceArray(const DeviceArray &other)
Copy constructor.
Definition device_array.hpp:60
pcl::gpu::DeviceMemory2D::DeviceMemory2D
DeviceMemory2D()
Empty constructor.
pcl::gpu::DeviceMemory::DeviceMemory
DeviceMemory()
Empty constructor.
pcl::device
Definition device_array.h:315
pcl::gpu
Definition device_array.h:45
pcl
Definition convolution.h:46