CUDA:cudaMemcpy 返回 __device__ 数组的 cudaErrorInvalidValue

发布于 2024-09-06 12:22:52 字数 660 浏览 1 评论 0原文

当我在设备上定义一个数组(在本例中使用“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒尘 2024-09-13 12:23:01

您应该使用 cudaMemcpyFromSymbol。

You should use cudaMemcpyFromSymbol.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文