pythonnumpyfortranf2py

matmul_r8_avx2: failed. in increasing dimension with python f2py


I am using Fortran wrapper f2py. It works previously with n=5~30 for matrix multiplication (f2py failed in converting to C/Fortran array).

When I increase to 50, it tells me problem 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)`.

Here is the python part 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 part, test.f90, run by 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

if I run the python code, e.g., python test-f2py.py I got

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)

Solution

  • Upgrade numpy and gfortran does not help. I figured out a walk around. If I use f2py instead of numpy.f2py, it tells me

    /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>
    

    Then I switched to python 3.9 env in coda, it works