传递布尔数组而不用`f2py`复制?

时间:2016-12-31 21:47:45

标签: python numpy fortran f2py

如果我打算用f2py传递一个布尔值的NumPy数组,应该如何输入Fortran变量?我已经尝试了integer*1logical*1,但这两个都表明数组已被复制。

例如,如果我编译文件foo.f95,包含:

subroutine foo(x, n)
    logical*1 x(n)
    !f2py intent(in) x
    !f2py intent(hide), depend(x) :: n=shape(x,0)
    ...
end subroutine

f2py -c -m foo foo.f90 -DF2PY_REPORT_ON_ARRAY_COPY=1并运行类似:

import numpy as np
import foo
x = np.random.randn(100) < 0
foo.foo(x)

打印

copied an array: size=100, elsize=1

如果我将logical*1更改为integer*1,我会得到相同的结果。 Fortran文件中布尔数组的正确类型是什么,以便不复制数组?

请注意,这不是内存连续性问题,因为数组是1D - foo.foo(np.asfortranarray(x))打印相同的复制邮件。

1 个答案:

答案 0 :(得分:3)

从某些实验(*)来看,似乎Python / f2py将np.int8视为与logical*1兼容,而np.boolnp.bool8则不是出于某种原因。将print *, "(fort) x = ", x插入foo.f90后,我们得到:

>>> foo.foo( np.array( [ True, False, False ], dtype=np.bool ) )
copied an array: size=3, elsize=1
 (fort) x =  T F F
>>> foo.foo( np.array( [ False, True, False ], dtype=np.bool8 ) )
copied an array: size=3, elsize=1
 (fort) x =  F T F
>>> foo.foo( np.array( [ False, False, True ], dtype=np.int8 ) ) # no copy
 (fort) x =  F F T

因为TrueFalse只是映射到1和0,所以在Python端使用int8数组可能很方便。

(*)一些实验

在这里,我将f2py intent注释更改为inout以查看我们是否可以从Fortran端修改数组。

foo.f90:

subroutine foo(x, n)
    use iso_c_binding
    implicit none
    integer n
    logical*1 x( n )
    ! logical x( n )
    ! logical(c_bool) x( n )

    !f2py intent(inout) x
    !f2py intent(hide), depend(x) :: n=shape(x,0)

    print *, "(fort) x = ", x
    print *, "(fort) sizeof(x(1)) = ", sizeof(x(1))
    print *, "(fort) resetting x(:) to true"
    x(:) = .true.
end subroutine

test.py:

import numpy as np
import foo

for T in [ np.bool, np.bool8,
           np.int,  np.int8,  np.int32,  np.int64,
           np.uint, np.uint8, np.uint32, np.uint64,
           np.dtype('b'), np.dtype('int8'), np.dtype('int32') ]:

    print( "-------------------------" )
    print( "dtype =", T )

    x = np.array( [ True, False, True ], dtype=T )
    print( "input x =", x )

    try:
        foo.foo( x )
        print( "output x =", x )
    except:
        print( "failed" )

logical*1的结果:

-------------------------
dtype = <class 'bool'>
input x = [ True False  True]
failed
-------------------------
dtype = <class 'numpy.bool_'>
input x = [ True False  True]
failed
-------------------------
dtype = <class 'int'>
input x = [1 0 1]
failed
-------------------------
dtype = <class 'numpy.int8'>
input x = [1 0 1]
 (fort) x =  T F T
 (fort) sizeof(x(1)) =                     1
 (fort) resetting x(:) to true
output x = [1 1 1]
-------------------------
dtype = <class 'numpy.int32'>
input x = [1 0 1]
failed
-------------------------
dtype = <class 'numpy.int64'>
input x = [1 0 1]
failed
-------------------------
dtype = <class 'numpy.uint64'>
input x = [1 0 1]
failed
-------------------------
dtype = <class 'numpy.uint8'>
input x = [1 0 1]
 (fort) x =  T F T
 (fort) sizeof(x(1)) =                     1
 (fort) resetting x(:) to true
output x = [1 1 1]
-------------------------
dtype = <class 'numpy.uint32'>
input x = [1 0 1]
failed
-------------------------
dtype = <class 'numpy.uint64'>
input x = [1 0 1]
failed
-------------------------
dtype = int8
input x = [1 0 1]
 (fort) x =  T F T
 (fort) sizeof(x(1)) =                     1
 (fort) resetting x(:) to true
output x = [1 1 1]
-------------------------
dtype = int8
input x = [1 0 1]
 (fort) x =  T F T
 (fort) sizeof(x(1)) =                     1
 (fort) resetting x(:) to true
output x = [1 1 1]
-------------------------
dtype = int32
input x = [1 0 1]
failed

logical的结果(默认类型):

-------------------------
dtype = <class 'bool'>
input x = [ True False  True]
failed
-------------------------
dtype = <class 'numpy.bool_'>
input x = [ True False  True]
failed
-------------------------
dtype = <class 'int'>
input x = [1 0 1]
failed
-------------------------
dtype = <class 'numpy.int8'>
input x = [1 0 1]
failed
-------------------------
dtype = <class 'numpy.int32'>
input x = [1 0 1]
 (fort) x =  T F T
 (fort) sizeof(x(1)) =                     4
 (fort) resetting x(:) to true
output x = [1 1 1]
-------------------------
dtype = <class 'numpy.int64'>
input x = [1 0 1]
failed
-------------------------
dtype = <class 'numpy.uint64'>
input x = [1 0 1]
failed
-------------------------
dtype = <class 'numpy.uint8'>
input x = [1 0 1]
failed
-------------------------
dtype = <class 'numpy.uint32'>
input x = [1 0 1]
 (fort) x =  T F T
 (fort) sizeof(x(1)) =                     4
 (fort) resetting x(:) to true
output x = [1 1 1]
-------------------------
dtype = <class 'numpy.uint64'>
input x = [1 0 1]
failed
-------------------------
dtype = int8
input x = [1 0 1]
failed
-------------------------
dtype = int8
input x = [1 0 1]
failed
-------------------------
dtype = int32
input x = [1 0 1]
 (fort) x =  T F T
 (fort) sizeof(x(1)) =                     4
 (fort) resetting x(:) to true
output x = [1 1 1]

logical(c_bool)的结果(通过iso_c_binding):

-------------------------
dtype = <class 'bool'>
input x = [ True False  True]
failed
-------------------------
dtype = <class 'numpy.bool_'>
input x = [ True False  True]
failed
-------------------------
dtype = <class 'int'>
input x = [1 0 1]
failed
-------------------------
dtype = <class 'numpy.int8'>
input x = [1 0 1]
failed
-------------------------
dtype = <class 'numpy.int32'>
input x = [1 0 1]
 (fort) x =  T F F
 (fort) sizeof(x(1)) =                     1
 (fort) resetting x(:) to true
output x = [65793     0     1]
-------------------------
dtype = <class 'numpy.int64'>
input x = [1 0 1]
failed
-------------------------
dtype = <class 'numpy.uint64'>
input x = [1 0 1]
failed
-------------------------
dtype = <class 'numpy.uint8'>
input x = [1 0 1]
failed
-------------------------
dtype = <class 'numpy.uint32'>
input x = [1 0 1]
 (fort) x =  T F F
 (fort) sizeof(x(1)) =                     1
 (fort) resetting x(:) to true
output x = [65793     0     1]
-------------------------
dtype = <class 'numpy.uint64'>
input x = [1 0 1]
failed
-------------------------
dtype = int8
input x = [1 0 1]
failed
-------------------------
dtype = int8
input x = [1 0 1]
failed
-------------------------
dtype = int32
input x = [1 0 1]
 (fort) x =  T F F
 (fort) sizeof(x(1)) =                     1
 (fort) resetting x(:) to true
output x = [65793     0     1]

出于某种原因,最后一个logical(c_bool)不能用于上述用法...(f2py似乎将logical(c_bool)视为4个字节,而gfortran将其视为1个字节,因此某些内容不一致...)