我正在使用 Fortran 包装程序
f2py
。它以前与 n=5~30
一起用于矩阵乘法(f2py 无法转换为 C/Fortran 数组)。
当我增加到
50
时,它告诉我问题python: /home/rdonnelly/mc/conda-bld/compilers_linux-64_1534627447954/work/.build/x86_64-conda_cos6-linux-gnu/src/gcc/libgfortran/generated/matmul_r8.c:642: matmul_r8_avx2: Assertion
((a)->dtype & 0x07) == 2 || ((b)->dtype & 0x07) == 2' 失败。
中止(核心转储)`.
这里是python部分
test-f2py.py
import numpy as np
import test
dim_n = 50
A = np.random.random((dim_n, dim_n))
B = np.random.random((dim_n, dim_n))
C = np.zeros((dim_n, dim_n))
A = np.asfortranarray(A)
B = np.asfortranarray(B)
C = np.asfortranarray(C)
test.wrap(A, B, C)
print("Fortran result:")
#print(C)
# Cross-check with numpy.einsum
C_check = np.einsum('ij,jk->ik', A, B)
print("Numpy einsum result:")
#print(C_check)
# Compare the two results
print("Are the results equal?")
print(np.allclose(C, C_check))
fortran 部分,
test.f90
,由 python -m numpy.f2py -c --f90flags='-O3' -m test test.f90
运行
!f2py dp selected_real_kind(15, 307)
subroutine wrap(A, B, C)
real(kind=8), intent(in) :: A(:, :), B(:, :)
real(kind=8), intent(inout) :: C(:, :)
integer :: m, n, p
m = size(A, 1)
n = size(A, 2)
p = size(B, 2)
if (n /= size(B, 1)) then
print *, "Error: Incompatible dimensions for matrix multiplication"
return
end if
C = matmul(A, B)
end subroutine wrap
如果我运行 python 代码,例如,
python test-f2py.py
我得到了
python: /home/rdonnelly/mc/conda-bld/compilers_linux-64_1534627447954/work/.build/x86_64-conda_cos6-linux-gnu/src/gcc/libgfortran/generated/matmul_r8.c:642: matmul_r8_avx2: Assertion `((a)->dtype & 0x07) == 2 || ((b)->dtype & 0x07) == 2' failed.
Aborted (core dumped)
升级
numpy
和gfortran
没有帮助。我想到了四处走走。如果我使用 f2py
而不是 numpy.f2py
,它会告诉我
/usr/include/python3.8/pyconfig.h:3:12: fatal error: x86_64-linux-gnu/python3.8/pyconfig.h: No such file or directory # include <x86_64-linux-gnu/python3.8/pyconfig.h>
然后我在 coda 中切换到 python 3.9 env,它有效