【问题标题】:cv::gpu::GpuMat constructor fails if there is a thrust::reduce() call LATER in the codecv::gpu::GpuMat 构造函数失败,如果代码中有一个thrust::reduce() 调用LATER
【发布时间】:2016-09-17 15:20:37
【问题描述】:

我有一个 VS 2013 项目,我在其中使用(有些过时的)OpenCV 2.4.9 和 CUDA 7.5。我发现如果代码包含一些 - 但不是全部 - thrust 调用(尤其是thrust::reduce()),那么 OpenCV GPU 代码会停止工作,即使它在任何 thrust 调用之前执行也是如此。特别是,cv::gpu::GpuMat()cudaMallocPitch 调用中失败,在NULL 位置发生访问冲突。在敦促大家升级到最新的 OpenCV 版本之前,我想知道我是否遗漏了什么。 (这可能有帮助,也可能没有帮助。)

这是重现错误的或多或少的最小代码:

// main.cu
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <opencv2/gpu/gpu.hpp>
#include <thrust/device_vector.h>
#include <thrust/transform.h>
#include <thrust/reduce.h>
#include <thrust/functional.h>

#include <stdio.h>

int main()
{
    const int arraySize = 5;
    float fc[arraySize] = { 0 };
    float* dev_c;

    cv::Mat m = cv::Mat::eye(100,100,CV_32F);
    cv::gpu::GpuMat g(m);

    cudaMalloc((void**)&dev_c, arraySize * sizeof(int));
    cudaMemcpy(dev_c, fc, arraySize * sizeof(int), cudaMemcpyHostToDevice);
    thrust::device_ptr<float> dev_ptr = thrust::device_pointer_cast(dev_c);
    // the line below works fine
    thrust::transform(dev_ptr, dev_ptr + arraySize, dev_ptr, dev_ptr, thrust::multiplies<float>());
    // the line below causes cv::gpu::GpuMat to crash, but the program works if it is commented
    float sum2 = thrust::reduce(dev_ptr, dev_ptr + arraySize, 0, thrust::plus<float>());
    cudaFree(dev_c);
}

【问题讨论】:

  • 您确定不只是内存或资源不足?在推力调用中编译将大大增加您的应用程序所需的代码和资源量
  • 嗯,问题出在项目设置上。但是,仅仅在一个小阵列上进行几次推力调用,这怎么可能是内存问题呢?这将使图书馆几乎无法使用。无论如何,谢谢,我只是在学习 CUDA,我还需要学习如何监控 GPU 资源。
  • 在运行硬件加速显示管理器的小型笔记本电脑 GPU 上,您的 CUDA 应用程序可能有几百 Mb 的可用空间。在您的应用程序在 GPU 上执行任何操作之前,运行时必须预先分配运行时支持所需的所有内存(堆栈、每线程本地内存等)。多少取决于编译到您的应用程序中的代码。您会惊讶于在某些情况下可以留下这么少的内存

标签: c++ opencv cuda thrust


【解决方案1】:

哇,我决定研究一下项目设置,默认情况下CUDA代码生成设置为compute_20,sm_20。我尝试将其更改为compute_50,sm_50,因为我使用的是 GTX 750 Ti,并且 OpenCV 也使用 CUDA_ARCH_BIN 设置为 5.0 进行编译,现在一切正常。

【讨论】:

    猜你喜欢
    • 2012-02-26
    • 2015-08-10
    • 1970-01-01
    • 2016-07-01
    • 1970-01-01
    • 2016-09-11
    • 1970-01-01
    • 2011-03-24
    • 1970-01-01
    相关资源
    最近更新 更多