CUDA:cudaMemcpy 返回 __device__ 数组的 cudaErrorInvalidValue
当我在设备上定义一个数组(在本例中使用“Hello”字符串初始化)并尝试将其复制到主机时,我收到错误代码 cudaErrorInvalidValue
。然而,从内核内部,可以访问d_helloStr[]
。参考CUDA编程指南第B.2.1章,这样的变量也应该可以通过运行时库访问。为什么这个示例代码不起作用?
#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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用 cudaMemcpyFromSymbol。
You should use cudaMemcpyFromSymbol.