问题描述
我正在尝试使用 f2py
来封装 C 代码。
module1.c
double multiply(double a,double b);
double multiply(double a,double b) {
return a*b;
}
module2.c
double divide(double a,double b);
double divide(double a,double b) {
return a/b;
}
我编译这两个文件如下:
$ gcc -c -fPIC -o module1.o module1.c
$ gcc -c -fPIC -o module2.o module2.c
然后我为这两个函数准备了以下 Fortran 模块包装器:
fortran_module.f90
! TEST FORTRAN WRAPPER
module fortran_module
use iso_c_binding
! Interface to C routine
! double get_price_step(double price);
interface
real(c_double) function multiply(a,b) bind(C)
use iso_c_binding
real(c_double),value :: a,b
end function
real(c_double) function divide(a,b
end function
end interface
end module
我编译模块使用:
$ f2py -c -m fortran_module fortran_module.f90 module1.o module2.o
我得到以下输出:
running build
running config_cc
unifing config_cc,config,build_clib,build_ext,build commands --compiler options
running config_fc
unifing config_fc,build commands --fcompiler options
running build_src
build_src
building extension "fortran_module" sources
f2py options: []
f2py:> /tmp/tmp9aa3w8b0/src.freebsd-12.2-RC1-amd64-3.7/fortran_modulemodule.c
creating /tmp/tmp9aa3w8b0/src.freebsd-12.2-RC1-amd64-3.7
Reading fortran codes...
Reading file 'fortran_module.f90' (format:free)
Post-processing...
Block: fortran_module
Block: fortran_module
In: :fortran_module:fortran_module.f90:fortran_module
get_useparameters: no module iso_c_binding info used by fortran_module
In: :fortran_module:fortran_module.f90:fortran_module:unkNown_interface
get_useparameters: no module iso_c_binding info used by unkNown_interface
Block: multiply
In: :fortran_module:fortran_module.f90:fortran_module:unkNown_interface:multiply
get_useparameters: no module iso_c_binding info used by multiply
Block: divide
In: :fortran_module:fortran_module.f90:fortran_module:unkNown_interface:divide
get_useparameters: no module iso_c_binding info used by divide
Post-processing (stage 2)...
Block: fortran_module
Block: unkNown_interface
Block: fortran_module
Block: unkNown_interface
Block: multiply
Block: divide
Building modules...
Building module "fortran_module"...
Constructing F90 module support for "fortran_module"...
Skipping interface unkNown_interface
Wrote C/API module "fortran_module" to file "/tmp/tmp9aa3w8b0/src.freebsd-12.2-RC1-amd64-3.7/fortran_modulemodule.c"
Fortran 90 wrappers are saved to "/tmp/tmp9aa3w8b0/src.freebsd-12.2-RC1-amd64-3.7/fortran_module-f2pywrappers2.f90"
adding '/tmp/tmp9aa3w8b0/src.freebsd-12.2-RC1-amd64-3.7/fortranobject.c' to sources.
adding '/tmp/tmp9aa3w8b0/src.freebsd-12.2-RC1-amd64-3.7' to include_dirs.
copying /usr/local/lib/python3.7/site-packages/numpy/f2py/src/fortranobject.c -> /tmp/tmp9aa3w8b0/src.freebsd-12.2-RC1-amd64-3.7
copying /usr/local/lib/python3.7/site-packages/numpy/f2py/src/fortranobject.h -> /tmp/tmp9aa3w8b0/src.freebsd-12.2-RC1-amd64-3.7
adding '/tmp/tmp9aa3w8b0/src.freebsd-12.2-RC1-amd64-3.7/fortran_module-f2pywrappers2.f90' to sources.
build_src: building npy-pkg config files
running build_ext
customize UnixCCompiler
C compiler: cc -pthread -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -O2 -pipe -fstack-protector-strong -fno-strict-aliasing -fPIC
creating /tmp/tmpjgjjhjtm/tmp
creating /tmp/tmpjgjjhjtm/tmp/tmpjgjjhjtm
compile options: '-MMD -MF /tmp/tmpjgjjhjtm/file.c.d -c'
cc: /tmp/tmpjgjjhjtm/file.c
customize UnixCCompiler using build_ext
get_default_fcompiler: matching types: '['gnu','gnu95']'
customize GnuFCompiler
Found executable /usr/local/bin/gfort
ran9
gnu: no Fortran 90 compiler found
gnu: no Fortran 90 compiler found
customize Gnu95FCompiler
customize Gnu95FCompiler
customize Gnu95FCompiler using build_ext
building 'fortran_module' extension
compiling C sources
C compiler: cc -pthread -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -O2 -pipe -fstack-protector-strong -fno-strict-aliasing -fPIC
creating /tmp/tmp9aa3w8b0/tmp
creating /tmp/tmp9aa3w8b0/tmp/tmp9aa3w8b0
creating /tmp/tmp9aa3w8b0/tmp/tmp9aa3w8b0/src.freebsd-12.2-RC1-amd64-3.7
compile options: '-I/tmp/tmp9aa3w8b0/src.freebsd-12.2-RC1-amd64-3.7 -I/usr/local/lib/python3.7/site-packages/numpy/core/include -I/usr/local/include/python3.7m -c'
cc: /tmp/tmp9aa3w8b0/src.freebsd-12.2-RC1-amd64-3.7/fortranobject.c
cc: /tmp/tmp9aa3w8b0/src.freebsd-12.2-RC1-amd64-3.7/fortran_modulemodule.c
In file included from /tmp/tmp9aa3w8b0/src.freebsd-12.2-RC1-amd64-3.7/fortran_modulemodule.c:15:
In file included from /tmp/tmp9aa3w8b0/src.freebsd-12.2-RC1-amd64-3.7/fortranobject.h:13:
In file included from /tmp/tmp9aa3w8b0/src.freebsd-12.2-RC1-amd64-3.7/fortranobject.c:2:
In file included from /tmp/tmp9aa3w8b0/src.freebsd-12.2-RC1-amd64-3.7/fortranobject.h:13:
In file included from /usr/local/lib/python3.7/site-packages/numpy/core/include/numpy/arrayobject.h:4:
In file included from /usr/local/lib/python3.7/site-packages/numpy/core/include/numpy/ndarrayobject.h:12:
In file included from /usr/local/lib/python3.7/site-packages/numpy/core/include/numpy/ndarraytypes.h:1822:
/usr/local/lib/python3.7/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:17:2: warning: "Using deprecated NumPy API,disable it with "
"#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-W#warnings]
#warning "Using deprecated NumPy API,disable it with " \
^
In file included from /usr/local/lib/python3.7/site-packages/numpy/core/include/numpy/arrayobject.h:4:
In file included from /usr/local/lib/python3.7/site-packages/numpy/core/include/numpy/ndarrayobject.h:12:
In file included from /usr/local/lib/python3.7/site-packages/numpy/core/include/numpy/ndarraytypes.h:1822:
/usr/local/lib/python3.7/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:17:2: warning: "Using deprecated NumPy API,disable it with " \
^
1 warning generated.
1 warning generated.
compiling Fortran 90 module sources
Fortran f77 compiler: /usr/local/bin/gfortran9 -Wall -g -ffixed-form -fno-second-underscore -fPIC -O3 -funroll-loops
Fortran f90 compiler: /usr/local/bin/gfortran9 -Wall -g -fno-second-underscore -fPIC -O3 -funroll-loops
Fortran fix compiler: /usr/local/bin/gfortran9 -Wall -g -ffixed-form -fno-second-underscore -Wall -g -fno-second-underscore -fPIC -O3 -funroll-loops
compile options: '-I/tmp/tmp9aa3w8b0/src.freebsd-12.2-RC1-amd64-3.7 -I/usr/local/lib/python3.7/site-packages/numpy/core/include -I/usr/local/include/python3.7m -c'
extra options: '-J/tmp/tmp9aa3w8b0/ -I/tmp/tmp9aa3w8b0/'
gfortran9:f90: fortran_module.f90
compiling Fortran sources
Fortran f77 compiler: /usr/local/bin/gfortran9 -Wall -g -ffixed-form -fno-second-underscore -fPIC -O3 -funroll-loops
Fortran f90 compiler: /usr/local/bin/gfortran9 -Wall -g -fno-second-underscore -fPIC -O3 -funroll-loops
Fortran fix compiler: /usr/local/bin/gfortran9 -Wall -g -ffixed-form -fno-second-underscore -Wall -g -fno-second-underscore -fPIC -O3 -funroll-loops
compile options: '-I/tmp/tmp9aa3w8b0/src.freebsd-12.2-RC1-amd64-3.7 -I/usr/local/lib/python3.7/site-packages/numpy/core/include -I/usr/local/include/python3.7m -c'
extra options: '-J/tmp/tmp9aa3w8b0/ -I/tmp/tmp9aa3w8b0/'
gfortran9:f90: /tmp/tmp9aa3w8b0/src.freebsd-12.2-RC1-amd64-3.7/fortran_module-f2pywrappers2.f90
/usr/local/bin/gfortran9 -Wall -g -Wall -g -shared /tmp/tmp9aa3w8b0/tmp/tmp9aa3w8b0/src.freebsd-12.2-RC1-amd64-3.7/fortran_modulemodule.o /tmp/tmp9aa3w8b0/tmp/tmp9aa3w8b0/src.freebsd-12.2-RC1-amd64-3.7/fortranobject.o /tmp/tmp9aa3w8b0/fortran_module.o /tmp/tmp9aa3w8b0/tmp/tmp9aa3w8b0/src.freebsd-12.2-RC1-amd64-3.7/fortran_module-f2pywrappers2.o module1.o module2.o -L/usr/local/lib/gcc9/gcc/x86_64-portbld-freebsd12.1/9.3.0 -L/usr/local/lib -lpython3.7m -lgfortran -o ./fortran_module.so
Removing build directory /tmp/tmp9aa3w8b0
这会在目录中创建 fortran_module.so
。
然后,回到 Python 我尝试做:
import fortran_module
但是我遇到了分段错误:
$ /usr/local/bin/python3.7
Python 3.7.9 (default,Oct 3 2020,01:29:35)
[Clang 8.0.1 (tags/RELEASE_801/final 366581)] on freebsd12
Type "help","copyright","credits" or "license" for more information.
readline: ~/.inputrc: line 1: nobeep: unkNown variable name
readline: ~/.inputrc: line 1: nobeep: unkNown variable name
>>> import fortran_module
Segmentation fault (core dumped)
欢迎任何关于我做错了什么的帮助或提示。我正在使用 gcc
和 FreeBSD
(我放弃了使用 clang
anf flang
的想法,因为 FreeBSD 的 flang 似乎不支持 iso_c_bindings
)。
注意:我还尝试使用 --fcompiler=gnu95
作为 f2py
的参数,以确保不使用 flang
或 clang
。
解决方法
假设 f2py
不能为带有 bind(C)
的 Fortran 例程生成 Python 绑定,解决方法可能是定义一个普通的(非绑定(C))例程并从内部调用 C 例程(所以有点类似于 this page 使用派生类型)。例如,一个可能的代码可能看起来像这样(但显然函数调用的开销更大......)
clib.c
double multiply(double a,double b) {
return a * b;
}
fmod.f90
module fmod
use iso_c_binding
implicit none
contains
function multiply(a,b) result(res)
real(8) :: a,b,res !! "8" just for test
interface
real(c_double) function c_multiply(a,b) bind(C,name="multiply")
import
real(c_double),value :: a,b
end
end interface
res = c_multiply(a,b)
end
end module
编译:
$ gcc -c -fPIC clib.c -o clib.o
$ python3.9 -m numpy.f2py -c fmod.f90 clib.o -m py_fmod
运行:
$ python3.9
>>> import py_fmod
>>> print( py_fmod.fmod.__doc__ )
res = multiply(a,b)
Wrapper for ``multiply``.
Parameters
----------
a : input float
b : input float
Returns
-------
res : float
>>> py_fmod.fmod.multiply( a= 2.0,b= 3.0 )
6.0
我想也可以在单独的 Fortran 模块文件中定义所有接口块,从 f2py
独立编译它,并从用 use
编译的例程 f2py
(因此,将 iso_c_binding + interface 块与 f2py 编译分开)。事实上,如果在 f2py 的模块头部分中直接定义这样的接口块似乎有问题(这看起来非常类似于派生类型等的问题)。