Re: [f2py] f2py-users Digest, Vol 65, Issue 5
Brandt Belson <bbelson-uX/[email protected]>
| Newsgroups | gmane.comp.python.f2py.user |
|---|---|
| Message-ID | <[email protected]> |
Hello,
I have a simple question about passing array arguments to f2py-wrapped
subroutines. If I'm not mistaken, sometimes the arrays are copied and this
operation can be computationally expensive. For my project, I am calling an
f2py function many times and passing large arrays. Using
the -DF2PY_REPORT_ON_ARRAY_COPY=1 compile option, I noticed that my arrays
are being copied. Is there a way to avoid this and achieve a speedup?
Just as a simple example, here is what I mean:
Fortran file testarrays.f:
subroutine testarrays(a,b,nx,ny)
implicit none
integer nx,ny,i,j
real*8 a(nx,ny),b(nx,ny)
cf2py intent(in) nx,ny,b
cf2py intent(in,out) a
cf2py depend(nx,ny) a
cf2py depend(nx,ny) b
do i=1,nx
do j=1,ny
a(i,j)=i+j + b(i,j)
end do
end do
end subroutine
Python file testarrays.py
import testarrays
from numpy import *
nx = 2
ny = 3
a=zeros((nx,ny))
b=zeros((nx,ny))
a=testarrays.testarrays(a,b,nx,ny)
print a
print b
Now I compile this with: f2py -c -m testarrays testarrays.f
--fcompiler=gfortran -DF2PY_REPORT_ON_ARRAY_COPY=1
(I'm using Mac OS X 10.6)
The output of the python script is:
$ python testarrays.py
copied an array: size=6, elsize=8
copied an array: size=6, elsize=8
[[ 2. 3. 4.]
[ 3. 4. 5.]]
[[ 0. 0. 0.]
[ 0. 0. 0.]]
Thank you
On Sat, Jul 17, 2010 at 5:00 AM, <f2py-users-request-Y4l6ocDipWCuvFJfX82//[email protected]> wrote:
> Send f2py-users mailing list submissions to
> f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
>
> To subscribe or unsubscribe via the World Wide Web, visit
> http://cens.ioc.ee/mailman/listinfo/f2py-users
> or, via email, send a message with subject or body 'help' to
> f2py-users-request-Y4l6ocDipWCuvFJfX82//[email protected]
>
> You can reach the person managing the list at
> f2py-users-owner-Y4l6ocDipWCuvFJfX82//[email protected]
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of f2py-users digest..."
>
>
> Today's Topics:
>
> 1. Re: Wraping Fortran code with allocatable arrays.
> (J?rgen Wieferink)
> 2. Re: Wraping Fortran code with allocatable arrays. (Pearu Peterson)
> 3. Re: Wraping Fortran code with allocatable arrays. (Benjamin Kutz)
> 4. Re: Wraping Fortran code with allocatable arrays.
> (J?rgen Wieferink)
> 5. Re: Wraping Fortran code with allocatable arrays. (Benjamin Kutz)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Fri, 16 Jul 2010 11:20:06 +0200
> From: J?rgen Wieferink <[email protected]>
> Subject: Re: [f2py] Wraping Fortran code with allocatable arrays.
> To: f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
> Message-ID: <201007161120.06571.wieferink-KuiJ5kEpwI6ELgA04lAiVw@public.gmane.org>
> Content-Type: Text/Plain; charset="iso-8859-1"
>
> Benjamin Kutz wrote (Friday 16 July 2010 10:18:52):
> > When it comes to allocatable arrays I have a problem. As far as I know I
> > can`t share an allocatable array with a common block. So I have to share
> > it in the parameter list of the subroutine. That means, that in case of
> > a wrapped routine I have to pass this array to Python and back to the
> > next wrapped Fortran routine. Therefore I have chosen the following
> > implementation.
>
> You cannot share allocatable arrays within common blocks, true. But
> then, common blocks are, well, depricated, to put it mildly. Your
> situation cries for using a fortran module with allocatable arrays
> as module data.
>
> > ! -*- f90 -*-
> > python module foo ! in
> > interface ! in :foo
> > subroutine init(abc,deg) ! in :foo:init.f
> > real allocatable,dimension(:), intend(out) :: abc
> > real allocatable,dimension(:,:), intend(out) :: deg
> > end subroutine init
> > subroutine calc(abc,deg) ! in :foo:calc.f
> > real allocatable,dimension(:), intend(in,out) :: abc
> > real allocatable,dimension(:,:), intend(in,out) :: deg
> > end subroutine calc
> > end interface
> > end python module foo
>
> Allocatable arrays in subprocedure interfaces, on the other hand,
> are a rather new feature, not introduced before Fortran 2003. It is
> by no way supported by f2py. But you know that you need to state
> 'allocatable' in the interface only if you really plan to allocate
> it from within, which is not that common to do.?
>
> Juergen
>
>
>
> --
> Juergen Wieferink
> Fritz-Haber-Institut der Max-Planck-Gesellschaft (Theory)
> Faradayweg 4-6, 14195 Berlin-Dahlem (Germany)
> Phone: +49 30/8413-4826, Fax: +49 30/8413-4701
> [email protected]
>
>
>
> ------------------------------
>
> Message: 2
> Date: Fri, 16 Jul 2010 12:57:20 +0300
> From: Pearu Peterson <[email protected]>
> Subject: Re: [f2py] Wraping Fortran code with allocatable arrays.
> To: f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
> Message-ID: <4C402D00.1050408-Y4l6ocDipWCuvFJfX82//[email protected]>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
>
>
> On 07/16/2010 12:20 PM, J?rgen Wieferink wrote:
>
> > Allocatable arrays in subprocedure interfaces, on the other hand,
> > are a rather new feature, not introduced before Fortran 2003. It is
> > by no way supported by f2py.
>
> Btw, f2py has basic support for allocatable arrays. See
>
>
> http://cens.ioc.ee/projects/f2py2e/usersguide/index.html#allocatable-arrays
>
> for examples.
>
> Pearu
>
>
>
> ------------------------------
>
> Message: 3
> Date: Fri, 16 Jul 2010 14:12:05 +0200
> From: Benjamin Kutz <[email protected]>
> Subject: Re: [f2py] Wraping Fortran code with allocatable arrays.
> To: f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
> Message-ID: <4C404C95.1090205-twh1gUx1uBUL63KmMnjC+CEWGD4kr0XT@public.gmane.org>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> Am 16.07.2010 11:20, schrieb J?rgen Wieferink:
> > Benjamin Kutz wrote (Friday 16 July 2010 10:18:52):
> >
> >> When it comes to allocatable arrays I have a problem. As far as I know I
> >> can`t share an allocatable array with a common block. So I have to share
> >> it in the parameter list of the subroutine. That means, that in case of
> >> a wrapped routine I have to pass this array to Python and back to the
> >> next wrapped Fortran routine. Therefore I have chosen the following
> >> implementation.
> >>
> > You cannot share allocatable arrays within common blocks, true. But
> > then, common blocks are, well, depricated, to put it mildly. Your
> > situation cries for using a fortran module with allocatable arrays
> > as module data.
> >
> >
> >> ! -*- f90 -*-
> >> python module foo ! in
> >> interface ! in :foo
> >> subroutine init(abc,deg) ! in :foo:init.f
> >> real allocatable,dimension(:), intend(out) :: abc
> >> real allocatable,dimension(:,:), intend(out) :: deg
> >> end subroutine init
> >> subroutine calc(abc,deg) ! in :foo:calc.f
> >> real allocatable,dimension(:), intend(in,out) :: abc
> >> real allocatable,dimension(:,:), intend(in,out) :: deg
> >> end subroutine calc
> >> end interface
> >> end python module foo
> >>
> > Allocatable arrays in subprocedure interfaces, on the other hand,
> > are a rather new feature, not introduced before Fortran 2003. It is
> > by no way supported by f2py. But you know that you need to state
> > 'allocatable' in the interface only if you really plan to allocate
> > it from within, which is not that common to do.?
> >
> > Juergen
> >
> >
> >
> >
> Hi,
>
> thanks for your fast answer.
> I still didn`t got the point. I just want to share these allocatable
> data arrays between the first and the second subroutine.
> I also knew the example out of the f2py manual, but I couldn`t realise
> it in my code, due to my limited knowledge in python and wrapping.
> Do you mean, that I have to write a fortran module for each allocatable?
> What does this module look like?
> At the moment I allocate in Fortan at the beginning with:
>
> subroutine init(abc,deg)
> integer max, max2
> max= ...
> max2=...
> real , dimension (:) , allocatable :: abc
> real , dimension (:,:) , allocatable :: deg
> allocate (abc(max))
> allocate (deg(max,max2))
> call initialxyz (abc,deg)
> end
>
> subroutine calc(abc,deg)
> call solve(abc,deg)
> if ( allocated( abc ) ) then
> deallocate (abc)
> ...
> endif
>
> I wrap both routines and want to call them one after another from python.
> Maybe you can help me with a little example? What does the foo.pyf have
> to look like?
> The problem is, that I have to wrap with f2py -c foo.pyf *.o. I can't
> wrap it directly from the foo.f90 routine.
>
> Greetings
> Ben
>
>
>
> ------------------------------
>
> Message: 4
> Date: Fri, 16 Jul 2010 15:30:01 +0200
> From: J?rgen Wieferink <[email protected]>
> Subject: Re: [f2py] Wraping Fortran code with allocatable arrays.
> To: f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
> Message-ID: <201007161530.01287.wieferink-KuiJ5kEpwI6ELgA04lAiVw@public.gmane.org>
> Content-Type: Text/Plain; charset="iso-8859-1"
>
> Hi,
>
> module mymod
> implicit none
> real, dimension(:), allocatable :: abc
> real, dimension(:,:), allocatable :: deg
> contains
> subroutine init
> implicit none
> ! do stuff on abc, deg
> end subroutine init
> subroutine calc
> implicit none
> ! do stuff on abc, deg
> end subroutine calc
> end module mymod
>
> From this, you can easily generate the wrapper mymod.pyf from
>
> $ f2py -m _mymod -h mymod.pyf mymod.f90
>
> As Pearu mentioned, even the allocatable arrays are then exposed to
> python as _mymod.mymod.abc and _mymod.mymod.deg.
>
> Juergen
>
>
>
> Benjamin Kutz wrote (Friday 16 July 2010 14:12:05):
> >
> > Hi,
> >
> > thanks for your fast answer.
> > I still didn`t got the point. I just want to share these allocatable
> > data arrays between the first and the second subroutine.
> > I also knew the example out of the f2py manual, but I couldn`t realise
> > it in my code, due to my limited knowledge in python and wrapping.
> > Do you mean, that I have to write a fortran module for each allocatable?
> > What does this module look like?
> > At the moment I allocate in Fortan at the beginning with:
> >
> > subroutine init(abc,deg)
> > integer max, max2
> > max= ...
> > max2=...
> > real , dimension (:) , allocatable :: abc
> > real , dimension (:,:) , allocatable :: deg
> > allocate (abc(max))
> > allocate (deg(max,max2))
> > call initialxyz (abc,deg)
> > end
> >
> > subroutine calc(abc,deg)
> > call solve(abc,deg)
> > if ( allocated( abc ) ) then
> > deallocate (abc)
> > ...
> > endif
> >
> > I wrap both routines and want to call them one after another from python.
> > Maybe you can help me with a little example? What does the foo.pyf have
> > to look like?
> > The problem is, that I have to wrap with f2py -c foo.pyf *.o. I can't
> > wrap it directly from the foo.f90 routine.
> >
> > Greetings
> > Ben
> >
> > _______________________________________________
> > f2py-users mailing list
> > f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
> > http://cens.ioc.ee/mailman/listinfo/f2py-users
> >
>
>
>
>
> ------------------------------
>
> Message: 5
> Date: Fri, 16 Jul 2010 17:13:04 +0200
> From: Benjamin Kutz <[email protected]>
> Subject: Re: [f2py] Wraping Fortran code with allocatable arrays.
> To: f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
> Message-ID: <4C407700.8070004-twh1gUx1uBUL63KmMnjC+CEWGD4kr0XT@public.gmane.org>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> Am 16.07.2010 15:30, schrieb J?rgen Wieferink:
> > Hi,
> >
> > module mymod
> > implicit none
> > real, dimension(:), allocatable :: abc
> > real, dimension(:,:), allocatable :: deg
> > contains
> > subroutine init
> > implicit none
> > ! do stuff on abc, deg
> > end subroutine init
> > subroutine calc
> > implicit none
> > ! do stuff on abc, deg
> > end subroutine calc
> > end module mymod
> >
> > > From this, you can easily generate the wrapper mymod.pyf from
> >
> > $ f2py -m _mymod -h mymod.pyf mymod.f90
> >
> > As Pearu mentioned, even the allocatable arrays are then exposed to
> > python as _mymod.mymod.abc and _mymod.mymod.deg.
> >
> > Juergen
> >
> >
> >
> > Benjamin Kutz wrote (Friday 16 July 2010 14:12:05):
> >
> >> Hi,
> >>
> >> thanks for your fast answer.
> >> I still didn`t got the point. I just want to share these allocatable
> >> data arrays between the first and the second subroutine.
> >> I also knew the example out of the f2py manual, but I couldn`t realise
> >> it in my code, due to my limited knowledge in python and wrapping.
> >> Do you mean, that I have to write a fortran module for each allocatable?
> >> What does this module look like?
> >> At the moment I allocate in Fortan at the beginning with:
> >>
> >> subroutine init(abc,deg)
> >> integer max, max2
> >> max= ...
> >> max2=...
> >> real , dimension (:) , allocatable :: abc
> >> real , dimension (:,:) , allocatable :: deg
> >> allocate (abc(max))
> >> allocate (deg(max,max2))
> >> call initialxyz (abc,deg)
> >> end
> >>
> >> subroutine calc(abc,deg)
> >> call solve(abc,deg)
> >> if ( allocated( abc ) ) then
> >> deallocate (abc)
> >> ...
> >> endif
> >>
> >> I wrap both routines and want to call them one after another from
> python.
> >> Maybe you can help me with a little example? What does the foo.pyf have
> >> to look like?
> >> The problem is, that I have to wrap with f2py -c foo.pyf *.o. I can't
> >> wrap it directly from the foo.f90 routine.
> >>
> >> Greetings
> >> Ben
> >>
> >> _______________________________________________
> >> f2py-users mailing list
> >> f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
> >> http://cens.ioc.ee/mailman/listinfo/f2py-users
> >>
> >>
> >
> > _______________________________________________
> > f2py-users mailing list
> > f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
> > http://cens.ioc.ee/mailman/listinfo/f2py-users
> >
> Hi,
>
> wonderful! As I can see your example does not differ much from that one
> in the manual.
> But I don't know why, now I found out how to do it ;)! Maybe it was the
> heat or the friday afternoon;) or just my "slow" brain :)
> Thanks for your quick help and have a nice weekend!
>
> Greetings
> Ben
>
>
>
>
> ------------------------------
>
> _______________________________________________
> f2py-users mailing list
> f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
> http://cens.ioc.ee/mailman/listinfo/f2py-users
>
>
> End of f2py-users Digest, Vol 65, Issue 5
> *****************************************
>
_______________________________________________
f2py-users mailing list
f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
http://cens.ioc.ee/mailman/listinfo/f2py-users