尝试将struct复制到设备的内存时,CUDA无效参数(cudaMemcpy)

时间:2014-06-27 20:44:34

标签: c++ c cuda

我正在试图弄清楚如何创建一个注定要发送到设备的结构/类,但我不断得到这个“无效参数”CUDA错误。我做了一个显示错误的小例子:

#include <iostream>
#include <cstdio>
using namespace std;

#define CUDA_WARN(XXX) \
    do { if (XXX != cudaSuccess) cerr << "CUDA Error: " << \
        cudaGetErrorString(XXX) << ", at line " << __LINE__ \
        << endl; cudaDeviceSynchronize(); } while (0)

struct P {
    double x,y;
    __host__ __device__ void init(const double &a, const double &b) {
        x = a; y = b; }
};

int main(int argc, char **argv)
{
    P hP, hQ, dP;
    cout << "Size of P: " << sizeof(P) << endl;
    CUDA_WARN(cudaMalloc((void**) &dP, sizeof(P)));
    printf("dP: %p\n", &dP); // print dP's address on the device
    hP.init(1.2,-2.1);
    hQ.init(0.,0.);
    CUDA_WARN(cudaMemcpy(&dP, &hP, sizeof(P), cudaMemcpyHostToDevice));
    CUDA_WARN(cudaMemcpy(&hQ, &dP, sizeof(P), cudaMemcpyDeviceToHost));
    cout << "Copy back: " << hQ.x << "\t" << hQ.y << endl;
    dP.init(3.,3.);
    CUDA_WARN(cudaMemcpy(&hP, &dP, sizeof(P), cudaMemcpyDeviceToHost));
    cout << "Copy new:  " << hP.x << "\t" << hP.y << endl;
    return 0;
}

我正在编译(我的卡是特斯拉C2050):

nvcc -arch sm_20 -o exec file.cu

我得到的结果是:

Size of P: 16
dP: 0x7fff82d4b7b0
CUDA Error: invalid argument, at line 24
CUDA Error: invalid argument, at line 25
Copy back: 0    0
CUDA Error: invalid argument, at line 28
Copy new:  1.2  -2.1


------------------
(program exited with code: 0)
Press return to continue

谢谢你,如果你可以帮我这个!

======评论@talonmies,@ Jackolantern,@ Rortrt Crovella =======

谢谢,伙计们!你真的帮了!根据评论,我可以更正我的代码,现在它正在运行。只需注册最终解决方案:

#include <iostream>
#include <cstdio>
using namespace std;

#define CUDA_WARN(XXX) \
    do { if (XXX != cudaSuccess) cerr << "CUDA Error: " << \
        cudaGetErrorString(XXX) << ", at line " << __LINE__ \
        << endl; cudaDeviceSynchronize(); } while (0)

struct P {
    double x,y;
    __host__ __device__ void init(const double &a, const double &b) {
        x = a; y = b; }
};

/* INCLUDED KERNEL FUNCTION */
__global__ void dev_P_init(P *p, double a, double b) {
    p->init(a,b);
}

int main(int argc, char **argv)
{
    P hP, hQ, *dP; //*changed*
    cout << "Size of P: " << sizeof(P) << endl;
    CUDA_WARN(cudaMalloc((void**) &dP, sizeof(P)));
    printf("dP: %p\n", &dP); // print dP's address on the device
    hP.init(1.2,-2.1);
    hQ.init(0.,0.);
    CUDA_WARN(cudaMemcpy(dP, &hP, sizeof(P), cudaMemcpyHostToDevice)); //*changed*
    CUDA_WARN(cudaMemcpy(&hQ, dP, sizeof(P), cudaMemcpyDeviceToHost)); //*changed*
    cout << "Copy back: " << hQ.x << "\t" << hQ.y << endl;
    dev_P_init<<< 1, 1 >>>(dP,3., 3.); //*call to kernel*
    CUDA_WARN(cudaMemcpy(&hP, dP, sizeof(P), cudaMemcpyDeviceToHost)); //*changed*
    cout << "Copy new:  " << hP.x << "\t" << hP.y << endl;
    return 0;
}

并更正了输出:

Size of P: 16
dP: 0x7fff6fa2e498
Copy back: 1.2  -2.1
Copy new:  3    3


------------------
(program exited with code: 0)
Press return to continue

1 个答案:

答案 0 :(得分:2)

正如@talonmies已经注意到的那样,&dP不是有效的设备指针。实际上,dP是驻留在主机上的变量,因此其地址指向主机内存空间。与此相反,当dP是指针时,cudaMalloc将接收其值作为参数,其值将指向设备存储空间。

这是您的代码的正确版本:

#include <iostream>
#include <cstdio>
using namespace std;

#define CUDA_WARN(XXX) \
    do { if (XXX != cudaSuccess) cerr << "CUDA Error: " << \
    cudaGetErrorString(XXX) << ", at line " << __LINE__ \
    << endl; cudaDeviceSynchronize(); } while (0)

struct P {
    double x,y;
    __host__ __device__ void init(const double &a, const double &b) {
    x = a; y = b; }
};

int main(int argc, char **argv)
{
    P *dP;
    P hP, hQ;
    CUDA_WARN(cudaMalloc((void**) &dP, sizeof(P)));
    CUDA_WARN(cudaMemcpy(dP, &hP, sizeof(P), cudaMemcpyHostToDevice));
    CUDA_WARN(cudaMemcpy(&hQ, dP, sizeof(P), cudaMemcpyDeviceToHost));
    CUDA_WARN(cudaMemcpy(&hP, dP, sizeof(P), cudaMemcpyDeviceToHost));

    return 0;
}