OpenMP GPU卸载数学库?

时间:2018-03-28 09:56:13

标签: c math gcc gpu openmp

我正在尝试使用OpenMP 4+指令卸载GPU代码。我使用的是ubuntu 16.04和GCC 7.2,对于一般情况它运行正常。当我试图卸载一个调用 sqrtf 函数的代码时出现问题,该函数在" math.h"中定义。麻烦的代码就是:

#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时,如何使用平方根操作(或其他数学函数)?

2 个答案:

答案 0 :(得分:0)

clang 9.0现在具有以等价的ptx代码版本(nvidia gpu)取代标准数学库功能的功能,而gcc 9.0尚不支持该功能。

编译并运行:https://www.hahnjo.de/blog/2018/10/08/clang-7.0-openmp-offloading-nvidia.html

clang的提交:https://reviews.llvm.org/D61399

答案 1 :(得分:0)

我遇到了类似的问题。 https://github.com/bisqwit/cpp_parallelization_examples/blob/master/README.md非常有帮助地描述了解决方案:

  

卸载时,如果出现以下情况,您可能会从数学函数中遇到链接器问题:   您进行了优化的构建。要解决,请添加-foffload = -lm   -fno-fast-math -fno-associative-math

作为参考,我在sqrt中遇到的错误:

libgomp: Link error log ptxas application ptx input, line 138; error   : Label expected for argument 0 of instruction 'call'
ptxas application ptx input, line 138; fatal   : Call target not recognized
ptxas <macro util>, line 9; error   : Illegal modifier '.div' for instruction 'mov'
ptxas fatal   : Ptx assembly aborted due to errors


libgomp: cuLinkAddData (ptx_code) error: a PTX JIT compilation failed

libgomp: Cannot map target functions or variables (expected 2, have 4294967295)

使用sqrtf:

unresolved symbol sqrtf
collect2: error: ld returned 1 exit status
mkoffload: fatal error: x86_64-pc-linux-gnu-accel-nvptx-none-gcc returned 1 exit status
compilation terminated.
lto-wrapper: fatal error: gcc/x86_64-pc-linux-gnu/7.3.0//accel/nvptx-none/mkoffload returned 1 exit status
compilation terminated.
/usr/bin/ld: error: lto-wrapper failed