Error occurs when coming back from subroutine of fortran by numpy.f2py

好久不见. 提交于 2021-02-10 05:44:05

问题


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 &lttype spec&gt(kind=&ltkindselector&gt)
declarations where &ltkindselector&gt 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!