You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用GCC+nvptx-none编译OpenMP Offload时遇_fputwc_r符号未解析错误

GCC nvptx-none目标编译OpenMP Offload程序时的链接错误问题

问题背景

使用GCC的nvptx-none目标编译面向Nvidia GPU的OpenMP Offload简单测试程序,已通过Spack安装gcc+nvptx包(自行编译gcc-13搭配nvptx-tools后结果一致),但链接阶段出现如下错误:

unresolved symbol _fputwc_r
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: /path/to/spack/opt/spack/linux-centos8-x86_64_v3/gcc-13.0.0/gcc-12.2.0-6olbpwbs53cquwnpsvrmuxprmaofwjtk/libexec/gcc/x86_64-pc-linux-gnu/12.2.0//accel/nvptx-none/mkoffload returned 1 exit status
compilation terminated.
/usr/bin/ld: error: lto-wrapper failed

尝试过的无效/有副作用的解决方案

  • 添加-fno-stack-protector编译选项,无法解决问题
  • 使用-fno-lto可消除错误,但会导致Offload功能失效,调整优化选项无影响
  • 将Spack安装的ld(路径为spack/linux-centos8-x86_64_v3/gcc-13.0.0/gcc-12.2.0-6olbpwbs53cquwnpsvrmuxprmaofwjtk/nvptx-none)加入PATH后,出现新错误:
as: unrecognized option '--64'
nvptx-as: missing .version directive at start of file '/tmp/cc9YfveM.s'

额外信息

  • 该问题仅在代码包含parallel for循环时出现,仅设置#pragma omp target时可正常运行,设备能被正常识别,且指令内代码可在设备上执行
  • 系统版本:Rocky Linux release 8.7 (Green Obsidian)

测试程序代码

#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
void saxpy(float a, float* x, float* y, int sz) {
#pragma omp target teams distribute parallel for simd \
   num_teams(3) map(to:x[0:sz]) map(tofrom:y[0:sz])
   for (int i = 0; i < sz; i++) {
      if (omp_is_initial_device()) {
         printf("Running on host\n");    
      } else {
         int nthreads= omp_get_num_threads();
         int nteams= omp_get_num_teams(); 
         printf("Running on device with %d teams (fixed) in total and %d threads in each team\n",nteams,nthreads);
      }
      fprintf(stdout, "Thread %d %i\n", omp_get_thread_num(), i );
      y[i] = a * x[i] + y[i];
   }
}
int main(int argc, char** argv) {
   float a = 2.0;
   int sz = 16;
   float *x = calloc( sz, sizeof *x );
   float *y = calloc( sz, sizeof *y );
   //Set values
   int num_devices = omp_get_num_devices();
   printf("Number of available devices %d\n", num_devices);
   saxpy( a, x, y, sz );
   return 0;
}

编译命令

gcc -O0 -fopenmp -foffload=nvptx-none -o mintest mintest.c

内容的提问来源于stack exchange,提问作者ZehDeckel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 13:23:11