how to use gpu::Stream in OpenCV?(如何在 OpenCV 中使用 gpu::Stream?)
问题描述
OpenCV 具有封装异步调用队列的 gpu::Stream
类.某些函数具有带有附加 gpu::Stream
参数的重载.除了 gpu-basics-similarity.cpp 示例代码,OpenCV 文档中关于如何以及何时使用 gpu::Stream
的信息很少.例如,(对我来说)不是很清楚 gpu::Stream::enqueueConvert
或 gpu::Stream::enqueueCopy
究竟是做什么的,或者如何使用 gpu::Stream
作为额外的重载参数.我正在寻找一些类似教程的 gpu::Stream
概述.
OpenCV has gpu::Stream
class that encapsulates a queue of asynchronous calls. Some functions have overloads with the additional gpu::Stream
parameter. Aside from gpu-basics-similarity.cpp sample code, there is very little information in OpenCV documentation on how and when to use gpu::Stream
. For example, it is not very clear (to me) what exactly gpu::Stream::enqueueConvert
or gpu::Stream::enqueueCopy
do, or how to use gpu::Stream
as additional overload parameter. I'm looking for some tutorial-like overview of gpu::Stream
.
推荐答案
默认情况下所有 gpu 模块功能都是同步的,即当前 CPU 线程被阻塞,直到操作完成.
By default all gpu module functions are synchronous, i.e. current CPU thread is blocked until operation finishes.
gpu::Stream
是 cudaStream_t
的包装器,允许使用异步非阻塞调用.CUDA异步并发执行的详细信息也可以阅读《CUDA C编程指南》.
gpu::Stream
is a wrapper for cudaStream_t
and allows to use asynchronous non-blocking call. You can also read "CUDA C Programming Guide" for detailed information about CUDA asynchronous concurrent execution.
大多数 gpu 模块函数都有额外的 gpu::Stream
参数.如果传递非默认流,函数调用将是异步的,调用将被添加到流命令队列中.
Most gpu module functions have additional gpu::Stream
parameter. If you pass non-default stream the function call will be asynchronous, and the call will be added to stream command queue.
还有 gpu::Stream
为 CPU<->GPU
和 GPU<->GPU
之间的异步内存传输提供方法.但是 CPU<->GPU
异步内存传输仅适用于页面锁定的主机内存.还有一个gpu::CudaMem
类封装了这种内存.
Also gpu::Stream
provides methos for asynchronous memory transfers between CPU<->GPU
and GPU<->GPU
. But CPU<->GPU
asynchronous memory transfers works only with page-locked host memory. There is another class gpu::CudaMem
that encapsulates such memory.
目前,如果相同的操作将不同的数据排入不同的流两次,您可能会遇到问题.一些函数使用常量或纹理 GPU 内存,下一次调用可能会在前一次调用完成之前更新内存.但是异步调用不同的操作是安全的,因为每个操作都有自己的常量缓冲区.对您持有的缓冲区进行内存复制/上传/下载/设置操作也是安全的.
Currently, you may face problems if same operation is enqueued twice with different data to different streams. Some functions use the constant or texture 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.
这是小样本:
// allocate page-locked memory
CudaMem host_src_pl(768, 1024, CV_8UC1, CudaMem::ALLOC_PAGE_LOCKED);
CudaMem host_dst_pl;
// get Mat header for CudaMem (no data copy)
Mat host_src = host_src_pl;
// fill mat on CPU
someCPUFunc(host_src);
GpuMat gpu_src, gpu_dst;
// create Stream object
Stream stream;
// next calls are non-blocking
// first upload data from host
stream.enqueueUpload(host_src_pl, gpu_src);
// perform blur
blur(gpu_src, gpu_dst, Size(5,5), Point(-1,-1), stream);
// download result back to host
stream.enqueueDownload(gpu_dst, host_dst_pl);
// call another CPU function in parallel with GPU
anotherCPUFunc();
// wait GPU for finish
stream.waitForCompletion();
// now you can use GPU results
Mat host_dst = host_dst_pl;
这篇关于如何在 OpenCV 中使用 gpu::Stream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 OpenCV 中使用 gpu::Stream?
基础教程推荐
- C++,'if' 表达式中的变量声明 2021-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- 运算符重载的基本规则和习语是什么? 2022-10-31
- 设计字符串本地化的最佳方法 2022-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01