OpenCV 4.12.0
開源計算機視覺
載入中...
搜尋中...
無匹配項
cv::cuda::BufferPool 類參考

用於 CUDA 流的 BufferPool更多...

#include <opencv2/core/cuda.hpp>

cv::cuda::BufferPool 協作圖

公共成員函式

 BufferPool (Stream &stream)
 獲取給定流的 BufferPool
 
Ptr< GpuMat::AllocatorgetAllocator () const
 返回與流關聯的分配器。
 
GpuMat getBuffer (int rows, int cols, int type)
 分配指定大小和型別的新 GpuMat
 
GpuMat getBuffer (Size size, int type)
 分配指定大小和型別的新 GpuMat
 

詳細描述

用於 CUDA 流的 BufferPool

BufferPool 利用 Stream 的分配器為 GpuMat 建立新緩衝區。它只有在使用 setBufferPoolUsage 啟用時才有用。

void setBufferPoolUsage(bool on)
BufferPool 管理(必須在 Stream 建立之前呼叫)
注意
setBufferPoolUsage 必須在任何 Stream 宣告之前呼叫。

使用者可以為 Stream 指定自定義分配器,並可以實現他們自己的利用相同底層 GPU 記憶體管理的基於流的函式。

如果未指定自定義分配器,BufferPool 預設使用 StackAllocator。StackAllocator 預先分配一塊 GPU 裝置記憶體,當 GpuMat 稍後宣告時,它會獲得預分配的記憶體。這種策略減少了對記憶體分配 API(如 cudaMalloc 或 cudaMallocPitch)的呼叫次數。

下面是一個利用 BufferPool 與 StackAllocator 的示例:

#include <opencv2/opencv.hpp>
using namespace cv;
using namespace cv::cuda
int main()
{
setBufferPoolUsage(true); // 告訴 OpenCV 我們將使用 BufferPool
setBufferPoolConfig(getDevice(), 1024 * 1024 * 64, 2); // 分配 64 MB,2 個棧(預設是 10 MB,5 個棧)
Stream stream1, stream2; // 每個流使用 1 個棧
BufferPool pool1(stream1), pool2(stream2);
GpuMat d_src1 = pool1.getBuffer(4096, 4096, CV_8UC1); // 16MB
GpuMat d_dst1 = pool1.getBuffer(4096, 4096, CV_8UC3); // 48MB,pool1 已滿
GpuMat d_src2 = pool2.getBuffer(1024, 1024, CV_8UC1); // 1MB
GpuMat d_dst2 = pool2.getBuffer(1024, 1024, CV_8UC3); // 3MB
cvtColor(d_src1, d_dst1, cv::COLOR_GRAY2BGR, 0, stream1);
cvtColor(d_src2, d_dst2, cv::COLOR_GRAY2BGR, 0, stream2);
}
用於 CUDA 流的 BufferPool。
定義 cuda.hpp:748
帶引用計數的 GPU 記憶體的基本儲存類。
定義 cuda.hpp:106
此類封裝了一個非同步呼叫佇列。
定義 cuda.hpp:917
#define CV_8UC1
定義 interface.h:88
CV_8UC3
#define CV_8UC3
int getDevice()
返回由 cuda::setDevice 設定或預設初始化的當前裝置索引。
void setBufferPoolConfig(int deviceId, size_t stackSize, int stackCount)
void cvtColor(InputArray src, OutputArray dst, int code, int dcn=0, Stream &stream=Stream::Null())
將影像從一個顏色空間轉換為另一個顏色空間。
@ COLOR_GRAY2BGR
定義 imgproc.hpp:559
int main(int argc, char *argv[])
定義 highgui_qt.cpp:3
定義 cuda.hpp:65
定義 core.hpp:107

如果在上面的示例中,我們在 pool1 上分配另一個 GpuMat,它將由 DefaultAllocator 執行,因為 pool1 的棧已滿。

GpuMat d_add1 = pool1.getBuffer(1024, 1024, CV_8UC1); // pool1 的棧已滿,記憶體由 DefaultAllocator 分配

如果在上面的示例中聲明瞭第三個流,那麼在該流中用 getBuffer 進行分配也將由 DefaultAllocator 執行,因為我們已用完棧。

Stream stream3; // 只分配了 2 個棧,我們已用完棧
BufferPool pool3(stream3);
GpuMat d_src3 = pool3.getBuffer(1024, 1024, CV_8UC1); // 記憶體由 DefaultAllocator 分配
警告
當使用 StackAllocator 時,釋放順序很重要。

就像棧一樣,釋放必須以 LIFO(後進先出)順序完成。下面是一個違反 LIFO 規則的錯誤用法示例。如果 OpenCV 以除錯模式編譯,此示例程式碼將發出 CV_Assert 錯誤。

int main()
{
setBufferPoolUsage(true); // 告訴 OpenCV 我們將使用 BufferPool
Stream stream; // 為此流分配了預設大小(10 MB)的棧
BufferPool pool(stream);
GpuMat mat1 = pool.getBuffer(1024, 1024, CV_8UC1); // 分配 mat1 (1MB)
GpuMat mat2 = pool.getBuffer(1024, 1024, CV_8UC1); // 分配 mat2 (1MB)
mat1.release(); // 錯誤用法:mat2 必須在 mat1 之前釋放
}
void release()
減少引用計數,當引用計數達到 0 時釋放資料

由於 C++ 區域性變數以與構造相反的順序銷燬,下面的程式碼示例滿足 LIFO 規則。區域性 GpuMat 被釋放,相應的記憶體會自動返回到池中以供後續使用。

int main()
{
setBufferPoolUsage(true); // 告訴 OpenCV 我們將使用 BufferPool
setBufferPoolConfig(getDevice(), 1024 * 1024 * 64, 2); // 分配 64 MB,2 個棧(預設是 10 MB,5 個棧)
Stream stream1, stream2; // 每個流使用 1 個棧
BufferPool pool1(stream1), pool2(stream2);
for (int i = 0; i < 10; i++)
{
GpuMat d_src1 = pool1.getBuffer(4096, 4096, CV_8UC1); // 16MB
GpuMat d_dst1 = pool1.getBuffer(4096, 4096, CV_8UC3); // 48MB,pool1 已滿
GpuMat d_src2 = pool2.getBuffer(1024, 1024, CV_8UC1); // 1MB
GpuMat d_dst2 = pool2.getBuffer(1024, 1024, CV_8UC3); // 3MB
d_src1.setTo(Scalar(i), stream1);
d_src2.setTo(Scalar(i), stream2);
cvtColor(d_src1, d_dst1, cv::COLOR_GRAY2BGR, 0, stream1);
cvtColor(d_src2, d_dst2, cv::COLOR_GRAY2BGR, 0, stream2);
// 區域性變數的銷燬順序是
// d_dst2 => d_src2 => d_dst1 => d_src1
// 滿足 LIFO 規則,此程式碼執行無誤
}
}
GpuMat & setTo(Scalar s)
將某些 GpuMat 元素設定為 s (阻塞呼叫)
Scalar_< double > Scalar
定義 types.hpp:709

建構函式 & 解構函式文件

◆ BufferPool()

cv::cuda::BufferPool::BufferPool ( Stream & )
顯式
Python
cv.cuda.BufferPool() -> <cuda_BufferPool object>

獲取給定流的 BufferPool

成員函式文件

◆ getAllocator()

Ptr< GpuMat::Allocator > cv::cuda::BufferPool::getAllocator ( ) const
inline
Python
cv.cuda.BufferPool.getAllocator() -> retval

返回與流關聯的分配器。

◆ getBuffer() [1/2]

GpuMat cv::cuda::BufferPool::getBuffer ( int rows,
int cols,
int type )
Python
cv.cuda.BufferPool.getBuffer(rows, cols, type) -> retval
cv.cuda.BufferPool.getBuffer(size, type) -> retval

分配指定大小和型別的新 GpuMat

◆ getBuffer() [2/2]

GpuMat cv::cuda::BufferPool::getBuffer ( Size size,
int type )
inline
Python
cv.cuda.BufferPool.getBuffer(rows, cols, type) -> retval
cv.cuda.BufferPool.getBuffer(size, type) -> retval

分配指定大小和型別的新 GpuMat

此函式的呼叫圖如下

此類的文件是從以下檔案生成的