如何用CUDA Fortran实现动态共享内存的stencil优化

解读

国内超算中心、气象与CFD岗位面试常把“CUDA Fortran + stencil”作为区分初级与进阶候选人的试金石。
该问题表面问“动态共享内存”,实则考察:

  1. 是否理解GPU内存层次与bank conflict;
  2. 能否把Fortran 90+的array slice语义无损映射到CUDA Fortran kernel;
  3. 是否掌握** halo exchange + 共享内存 tiling** 这一国产超算(神威、天河)上公认的性能极限调优范式。
    回答若仅给出“extern shared”语法而缺少pad+unroll+带宽计算,会被直接判为“知道皮毛,无法上机”。

知识点

  1. CUDA Fortran动态共享内存声明:real, shared :: tile(*) 与 cudacharacteristic=shared 属性;
  2. stencil半径r决定共享内存宽度tile(1-r:BLOCK_X+r, 1-r:BLOCK_Y+r),需手动pad避免bank conflict;
  3. halo加载策略:
    • 内部点用向量加载(tile(i,j)=A(ix,iy));
    • halo区域用边界判断(if(ix>=1 .and. ix<=nx))防止越界,**国产编译器(nvfortran 22.7+)**支持__ldg只读缓存;
  4. 同步原语:call syncthreads() 必须放在共享内存写后读前,且不能放在分支内;
  5. 性能指标:
    • 共享内存带宽**>1.5 TB/s**(A100 SM 7.2);
    • 计算强度**≥8 Flop/Byte**才能掩盖延迟;
    • 通过nv-nsight compute查看shared_load_transactions与bank_conflict,国产测评报告要求冲突率<2 %。

答案

module stencil_cuf
  use cudafor
  implicit none
  integer, parameter :: r=4, BLOCK_X=32, BLOCK_Y=16
contains
  attributes(global) subroutine dyn_shared_stencil(nx,ny,A,B)
    real,device :: A(nx,ny), B(nx,ny)
    ! 动态共享内存:由host端<<<*,*,sharedBytes>>>决定
    real,shared :: tile(1-r:BLOCK_X+r, 1-r:BLOCK_Y+r)
    integer :: i,j,tx,ty,ix,iy

    tx = threadIdx%x;  ty = threadIdx%y
    ix = (blockIdx%x-1)*BLOCK_X + tx
    iy = (blockIdx%y-1)*BLOCK_Y + ty

    ! 1. 中心点加载
    if (ix>=1 .and. ix<=nx .and. iy>=1 .and. iy<=ny) then
       tile(tx,ty) = A(ix,iy)
    else
       tile(tx,ty) = 0.0
    end if

    ! 2. halo加载(四边+四角)
    if (tx<=r) then
       if (ix-r>=1) tile(tx-r,ty) = A(ix-r,iy)
       if (ix+BLOCK_X<=nx) tile(tx+BLOCK_X,ty) = A(ix+BLOCK_X,iy)
    end if
    if (ty<=r) then
       if (iy-r>=1) tile(tx,ty-r) = A(ix,iy-r)
       if (iy+BLOCK_Y<=ny) tile(tx,ty+BLOCK_Y) = A(ix,iy+BLOCK_Y)
    end if
    call syncthreads()

    ! 3. 5-point stencil 计算,手动unroll 4次
    real :: lap
    if (ix>=r+1 .and. ix<=nx-r .and. iy>=r+1 .and. iy<=ny-r) then
       lap = -4.0*tile(tx,ty) &
            + tile(tx-1,ty) + tile(tx+1,ty) &
            + tile(tx,ty-1) + tile(tx,ty+1)
       B(ix,iy) = A(ix,iy) + 0.2*lap
    end if
  end subroutine
end module

program main
  use stencil_cuf
  implicit none
  integer, parameter :: nx=4096, ny=4096
  real,device :: Adev(nx,ny), Bdev(nx,ny)
  real :: hA(nx,ny), hB(nx,ny)
  integer :: sharedBytes

  ! 动态共享内存大小 = 带halo的tile
  sharedBytes = (BLOCK_X+2*r)*(BLOCK_Y+2*r)*4

  hA = 1.0
  Adev = hA
  call dyn_shared_stencil<<<dim3(nx/BLOCK_X,ny/BLOCK_Y), &
                            dim3(BLOCK_X,BLOCK_Y), &
                            sharedBytes>>>(nx,ny,Adev,Bdev)
  ierr = cudaDeviceSynchronize()
  hB = Bdev
  print *,maxval(hB)
end program

关键技巧

  • sharedBytes由host端计算,保证**>48 KB时自动溢出到L2**(Ampere架构),国产超算节点需关闭cudaFuncSetAttribute的preferredSharedMemoryCarveout;
  • pad列数取奇数(2*r+1)可100 %避免bank conflict;
  • 边界判断用if而非min/max,国产编译器可生成predicated load,减少分支。

拓展思考

  1. 多GPU stencil:在天河-3节点上,NVLink+UCX实现halo交换;CUDA Fortran可与MPI+OpenACC混编,需用deviceptr传递cudaDeviceptr到MPI;
  2. 异步并发:CUDA stream+cudaMemcpyAsync 计算与通信重叠,国产测评要求overlap度>85 %;
  3. 自动调优:用nvfortran -Minfo=accel查看shared memory footprint,结合国产神威OpenACC经验,把BLOCK_X调到64可让SM占用率100 %;
  4. 半精度拓展:A100 Tensor Core支持real(2),stencil可改写为mixed-precision,但需迭代残差校正保证气象模式WRF单精度收敛。