[f2py] "natural" Fortran calling conventions?

Pearu Peterson pearu.peterson at gmail.com
Thu Oct 15 10:43:03 EEST 2009



Thomas Breuel wrote:
>> You don't have to write a whole wrapper to do this, you can use the
>> commenting method:
>>
>> subroutine f2py_trsps_f(n,m,l,h)
>> !f2py intent(c) h
>>  implicit none
>>  integer :: n,m,l
>>  real*8  :: h(n,m,l)
>>  print*,h(1:8,1,1)
>>  return
>> end subroutine f2py_trsps_f
> 
> OK, I tried this and it isn't working for me.  intent(c) seems to
> instruct Python to pass the data in C order, but Fortran doesn't seem
> to know that, so the elements end up in the wrong order.

Could you send a simple example demonstrating the problem?

> Furthermore, f2py seems to generate some kind of array bounds checking
> code so that I can't interchange the array bounds in the Fortran code.
>  intent(cache) also isn't doing the trick.

In case it would help, you can disable checks by specifying `check()`
for the corresponding variable:

!f2py check() h

> Even if they did, the meaning of intent(c) and intent(cache) when used
> in this way could change any release (and likely will once f2py
> supports new Fortran arrays).
> 
> Altogether, I still think f2py should get an "intent(natural)"; with
> that, a C array declared as:
> 
>     float a[17][99][3];
> 
> would get passed to Fortran as:
> 
>     subroutine f(a)
>     !f2py intent(natural) a
>     real, intent(in) :: a(3,99,17)
>     ...
>     end subroutine f
> 
> This would let me write natural, efficient Fortran loops that
> correspond to the natural, efficient C loops, and it would mean that
> there is no cost in calling from Python to Fortran and back.

I'll keep this in mind when I get a chance to work with f2py..

Pearu



More information about the f2py-users mailing list