用INTERFACE为不同kind实数实现统一solve线性方程接口

解读

国内高性能计算岗位面试时,**“一个接口、多种精度”**是考察候选人是否真正写过可维护科学代码的经典题。
题目要求:

  1. 只暴露一个**solve(A,b,x)**给上层调用;
  2. 底层根据A、b的kind自动分派到单精度、双精度、四倍精度三套实现;
  3. 不允许运行时if-kind判断,必须在编译期完成分派;
  4. 接口必须支持显式接口检查,防止常见shape-mismatch错误;
  5. 代码需符合现代Fortran 2008+语法,可在国产超算(如鲲鹏+ARM Flang/LLVM或Intel oneAPI)上直接编译。

知识点

  • INTERFACE … MODULE PROCEDURE 重载机制
  • PARAMETER(kp=selected_real_kind(…)) 精度参数化
  • GENERIC :: solve => solve_sp, solve_dp, solve_qp 泛型绑定
  • CONTIGUOUS 属性保证数组连续内存,适配SIMD优化
  • LAPACK95接口(国产环境常用OpenBLAS/MKL)的sgesv/dgesv/qgesv差异
  • 编译期kind常量传播与USE, ONLY隔离精度符号,避免隐式转换
  • 错误码intent(out) + iostat 与国产调度系统日志对接习惯

答案

! precision_kinds.f90
module precision_kinds
  implicit none
  integer, parameter :: sp = selected_real_kind(6,37)
  integer, parameter :: dp = selected_real_kind(15,307)
  integer, parameter :: qp = selected_real_kind(33,4931)
end module precision_kinds

! solver_interfaces.f90
module solver_interfaces
  use precision_kinds, only: sp,dp,qp
  implicit none
  private
  public :: solve

  interface solve
    module procedure solve_sp, solve_dp, solve_qp
  end interface

contains

  subroutine solve_sp(a,b,x,info)
    real(sp), contiguous, intent(in)  :: a(:,:)
    real(sp), contiguous, intent(in)  :: b(:)
    real(sp), contiguous, intent(out) :: x(:)
    integer,  intent(out) :: info
    integer :: n,lda,ldb,nrhs,ipiv(size(a,1))
    n=size(a,1); lda=n; ldb=n; nrhs=1
    x=b
    call sgesv(n,nrhs,a,lda,ipiv,x,ldb,info)
  end subroutine solve_sp

  subroutine solve_dp(a,b,x,info)
    real(dp), contiguous, intent(in)  :: a(:,:)
    real(dp), contiguous, intent(in)  :: b(:)
    real(dp), contiguous, intent(out) :: x(:)
    integer,  intent(out) :: info
    integer :: n,lda,ldb,nrhs,ipiv(size(a,1))
    n=size(a,1); lda=n; ldb=n; nrhs=1
    x=b
    call dgesv(n,nrhs,a,lda,ipiv,x,ldb,info)
  end subroutine solve_dp

  subroutine solve_qp(a,b,x,info)
    real(qp), contiguous, intent(in)  :: a(:,:)
    real(qp), contiguous, intent(in)  :: b(:)
    real(qp), contiguous, intent(out) :: x(:)
    integer,  intent(out) :: info
    ! 国产OpenBLAS通常无qgesv,改用高精度迭代 refinement
    x = b
    block
      real(qp), allocatable :: work(:),af(:,:)
      integer :: n
      n=size(a,1); allocate(af(n,n),work(n))
      af=a
      call dgetrf_128(n,n,af,n,ipiv,info)  ! 假设提供128位扩展LAPACK
      if(info==0) call dgetrs_128('N',n,1,af,n,ipiv,x,n,info)
    end block
  end subroutine solve_qp

end module solver_interfaces

! demo.f90
program demo
  use solver_interfaces, only: solve
  use precision_kinds , only: sp,dp,qp
  implicit none
  real(dp) :: a(3,3),b(3),x(3)
  integer  :: info
  a = reshape([2,1,1,  1,3,1,  1,1,4], [3,3])
  b = [1,2,3]
  call solve(a,b,x,info)
  print *, 'x=',x,' info=',info
end program demo

编译脚本(国产超算常见环境)
mpiifort -O3 -qmkl -module $PWD/m precision_kinds.f90 solver_interfaces.f90 demo.f90 -o demo.x

拓展思考

  1. Coarray并行扩展:若矩阵按块分布,可在solve_dp内部加sync all,并用CAF发送对角块,实现国产神威多核加速。
  2. 对象封装:把系数矩阵提升为derived type(matrix_t),内部存放class(*), allocatable :: data(:,:),用SELECT TYPE做运行时多态,可应对用户传入稀疏/稠密混合场景,但会牺牲零开销抽象;面试时可讨论**“编译期分派+对象封装”**折中。
  3. 与Python端交互:通过iso_c_binding导出solve_c符号,精度参数用int32_t precision_flag控制,在pybind11层做NPY_FLOAT32/NPY_FLOAT64映射,实现天河平台上双精度Python驱动+单精度GPU校验的混合精度工作流。