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.
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.