OpenCV
3.4.3
Open Source Computer Vision
|
BufferPool for use with CUDA streams. More...
#include "cuda.hpp"
Public Member Functions | |
BufferPool (Stream &stream) | |
Gets the BufferPool for the given stream. More... | |
Ptr< GpuMat::Allocator > | getAllocator () const |
Returns the allocator associated with the stream. More... | |
GpuMat | getBuffer (int rows, int cols, int type) |
Allocates a new GpuMat of given size and type. More... | |
GpuMat | getBuffer (Size size, int type) |
Allocates a new GpuMat of given size and type. More... | |
BufferPool for use with CUDA streams.
BufferPool utilizes Stream's allocator to create new buffers for GpuMat's. It is only useful when enabled with setBufferPoolUsage.
Users may specify custom allocator for Stream and may implement their own stream based functions utilizing the same underlying GPU memory management.
If custom allocator is not specified, BufferPool utilizes StackAllocator by default. StackAllocator allocates a chunk of GPU device memory beforehand, and when GpuMat is declared later on, it is given the pre-allocated memory. This kind of strategy reduces the number of calls for memory allocating APIs such as cudaMalloc or cudaMallocPitch.
Below is an example that utilizes BufferPool with StackAllocator:
If we allocate another GpuMat on pool1 in the above example, it will be carried out by the DefaultAllocator since the stack for pool1 is full.
If a third stream is declared in the above example, allocating with getBuffer within that stream will also be carried out by the DefaultAllocator because we've run out of stacks.
Just like a stack, deallocation must be done in LIFO order. Below is an example of erroneous usage that violates LIFO rule. If OpenCV is compiled in Debug mode, this sample code will emit CV_Assert error.
Since C++ local variables are destroyed in the reverse order of construction, the code sample below satisfies the LIFO rule. Local GpuMat's are deallocated and the corresponding memory is automatically returned to the pool for later usage.
|
explicit |
Gets the BufferPool for the given stream.
|
inline |
Returns the allocator associated with the stream.
GpuMat cv::cuda::BufferPool::getBuffer | ( | int | rows, |
int | cols, | ||
int | type | ||
) |
Allocates a new GpuMat of given size and type.
Allocates a new GpuMat of given size and type.