[f2py] pass numpy.memmap object in fortran-order to f2py extension

Pearu Peterson pearu.peterson at gmail.com
Thu Aug 14 16:48:55 EEST 2008


On Thu, Aug 14, 2008 at 4:25 PM, Steve Schmerler <elcorto at gmx.net> wrote:
> Hi all
>
> I have a numpy.memmap'ed a file (3d array) in Fortran order and pass this to a
> f2py-generated extension. I wonder why the wrapper makes a copy of the array
> even if it's in F-order already. Please see attached files.
>
> This is with numpy 1.1.0 and it's f2py 2_5237. Thanks for any hints!
>
>    $ make -f Makefile.foo
>    $ python foo.py
>
> Result is:
>
>    a.shape: (8, 200, 3)
>    a.flags:   C_CONTIGUOUS : True
>      F_CONTIGUOUS : False
>      OWNDATA : True
>      WRITEABLE : True
>      ALIGNED : True
>      UPDATEIFCOPY : False
>    ---------------
>    b.shape: (8, 200, 3)
>    b.flags:   C_CONTIGUOUS : False
>      F_CONTIGUOUS : True
>      OWNDATA : False
>      WRITEABLE : False
>      ALIGNED : True
>      UPDATEIFCOPY : False
>    ---------------
>    foo(a)
>    copied an array: size=4800, elsize=8
>    ---------------
>    foo(N.array(a, order='F'))
>    ---------------
>    foo(b)
>    copy here on my machine, why?           <<<<<<<<<<<<<<<<<
>    copied an array: size=4800, elsize=8
>    ---------------

This is because f2py wrappers don't know about
memmap. I am not even sure that making a copy
can be avoided because Fortran function needs
a contiguous array as an argument and memmap
is usually a slice-like object that needs to be copied
anyway. However, if memmap contains the whole
array then using foo(N.array(b)) should ensure
that copy is made only when really necessarry.

Pearu



More information about the f2py-users mailing list