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
.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.