python - 可分配数组的 f2py 错误

标签 python fortran f2py

我有一个要在 Python 中使用的 Fortran 子例程。

subroutine create_hist(a, n, dr, bins, hist)
    integer, intent(in) :: n
    real(8), intent(in) :: a(n)
    real(8), intent(in) :: dr
    integer, intent(out), allocatable :: hist(:)
    real(8), intent(out), allocatable :: bins(:)

    n_b = n_bins(a, n, dr)  ! a function calculating the number of bins
    allocate(bins(n_b+1))
    allocate(hist(n_b))
    hist = 0

    !... Create the histogram hist by putting elems of a into bins
end subroutine

这是一个获取数字数组 a 并根据给定 bin 大小 dr 创建直方图的简单程序。首先,它使用函数 n_bins 获取 bin 的数量,然后相应地为数组 binshist 分配空间。

虽然 gfortran 可以很好地编译此代码,但 f2py 会引发错误:

/tmp/tmpY5badz/src.linux-x86_64-2.6/mat_ops-f2pywrappers2.f90:32.37:
      call create_hist_gnu(a, n, dr, bins, hist)
Error: Actual argument for 'bins' must be ALLOCATABLE at (1)

据我所知,f2py 不允许在运行时为数组分配空间(不知道为什么,因为这似乎是一种自然的科学需求)。

谁能建议一种在运行时分配 Fortran 数组的方法,以便 f2py 对此感到满意?

最佳答案

您可以使用一些有效的选项/变通方法。

1. 可能最简单的方法是安排 python 调用函数 n_bins,然后使用该函数的结果调用(稍微修改的版本) create_hist 函数具有正确大小的不可分配输出数组。

即在 Python 中:

n_b = n_bins(a,dr) # don't need to pass n - inferred from a
bins,hist = create_hist(a,dr,n_b) # bins and hist are auto-allocated

create_hist 的 Fortran 接口(interface)现在定义为

subroutine create_hist(a, n, dr, n_b, bins, hist)
    integer, intent(in) :: n
    real(8), intent(in) :: a(n)
    real(8), intent(in) :: dr
    integer, intent(in) :: n_b
    integer, intent(out) :: hist(n_b)
    real(8), intent(out) :: bins(n_b+1)

    ! code follows

! you also need to specify the function n_bins...

这仅适用于您可以从 create_hist 外部廉价调用 n_bins 的情况。我知道有 2 种方法可以在不适用的情况下模拟可分配数组(即计算数组大小的代码很昂贵并且不容易分离)。

2. 第一种是使用模块级可分配数组(在文档 here 中描述)。这本质上是一个可分配的全局变量——您调用您的函数,它将数据保存到全局变量中,然后您可以从 Python 访问它。缺点是它不是线程安全的(即,如果您同时并行调用 create_hist 是很糟糕的)

module something
    real(8), allocatable :: bins(:)
    integer, allocatable :: hist(:)
contains
    subroutine create_hist(a,n,dr)
        integer, intent(in) :: n
        real(8), intent(in) :: a(n)
        real(8), intent(in) :: dr
        integer :: n_b

        n_b = n_bins(a,n,dr)

        allocate(bins(n_b+1))
        allocate(hist(n_b))
        ! code follows
    end subroutine
end module

Python 调用看起来像

something.create_hist(a,n,dr)
bins = something.bins # or possible bins = copy.copy(something.bins)
hist = something.hist # or possible hist = copy.copy(something.hist)

3. 我非常喜欢的另一种方法是只在函数内分配数组(即不要将它们作为参数传入/传出)。但是,您传递的是一个 Python 回调函数,该函数在最后调用并保存数组。这是线程安全的(我相信)。

然后 Fortran 代码看起来像这样

subroutine create_hist(a,n,dr,callback)
    integer, intent(in) :: n
    real(8), intent(in) :: a(n)
    real(8), intent(in) :: dr
    external callable ! note: better to specify the type with an interface block (see http://www.fortran90.org/src/best-practices.html#callbacks)
    integer :: n_b
    real(8), allocatable :: bins(:)
    integer, allocatable :: hist(:)

    n_b = n_bins(a,n,dr)

    allocate(bins(n_b+1))
    allocate(hist(n_b))
    ! code follows
    call callable(bins,hist,n_b)
end subroutine

不幸的是,它会稍微复杂一些。您需要使用以下命令创建一个签名文件视情况而定),然后修改其中的相关行,明确回调函数的维度:

python module create_hist__user__routines 
    interface create_hist_user_interface 
        subroutine callable(bins,hist,n_b) ! in :f:my_fortran_file.f90:stuff:create_hist:unknown_interface
            real(kind=8), dimension(n_b+1) :: bins
            integer, dimension(n_b) :: hist
            integer :: n_b
        end subroutine callable
    end interface create_hist_user_interface
end python module create_hist__user__routines

f2py -c fortran_module.pyf my_fortran_file.f90 编译它

然后是一个简短的 Python 包装器(给你一个简单的界面),看起来像

def create_hist(a,dr):
    class SaveArraysCallable(object):
        def __call__(self,bins,hist):
            self.bins = bins.copy()
            self.hist = hist.copy()

    f = SaveArrayCallable()
    fortran_module.create_hist(a,dr,f)
    return f.bins, f.hist

总结

在许多情况下,选项 1 可能是最好的。否则选项 2 或选项 3。我更喜欢选项 3,因为没有要避免的多线程陷阱(但实际上这是一个您可能永远不会看到的极端情况)。选项 2 更容易实现。

关于python - 可分配数组的 f2py 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34579769/

相关文章:

python - 如何在非主线程中创建 GUI 小部件

r - 从 R 中调试 Fortran 代码

module - 如何修复 CMake Fortran 模块依赖性?

c++ - 使用 gfortran 链接 c++ 时 std::basic_string 的 undefined symbol ?

python - 计算向量python的旋转角度

python - 'not foo is none' 和 'foo is not none' 和有什么区别?

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

python - f2py:输入不是 Fortran 连续的

fortran - f2py Fortran 子例程中的 check 和 dependent 属性是什么?

python - 在每个字符之间添加点。什么是最 Pythonic 的方式?