cudaMemcpyToSymbol() on cuda-10.0

Hi,

I am using cuda-10.0. I can’t get around the compile error for the following code snippet:

extern __constant__ float precision;
.
.
.
int main(int argc, char **argv)
{
float prec = 0.001;
...
cudaMemcpyToSymbol(precision, &prec, sizeof(float));
...
}

The error is:

error:cannot convert 'float' to 'const void *' for argument '1' to 'cudaError_t cudaMemcpyToSymbol(const void*, const void*, size_t, size_t, cudaMemcpyKind)'

I understand the error message (reference to second argument type should be ‘const void*’) but since I’ve found many examples of cudaMemcpyToSymbol() usage which are like in my case, I wonder what am I doing wrong?

what compiler are you using?

I don’t have any trouble when compiling your code with nvcc

If you are using some other compiler like g++ or cl.exe from visual studio, that cannot be done.

constant is not part of the C++ language, and cannot be passed through an ordinary host compiler. You must use nvcc to compile such codes.

Here’s an example. Note the use of -rdc=true, this is necessary for device code linking between compilation units.

$ cat t27.cu
__constant__ float precision;
$ cat t26.cu
extern __constant__ float precision;
int main(int argc, char **argv)
{
        float prec = 0.001;
        cudaMemcpyToSymbol(precision, &prec, sizeof(float));
}
$ nvcc t26.cu t27.cu -rdc=true -o test
$

I’m using Makefile from the simulation samples which came as part of the Jetson Nano JetPack. I adapted the Makefile to suit my simple example and it worked well until I added usage of constant device memory. So, it is nvcc, g++ is referenced with ‘-ccbin g++’.
I have given it a try with -rdc=true as you suggested but the same errors are present.

My code is divided in two files, one is .cpp file with host code and another .cu file with device code. It compiled, linked and run well until I used constant device memory.

Change the .cpp file to a .cu file. I assume the .cpp file has constant in it.

When a file name ends in .cpp, nvcc by default more or less hands it off to the host compiler. So you can’t use constant in a file ending in .cpp, even if you compile it with nvcc

Yes - having .cpp extension changed to .cu worked well for the file which contains constant declaration and the reference to it via cudaMemcpyToSymbol().

Thanks for the explanation!