Re: [f2py] "natural" Fortran calling conventions?

Kevin Mitchell <[email protected]>
Newsgroups gmane.comp.python.f2py.user
Message-ID <[email protected]>
> I don't want my Fortran subroutine to have intent(c); if I did that,
> I'd have to write an intent(c) wrapper for every Fortran routine I
> want to call, which defeats the purpose.  Or if I write Fortran code
> optimized for intent(c), it won't be idiomatic Fortran and hard to
> maintain.

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

If you f2py this with "f2py -c f2py_trsps_f.f90" (and as far as I know
the same thing should work with fortran 77), you'll note that

<<< import time,f2py_trsps_f
<<< l,n,m=2**10,2**6,2**6
<<< h=arange(l*n*m).reshape([l,n,m])
<<< st=time.time();f2py_trsps_f.f2py_trsps_f(h);print 'c-order=',time.time()-st
   0.0000000000000000        1.0000000000000000
2.0000000000000000        3.0000000000000000        4.0000000000000000
       5.0000000000000000        6.0000000000000000
7.0000000000000000
c-order= 0.0583457946777
<<< h=asfortranarray(h)
<<< st=time.time();f2py_trsps_f.f2py_trsps_f(h);print 'f-order=',time.time()-st
   0.0000000000000000        1.0000000000000000
2.0000000000000000        3.0000000000000000        4.0000000000000000
       5.0000000000000000        6.0000000000000000
7.0000000000000000
f-order= 0.225140094757

That is, things work as expected (and more efficiently) if you give it
a python-natual c-order array. I agree that getting this behaviour
from "inent(c)" is a little counter-intuitive, but I think its the
closest thing to what you're looking for.

Once you get into the Fortran subroutine everything works normally.
You can forget that python or c was even involved once you get past
the f2py comment which the compiler doesn't see anyway. So no, I
wouldn't say this is idiomatic Fortran.

> And I'd like that with an error message if it can't be done
> efficiently
As far as I know, this can't currently be done, but I would say it's a
good idea.

Kevin
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.