user328062
user328062

Reputation:

Cuda cudaMemcpy "invalid argument"

Trying to debug a larger application I'm having an issue where I can't seem to be able to copy values from the host to the device. I provide below a minimal example which I think should copy the 6 to the device and then back.

#include <stdio.h>
__device__ float a_d;

main(){
    float a = 6.0;
    float b;
    puts(cudaGetErrorString(cudaMemcpy(&a_d,&a,sizeof(float),cudaMemcpyHostToDevice)));
    puts(cudaGetErrorString(cudaMemcpy(&b,&a_d,sizeof(float),cudaMemcpyDeviceToHost)));
    printf("%e",b);
}

I get the following output with CUDA 5.5 on 64-bit Linux.

$ nvcc test.cu -run
invalid argument
invalid argument
0.000000e+00

whereas I'd expect cudaSuccess and 6.000000e+00.

Upvotes: 1

Views: 996

Answers (1)

Robert Crovella
Robert Crovella

Reputation: 152173

You cannot use cudaMemcpy directly with __device__ variables. The correct API to use is cudaMemcpyTo/FromSymbol.

The following should work:

#include <stdio.h>
__device__ float a_d;

main(){
    float a = 6.0;
    float b;
    puts(cudaGetErrorString(cudaMemcpyToSymbol(a_d,&a,sizeof(float)));
    puts(cudaGetErrorString(cudaMemcpyFromSymbol(&b,a_d,sizeof(float)));
    printf("%e",b);
}

It's not clear why you'd expect 0.000000e+00. Based on your code I would expect 6.000000e+00, or something like that.

Upvotes: 2

Related Questions