CudaMemCpy在复制vector <cv :: point3f>时返回cudaErrorInvalidValue

时间:2015-05-18 13:39:41

标签: c++ opencv cuda

CudaMemCpy在将矢量复制到设备上时返回cudaErrorInvalidValue。我试过给“&amp; input”,“&amp; input [0]”,...我总是得到同样的错误,但不明白为什么?

您可以使用cudaMemcpy复制矢量吗?或者我是否需要先在新数组中复制该矢量的内容?

void computeDepthChangeMap(unsigned char* depthChangeMap, size_t size, std::vector<cv::Point3f>* input, float dcf, int width, int height)                                           {
    unsigned char* dev_depthChangeMap = 0;
    float* dev_dcf = 0;
    int* dev_wdt = 0;
    int arraySize = size;
    cv::Point3f* dev_input = 0;
    cudaError_t cudaStatus;

    cudaStatus = cudaSetDevice(0);
    cudaStatus = cudaMalloc((void**)&dev_depthChangeMap, size);
    cudaStatus = cudaMalloc((void**)&dev_input, size);
    cudaStatus = cudaMalloc((void**)&dev_dcf, sizeof(float));
    cudaStatus = cudaMalloc((void**)&dev_wdt, sizeof(int));

    cudaStatus = cudaMemcpy(dev_depthChangeMap, depthChangeMap, size, cudaMemcpyHostToDevice);
    cudaStatus = cudaMemcpy(dev_wdt, &width, sizeof(int), cudaMemcpyHostToDevice);
    cudaStatus = cudaMemcpy(dev_dcf, &dcf, sizeof(float), cudaMemcpyHostToDevice);
    cudaStatus = cudaMemcpy(dev_input, &input[0], sizeof(cv::Point3f)*size, cudaMemcpyHostToDevice);

    //cuaStatus returns cudaErrorInvalidValue >> PROBLEM HERE << 

    dim3 threadsPerBlock(8, 8); //init x, y
    dim3 numBlocks(width / threadsPerBlock.x, height / threadsPerBlock.y);

    addKernel <<<numBlocks, threadsPerBlock >>>(dev_depthChangeMap, dev_dcf, dev_input, dev_wdt);


    cudaStatus = cudaGetLastError();   
    cudaStatus = cudaDeviceSynchronize();
    cudaStatus = cudaMemcpy(depthChangeMap, dev_depthChangeMap, size, cudaMemcpyDeviceToHost);
}

__global__ void addKernel(unsigned char* dev_depthChangeMap, float* dcf, cv::Point3f* inp, int* wdt)
{
    register int row_idx = (blockIdx.x * blockDim.x) + threadIdx.x;
    register int col_idx = (blockIdx.y * blockDim.y) + threadIdx.y;
    register int idx = row_idx * (*wdt) + col_idx;

    register float depth = inp[idx].z;
    register float depthR = inp[idx + 1].z;
    register float depthD = inp[idx + *wdt].z;

    //and so on

}

1 个答案:

答案 0 :(得分:3)

是的,您可以使用std::vectorcudaMemcpy复制。

您的尺寸设置不正确:

void computeDepthChangeMap(unsigned char* depthChangeMap, size_t size, std::vector<cv::Point3f>* input, float dcf, int width, int height)                                           {

...
cudaStatus = cudaMalloc((void**)&dev_input, size);
                                            ^^^^

cudaStatus = cudaMemcpy(dev_input, &input[0], sizeof(cv::Point3f)*size, cudaMemcpyHostToDevice);
                                                     ^^^^^^^^^^^^^^^^^

这些大小参数都应该是 bytes 。您不能将长度为sizeof(cv::Point3f)*size个字节的数据复制到长度为size个字节的分配中。

此外,您的函数参数似乎是一个指向矢量的指针:

std::vector<cv::Point3f>* input,

根据您显示的代码,这可能不是您想要的。您可能要么按值传递

std::vector<cv::Point3f> input,

或更有可能,通过引用

std::vector<cv::Point3f> &input,

由于您还没有显示您打算如何调用此功能,因此无法完全确定此处的最佳功能。