为什么 OpenMP 不将数组卸载到 GPU?
我目前正在用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,这不是代码本身造成的,而是一个额外的编译标志造成的。之前使用过一个编译标志“托管”。通过删除此标志,代码将执行预期的操作。
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.