为什么 OpenMP 不将数组卸载到 GPU?

发布于 2025-01-14 15:41:34 字数 791 浏览 5 评论 0原文

我目前正在用 C 语言编写一些代码,并希望利用 GPU 来进行计算。我的代码有一个这样的测试函数:

void test_func(int *x, int N){
  // x is allocated using x = malloc(N*(sizeof *x)) elsewhere. It's done on cpu.
  // N is a parameter unknown at compile time.
  printf("CPU pointer x %p\n",x);
  #pragma omp target map(to:x[0:N]){
    printf("GPU pointer x %p\n",x);
  }
}

但是,打印的两个指针完全相同相同:

CPU pointer x 0x7f277037f880
GPU pointer x 0x7f277037f880

我想这意味着GPU也读取与CPU相同的内存位置。但我想要的是将数组 x 复制到 GPU,而不是在 GPU 上生成指向同一内存位置的指针。为什么会发生这种情况?是因为编译时数组大小未知吗?

我的编译器信息是:

mpicc --version
nvc 21.7-0 64-bit target on x86-64 Linux -tp zen
NVIDIA Compilers and Tools
Copyright (c) 2021, NVIDIA CORPORATION & AFFILIATES. ALL rights reserved.

I am currently writing some codes in C and want to utilize GPUs to do the calculation. My code has a test function like this:

void test_func(int *x, int N){
  // x is allocated using x = malloc(N*(sizeof *x)) elsewhere. It's done on cpu.
  // N is a parameter unknown at compile time.
  printf("CPU pointer x %p\n",x);
  #pragma omp target map(to:x[0:N]){
    printf("GPU pointer x %p\n",x);
  }
}

However, the two pointers printed are exactly the same:

CPU pointer x 0x7f277037f880
GPU pointer x 0x7f277037f880

I guess this means that GPU is also reading the same memory location as CPU. But what I want is to copy the array x to GPU, instead of generating a pointer on GPU pointing to the same memory location. Why is this happening? Is it because the array size is unknown at compile time?

My compiler information is:

mpicc --version
nvc 21.7-0 64-bit target on x86-64 Linux -tp zen
NVIDIA Compilers and Tools
Copyright (c) 2021, NVIDIA CORPORATION & AFFILIATES. ALL rights reserved.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

单身情人 2025-01-21 15:41:34

事实证明,这不是代码本身造成的,而是一个额外的编译标志造成的。之前使用过一个编译标志“托管”。通过删除此标志,代码将执行预期的操作。

It turns out that this is not caused by the code itself, but caused by an extra compilation flag. There was a compile flag "managed" used before. By removing this flag, the code does what is expected.

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