如果稍后在代码中调用 thrust::reduce(),cv::gpu::GpuMat 构造函数将失败

cv::gpu::GpuMat constructor fails if there is a thrust::reduce() call LATER in the code

我有一个 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代码生成设置为compute_20,sm_20。我尝试将其更改为 compute_50,sm_50,因为我使用的是 GTX 750 Ti,并且 OpenCV 也是在 CUDA_ARCH_BIN 设置为 5.0 的情况下编译的,现在一切正常。