When I define an array on the device (that is initialized with a "Hello" string in this example) and try to copy this to the host, I get the error code cudaErrorInvalidValue
. However, from inside a kernel, the d_helloStr[]
can be accessed. Referring to the CUDA programming guide chapter B.2.1, such a variable should also be accessible through the runtime library. Why does this example code not work?
#include <cuda.h>
#include <stdio.h>
__device__ char d_helloStr[] = {'H','e','l','l','o','\0'};
// Host function
int
main(int argc, char** argv)
{
cudaError_t err;
char h_helloStr [sizeof(d_helloStr)];
// copy device string to host string:
err = cudaMemcpy(h_helloStr, d_helloStr, sizeof(d_helloStr), cudaMemcpyDeviceToHost);
printf("err = %d\n", err);
// result string:
printf("%s\n", h_helloStr);
return 0;
}