样板代码减少和模板化#
使用 FYPP 绑定通用接口#
f2py
当前不支持绑定接口块。但是,目前有一些变通方法。或许最广为人知的是使用 tempita
来处理 .pyf.src
文件,就像 scipy 的绑定中所做的那样。tempita 支持已被移除,并且在任何情况下都不再推荐使用。
注意
接口无法在 f2py
内部得到支持的原因是它们不对应于编译库中的导出符号。
❯ nm gen.o
0000000000000078 T __add_mod_MOD_add_complex
0000000000000000 T __add_mod_MOD_add_complex_dp
0000000000000150 T __add_mod_MOD_add_integer
0000000000000124 T __add_mod_MOD_add_real
00000000000000ee T __add_mod_MOD_add_real_dp
在这里,我们将讨论几种利用 f2py
结合 fypp 来模拟通用接口并简化多个(类似)函数绑定的技术。
基本示例:加法模块#
让我们基于用户指南中的示例(F2PY 示例),构建一个接收两个数组并返回其和的子例程。
C
SUBROUTINE ZADD(A,B,C,N)
C
DOUBLE COMPLEX A(*)
DOUBLE COMPLEX B(*)
DOUBLE COMPLEX C(*)
INTEGER N
DO 20 J = 1, N
C(J) = A(J)+B(J)
20 CONTINUE
END
我们将把这重构为现代 Fortran
module adder
implicit none
contains
subroutine zadd(a, b, c, n)
integer, intent(in) :: n
double complex, intent(in) :: a(n), b(n)
double complex, intent(out) :: c(n)
integer :: j
do j = 1, n
c(j) = a(j) + b(j)
end do
end subroutine zadd
end module adder
我们可以像原始示例那样继续,手工添加意图等,但在实际生产中通常还有其他考虑。例如,我们可以通过 FYPP 模板化类似函数的构建。
module adder
implicit none
contains
#:def add_subroutine(dtype_prefix, dtype)
subroutine ${dtype_prefix}$add(a, b, c, n)
integer, intent(in) :: n
${dtype}$, intent(in) :: a(n), b(n)
${dtype}$ :: c(n)
integer :: j
do j = 1, n
c(j) = a(j) + b(j)
end do
end subroutine ${dtype_prefix}$add
#:enddef
#:for dtype_prefix, dtype in [('i', 'integer'), ('s', 'real'), ('d', 'real(kind=8)'), ('c', 'complex'), ('z', 'double complex')]
@:add_subroutine(${dtype_prefix}$, ${dtype}$)
#:endfor
end module adder
这可以进行预处理以生成完整的 Fortran 代码
❯ fypp gen_adder.f90.fypp > adder.f90
正如所料,这随后可以由 f2py
进行封装。
现在我们将考虑将绑定维护在一个单独的文件中。请注意以下基本的 .pyf
文件,它可以为单个子例程通过 f2py -m adder adder_base.f90 -h adder.pyf
生成。
! -*- f90 -*-
! Note: the context of this file is case sensitive.
python module adder ! in
interface ! in :adder
module adder ! in :adder:adder_base.f90
subroutine zadd(a,b,c,n) ! in :adder:adder_base.f90:adder
double complex dimension(n),intent(in) :: a
double complex dimension(n),intent(in),depend(n) :: b
double complex dimension(n),intent(out),depend(n) :: c
integer, optional,intent(in),check(shape(a, 0) == n),depend(a) :: n=shape(a, 0)
end subroutine zadd
end module adder
end interface
end python module adder
! This file was auto-generated with f2py (version:2.0.0.dev0+git20240101.bab7280).
! See:
! https://web.archive.org/web/20140822061353/http://cens.ioc.ee/projects/f2py2e
带有文档字符串
c = zadd(a,b,[n])
Wrapper for ``zadd``.
Parameters
----------
a : input rank-1 array('D') with bounds (n)
b : input rank-1 array('D') with bounds (n)
Other Parameters
----------------
n : input int, optional
Default: shape(a, 0)
Returns
-------
c : rank-1 array('D') with bounds (n)
这已经相当不错了。然而,n
本来就不应该被传递,因此我们将进行一些小的调整。
! -*- f90 -*-
! Note: the context of this file is case sensitive.
python module adder ! in
interface ! in :adder
module adder ! in :adder:adder_base.f90
subroutine zadd(a,b,c,n) ! in :adder:adder_base.f90:adder
integer intent(hide),depend(a) :: n=len(a)
double complex dimension(n),intent(in) :: a
double complex dimension(n),intent(in),depend(n) :: b
double complex dimension(n),intent(out),depend(n) :: c
end subroutine zadd
end module adder
end interface
end python module adder
! This file was auto-generated with f2py (version:2.0.0.dev0+git20240101.bab7280).
! See:
! https://numpy.com.cn/doc/stable/f2py/
它对应于
In [3]: ?adder.adder.zadd
Call signature: adder.adder.zadd(*args, **kwargs)
Type: fortran
String form: <fortran function zadd>
Docstring:
c = zadd(a,b)
Wrapper for ``zadd``.
Parameters
----------
a : input rank-1 array('D') with bounds (n)
b : input rank-1 array('D') with bounds (n)
Returns
-------
c : rank-1 array('D') with bounds (n)
最后,我们可以以类似的方式对其进行模板化,以达到原始目标:拥有使用 f2py
指令且具有最少冗余重复的绑定。
! -*- f90 -*-
! Note: the context of this file is case sensitive.
python module adder ! in
interface ! in :adder
module adder ! in :adder:adder_base.f90
#:def add_subroutine(dtype_prefix, dtype)
subroutine ${dtype_prefix}$add(a,b,c,n) ! in :adder:adder_base.f90:adder
integer intent(hide),depend(a) :: n=len(a)
${dtype}$ dimension(n),intent(in) :: a
${dtype}$ dimension(n),intent(in),depend(n) :: b
${dtype}$ dimension(n),intent(out),depend(n) :: c
end subroutine ${dtype_prefix}$add
#:enddef
#:for dtype_prefix, dtype in [('i', 'integer'), ('s', 'real'), ('d', 'real(kind=8)'), ('c', 'complex'), ('z', 'complex(kind=8)')]
@:add_subroutine(${dtype_prefix}$, ${dtype}$)
#:endfor
end module adder
end interface
end python module adder
! This file was auto-generated with f2py (version:2.0.0.dev0+git20240101.bab7280).
! See:
! https://numpy.com.cn/doc/stable/f2py/
用法归结为
fypp gen_adder.f90.fypp > adder.f90
fypp adder.pyf.fypp > adder.pyf
f2py -m adder -c adder.pyf adder.f90 --backend meson