python - 使用 f2py 将 0 长度数组从 fortran 返回到 python

标签 python numpy fortran f2py

我正在使用 f2py 为用 Fortran 编写的基于 MPI 的库生成包装器。由于我使用的数组分区方案,如果有足够的 MPI 进程,进程可能拥有长度为 0 的本地数组。这会在我有权访问的 Cray 系统上触发以下错误:

ValueError: failed to create intent(cache|hide)|optional array-- 
must have defined dimensions but got (0,)

我的桌面上没有收到相同的错误。这可能与我安装的python和numpy的版本有关。在我的桌面上,它们是 numpy 版本 1.16.4 和 python 2.7.15+,在集群上,它们是 numpy 1.13.3 和 python 2.7.14。由于我无法升级集群上的软件包,我想知道是否存在简单的解决方法。以下代码重现了该错误:

文件“fortran_sub.f90”:

subroutine sub(a_size, a)                                                       
                                                                            
    integer, intent(in) :: a_size                                               
    real, dimension(a_size), intent(out) :: a                                   
                                                                          
    if (size(a) > 0) then                                                       
        a = size(a)                                                             
    endif                                                                       
                                                                              
end subroutine sub  

使用f2py包装并编译如下:

python2 -m numpy.f2py -h --overwrite-signature fortran_sub.pyf -m 
fortran_sub fortran_sub.f90

python2 -m numpy.f2py --f90exec="ftn" -c fortran_sub.pyf -m 
fortran_sub fortran_sub.f90

生成的.pyf是:

!    -*- f90 -*-
! Note: the context of this file is case sensitive.

python module fortran_sub ! in 
    interface  ! in :fortran_sub
        subroutine sub(a_size,a) ! in :fortran_sub:fortran_sub.f90
            integer intent(in) :: a_size
            real dimension(a_size),intent(out),depend(a_size) :: a
        end subroutine sub
     end interface 
end python module fortran_sub

! This file was auto-generated with f2py (version:2).
! See http://cens.ioc.ee/projects/f2py2e/

使用 python2 pytest.py 运行以下 python 程序“pytest.py”:

import fortran_sub

a = fortran_sub.sub(2)
print(a)

a = fortran_sub.sub(1)
print(a)

a = fortran_sub.sub(0)
print(a)

我得到以下输出:

[ 2.  2.]
[ 1.]
Traceback (most recent call last):
  File "pytest.py", line 11, in <module>
   a = fortran_sub.sub(0)
ValueError: failed to create intent(cache|hide)|optional array-- must have defined dimensions but got (0,)

最佳答案

自从我了解 Python-Fortran 接口(interface)的 ctypes 以来,我已经很长时间没有使用 f2py 了。这是一个基于 ctypes 的解决方案,它还可以处理零大小的数组(技术上也应该与 MPI 一起使用),

module sub_mod
contains
    subroutine sub(a_size, a) bind(C,name="sub")
        !DEC$ ATTRIBUTES DLLEXPORT :: sub
        use iso_c_binding, only: c_size_t, c_double
        integer(c_size_t), intent(in) :: a_size
        real(c_double), intent(inout) :: a(a_size)
        if (size(a) > 0) then
            a = size(a)
        endif
    end subroutine sub
end module sub_mod

使用 Intel 或(其他编译器)编译它以创建 DLL 库,

ifort /dll main.f90

来自 ifort 的输出消息如下所示,

Intel(R) Visual Fortran Intel(R) 64 Compiler for applications running on Intel(R) 64, Version 19.0.4.245 Build 20190417
Copyright (C) 1985-2019 Intel Corporation.  All rights reserved.

Microsoft (R) Incremental Linker Version 14.22.27905.0
Copyright (C) Microsoft Corporation.  All rights reserved.

-out:main.dll
-dll
-implib:main.lib
main.obj
   Creating library main.lib and object main.exp

然后可以通过 ctypes 从 Python 调用,如下所示,

#!/usr/bin/env python

import ctypes as ct
import numpy as np

# import Fortran DLL
lib = ct.CDLL("main.dll")
lib.sub.restype = None    # define subroutine result type

# define subroutine argument type
lib.sub.argtypes =  [ ct.POINTER(ct.c_size_t)        # a_size
                    , ct.POINTER(ct.c_double)       # a array
                    ]
# all Fortran DLL
for i in [2,1,0]:
    a_size = i
    a = np.zeros(shape=[a_size])
    lib.sub ( ct.byref( ct.c_size_t(a_size) ) # Fortran passes everything around by reference
            , np.ctypeslib.as_ctypes( a ) # pointer to numpy array
            )
    print("a_size = {}, a = {}".format(a_size, a))

给出以下内容,

a_size = 2, a = [2. 2.]
a_size = 1, a = [1.]
a_size = 0, a = []

关于python - 使用 f2py 将 0 长度数组从 fortran 返回到 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57968189/

相关文章:

python - fortran 库的 python 结构内部和外部 c_char 数组之间的区别

python - 标准差为零的归一化

python - AWS Lambda (python) 中的 Cassandra 数据库 session 重用

python - 从字典列表中优化平均计算

python-2.7 - 如何在 Matplotlib (Numpy) 中生成 MATLAB 图(插值)?

fortran - Fortran 中的稀疏数组

fortran - 矩阵上的函数是应用于整个矩阵还是 Fortran 中的每一行?

python - 计算相同字符的最大子串

python - 如何在没有交叉验证的情况下检查机器学习的准确性

python - 按名称和索引排列的 Numpy 结构化数组