fortran - 尝试使用 f2py 围绕两个 C 对象编写 Fortran 包装器时出现段错误

标签 fortran f2py fortran-iso-c-binding

我正在尝试使用f2py来封装C代码。

作为第一个测试,我准备了两个非常基本的 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) bind(C)
                        use iso_c_binding
                        real(c_double), value :: 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, config, build_clib, build_ext, 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 "
      "#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-W#warnings]
#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)

欢迎任何有关我做错的事情的帮助或提示。我正在使用 gccFreeBSD (我放弃了使用 clang anf flang 的想法,因为 FreeBSD 的 flang 似乎不适合支持iso_c_bindings)。

注意:我还尝试使用 --fcompiler=gnu95 作为 f2py 的参数,试图确保 flangclang 未使用。

最佳答案

假设f2py无法使用bind(C)为Fortran例程生成Python绑定(bind),解决方法可能是定义一个常用的(非bind(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 模块文件中定义所有接口(interface) block ,独立于 f2py 进行编译,并从使用以下代码编译的例程中使用 f2py (因此,将 iso_c_binding + 接口(interface) block 与 f2py 编译分开)。事实上,如果直接在 f2py 的模块头部分中定义这样的接口(interface) block 似乎是有问题的(这似乎与派生类型等问题非常相似)。

关于fortran - 尝试使用 f2py 围绕两个 C 对象编写 Fortran 包装器时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68121634/

相关文章:

python - 从 python 中的共享 fortran 库调用函数

c - 将 C 字符串数组传递给 Fortran (iso_c_binding)

loops - 如何终止一个do循环?

c - 将 Fortran 整数数组传递给 C 子例程,仅传递第一个元素

python - 当 python 使用 f2py 调用时,fortran 矩阵乘积变慢

distutils - 将F2PY编译步骤翻译成setup.py

fortran - ISO_C_BINDING 从 Fortran 调用 C 例程(使用 double 和数组)

fortran - 将具有派生类型作为参数的函数作为 Fortran 中的参数传递

compiler-errors - 最大艾芬特18艾芬特19

python - 调试/分析 python 扩展