File: data_structures.rst

package info (click to toggle)
opencv 2.4.9.1%2Bdfsg-1%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 126,800 kB
  • ctags: 62,729
  • sloc: xml: 509,055; cpp: 490,794; lisp: 23,208; python: 21,174; java: 19,317; ansic: 1,038; sh: 128; makefile: 72
file content (388 lines) | stat: -rw-r--r-- 13,659 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
Data Structures
===============

.. highlight:: cpp



gpu::PtrStepSz
---------------
.. ocv:class:: gpu::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
    {
        int cols;
        int rows;
        T* data;
        size_t step;

        PtrStepSz() : cols(0), rows(0), data(0), step(0){};
        PtrStepSz(int rows, int cols, T *data, size_t step);

        template <typename U>
        explicit PtrStepSz(const PtrStepSz<U>& d);

        typedef T elem_type;
        enum { elem_size = sizeof(elem_type) };

        __CV_GPU_HOST_DEVICE__ size_t elemSize() const;

        /* returns pointer to the beginning of the given image row */
        __CV_GPU_HOST_DEVICE__ T* ptr(int y = 0);
        __CV_GPU_HOST_DEVICE__ const T* ptr(int y = 0) const;
    };

    typedef PtrStepSz<unsigned char> PtrStepSzb;
    typedef PtrStepSz<float> PtrStepSzf;
    typedef PtrStepSz<int> PtrStepSzi;



gpu::PtrStep
--------------
.. ocv:class:: gpu::PtrStep

Structure similar to :ocv:class:`gpu::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
    {
            T* data;
            size_t step;

            PtrStep();
            PtrStep(const PtrStepSz<T>& mem);

            typedef T elem_type;
            enum { elem_size = sizeof(elem_type) };

            __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;
    };

    typedef PtrStep<unsigned char> PtrStep;
    typedef PtrStep<float> PtrStepf;
    typedef PtrStep<int> PtrStepi;


gpu::GpuMat
-----------
.. ocv:class:: gpu::GpuMat

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

Beware that the latter limitation may lead to overloaded matrix operators that cause memory allocations. The ``GpuMat`` class is convertible to :ocv:class:`gpu::PtrStepSz` and :ocv:class:`gpu::PtrStep` so it can be passed directly to the kernel.

.. 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:
            //! default constructor
            GpuMat();

            GpuMat(int rows, int cols, int type);
            GpuMat(Size size, int type);

            .....

            //! builds GpuMat from Mat. Blocks uploading to device.
            explicit GpuMat (const Mat& m);

            //! 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;

            //! blocks uploading data to GpuMat.
            void upload(const cv::Mat& m);
            void upload(const CudaMem& m, Stream& stream);

            //! downloads data from device to host memory. Blocking calls.
            void download(cv::Mat& m) const;

            //! download async
            void download(CudaMem& m, Stream& stream) const;
    };


.. 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`



gpu::createContinuous
-------------------------
Creates a continuous matrix in the GPU memory.

.. ocv:function:: void gpu::createContinuous(int rows, int cols, int type, GpuMat& m)

.. ocv:function:: GpuMat gpu::createContinuous(int rows, int cols, int type)

.. ocv:function:: void gpu::createContinuous(Size size, int type, GpuMat& m)

.. ocv:function:: GpuMat gpu::createContinuous(Size size, int type)

    :param rows: Row count.

    :param cols: Column count.

    :param type: Type of the matrix.

    :param m: Destination matrix. This parameter changes only if it has a proper type and area ( :math:`\texttt{rows} \times \texttt{cols}` ).

Matrix is called continuous if its elements are stored continuously, that is, without gaps at the end of each row.



gpu::ensureSizeIsEnough
---------------------------
Ensures that the size of a matrix is big enough and the matrix has a proper type.

.. ocv:function:: void gpu::ensureSizeIsEnough(int rows, int cols, int type, GpuMat& m)

.. ocv:function:: void gpu::ensureSizeIsEnough(Size size, int type, GpuMat& m)

    :param rows: Minimum desired number of rows.

    :param cols: Minimum desired number of columns.

    :param size: Rows and columns passed as a structure.

    :param type: Desired matrix type.

    :param m: Destination matrix.

The function does not reallocate memory if the matrix has proper attributes already.



gpu::registerPageLocked
-------------------------------
Page-locks the memory of matrix and maps it for the device(s).

.. ocv:function:: void gpu::registerPageLocked(Mat& m)

    :param m: Input matrix.



gpu::unregisterPageLocked
-------------------------------
Unmaps the memory of matrix and makes it pageable again.

.. ocv:function:: void gpu::unregisterPageLocked(Mat& m)

    :param m: Input matrix.



gpu::CudaMem
------------
.. ocv:class:: gpu::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.

* **ALLOC_PAGE_LOCKED** sets a page locked memory type used commonly for fast and asynchronous uploading/downloading data from/to GPU.
* **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:: 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  { ALLOC_PAGE_LOCKED = 1, ALLOC_ZEROCOPY = 2,
                     ALLOC_WRITE_COMBINED = 4 };

            CudaMem(Size size, int type, int alloc_type = ALLOC_PAGE_LOCKED);

            //! creates from cv::Mat with coping data
            explicit CudaMem(const Mat& m, int alloc_type = ALLOC_PAGE_LOCKED);

             ......

            void create(Size size, int type, int alloc_type = ALLOC_PAGE_LOCKED);

            //! returns matrix header with disabled ref. counting for CudaMem data.
            Mat createMatHeader() const;
            operator Mat() const;

            //! maps host memory into device address space
            GpuMat createGpuMatHeader() const;
            operator GpuMat() const;

            //if host memory can be mapped to gpu address space;
            static bool canMapHostMemory();

            int alloc_type;
    };



gpu::CudaMem::createMatHeader
---------------------------------
Creates a header without reference counting to :ocv:class:`gpu::CudaMem` data.

.. ocv:function:: Mat gpu::CudaMem::createMatHeader() const



gpu::CudaMem::createGpuMatHeader
------------------------------------
Maps CPU memory to GPU address space and creates the :ocv:class:`gpu::GpuMat` header without reference counting for it.

.. ocv:function:: GpuMat gpu::CudaMem::createGpuMatHeader() const

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.



gpu::CudaMem::canMapHostMemory
----------------------------------
Returns ``true`` if the current hardware supports address space mapping and ``ALLOC_ZEROCOPY`` memory allocation.

.. ocv:function:: static bool gpu::CudaMem::canMapHostMemory()



gpu::Stream
-----------
.. ocv:class:: gpu::Stream

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 :ocv:func:`gpu::Stream::queryIfComplete`. You can asynchronously upload/download data from/to page-locked buffers, using the :ocv:class:`gpu::CudaMem` or :ocv:class:`Mat` header that points to a region of :ocv:class:`gpu::CudaMem`.

.. 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();
        ~Stream();

        Stream(const Stream&);
        Stream& operator=(const Stream&);

        bool queryIfComplete();
        void waitForCompletion();

        void enqueueDownload(const GpuMat& src, CudaMem& dst);
        void enqueueDownload(const GpuMat& src, Mat& dst);

        void enqueueUpload(const CudaMem& src, GpuMat& dst);
        void enqueueUpload(const Mat& src, GpuMat& dst);

        void enqueueCopy(const GpuMat& src, GpuMat& dst);

        void enqueueMemSet(const GpuMat& src, Scalar val);
        void enqueueMemSet(const GpuMat& src, Scalar val, const GpuMat& mask);

        void enqueueConvert(const GpuMat& src, GpuMat& dst, int type,
                            double a = 1, double b = 0);

        typedef void (*StreamCallback)(Stream& stream, int status, void* userData);
        void enqueueHostCallback(StreamCallback callback, void* userData);
    };



gpu::Stream::queryIfComplete
----------------------------
Returns ``true`` if the current stream queue is finished. Otherwise, it returns false.

.. ocv:function:: bool gpu::Stream::queryIfComplete()



gpu::Stream::waitForCompletion
------------------------------
Blocks the current CPU thread until all operations in the stream are complete.

.. ocv:function:: void gpu::Stream::waitForCompletion()



gpu::Stream::enqueueDownload
----------------------------
Copies data from device to host.

.. ocv:function:: void gpu::Stream::enqueueDownload(const GpuMat& src, CudaMem& dst)

.. ocv:function:: void gpu::Stream::enqueueDownload(const GpuMat& src, Mat& dst)

.. note:: ``cv::Mat`` must point to page locked memory (i.e. to ``CudaMem`` data or to its subMat) or must be registered with :ocv:func:`gpu::registerPageLocked` .



gpu::Stream::enqueueUpload
--------------------------
Copies data from host to device.

.. ocv:function:: void gpu::Stream::enqueueUpload(const CudaMem& src, GpuMat& dst)

.. ocv:function:: void gpu::Stream::enqueueUpload(const Mat& src, GpuMat& dst)

.. note:: ``cv::Mat`` must point to page locked memory (i.e. to ``CudaMem`` data or to its subMat) or must be registered with :ocv:func:`gpu::registerPageLocked` .



gpu::Stream::enqueueCopy
------------------------
Copies data from device to device.

.. ocv:function:: void gpu::Stream::enqueueCopy(const GpuMat& src, GpuMat& dst)



gpu::Stream::enqueueMemSet
--------------------------
Initializes or sets device memory to a value.

.. ocv:function:: void gpu::Stream::enqueueMemSet( GpuMat& src, Scalar val )

.. ocv:function:: void gpu::Stream::enqueueMemSet( GpuMat& src, Scalar val, const GpuMat& mask )



gpu::Stream::enqueueConvert
---------------------------
Converts matrix type, ex from float to uchar depending on type.

.. ocv:function:: void gpu::Stream::enqueueConvert( const GpuMat& src, GpuMat& dst, int dtype, double a=1, double b=0 )



gpu::Stream::enqueueHostCallback
--------------------------------
Adds a callback to be called on the host after all currently enqueued items in the stream have completed.

.. ocv:function:: void gpu::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.



gpu::StreamAccessor
-------------------
.. ocv:struct:: gpu::StreamAccessor

Class that enables getting ``cudaStream_t`` from :ocv: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. ::

    struct StreamAccessor
    {
        CV_EXPORTS static cudaStream_t getStream(const Stream& stream);
    };