【问题标题】:F2PY wrap procedure that uses derived types only internally仅在内部使用派生类型的 F2PY 包装过程
【发布时间】:2021-05-13 09:29:58
【问题描述】:

很多地方都说 F2PY 不“支持派生类型”,但是我不清楚这是否意味着

  • 派生类型不能用作被包装的过程的参数 F2PY
  • 派生类型甚至不能在我想用 F2PY 包装的过程内部使用

对我的用例来说,第一点是不便之处,但第二点却是一个交易破坏者。

例如,考虑这个计算向量和的模块:

module derived_types

implicit none
public :: point, add

type :: point
   real :: x
   real :: y
end type point

contains

type(point) function add(p1, p2)

type(point), intent(in) :: p1
type(point), intent(in) :: p2

add%x = p1%x + p2%x
add%y = p1%y + p2%y

end function add

end module derived_types


module expose

use derived_types, only: point, add
implicit none

contains

subroutine vector_sum(x1, y1, x2, y2, x3, y3)

real, intent(in) ::  x1, y1, x2, y2
real, intent(out) :: x3, y3

type(point) :: p1, p2, p3

p1 = point(x1, y1)
p2 = point(x2, y2)

p3 = add(p1, p2)

x3 = p3%x
y3 = p3%y

end subroutine vector_sum

end module expose

子程序vector_sum 应暴露给 Python。派生类型point 不能在 Python 和 Fortran 之间传递。

这可以作为普通的 Fortran 程序工作(添加了适当的程序块),但 F2PY 失败:

f2py -c ff.f90 only: vector_sum

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 "untitled" sources
f2py options: ['only:', 'vector_sum', ':']
f2py:> /tmp/tmpjdq8b9dq/src.linux-x86_64-3.8/untitledmodule.c
creating /tmp/tmpjdq8b9dq/src.linux-x86_64-3.8
Reading fortran codes...
    Reading file 'ff.f90' (format:free)
Line #7 in ff.f90:"type :: point "
    analyzeline: No name/args pattern found for line.
Post-processing...
    Block: untitled
            Block: derived_types
                Block: unknown_type
            Block: expose
                Block: vector_sum
            Block: run
Post-processing (stage 2)...
    Block: untitled
        Block: unknown_interface
            Block: derived_types
                Block: unknown_type
            Block: expose
                Block: vector_sum
            Block: run
Building modules...
    Building module "untitled"...
        Constructing F90 module support for "derived_types"...
          Variables: point add
getctype: No C-type found in "{'attrspec': ['public']}", assuming void.
Traceback (most recent call last):
File "/home/me/.pyenv/versions/anaconda3-2020.11/lib/python3.8/site-packages/numpy/f2py/f90mod_rules.py", line 143, in buildhooks
    at = capi_maps.c2capi_map[ct]
KeyError: 'void'

这样的事情完全可以使用 F2PY 完成吗?

【问题讨论】:

    标签: python numpy fortran f2py


    【解决方案1】:

    您可以使用单个过程,在其中定义派生类型以及使用包含过程。 这是一种快速而肮脏的方法,仅适用于较小的问题。

    下面是一个示例实现

    ! file: a.f90
    subroutine vector_sum(x1, y1, x2, y2, x3, y3)
      real, intent(in)  :: x1, y1, x2, y2
      real, intent(out) :: x3, y3
    
      type point
        real :: x(2)
      end type
    
      type(point) :: p1, p2, p3
    
      p1%x = [x1, y1]
      p2%x = [x2, y2]
    
      p3 = add(p1, p2)
    
      x3 = p3%x(1)
      y3 = p3%x(2)
    
    contains
    
      type(point) function add(p1, p2)
        type(point), intent(in) :: p1
        type(point), intent(in) :: p2
    
        add%x = p1%x + p2%x
      end function
    
    end subroutine
    

    编译

    $ f2py -c a.f90 -m amod
    

    在python中的使用

    >>> import amod
    >>> amod.vector_sum(1, 2, 3, 4)
    (4.0, 6.0)
    

    【讨论】:

    • 这有效并回答了问题 (+1),但阻止了派生类型在不同子例程之间共享。
    • 您可以在包含的过程之间传递对象。但这对于大型项目可能用途有限......
    【解决方案2】:

    经过一番修改,我认为首先将定义所有派生类型的代码编译到静态库中可能是最简单的,即:

    libff.f90 包含:

    module derived_types
    
    implicit none
    
    public :: point, add
    
    type :: point
       real :: x
       real :: y
    end type point
    
    contains
    
    type(point) function add(p1, p2)
    
    type(point), intent(in) :: p1
    type(point), intent(in) :: p2
    
    add%x = p1%x + p2%x
    add%y = p1%y + p2%y
    
    end function add
    
    end module derived_types
    
    

    与gfortran -free -c libff.f90 编译生成libff.o。

    那么使用预编译模块的派生类型的代码可以在链接库时用F2PY编译,即:

    ff.f90 包含:

    module expose
    
    use derived_types, only: point, add
    implicit none
    
    contains
    
    subroutine vector_sum(x1, y1, x2, y2, x3, y3)
    
    real, intent(in) ::  x1, y1, x2, y2
    real, intent(out) :: x3, y3
    
    type(point) :: p1, p2, p3
    
    p1 = point(x1, y1)
    p2 = point(x2, y2)
    
    p3 = add(p1, p2)
    
    x3 = p3%x
    y3 = p3%y
    
    end subroutine vector_sum
    
    end module expose
    
    

    使用F2PY构建f2py -c ff.f90 libff.o -m ff的Python扩展后,我们可以用from ff.expose import vector_sum在Python中导入。

    经验教训:将派生类型的定义放入预编译库中,然后使用 F2PY 仅编译使用这些类型的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-23
      • 2021-05-17
      相关资源
      最近更新 更多