我使用Fortran Package 器f2py
。它以前使用n=5~30
进行矩阵乘法(f2py failed in converting to C/Fortran array)。
当我增加到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 & 0x 07)== 2||((B)-〉dtype & 0x 07)== 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)
1条答案
按热度按时间i34xakig1#
升级
numpy
和gfortran
没有帮助。我想出了一个散步。如果我使用f2py
而不是numpy.f2py
,它会告诉我然后我在coda中切换到python3.9env,它工作正常