【发布时间】:2022-06-30 16:52:37
【问题描述】:
我想:
- 将数据上传到 CUDA 世界
- 执行多个 CUDA 操作(gemm、阈值、dft 等)
- 将结果下载到 CPU 世界
如何以最佳方式优化 CUDA 块部分 有没有办法调用.cu代码?
这是我正在做的一个例子
cv::cuda::GpuMat _emptyGpuMat;
cv::cuda::GpuMat _resultGPU;
cv::cuda::Stream GPUstream;
// -----------------------------
// Upload CPU data to the GPU
// -----------------------------
_mat1GPU.upload(_mat1);
_mat2GPU.upload(_mat2);
const auto _startTimeGPU = std::chrono::high_resolution_clock::now();
// to show several things done in a block of CUDA operations
{
cv::cuda::gemm(_mat1GPU, _mat2GPU, 1.0, _emptyGpuMat, 0.0, _resultGPU,0, GPUstream);
cv::cuda::threshold(_mat2GPU, _mat2GPU, .01, std::numeric_limits<double>::max(), cv::THRESH_TOZERO);
}
GPUstream.waitForCompletion();
// -----------------------------
// Download GPU data to the CPU
// -----------------------------
cv::Mat _matResult;
_resultGPU.download(_matResult);
(void)_matResult;
// ---------------------------------------------------------------
// Deallocate data here, otherwise deallocation will be performed
// after context is extracted from the stack
// ---------------------------------------------------------------
_mat1GPU.release();
_mat2GPU.release();
_resultGPU.release();
【问题讨论】: