f2py 触发 malloc 错误

f2py triggers malloc error

我目前正在用 f2py 包装 Fortran 代码。然而,即使是一些简单的测试也会触发 malloc 错误。例如:

! file name is simple.F90
SUBROUTINE test(aout, ain, rank)
    INTEGER :: rank
    COMPLEX(kind=16) :: ain(rank, rank)
    COMPLEX(kind=16), INTENT(out) :: aout(rank, rank)
    print *, ain
    aout = ain
END SUBROUTINE test

我用 f2py -m simple -c simple.F90 包装了这段代码并使用了它:

import simple
import numpy as np
ain = np.array([[1,0],[0,1]],dtype="complex")
simple.test(ain,2)

输出相当混乱:

(2.98323129689261721470924741607413318E-4947,0.00000000000000000000000000000000000) (0.00000000000000000000000000000000000,2.98323129689261721470924741607413318E-4947) (5.43913343041483495234482095052061699E-4937,1.19445898260724927749750105155089048E-4946) (2.03058027046167518900674768173335535E-4945,5.42471021397611822645684765587671432E-4937)
Python(9481,0x7fff7ad40000) malloc: \*** error for object 0x7fabcaa1adc8: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6

Fortran 子程序没有得到正确的数组,内存有问题。

更多信息:

正如所有评论所指出的,kind 是这里的罪魁祸首。我使用以下约定后所有问题都解决了。

INTEGER, PARAMETER :: dp = kind(1.0d0)
REAL(kind=dp) :: a_real_number
COMPLEX(kind=dp) :: a_complex_number