CUDA - Transfering CPU variables to GPU __constant

2020-04-13 17:22发布

问题:

As anything with CUDA, the most basic things are sometimes the hardest...

So...I just want to copy a variable from the CPU to a GPU's constant variable, and I am having a hard time.

This is what i have:

__constant__ int contadorlinhasx_d;

int main(){

(...)
int contadorlinhasx=100;
     status=cudaMemcpyToSymbol(contadorlinhasx_d,contadorlinhasx,1*sizeof(int),0,cudaMemcpyHostToDevice);

And i get this error

presortx.cu(222): error: no instance of overloaded function "cudaMemcpyToSymbol" matches the argument list
        argument types are: (int, int, unsigned long, int, cudaMemcpyKind)

Could anyone help me? I know it is some stupid error, but I am tired of googling it, and I have spent almost 30 minutes just trying to copy a stupid variable :/

Thanks in advance

回答1:

You need to do something like

cudaMemcpyToSymbol("contadorlinhasx_d",
                   &contadorlinhasx,
                   1*sizeof(int),
                   0,
                   cudaMemcpyHostToDevice);

[Note this is the old API call, now deprecated in CUDA 4.0 and newer]

or

cudaMemcpyToSymbol(contadorlinhasx_d,
                   &contadorlinhasx,
                   1*sizeof(int),
                   0,
                   cudaMemcpyHostToDevice);

If you look at the API documentation, the first two arguments are pointers. The first can either be a string, which will force a symbol lookup internally in the API (pre CUDA 4), or a device symbol address (CUDA 4 and later). The second argument is the address of the host source memory for the copy. The compiler error message is pretty explicit - you are passing the wrong types of argument and the compiler can't find an instance in the library which matches.



标签: cuda gpu