2011-02-22 21:43:26 +01:00
Data Structures
===============
2011-04-19 13:41:12 +02:00
.. highlight :: cpp
2011-03-29 01:16:20 +02:00
.. index :: gpu::DevMem2D\_
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
gpu::DevMem2D\_
2011-03-03 08:29:55 +01:00
---------------
2011-03-29 01:16:20 +02:00
.. cpp:class :: gpu::DevMem2D\_
2011-02-22 21:43:26 +01:00
2011-04-01 00:07:17 +02:00
This lightweight class encapsulates pitched memory on a GPU and is passed to nvcc-compiled code (CUDA kernels). Typically, it is used internally by OpenCV and by users who write device code. You can call its members from both host and device code. ::
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
template <typename T> struct DevMem2D_
{
int cols;
int rows;
T* data;
size_t step;
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
DevMem2D_() : cols(0), rows(0), data(0), step(0){};
2011-03-03 08:29:55 +01:00
DevMem2D_(int rows, int cols, T *data, size_t step);
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
template <typename U>
explicit DevMem2D_(const DevMem2D_<U>& d);
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
typedef T elem_type;
enum { elem_size = sizeof(elem_type) };
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
__CV_GPU_HOST_DEVICE__ size_t elemSize() const;
2011-02-22 21:43:26 +01:00
2011-04-27 10:24:24 +02:00
/* returns pointer to the beginning of the given image row * /
2011-02-26 12:05:10 +01:00
__CV_GPU_HOST_DEVICE__ T* ptr(int y = 0);
__CV_GPU_HOST_DEVICE__ const T* ptr(int y = 0) const;
};
2011-02-28 22:26:43 +01:00
2011-03-23 11:56:20 +01:00
typedef DevMem2D_<unsigned char> DevMem2D;
typedef DevMem2D_<float> DevMem2Df;
typedef DevMem2D_<int> DevMem2Di;
2011-04-27 10:24:24 +02:00
..
2011-03-23 11:56:20 +01:00
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
.. index :: gpu::PtrStep\_
2011-02-22 21:43:26 +01:00
2011-03-03 08:29:55 +01:00
gpu::PtrStep\_
--------------
2011-03-29 01:16:20 +02:00
.. cpp:class :: gpu::PtrStep\_
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
This structure is similar to
:cpp:class: `DevMem2D_` but contains only a pointer and row step. Width and height fields are excluded due to performance reasons. The structure is intended for internal use or for users who write device code.
::
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
template<typename T> struct PtrStep_
{
2011-03-29 01:16:20 +02:00
T* data;
size_t step;
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
PtrStep_();
PtrStep_(const DevMem2D_<T>& mem);
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
typedef T elem_type;
enum { elem_size = sizeof(elem_type) };
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
__CV_GPU_HOST_DEVICE__ size_t elemSize() const;
__CV_GPU_HOST_DEVICE__ T* ptr(int y = 0);
__CV_GPU_HOST_DEVICE__ const T* ptr(int y = 0) const;
2011-02-26 12:05:10 +01:00
};
2011-02-28 22:26:43 +01:00
2011-03-23 11:56:20 +01:00
typedef PtrStep_<unsigned char> PtrStep;
typedef PtrStep_<float> PtrStepf;
typedef PtrStep_<int> PtrStepi;
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
.. index :: gpu::PtrElemStrp\_
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
gpu::PtrElemStrp\_
2011-03-03 08:29:55 +01:00
------------------
2011-03-29 01:16:20 +02:00
.. cpp:class :: gpu::PtrElemStrp\_
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
This structure is similar to
:cpp:class: `DevMem2D_` but contains only pointer and row step in elements. Width and height fields are excluded due to performance reasons. This class can only be constructed if `` sizeof(T) `` is a multiple of 256. The structure is intended for internal use or for users who write device code.
::
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
template<typename T> struct PtrElemStep_ : public PtrStep_<T>
{
2011-03-29 01:16:20 +02:00
PtrElemStep_(const DevMem2D_<T>& mem);
__CV_GPU_HOST_DEVICE__ T* ptr(int y = 0);
__CV_GPU_HOST_DEVICE__ const T* ptr(int y = 0) const;
2011-02-26 12:05:10 +01:00
};
2011-02-28 22:26:43 +01:00
2011-02-22 21:43:26 +01:00
.. index :: gpu::GpuMat
gpu::GpuMat
-----------
2011-03-23 11:56:20 +01:00
.. cpp:class :: gpu::GpuMat
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
This is a base storage class for GPU memory with reference counting. Its interface matches the
:c:type: `Mat` interface with the following limitations:
*
no arbitrary dimensions support (only 2D)
*
no functions that return references to their data (because references on GPU are not valid for CPU)
*
no expression templates technique support
2011-04-01 00:07:17 +02:00
Beware that the latter limitation may lead to overloaded matrix operators that cause memory allocations. The `` GpuMat `` class is convertible to :cpp:class: `gpu::DevMem2D_` and :cpp:class: `gpu::PtrStep_` so it can be passed directly to kernel.
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
**Note:**
2011-04-01 00:07:17 +02:00
2011-03-29 01:16:20 +02:00
In contrast with :c:type: `Mat` , in most cases `` GpuMat::isContinuous() == false `` . This means that rows are aligned to size depending on the hardware. Single-row `` GpuMat `` is always a continuous matrix. ::
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
class CV_EXPORTS GpuMat
2011-02-26 12:05:10 +01:00
{
public:
2011-03-29 01:16:20 +02:00
//! default constructor
GpuMat();
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
GpuMat(int rows, int cols, int type);
GpuMat(Size size, int type);
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
.....
2011-02-26 12:05:10 +01:00
2011-03-29 01:16:20 +02:00
//! builds GpuMat from Mat. Blocks uploading to device.
explicit GpuMat (const Mat& m);
2011-02-26 12:05:10 +01:00
2011-03-29 01:16:20 +02:00
//! returns lightweight DevMem2D_ structure for passing
//to nvcc-compiled code. Contains size, data ptr and step.
template <class T> operator DevMem2D_<T>() const;
template <class T> operator PtrStep_<T>() const;
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
//! blocks uploading data to GpuMat.
void upload(const cv::Mat& m);
void upload(const CudaMem& m, Stream& stream);
2011-02-26 12:05:10 +01:00
2011-03-29 01:16:20 +02:00
//! downloads data from device to host memory. Blocking calls.
operator Mat() const;
void download(cv::Mat& m) const;
2011-02-26 12:05:10 +01:00
2011-03-29 01:16:20 +02:00
//! download async
void download(CudaMem& m, Stream& stream) const;
2011-02-26 12:05:10 +01:00
};
2011-02-28 22:26:43 +01:00
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
**Note:**
2011-04-01 00:07:17 +02:00
2011-03-29 01:16:20 +02:00
You are not recommended to leave static or global `` GpuMat `` variables allocated, that is to rely on its destructor. The destruction order of such variables and CUDA context is undefined. GPU memory release function returns error if the CUDA context has been destroyed before.
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
See Also:
:func: `Mat`
2011-02-22 21:43:26 +01:00
2011-03-03 08:29:55 +01:00
.. index :: gpu::CudaMem
2011-02-22 21:43:26 +01:00
gpu::CudaMem
------------
2011-03-23 11:56:20 +01:00
.. cpp:class :: gpu::CudaMem
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
This class with reference counting wraps special memory type allocation functions from CUDA. Its interface is also
:func: `Mat` -like but with additional memory type parameters.
*
2011-04-01 00:07:17 +02:00
`` ALLOC_PAGE_LOCKED `` : Sets a page locked memory type, used commonly for fast and asynchronous uploading/downloading data from/to GPU.
2011-03-29 01:16:20 +02:00
*
`` ALLOC_ZEROCOPY `` : Specifies a zero copy memory allocation that enables mapping the host memory to GPU address space, if supported.
*
`` ALLOC_WRITE_COMBINED `` : Sets the write combined buffer that is not cached by CPU. Such buffers are used to supply GPU with data when GPU only reads it. The advantage is a better CPU cache utilization.
**Note:**
2011-04-01 00:07:17 +02:00
Allocation size of such memory types is usually limited. For more details, see "CUDA 2.2 Pinned Memory APIs" document or "CUDA C Programming Guide".
2011-03-29 01:16:20 +02:00
::
class CV_EXPORTS CudaMem
2011-02-26 12:05:10 +01:00
{
public:
2011-03-29 01:16:20 +02:00
enum { ALLOC_PAGE_LOCKED = 1, ALLOC_ZEROCOPY = 2,
ALLOC_WRITE_COMBINED = 4 };
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
CudaMem(Size size, int type, int alloc_type = ALLOC_PAGE_LOCKED);
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
//! creates from cv::Mat with coping data
explicit CudaMem(const Mat& m, int alloc_type = ALLOC_PAGE_LOCKED);
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
......
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
void create(Size size, int type, int alloc_type = ALLOC_PAGE_LOCKED);
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
//! returns matrix header with disabled ref. counting for CudaMem data.
Mat createMatHeader() const;
operator Mat() const;
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
//! maps host memory into device address space
GpuMat createGpuMatHeader() const;
operator GpuMat() const;
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
//if host memory can be mapped to gpu address space;
static bool canMapHostMemory();
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
int alloc_type;
2011-02-26 12:05:10 +01:00
};
2011-02-28 22:26:43 +01:00
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
.. index :: gpu::CudaMem::createMatHeader
2011-02-22 21:43:26 +01:00
2011-03-03 08:29:55 +01:00
gpu::CudaMem::createMatHeader
2011-02-26 12:05:10 +01:00
---------------------------------
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
.. cpp:function :: Mat gpu::CudaMem::createMatHeader() const
2011-03-29 01:16:20 +02:00
Creates a header without reference counting to :cpp:class: `gpu::CudaMem` data.
2011-02-22 21:43:26 +01:00
.. index :: gpu::CudaMem::createGpuMatHeader
2011-03-03 08:29:55 +01:00
gpu::CudaMem::createGpuMatHeader
2011-02-22 21:43:26 +01:00
------------------------------------
2011-03-23 11:56:20 +01:00
.. cpp:function :: GpuMat gpu::CudaMem::createGpuMatHeader() const
2011-04-01 00:07:17 +02:00
Maps CPU memory to GPU address space and creates the :cpp:class: `gpu::GpuMat` header without reference counting for it. This can be done only if memory was allocated with the `` ALLOC_ZEROCOPY `` flag and if it is supported by the hardware (laptops often share video and CPU memory, so address spaces can be mapped, which eliminates an extra copy).
2011-02-22 21:43:26 +01:00
.. index :: gpu::CudaMem::canMapHostMemory
2011-03-03 08:29:55 +01:00
gpu::CudaMem::canMapHostMemory
----------------------------------
2011-03-23 11:56:20 +01:00
.. cpp:function :: static bool gpu::CudaMem::canMapHostMemory()
2011-04-01 00:07:17 +02:00
Returns `` true `` if the current hardware supports address space mapping and `` ALLOC_ZEROCOPY `` memory allocation.
2011-03-23 11:56:20 +01:00
2011-02-22 21:43:26 +01:00
.. index :: gpu::Stream
gpu::Stream
-----------
2011-03-23 11:56:20 +01:00
.. cpp:class :: gpu::Stream
2011-02-22 21:43:26 +01:00
2011-04-01 00:07:17 +02:00
This class encapsulates a queue of asynchronous calls. Some functions have overloads with the additional `` gpu::Stream `` parameter. The overloads do initialization work (allocate output buffers, upload constants, and so on), start the GPU kernel, and return before results are ready. You can check whether all operations are complete via :cpp:func: `gpu::Stream::queryIfComplete` . You can asynchronously upload/download data from/to page-locked buffers, using the :cpp:class: `gpu::CudaMem` or :c:type: `Mat` header that points to a region of :cpp:class: `gpu::CudaMem` .
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
**Note:**
2011-04-01 00:07:17 +02:00
2011-03-29 01:16:20 +02:00
Currently, you may face problems if an operation is enqueued twice with different data. Some functions use the constant GPU memory, and next call may update the memory before the previous one has been finished. But calling different operations asynchronously is safe because each operation has its own constant buffer. Memory copy/upload/download/set operations to the buffers you hold are also safe.
::
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
class CV_EXPORTS Stream
2011-02-26 12:05:10 +01:00
{
public:
2011-03-29 01:16:20 +02:00
Stream();
~Stream();
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
Stream(const Stream&);
Stream& operator=(const Stream&);
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
bool queryIfComplete();
void waitForCompletion();
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
//! downloads asynchronously.
// Warning! cv::Mat must point to page locked memory
(i.e. to CudaMem data or to its subMat)
void enqueueDownload(const GpuMat& src, CudaMem& dst);
void enqueueDownload(const GpuMat& src, Mat& dst);
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
//! uploads asynchronously.
// Warning! cv::Mat must point to page locked memory
(i.e. to CudaMem data or to its ROI)
void enqueueUpload(const CudaMem& src, GpuMat& dst);
void enqueueUpload(const Mat& src, GpuMat& dst);
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
void enqueueCopy(const GpuMat& src, GpuMat& dst);
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
void enqueueMemSet(const GpuMat& src, Scalar val);
void enqueueMemSet(const GpuMat& src, Scalar val, const GpuMat& mask);
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
// converts matrix type, ex from float to uchar depending on type
void enqueueConvert(const GpuMat& src, GpuMat& dst, int type,
double a = 1, double b = 0);
2011-02-26 12:05:10 +01:00
};
2011-02-28 22:26:43 +01:00
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
.. index :: gpu::Stream::queryIfComplete
2011-02-22 21:43:26 +01:00
2011-03-03 08:29:55 +01:00
gpu::Stream::queryIfComplete
2011-02-26 12:05:10 +01:00
--------------------------------
2011-03-23 11:56:20 +01:00
.. cpp:function :: bool gpu::Stream::queryIfComplete()
2011-02-22 21:43:26 +01:00
2011-04-01 00:07:17 +02:00
Returns `` true `` if the current stream queue is finished. Otherwise, it returns false.
2011-02-22 21:43:26 +01:00
.. index :: gpu::Stream::waitForCompletion
2011-03-03 08:29:55 +01:00
gpu::Stream::waitForCompletion
2011-02-22 21:43:26 +01:00
----------------------------------
2011-03-23 11:56:20 +01:00
.. cpp:function :: void gpu::Stream::waitForCompletion()
2011-02-22 21:43:26 +01:00
2011-04-19 13:41:12 +02:00
Blocks the current CPU thread until all operations in the stream are complete.
2011-02-22 21:43:26 +01:00
2011-03-23 11:56:20 +01:00
.. index :: gpu::StreamAccessor
2011-02-22 21:43:26 +01:00
gpu::StreamAccessor
-------------------
2011-03-28 12:34:44 +02:00
.. cpp:class :: gpu::StreamAccessor
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
This class enables getting `` cudaStream_t `` from :cpp:class: `gpu::Stream` and is declared in `` stream_accessor.hpp `` because it is the only public header that depends on the CUDA Runtime API. Including it brings a dependency to your code.
::
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
struct StreamAccessor
{
2011-03-29 01:16:20 +02:00
CV_EXPORTS static cudaStream_t getStream(const Stream& stream);
2011-02-26 12:05:10 +01:00
};
2011-02-28 22:26:43 +01:00
2011-02-22 21:43:26 +01:00
.. index :: gpu::createContinuous
2011-03-03 08:29:55 +01:00
gpu::createContinuous
2011-02-22 21:43:26 +01:00
-------------------------
2011-03-23 11:56:20 +01:00
.. cpp:function :: void gpu::createContinuous(int rows, int cols, int type, GpuMat& m)
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
Creates a continuous matrix in the GPU memory.
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
:param rows: Row count.
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
:param cols: Column count.
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
:param type: Type of the matrix.
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
:param m: Destination matrix. This parameter changes only if it has a proper type and area (``rows x cols``).
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
The following wrappers are also available:
2011-04-01 00:07:17 +02:00
* .. cpp:function:: GpuMat gpu::createContinuous(int rows, int cols, int type)
* .. cpp:function:: void gpu::createContinuous(Size size, int type, GpuMat& m)
* .. cpp:function:: GpuMat gpu::createContinuous(Size size, int type)
2011-03-23 11:56:20 +01:00
2011-04-01 00:07:17 +02:00
Matrix is called continuous if its elements are stored continuously, that is without gaps in the end of each row.
2011-03-23 11:56:20 +01:00
2011-02-22 21:43:26 +01:00
.. index :: gpu::ensureSizeIsEnough
2011-03-03 08:29:55 +01:00
gpu::ensureSizeIsEnough
2011-02-22 21:43:26 +01:00
---------------------------
2011-03-23 11:56:20 +01:00
.. cpp:function :: void gpu::ensureSizeIsEnough(int rows, int cols, int type, GpuMat& m)
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
.. cpp:function :: void gpu::ensureSizeIsEnough(Size size, int type, GpuMat& m)
Ensures that the size of a matrix is big enough and the matrix has a proper type. The function does not reallocate memory if the matrix has proper attributes already.
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
:param rows: Minimum desired number of rows.
2011-02-22 21:43:26 +01:00
2011-03-29 01:16:20 +02:00
:param cols: Minimum desired number of columns.
2011-04-01 00:07:17 +02:00
:param size: Rows and coumns passed as a structure.
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
:param type: Desired matrix type.
2011-02-22 21:43:26 +01:00
2011-04-19 13:41:12 +02:00
:param m: Destination matrix.
2011-02-22 21:43:26 +01:00