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

使用nvfortran在GPU上并行化Fortran DO循环时运行崩溃问题

问题分析与解决方案

问题根源

代码中定义的变量名count与Fortran内置函数count()重名。在nvfortran -stdpar=gpu的GPU加速编译模式下,编译器对内置函数的解析逻辑更严格,变量名与内置函数同名会导致运行时符号解析冲突,进而引发崩溃。而普通CPU编译模式(gfortran、nvfortran无-stdpar参数)下,编译器的宽松处理掩盖了这个命名冲突问题。

修复步骤

将变量count重命名为非内置函数名(比如cnt),修改后的代码如下:

program pi
  implicit none
  integer :: cnt, n, i
  real :: r
  real, dimension(10000) :: x,y
  logical , dimension(10000) :: c
  c = .false.
  n=size(x,1)
  print*,count(c)
  call RANDOM_SEED
  call random_number(x)
  call random_number(y)

  do concurrent (i = 1: n)
    if (x(i)**2 + y(i)**2 <1.0) c(i)=.true.
  end do

  r = 4 * real(count(c))/n
  print *, r
end program pi

编译验证

使用nvfortran -stdpar=gpu pi.f90 -o pi编译修改后的代码,运行即可正常输出圆周率近似值。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 23:50:26