numpy.f2py 从 fortran 的子程序返回时发生错误
问题描述
我使 hoge 尽可能简单,但仍然会出现错误.请告诉我有什么问题.
这是我的 Fortran 子程序代码.
子程序 hoge(d)复杂(种类(0d0)),意图(出)::d(5,10,15)!5 10 15 没有特殊含义..!这两行有效..!整数,参数 :: dp = kind(0d0)!复杂(dp),意图(out)::d(5,10,15)做我=1,15做 j=1,10做 k=1,5d(k,j,i)=0结束结束结束!反而!d(1:5,1:10,1:15)=0 或!d(:,:,:)=0 也会带来错误.!打印*,'返回'返回结束子程序
我想使用 Fortran 子例程.我编译如下
python -m numpy.f2py -c hoge.f90 -m hoge
并如下使用
进口猪hoge.hoge()
那么结果就是下面的三行:
返回双重免费或腐败(出)中止(核心转储)
我完全不知道...请告诉我有什么问题.
当线路发生如下变化时
做 j=1,10 ->做 j=1,5
错误不会发生...(供您参考..)1..6带来了错误.
解决方案根据 F2PY 用户指南和参考手册:
<块引用>目前F2PY只能处理<type spec>(kind=<kindselector>)<kindselector> 是数字整数的声明(例如 1、2、4,...),但不是函数调用 KIND(..) 或任何其他表达式.
因此,您的代码示例中的声明 complex(kind(0d0))
正是 f2py 无法解释的函数调用或其他表达式.
正如您所发现的(但在代码中已注释掉),一种解决方法是首先生成一个整数种类说明符(integer, parameter :: dp = kind(0d0)
),然后然后在复杂变量 complex(dp)
的种类说明符中使用它.
如果您无法像这样更改 Fortran 源代码,文档会进一步概述 映射文件(默认名称为 .f2py_f2cmap
或使用命令传递可以创建和使用行标志 --f2cmap
).在你的情况下,你可以例如使用包含以下内容的默认文件:
$ cat .f2py_f2cmap{'complex': {'KIND(0D0)': 'complex_double'}}
并像以前一样编译,以获得所需的行为.
I made hoge as simple as possible and errors still coming. Please tell me what problems are.
This is my Fortran subroutine code.
subroutine hoge(d)
complex(kind(0d0)), intent(out):: d(5,10,15) ! 5 10 15 does not have special meanings..
! these two lines works..
! integer, parameter :: dp = kind(0d0)
! complex(dp), intent(out) :: d(5,10,15)
do i=1,15
do j=1,10
do k=1,5
d(k,j,i)=0
enddo
enddo
enddo
! instead
! d(1:5,1:10,1:15)=0 or
! d(:,:,:)=0 also brings the error.
!
print*,'returning'
return
end subroutine hoge
I want to use a Fortran subroutine. I compiled like below
python -m numpy.f2py -c hoge.f90 -m hoge
and use as below
import hoge
hoge.hoge()
then the result is the three lines below:
Returning
double free or corruption (out)
Aborted (core dumped)
I totally have no idea... please tell me what problems are.
When the line has a change like below
do j=1,10 -> do j=1,5
the error does not occur... (for your information..) 1..6 brings the error.
解决方案According to the F2PY User Guide and Reference Manual:
Currently, F2PY can handle only <type spec>(kind=<kindselector>) declarations where <kindselector> is a numeric integer (e.g. 1, 2, 4,…), but not a function call KIND(..) or any other expression.
Thus, the declaration complex(kind(0d0))
in your code example is exactly the kind of function call or other expression that f2py cannot interpret.
As you have found out (but commented out in the code), one work around is to first generate an integer kind specifier (integer, parameter :: dp = kind(0d0)
), and then use that in the kind specifier of your complex variable complex(dp)
.
If changing the Fortran source code like this is not an option for you, the documentation further outlines how a mapping file (with default name .f2py_f2cmap
or passed using command line flag --f2cmap <filename>
) can be created and used instead. In your case, you can e.g. use the default file with the following contents:
$ cat .f2py_f2cmap
{'complex': {'KIND(0D0)': 'complex_double'}}
and compile as before, to get the desired behaviour.
相关文章