我正在尝试使用OpenMP 4+指令卸载GPU代码。我使用的是Ubuntu16.04和GCC7.2,一般情况下运行良好。当我试图卸载一个调用“math.h”中定义的sqrtf函数的代码时,我的问题就来了。麻烦代码是这样的:

#pragma omp target teams distribute \
map(to:posx[:n],posy[:n],posz[:n]) \
map(from:frcx[:n],frcy[:n],frcz[:n])
for (int i = 0; i < n; i++) {
  frcx[i] = 0.0f;
  frcy[i] = 0.0f;
  frcz[i] = 0.0f;

  for (int j = 0; j < n; j++) {
    float dx = posx[j] - posx[i];
    float dy = posy[j] - posy[i];
    float dz = posz[j] - posz[i];
    float distSqr = dx*dx + dy*dy + dz*dz + SOFTENING;
    float invDist = 1.0f / sqrtf(distSqr);
    float invDist3 = invDist * invDist * invDist;

    frcx[i] += dx * invDist3;
    frcy[i] += dy * invDist3;
    frcz[i] += dz * invDist3;
  }
}

当我试图编译它时:
$ gcc -Wall -O2 -march=native -mtune=native -fopenmp -o nbody_cpu_arrays_parallel_gpu common_funcs.c nbody_cpu_arrays_parallel_gpu.c -lm
unresolved symbol sqrtf
collect2: error: ld returned 1 exit status
mkoffload: fatal error: x86_64-linux-gnu-accel-nvptx-none-gcc-7 returned 1 exit status
compilation terminated.
lto-wrapper: fatal error: /usr/lib/gcc/x86_64-linux-gnu/7//accel/nvptx-none/mkoffload returned 1 exit status
compilation terminated.
/usr/bin/ld: error: lto-wrapper failed
collect2: error: ld returned 1 exit status

在将OMP代码卸载到gpu时,如何利用平方根运算(或其他数学函数)?

最佳答案

clang 9.0现在有一个特性,可以用等价版本的ptx代码(nvidia gpu)替换标准的数学库函数,gcc 9.0还不支持这个特性。
编译并运行:https://www.hahnjo.de/blog/2018/10/08/clang-7.0-openmp-offloading-nvidia.html
叮当声:https://reviews.llvm.org/D61399

10-01 20:38
查看更多