Niklas M.
Niklas M.

Reputation: 91

F2PY with Subroutine having a Call-back argument and an assumed-shape array argument

I am trying to use F2PY to wrap a fortran subroutine which takes a function and an assumed-shape array as arguments. This results in an error. I am using GNU Fortran (GCC) 4.8.5 20150623 (Red Hat 4.8.5-11) and F2PY version 2 with Scientific Linux 7.3.

A minimal example would be a file 'callback.f90' :

!file callback.f90 
subroutine sub(arr,func)
implicit none
  integer,intent(in),dimension(:) :: arr
  external func
  integer :: func
  integer :: i

  i = arr(1)
  print *, func(i)
end subroutine sub  

I wrap it in the Terminal with

f2py -m pymod -h sign.pyf callback.f90
f2py -c sign.pyf callback.f90

This results in the following error (translated from German):

gfortran:f77: /usr/tmp/tmpsADmTS/src.linux-x86_64-2.7/pymod-f2pywrappers.f
/usr/tmp/tmpsADmTS/src.linux-x86_64-2.7/pymod-f2pywrappers.f:6.19:

            use sub__user__routines                                 
               1
Error: Module file »sub__user__routines.mod« at (1) can't be opened
       for reading: File not found

However, this works perfectly fine, if arr is not assumed shape, but fixed size. F2PY with assumed shape arrays is also working fine for me, as long as no Call-back arguments are involved, thus the problem seems to be some kind of interaction between these two, which I can't figure out.

Upvotes: 4

Views: 437

Answers (2)

ert
ert

Reputation: 516

Seems like this is really a bug in f2py. The problem is, that f2py incorrectly inserts use ...__user__routines into a Fortran file that is compiled on the way. I have filed it on https://github.com/numpy/numpy/issues/17797 .

The proposed fix commented by percuse seems to work on first sight. Replacing dimension(:) by dimension(*) in the .pyf file made the f2py compilation run. I have not checked yet if this breaks the interface.

Upvotes: 1

Rafik Zurob
Rafik Zurob

Reputation: 361

Assumed-shape array dummy arguments are implemented using descriptors (sometimes called dope vectors) by most (all?) Fortran compilers. What this means is that the first argument (arr) of subroutine sub in your example is not just the address of the array. It's the address of a structure that contains the address of the array data as well as the array bounds information, among other things. I'm not familiar with the inner workings of F2PY, but perhaps it doesn't support by-descriptor arguments? (Other examples of by-descriptor arguments are arguments that have the POINTER or ALLOCATABLE attributes.)

Upvotes: 0

Related Questions