Open Source Computer Vision Library https://opencv.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

312 lines
11 KiB

Data Structures
===============
.. highlight:: cpp
13 years ago
11 years ago
cuda::PtrStepSz
---------------
.. ocv:class:: cuda::PtrStepSz
Lightweight class encapsulating pitched memory on a GPU and 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. ::
template <typename T> struct PtrStepSz : public PtrStep<T>
{
__CV_GPU_HOST_DEVICE__ PtrStepSz() : cols(0), rows(0) {}
__CV_GPU_HOST_DEVICE__ PtrStepSz(int rows_, int cols_, T* data_, size_t step_)
: PtrStep<T>(data_, step_), cols(cols_), rows(rows_) {}
template <typename U>
explicit PtrStepSz(const PtrStepSz<U>& d) : PtrStep<T>((T*)d.data, d.step), cols(d.cols), rows(d.rows){}
int cols;
int rows;
};
typedef PtrStepSz<unsigned char> PtrStepSzb;
typedef PtrStepSz<float> PtrStepSzf;
typedef PtrStepSz<int> PtrStepSzi;
11 years ago
cuda::PtrStep
-------------
.. ocv:class:: cuda::PtrStep
11 years ago
Structure similar to :ocv:class:`cuda::PtrStepSz` but containing 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. ::
template <typename T> struct PtrStep : public DevPtr<T>
{
__CV_GPU_HOST_DEVICE__ PtrStep() : step(0) {}
__CV_GPU_HOST_DEVICE__ PtrStep(T* data_, size_t step_) : DevPtr<T>(data_), step(step_) {}
//! stride between two consecutive rows in bytes. Step is stored always and everywhere in bytes!!!
size_t step;
__CV_GPU_HOST_DEVICE__ T* ptr(int y = 0) { return ( T*)( ( char*)DevPtr<T>::data + y * step); }
__CV_GPU_HOST_DEVICE__ const T* ptr(int y = 0) const { return (const T*)( (const char*)DevPtr<T>::data + y * step); }
__CV_GPU_HOST_DEVICE__ T& operator ()(int y, int x) { return ptr(y)[x]; }
__CV_GPU_HOST_DEVICE__ const T& operator ()(int y, int x) const { return ptr(y)[x]; }
};
typedef PtrStep<unsigned char> PtrStepb;
typedef PtrStep<float> PtrStepf;
typedef PtrStep<int> PtrStepi;
11 years ago
cuda::GpuMat
------------
.. ocv:class:: cuda::GpuMat
13 years ago
Base storage class for GPU memory with reference counting. Its interface matches the :ocv:class:`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
11 years ago
Beware that the latter limitation may lead to overloaded matrix operators that cause memory allocations. The ``GpuMat`` class is convertible to :ocv:class:`cuda::PtrStepSz` and :ocv:class:`cuda::PtrStep` so it can be passed directly to the kernel.
13 years ago
.. note:: In contrast with :ocv:class:`Mat`, in most cases ``GpuMat::isContinuous() == false`` . This means that rows are aligned to a size depending on the hardware. Single-row ``GpuMat`` is always a continuous matrix.
::
class CV_EXPORTS GpuMat
{
public:
11 years ago
//! default constructor
GpuMat();
11 years ago
//! constructs GpuMat of the specified size and type
GpuMat(int rows, int cols, int type);
GpuMat(Size size, int type);
11 years ago
.....
11 years ago
//! builds GpuMat from host memory (Blocking call)
explicit GpuMat(InputArray arr);
11 years ago
//! returns lightweight PtrStepSz structure for passing
//to nvcc-compiled code. Contains size, data ptr and step.
template <class T> operator PtrStepSz<T>() const;
template <class T> operator PtrStep<T>() const;
11 years ago
//! pefroms upload data to GpuMat (Blocking call)
void upload(InputArray arr);
11 years ago
//! pefroms upload data to GpuMat (Non-Blocking call)
void upload(InputArray arr, Stream& stream);
11 years ago
//! pefroms download data from device to host memory (Blocking call)
void download(OutputArray dst) const;
11 years ago
//! pefroms download data from device to host memory (Non-Blocking call)
void download(OutputArray dst, Stream& stream) const;
};
13 years ago
.. note:: 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.
.. seealso:: :ocv:class:`Mat`
11 years ago
cuda::createContinuous
----------------------
Creates a continuous matrix.
13 years ago
11 years ago
.. ocv:function:: void cuda::createContinuous(int rows, int cols, int type, OutputArray arr)
13 years ago
:param rows: Row count.
:param cols: Column count.
:param type: Type of the matrix.
:param arr: Destination matrix. This parameter changes only if it has a proper type and area ( :math:`\texttt{rows} \times \texttt{cols}` ).
13 years ago
Matrix is called continuous if its elements are stored continuously, that is, without gaps at the end of each row.
11 years ago
cuda::ensureSizeIsEnough
------------------------
13 years ago
Ensures that the size of a matrix is big enough and the matrix has a proper type.
11 years ago
.. ocv:function:: void cuda::ensureSizeIsEnough(int rows, int cols, int type, OutputArray arr)
13 years ago
:param rows: Minimum desired number of rows.
:param cols: Minimum desired number of columns.
:param type: Desired matrix type.
:param arr: Destination matrix.
13 years ago
The function does not reallocate memory if the matrix has proper attributes already.
11 years ago
cuda::CudaMem
-------------
.. ocv:class:: cuda::CudaMem
Class with reference counting wrapping special memory type allocation functions from CUDA. Its interface is also :ocv:func:`Mat`-like but with additional memory type parameters.
* **PAGE_LOCKED** sets a page locked memory type used commonly for fast and asynchronous uploading/downloading data from/to GPU.
* **SHARED** specifies a zero copy memory allocation that enables mapping the host memory to GPU address space, if supported.
* **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.
13 years ago
.. note:: 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*.
::
class CV_EXPORTS CudaMem
{
public:
enum AllocType { PAGE_LOCKED = 1, SHARED = 2, WRITE_COMBINED = 4 };
explicit CudaMem(AllocType alloc_type = PAGE_LOCKED);
CudaMem(int rows, int cols, int type, AllocType alloc_type = PAGE_LOCKED);
CudaMem(Size size, int type, AllocType alloc_type = PAGE_LOCKED);
//! creates from host memory with coping data
explicit CudaMem(InputArray arr, AllocType alloc_type = PAGE_LOCKED);
......
//! returns matrix header with disabled reference counting for CudaMem data.
Mat createMatHeader() const;
//! maps host memory into device address space and returns GpuMat header for it. Throws exception if not supported by hardware.
GpuMat createGpuMatHeader() const;
......
AllocType alloc_type;
};
11 years ago
cuda::CudaMem::createMatHeader
------------------------------
Creates a header without reference counting to :ocv:class:`cuda::CudaMem` data.
11 years ago
.. ocv:function:: Mat cuda::CudaMem::createMatHeader() const
11 years ago
cuda::CudaMem::createGpuMatHeader
---------------------------------
Maps CPU memory to GPU address space and creates the :ocv:class:`cuda::GpuMat` header without reference counting for it.
11 years ago
.. ocv:function:: GpuMat cuda::CudaMem::createGpuMatHeader() const
This can be done only if memory was allocated with the ``SHARED`` 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.
13 years ago
11 years ago
cuda::registerPageLocked
------------------------
Page-locks the memory of matrix and maps it for the device(s).
11 years ago
.. ocv:function:: void cuda::registerPageLocked(Mat& m)
:param m: Input matrix.
11 years ago
cuda::unregisterPageLocked
--------------------------
Unmaps the memory of matrix and makes it pageable again.
11 years ago
.. ocv:function:: void cuda::unregisterPageLocked(Mat& m)
13 years ago
:param m: Input matrix.
11 years ago
cuda::Stream
------------
.. ocv:class:: cuda::Stream
This class encapsulates a queue of asynchronous calls.
13 years ago
.. note:: 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.
::
class CV_EXPORTS Stream
{
public:
Stream();
//! queries an asynchronous stream for completion status
bool queryIfComplete() const;
//! waits for stream tasks to complete
void waitForCompletion();
//! makes a compute stream wait on an event
void waitEvent(const Event& event);
//! adds a callback to be called on the host after all currently enqueued items in the stream have completed
void enqueueHostCallback(StreamCallback callback, void* userData);
//! return Stream object for default CUDA stream
static Stream& Null();
//! returns true if stream object is not default (!= 0)
operator bool_type() const;
};
11 years ago
cuda::Stream::queryIfComplete
-----------------------------
13 years ago
Returns ``true`` if the current stream queue is finished. Otherwise, it returns false.
11 years ago
.. ocv:function:: bool cuda::Stream::queryIfComplete()
11 years ago
cuda::Stream::waitForCompletion
-------------------------------
13 years ago
Blocks the current CPU thread until all operations in the stream are complete.
11 years ago
.. ocv:function:: void cuda::Stream::waitForCompletion()
11 years ago
cuda::Stream::waitEvent
-----------------------
Makes a compute stream wait on an event.
11 years ago
.. ocv:function:: void cuda::Stream::waitEvent(const Event& event)
11 years ago
cuda::Stream::enqueueHostCallback
---------------------------------
Adds a callback to be called on the host after all currently enqueued items in the stream have completed.
11 years ago
.. ocv:function:: void cuda::Stream::enqueueHostCallback(StreamCallback callback, void* userData)
.. note:: Callbacks must not make any CUDA API calls. Callbacks must not perform any synchronization that may depend on outstanding device work or other callbacks that are not mandated to run earlier. Callbacks without a mandated order (in independent streams) execute in undefined order and may be serialized.
11 years ago
cuda::StreamAccessor
--------------------
.. ocv:struct:: cuda::StreamAccessor
11 years ago
Class that enables getting ``cudaStream_t`` from :ocv:class:`cuda::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. ::
struct StreamAccessor
{
CV_EXPORTS static cudaStream_t getStream(const Stream& stream);
};