Re: [f2py] Create array in Python, deallocate in Fortran
Ben Forbes <[email protected]> Sat, 30 Apr 2011 16:44:01 +1000
| Newsgroups | gmane.comp.python.f2py.user |
|---|---|
| Message-ID | <[email protected]> |
I'll use some large arrays and monitor memory usage to verify this, but it seems reasonable. In any case, there should usually be enough memory to temporarily allocate an initialisation array. On Sat, Apr 30, 2011 at 3:13 PM, Pearu Peterson <[email protected]> wrote: > > > On Sat, Apr 30, 2011 at 4:20 AM, Ben Forbes <[email protected]> wrote: >> >> Thanks that clears it up. So would the most efficient use of memory be >> to allocate directly from Fortran? It looks like an array can't be >> created in Python and used in Fortran without a copy being made. >> > > Yes. > > But note that > > helloworld.mymodule.b=arr > arr = helloworld.mymodule.b > > will free the original arr object (provided that it was > created to only initialize b) and in addition, arr is a > reference to Fortran array (IIRC). That is, > arr[0,0] = 100 > will modify Fortran array. > > Pearu > >> >> On Fri, Apr 29, 2011 at 5:31 PM, Pearu Peterson >> <[email protected]> wrote: >> > >> > >> > On 04/29/2011 09:19 AM, Ben Forbes wrote: >> >> When I create an array in Python, then deallocate it in Fortran, it >> >> doesn't appear to do anything to the original Python array. Here is >> >> the code: >> >> >> >> helloworld.f90: >> >> >> >> module mymodule >> >> real(8),allocatable,dimension(:,:)::b >> >> subroutine foo() >> >> deallocate(b) >> >> write(*,*) allocated(b) >> >> end subroutine foo2 >> >> end module mymodule >> >> >> >> >> >> Python: >> >> >> >> import helloworld >> >> >> >> arr=np.array([[1,2],[3,4]],dtype=np.float64,order='F') >> >> Out[2]: >> >> array([[ 1., 2.], >> >> [ 3., 4.]]) >> >> >> >> helloworld.mymodule.b=arr >> > >> > Note that this statement creates helloworld.mymodule.b (via Fortran >> > allocate) and then initialises with arr (via copy). >> > So, helloworld.mymodule.b and arr are actually two different arrays. >> > And so, when you deallocate helloworld.mymodule.b, then nothing happends >> > to arr indeed. >> > >> >> Out[6]: >> >> array([[ 1., 2.], >> >> [ 3., 4.]]) >> >> >> >> helloworld.mymodule.foo() >> >> Out: F >> >> >> >> arr >> >> Out[2]: >> >> array([[ 1., 2.], >> >> [ 3., 4.]]) >> > >> > Try to print helloworld.mymodule.b before and after calling >> > helloworld.mymodule.foo() to see the effect. >> > >> > >> >> Fortran thinks it has deallocated b, but Python still has the original >> >> array. I've included the directive -DF2PY_REPORT_ON_ARRAY_COPY=2, and >> >> it doesn't give any array copy warnings. >> >> >> >> How can I understand this behaviour? >> > >> > See above. >> > >> >> Is it potentially dangerous to deallocate from Fortran? >> > >> > No, it should not be. >> > >> > HTH, >> > Pearu >> > >> > _______________________________________________ >> > f2py-users mailing list >> > f2py-users-Y4l6ocDipWCuvFJfX82//[email protected] >> > http://cens.ioc.ee/mailman/listinfo/f2py-users >> > >> >> >> >> -- >> Benjamin D. Forbes >> School of Physics >> The University of Melbourne >> Parkville, VIC 3010, Australia >> >> _______________________________________________ >> 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 > > -- Benjamin D. Forbes School of Physics The University of Melbourne Parkville, VIC 3010, Australia