问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/61849880/error-occurs-when-coming-back-from-subroutine-of-fortran-by-numpy-f2py