[f2py] Create array in Python, deallocate in Fortran

Ben Forbes bdforbes at gmail.com
Sat Apr 30 09:44:01 EEST 2011


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 <pearu at f2py.com> wrote:
>
>
> On Sat, Apr 30, 2011 at 4:20 AM, Ben Forbes <bdforbes at gmail.com> 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
>> <pearu.peterson at gmail.com> 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 at cens.ioc.ee
>> > 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 at cens.ioc.ee
>> http://cens.ioc.ee/mailman/listinfo/f2py-users
>
>
> _______________________________________________
> f2py-users mailing list
> f2py-users at cens.ioc.ee
> http://cens.ioc.ee/mailman/listinfo/f2py-users
>
>



-- 
Benjamin D. Forbes
School of Physics
The University of Melbourne
Parkville, VIC 3010, Australia



More information about the f2py-users mailing list